1<?php
2
3/**
4 * DokuWiki Plugin authorstats (Helper Functions)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  George Chatzisofroniou <sophron@latthi.com>
8 * @author  Constantinos Xanthopoulos <conx@xanthopoulos.info>
9 */
10
11class helper_plugin_authorstats extends DokuWiki_Plugin
12{
13
14    var $basedir;
15    var $summaryfile;
16
17    function __construct()
18    {
19        global $conf;
20        $this->basedir = $conf['cachedir'] . '/_authorstats';
21        $this->summaryfile = $this->basedir . "/summary.json";
22    }
23
24    // Creat directory if missing
25    public function createDirIfMissing()
26    {
27        if (!file_exists($this->basedir)) {
28            mkdir($this->basedir, 0755);
29        }
30    }
31
32    public function statsFileExists()
33    {
34        return file_exists($this->basedir) ? true : false;
35    }
36
37    // Read the saved statistics from the JSON file
38    public function readJSON()
39    {
40        $file = @file_get_contents($this->summaryfile);
41        if (!$file) return array();
42        return json_decode($file, true);
43    }
44
45    // Save the statistics into the JSON file
46    public function saveJSON($authors)
47    {
48        $this->createDirIfMissing();
49        $json = json_encode($authors, true);
50        file_put_contents($this->summaryfile, $json);
51    }
52
53    // Read the saved statistics for user from the JSON file
54    public function readUserJSON($loginname)
55    {
56        $file_contents = @file_get_contents($this->basedir . "/" . $loginname . ".json");
57        if (!$file_contents) return array();
58        return json_decode($file_contents, true);
59    }
60
61    // Save the statistics of user into the JSON file
62    public function saveUserJSON($loginname, $pages)
63    {
64        $this->createDirIfMissing();
65        $json = json_encode($pages, true);
66        file_put_contents($this->basedir . "/" . $loginname . ".json", $json);
67    }
68
69    # Recursive version of glob
70    # Source: https://stackoverflow.com/questions/17160696/php-glob-scan-in-subfolders-for-a-file
71    public function rglob($pattern, $flags = 0)
72    {
73        $files = glob($pattern, $flags);
74        foreach (glob(dirname($pattern) . "/*", GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
75            $files = array_merge(
76                [],
77                ...[$files, $this->rglob($dir . "/" . basename($pattern), $flags)]
78            );
79        }
80        return $files;
81    }
82}
83