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