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