xref: /dokuwiki/inc/TaskRunner.php (revision ab9790ca81397622fcfd58faf56a69b68bd36257)
1<?php
2
3namespace dokuwiki;
4
5use dokuwiki\Extension\Event;
6use dokuwiki\Sitemap\Mapper;
7use dokuwiki\Subscriptions\BulkSubscriptionSender;
8use dokuwiki\ChangeLog\ChangeLog;
9
10/**
11 * Class TaskRunner
12 *
13 * Run an asynchronous task.
14 */
15class TaskRunner
16{
17    /**
18     * Run the next task
19     *
20     * @todo refactor to remove dependencies on globals
21     * @triggers INDEXER_TASKS_RUN
22     */
23    public function run()
24    {
25        global $INPUT, $conf, $ID;
26
27        // keep running after browser closes connection
28        @ignore_user_abort(true);
29
30        // check if user abort worked, if yes send output early
31        $defer = !@ignore_user_abort() || $conf['broken_iua'];
32        $output = $INPUT->has('debug') && $conf['allowdebug'];
33        if(!$defer && !$output){
34            $this->sendGIF();
35        }
36
37        $ID = cleanID($INPUT->str('id'));
38
39        // Catch any possible output (e.g. errors)
40        if(!$output) {
41            ob_start();
42        } else {
43            header('Content-Type: text/plain');
44        }
45
46        // run one of the jobs
47        $tmp = []; // No event data
48        $evt = new Event('INDEXER_TASKS_RUN', $tmp);
49        if ($evt->advise_before()) {
50            if (!(
51                $this->runIndexer() ||
52                $this->runSitemapper() ||
53                $this->sendDigest() ||
54                $this->runTrimRecentChanges() ||
55                $this->runTrimRecentChanges(true))
56            ) {
57                $evt->advise_after();
58            }
59        }
60
61        if(!$output) {
62            ob_end_clean();
63            if($defer) {
64                $this->sendGIF();
65            }
66        }
67    }
68
69    /**
70     * Just send a 1x1 pixel blank gif to the browser
71     *
72     * @author Andreas Gohr <andi@splitbrain.org>
73     * @author Harry Fuecks <fuecks@gmail.com>
74     */
75    protected function sendGIF()
76    {
77        $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
78        header('Content-Type: image/gif');
79        header('Content-Length: '.strlen($img));
80        header('Connection: Close');
81        print $img;
82        tpl_flush();
83        // Browser should drop connection after this
84        // Thinks it's got the whole image
85    }
86
87    /**
88     * Trims the recent changes cache (or imports the old changelog) as needed.
89     *
90     * @param bool $media_changes   If the media changelog shall be trimmed instead of
91     *                              the page changelog
92     *
93     * @return bool
94     * @triggers TASK_RECENTCHANGES_TRIM
95     * @author Ben Coburn <btcoburn@silicodon.net>
96     */
97    protected function runTrimRecentChanges($media_changes = false)
98    {
99        global $conf;
100
101        echo "runTrimRecentChanges($media_changes): started" . NL;
102
103        $fn = ($media_changes ? $conf['media_changelog'] : $conf['changelog']);
104
105        // Trim the Recent Changes
106        // Trims the recent changes cache to the last $conf['changes_days'] recent
107        // changes or $conf['recent'] items, which ever is larger.
108        // The trimming is only done once a day.
109        if (file_exists($fn) &&
110            (@filemtime($fn . '.trimmed') + 86400) < time() &&
111            !file_exists($fn . '_tmp')) {
112            @touch($fn . '.trimmed');
113            io_lock($fn);
114            $lines = file($fn);
115            if (count($lines) <= $conf['recent']) {
116                // nothing to trim
117                io_unlock($fn);
118                echo "runTrimRecentChanges($media_changes): finished" . NL;
119                return false;
120            }
121
122            io_saveFile($fn . '_tmp', '');          // presave tmp as 2nd lock
123            $trim_time = time() - $conf['recent_days'] * 86400;
124            $out_lines = [];
125            $old_lines = [];
126            $counter = count($lines);
127            for ($i = 0; $i < $counter; $i++) {
128                $log = ChangeLog::parseLogLine($lines[$i]);
129                if ($log === false) {
130                    continue; // discard junk
131                }
132
133                if ($log['date'] < $trim_time) {
134                    // keep old lines for now (append .$i to prevent key collisions)
135                    $old_lines[$log['date'] . ".$i"] = $lines[$i];
136                } else {
137                    // definitely keep these lines
138                    $out_lines[$log['date'] . ".$i"] = $lines[$i];
139                }
140            }
141
142            if (count($lines) == count($out_lines)) {
143                // nothing to trim
144                @unlink($fn . '_tmp');
145                io_unlock($fn);
146                echo "runTrimRecentChanges($media_changes): finished" . NL;
147                return false;
148            }
149
150            // sort the final result, it shouldn't be necessary,
151            //   however the extra robustness in making the changelog cache self-correcting is worth it
152            ksort($out_lines);
153            $extra = $conf['recent'] - count($out_lines);        // do we need extra lines do bring us up to minimum
154            if ($extra > 0) {
155                ksort($old_lines);
156                $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines);
157            }
158
159            $eventData = [
160                'isMedia' => $media_changes,
161                'trimmedChangelogLines' => $out_lines,
162                'removedChangelogLines' => $extra > 0 ? array_slice($old_lines, 0, -$extra) : $old_lines,
163            ];
164            Event::createAndTrigger('TASK_RECENTCHANGES_TRIM', $eventData);
165            $out_lines = $eventData['trimmedChangelogLines'];
166
167            // save trimmed changelog
168            io_saveFile($fn . '_tmp', implode('', $out_lines));
169            @unlink($fn);
170            if (!rename($fn . '_tmp', $fn)) {
171                // rename failed so try another way...
172                io_unlock($fn);
173                io_saveFile($fn, implode('', $out_lines));
174                @unlink($fn . '_tmp');
175            } else {
176                io_unlock($fn);
177            }
178            echo "runTrimRecentChanges($media_changes): finished" . NL;
179            return true;
180        }
181
182        // nothing done
183        echo "runTrimRecentChanges($media_changes): finished" . NL;
184        return false;
185    }
186
187
188    /**
189     * Runs the indexer for the current page
190     *
191     * @author Andreas Gohr <andi@splitbrain.org>
192     */
193    protected function runIndexer()
194    {
195        global $ID;
196        print 'runIndexer(): started' . NL;
197
198        if ((string) $ID === '') {
199            return false;
200        }
201
202        // do the work
203        return idx_addPage($ID, true);
204    }
205
206    /**
207     * Builds a Google Sitemap of all public pages known to the indexer
208     *
209     * The map is placed in the root directory named sitemap.xml.gz - This
210     * file needs to be writable!
211     *
212     * @author Andreas Gohr
213     * @link   https://www.google.com/webmasters/sitemaps/docs/en/about.html
214     */
215    protected function runSitemapper()
216    {
217        print 'runSitemapper(): started' . NL;
218        $result = Mapper::generate() && Mapper::pingSearchEngines();
219        print 'runSitemapper(): finished' . NL;
220        return $result;
221    }
222
223    /**
224     * Send digest and list mails for all subscriptions which are in effect for the
225     * current page
226     *
227     * @author Adrian Lang <lang@cosmocode.de>
228     */
229    protected function sendDigest()
230    {
231        global $ID;
232
233        echo 'sendDigest(): started' . NL;
234        if (!actionOK('subscribe')) {
235            echo 'sendDigest(): disabled' . NL;
236            return false;
237        }
238        $sub = new BulkSubscriptionSender();
239        $sent = $sub->sendBulk($ID);
240
241        echo "sendDigest(): sent $sent mails" . NL;
242        echo 'sendDigest(): finished' . NL;
243        return (bool)$sent;
244    }
245}
246