xref: /plugin/recommend/helper/log.php (revision c6f9d3d590f898165ce791277f4554ff3ef97548)
1<?php
2
3class helper_plugin_recommend_log
4{
5    protected $path;
6
7    /**
8     * @param $month
9     */
10    public function __construct($month)
11    {
12        $this->path = DOKU_INC . 'data/cache/recommend';
13        if (!file_exists($this->path)) {
14            mkdir($this->path);
15        }
16        $this->path .= '/' . $month . '.log';
17    }
18
19    public function getLogs()
20    {
21        return array_map([$this, 'recommendStripExtension'], glob(DOKU_INC . 'data/cache/recommend/*.log'));
22    }
23
24    public function getEntries()
25    {
26        return @file($this->path);
27    }
28
29    public function writeEntry($page, $sender, $receiver, $comment)
30    {
31        $comment = str_replace(["\n", '"'], ['', '\''], $comment);
32        $logfile = fopen($this->path, 'a');
33        fwrite($logfile, date('r') . ': ' .
34                         "“${sender}” recommended “${page}” to " .
35                         "“${receiver}” with comment “${comment}”.\n");
36        fclose($logfile);
37    }
38
39
40    protected function recommendStripExtension($str)
41    {
42        return substr(basename($str), 0, -4);
43    }
44}
45