1<?php
2if (!defined('DOKU_INC')) die();
3
4class helper_plugin_heatmap extends DOKUWIKI_Plugin {
5    public function search_pages( $namespace, $year ) {
6        global $conf;
7        require_once( DOKU_INC . 'inc/search.php' );
8
9        $ns = cleanID ($namespace);
10        $result = [];
11
12        $ns_pattern = $ns ? "/^$ns:?.*/" : "";
13        $since_ts = strtotime("$year-01-01");
14        $until_ts = strtotime("$year-12-31");
15
16        search($result, $conf['datadir'], 'search_universal', [
17            'depth' => 0,
18            'listfiles' => true,
19            'listdirs'  => false,
20            'pagesonly' => true,
21            'skipacl' => false,
22            'filematch' => "",
23            'meta' => true,
24            'ns' => 'til',
25        ]);
26
27        // filter with namespace and mtime
28        $result = array_filter($result, function($item) use ($ns_pattern, $since_ts, $until_ts) {
29            if ($ns_pattern) {
30                return preg_match($ns_pattern, $item['ns']) && $item['mtime'] >= $since_ts && $item['mtime'] <= $until_ts;
31            } else {
32                return $item['mtime'] >= $since_ts && $item['mtime'] <= $until_ts;
33            }
34        });
35
36        dbglog($result);
37
38        return $result;
39    }
40
41    public function parse_data( $year, $posts ) {
42        $postCount = [];
43        $postMap = [];
44
45        // Iterate over the array to populate postCount and postMap
46        foreach ($posts as $post) {
47            $meta = p_get_metadata($post['id']);
48            $date = date('Y-m-d', $meta['date']['created']);
49            // $date = date('Y-m-d', $post['mtime']);
50
51            // Update postCount
52            if (!isset($postCount[$date])) {
53                $postCount[$date] = 0;
54            }
55            $postCount[$date]++;
56
57            // Update postMap
58            if (!isset($postMap[$date])) {
59                $postMap[$date] = [];
60            }
61
62            $postMap[$date][] = [
63                'title' => $meta['title'],
64                'url' => wl($post['id'], '', true)
65            ];
66        }
67
68        // Format postCount as a 2D array
69        $postCountFormatted = [];
70        foreach ($postCount as $date => $count) {
71            $postCountFormatted[] = [$date, $count];
72        }
73
74        // Convert postMap to a JSON-friendly structure
75        $postMapFormatted = new stdClass();
76        foreach ($postMap as $date => $posts) {
77            $postMapFormatted->$date = $posts;
78        }
79
80        // Encode to JSON
81        $result = json_encode([
82            'dateRange' => [ "$year-01-01", "$year-12-31" ],
83            'postCount' => $postCountFormatted,
84            'postMap' => $postMapFormatted
85        ]);
86
87        return $result;
88    }
89}
90
91?>
92