xref: /dokuwiki/inc/TaskRunner.php (revision b5cf9c44d4749147db308f747f8dafd443e86c35)
1<?php
2
3namespace dokuwiki;
4
5use Doku_Event;
6use Sitemapper;
7use Subscription;
8
9class TaskRunner
10{
11    public function run()
12    {
13        // run one of the jobs
14        $tmp = []; // No event data
15        $evt = new Doku_Event('INDEXER_TASKS_RUN', $tmp);
16        if ($evt->advise_before()) {
17            $this->runIndexer() or
18            $this->runSitemapper() or
19            $this->sendDigest() or
20            $this->runTrimRecentChanges() or
21            $this->runTrimRecentChanges(true) or
22            $evt->advise_after();
23        }
24    }
25
26    /**
27     * Trims the recent changes cache (or imports the old changelog) as needed.
28     *
29     * @param bool $media_changes   If the media changelog shall be trimmed instead of
30     *                              the page changelog
31     *
32     * @return bool
33     *
34     * @author Ben Coburn <btcoburn@silicodon.net>
35     */
36    protected function runTrimRecentChanges($media_changes = false)
37    {
38        global $conf;
39
40        echo "runTrimRecentChanges($media_changes): started" . NL;
41
42        $fn = ($media_changes ? $conf['media_changelog'] : $conf['changelog']);
43
44        // Trim the Recent Changes
45        // Trims the recent changes cache to the last $conf['changes_days'] recent
46        // changes or $conf['recent'] items, which ever is larger.
47        // The trimming is only done once a day.
48        if (file_exists($fn) &&
49            (@filemtime($fn . '.trimmed') + 86400) < time() &&
50            !file_exists($fn . '_tmp')) {
51            @touch($fn . '.trimmed');
52            io_lock($fn);
53            $lines = file($fn);
54            if (count($lines) <= $conf['recent']) {
55                // nothing to trim
56                io_unlock($fn);
57                echo "runTrimRecentChanges($media_changes): finished" . NL;
58                return false;
59            }
60
61            io_saveFile($fn . '_tmp', '');          // presave tmp as 2nd lock
62            $trim_time = time() - $conf['recent_days'] * 86400;
63            $out_lines = [];
64            $old_lines = [];
65            for ($i = 0; $i < count($lines); $i++) {
66                $log = parseChangelogLine($lines[$i]);
67                if ($log === false) {
68                    continue;
69                }                      // discard junk
70                if ($log['date'] < $trim_time) {
71                    $old_lines[$log['date'] . ".$i"] = $lines[$i];     // keep old lines for now (append .$i to prevent key collisions)
72                } else {
73                    $out_lines[$log['date'] . ".$i"] = $lines[$i];     // definitely keep these lines
74                }
75            }
76
77            if (count($lines) == count($out_lines)) {
78                // nothing to trim
79                @unlink($fn . '_tmp');
80                io_unlock($fn);
81                echo "runTrimRecentChanges($media_changes): finished" . NL;
82                return false;
83            }
84
85            // sort the final result, it shouldn't be necessary,
86            //   however the extra robustness in making the changelog cache self-correcting is worth it
87            ksort($out_lines);
88            $extra = $conf['recent'] - count($out_lines);        // do we need extra lines do bring us up to minimum
89            if ($extra > 0) {
90                ksort($old_lines);
91                $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines);
92            }
93
94            // save trimmed changelog
95            io_saveFile($fn . '_tmp', implode('', $out_lines));
96            @unlink($fn);
97            if (!rename($fn . '_tmp', $fn)) {
98                // rename failed so try another way...
99                io_unlock($fn);
100                io_saveFile($fn, implode('', $out_lines));
101                @unlink($fn . '_tmp');
102            } else {
103                io_unlock($fn);
104            }
105            echo "runTrimRecentChanges($media_changes): finished" . NL;
106            return true;
107        }
108
109        // nothing done
110        echo "runTrimRecentChanges($media_changes): finished" . NL;
111        return false;
112    }
113
114
115    /**
116     * Runs the indexer for the current page
117     *
118     * @author Andreas Gohr <andi@splitbrain.org>
119     */
120    protected function runIndexer()
121    {
122        global $ID;
123        global $conf;
124        print 'runIndexer(): started' . NL;
125
126        if (!$ID) {
127            return false;
128        }
129
130        // do the work
131        return idx_addPage($ID, true);
132    }
133
134    /**
135     * Builds a Google Sitemap of all public pages known to the indexer
136     *
137     * The map is placed in the root directory named sitemap.xml.gz - This
138     * file needs to be writable!
139     *
140     * @author Andreas Gohr
141     * @link   https://www.google.com/webmasters/sitemaps/docs/en/about.html
142     */
143    protected function runSitemapper()
144    {
145        print 'runSitemapper(): started' . NL;
146        $result = Sitemapper::generate() && Sitemapper::pingSearchEngines();
147        print 'runSitemapper(): finished' . NL;
148        return $result;
149    }
150
151    /**
152     * Send digest and list mails for all subscriptions which are in effect for the
153     * current page
154     *
155     * @author Adrian Lang <lang@cosmocode.de>
156     */
157    protected function sendDigest()
158    {
159        global $conf;
160        global $ID;
161
162        echo 'sendDigest(): started' . NL;
163        if (!actionOK('subscribe')) {
164            echo 'sendDigest(): disabled' . NL;
165            return false;
166        }
167        $sub = new Subscription();
168        $sent = $sub->send_bulk($ID);
169
170        echo "sendDigest(): sent $sent mails" . NL;
171        echo 'sendDigest(): finished' . NL;
172        return (bool)$sent;
173    }
174}
175