xref: /plugin/renderrevisions/helper/storage.php (revision ab3c4129fda348db287dd95b116df414e354e3c9)
1<?php
2
3use dokuwiki\Extension\Plugin;
4
5/**
6 * DokuWiki Plugin renderrevisions (Helper Component)
7 *
8 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
9 * @author Andreas Gohr <dokuwiki@cosmocode.de>
10 */
11class helper_plugin_renderrevisions_storage extends Plugin
12{
13
14    /**
15     * Get the filename for a rendered revision
16     *
17     * @param string $id
18     * @param int $rev
19     * @return string
20     */
21    public function getFilename($id, $rev)
22    {
23        global $conf;
24        return $conf['olddir'] . '/' . utf8_encodeFN($id) . '.' . $rev . '.renderevision.xhtml';
25    }
26
27    /**
28     * Save the rendered content of a revision
29     *
30     * @param string $id
31     * @param int $rev
32     * @param string $content
33     * @return void
34     */
35    public function saveRevision($id, $rev, $content)
36    {
37        $file = $this->getFilename($id, $rev);
38        file_put_contents($file, $content);
39    }
40
41    /**
42     * Load the rendered content of a revision
43     *
44     * @param string $id
45     * @param int $rev
46     * @return false|string
47     */
48    public function getRevision($id, $rev)
49    {
50        if(!$this->hasRevision($id, $rev)) return false;
51        $file = $this->getFilename($id, $rev);
52        return file_get_contents($file);
53    }
54
55    /**
56     * Does a rendered revision exist?
57     *
58     * @param string $id
59     * @param int $rev
60     * @return bool
61     */
62    public function hasRevision($id, $rev)
63    {
64        $file = $this->getFilename($id, $rev);
65        if (!file_exists($file)) return false;
66        return true;
67    }
68
69    /**
70     * Delete rendered revisions that are older than $conf['recent_days']
71     *
72     * @param string $id
73     * @return void
74     */
75    public function cleanUp($id)
76    {
77        global $conf;
78        $files = glob($this->getFilename($id, '*'));
79        foreach ($files as $file) {
80            if (filemtime($file) < time() - $conf['recent_days'] * 24 * 60 * 60) {
81                unlink($file);
82            }
83        }
84    }
85}
86