1<?php
2
3
4class helper_plugin_stale extends DokuWiki_Plugin
5{
6
7
8    const CONF_ADMIN_ONLY = 'admin_only';
9    const PLUGIN_NAME = 'stale';
10
11    /**
12     * Touching the configuration file will reset the cache
13     */
14    function touchConfFiles()
15    {
16
17        touch(DOKU_CONF . "local.php");
18        // geshi dependent
19        // https://forum.dokuwiki.org/d/20833-how-to-make-change-to-geshi-language-file-have-effect-on-page-display/11
20        touch(DOKU_CONF . "dokuwiki.php");
21        $dir = new DirectoryIterator(DOKU_PLUGIN);
22        foreach ($dir as $file) {
23            if ($file->isDir() && !$file->isDot()) {
24                $infoPlugin = $file->getPathname() . "/plugin.info.txt";
25                if (file_exists($infoPlugin)) {
26                    touch($infoPlugin);
27                }
28            }
29        }
30
31    }
32
33    /**
34     * @return bool true if the user can touch the file or the reason why it can't
35     *
36     * Because a non-empty string is also true
37     * Use it like this:
38     * $stale->canTouch()!==true
39     *
40     */
41    public function canTouch()
42    {
43        $canTouch = true;
44        if ($this->getConf(self::CONF_ADMIN_ONLY)) {
45            global $USERINFO;
46            if (!auth_isadmin($_SERVER['REMOTE_USER'] ?? null, $USERINFO['grps'] ?? null)) {
47                return "Only admin can touch";
48            }
49        }
50        return $canTouch;
51    }
52
53    public function getIcon()
54    {
55        return __DIR__ . '/images/hand-index-fill.svg';
56    }
57
58    public function deleteSitemap()
59    {
60        global $conf;
61        $cacheDirectory = $conf['cachedir'];
62        $file = $cacheDirectory . "/sitemap.xml.gz";
63        if (file_exists($file)) {
64            unlink($file);
65            return true;
66        } else {
67            return false;
68        }
69
70    }
71
72    public function stale()
73    {
74        $this->touchConfFiles();
75        $message = "The configurations files were touched.";
76        $deleted = $this->deleteSitemap();
77        if ($deleted) {
78            $message .= "<br>The sitemap file was deleted.";
79        } else {
80            $message .= "<br>No sitemap was present.";
81        }
82        $message .= "<br>The cache is now stale.";
83        return $message;
84    }
85
86}
87