xref: /dokuwiki/inc/TaskRunner.php (revision 3b58faf6603a05d52ceb517f2e7a172e94e393eb)
19493c275SMichael Große<?php
29493c275SMichael Große
39493c275SMichael Großenamespace dokuwiki;
49493c275SMichael Große
59493c275SMichael Großeuse Doku_Event;
69493c275SMichael Großeuse Sitemapper;
79493c275SMichael Großeuse Subscription;
89493c275SMichael Große
99493c275SMichael Großeclass TaskRunner
109493c275SMichael Große{
119493c275SMichael Große    public function run()
129493c275SMichael Große    {
13*3b58faf6SMichael Große        global $INPUT, $conf, $ID;
14*3b58faf6SMichael Große
15*3b58faf6SMichael Große        // keep running after browser closes connection
16*3b58faf6SMichael Große        @ignore_user_abort(true);
17*3b58faf6SMichael Große
18*3b58faf6SMichael Große        // check if user abort worked, if yes send output early
19*3b58faf6SMichael Große        $defer = !@ignore_user_abort() || $conf['broken_iua'];
20*3b58faf6SMichael Große        $output = $INPUT->has('debug') && $conf['allowdebug'];
21*3b58faf6SMichael Große        if(!$defer && !$output){
22*3b58faf6SMichael Große            $this->sendGIF();
23*3b58faf6SMichael Große        }
24*3b58faf6SMichael Große
25*3b58faf6SMichael Große        $ID = cleanID($INPUT->str('id'));
26*3b58faf6SMichael Große
27*3b58faf6SMichael Große        // Catch any possible output (e.g. errors)
28*3b58faf6SMichael Große        if(!$output) {
29*3b58faf6SMichael Große            ob_start();
30*3b58faf6SMichael Große        } else {
31*3b58faf6SMichael Große            header('Content-Type: text/plain');
32*3b58faf6SMichael Große        }
33*3b58faf6SMichael Große
349493c275SMichael Große        // run one of the jobs
359493c275SMichael Große        $tmp = []; // No event data
369493c275SMichael Große        $evt = new Doku_Event('INDEXER_TASKS_RUN', $tmp);
379493c275SMichael Große        if ($evt->advise_before()) {
389493c275SMichael Große            $this->runIndexer() or
399493c275SMichael Große            $this->runSitemapper() or
409493c275SMichael Große            $this->sendDigest() or
419493c275SMichael Große            $this->runTrimRecentChanges() or
429493c275SMichael Große            $this->runTrimRecentChanges(true) or
439493c275SMichael Große            $evt->advise_after();
449493c275SMichael Große        }
45*3b58faf6SMichael Große
46*3b58faf6SMichael Große        if(!$output) {
47*3b58faf6SMichael Große            ob_end_clean();
48*3b58faf6SMichael Große            if($defer) {
49*3b58faf6SMichael Große                $this->sendGIF();
50*3b58faf6SMichael Große            }
51*3b58faf6SMichael Große        }
52*3b58faf6SMichael Große    }
53*3b58faf6SMichael Große
54*3b58faf6SMichael Große    /**
55*3b58faf6SMichael Große     * Just send a 1x1 pixel blank gif to the browser
56*3b58faf6SMichael Große     *
57*3b58faf6SMichael Große     * @author Andreas Gohr <andi@splitbrain.org>
58*3b58faf6SMichael Große     * @author Harry Fuecks <fuecks@gmail.com>
59*3b58faf6SMichael Große     */
60*3b58faf6SMichael Große    function sendGIF(){
61*3b58faf6SMichael Große        $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
62*3b58faf6SMichael Große        header('Content-Type: image/gif');
63*3b58faf6SMichael Große        header('Content-Length: '.strlen($img));
64*3b58faf6SMichael Große        header('Connection: Close');
65*3b58faf6SMichael Große        print $img;
66*3b58faf6SMichael Große        tpl_flush();
67*3b58faf6SMichael Große        // Browser should drop connection after this
68*3b58faf6SMichael Große        // Thinks it's got the whole image
699493c275SMichael Große    }
709493c275SMichael Große
719493c275SMichael Große    /**
729493c275SMichael Große     * Trims the recent changes cache (or imports the old changelog) as needed.
739493c275SMichael Große     *
749493c275SMichael Große     * @param bool $media_changes   If the media changelog shall be trimmed instead of
759493c275SMichael Große     *                              the page changelog
76b5cf9c44SMichael Große     *
779493c275SMichael Große     * @return bool
789493c275SMichael Große     *
799493c275SMichael Große     * @author Ben Coburn <btcoburn@silicodon.net>
809493c275SMichael Große     */
81b5cf9c44SMichael Große    protected function runTrimRecentChanges($media_changes = false)
82b5cf9c44SMichael Große    {
839493c275SMichael Große        global $conf;
849493c275SMichael Große
859493c275SMichael Große        echo "runTrimRecentChanges($media_changes): started" . NL;
869493c275SMichael Große
879493c275SMichael Große        $fn = ($media_changes ? $conf['media_changelog'] : $conf['changelog']);
889493c275SMichael Große
899493c275SMichael Große        // Trim the Recent Changes
909493c275SMichael Große        // Trims the recent changes cache to the last $conf['changes_days'] recent
919493c275SMichael Große        // changes or $conf['recent'] items, which ever is larger.
929493c275SMichael Große        // The trimming is only done once a day.
939493c275SMichael Große        if (file_exists($fn) &&
949493c275SMichael Große            (@filemtime($fn . '.trimmed') + 86400) < time() &&
959493c275SMichael Große            !file_exists($fn . '_tmp')) {
969493c275SMichael Große            @touch($fn . '.trimmed');
979493c275SMichael Große            io_lock($fn);
989493c275SMichael Große            $lines = file($fn);
999493c275SMichael Große            if (count($lines) <= $conf['recent']) {
1009493c275SMichael Große                // nothing to trim
1019493c275SMichael Große                io_unlock($fn);
1029493c275SMichael Große                echo "runTrimRecentChanges($media_changes): finished" . NL;
1039493c275SMichael Große                return false;
1049493c275SMichael Große            }
1059493c275SMichael Große
1069493c275SMichael Große            io_saveFile($fn . '_tmp', '');          // presave tmp as 2nd lock
1079493c275SMichael Große            $trim_time = time() - $conf['recent_days'] * 86400;
108b5cf9c44SMichael Große            $out_lines = [];
109b5cf9c44SMichael Große            $old_lines = [];
1109493c275SMichael Große            for ($i = 0; $i < count($lines); $i++) {
1119493c275SMichael Große                $log = parseChangelogLine($lines[$i]);
112b5cf9c44SMichael Große                if ($log === false) {
113b5cf9c44SMichael Große                    continue;
114b5cf9c44SMichael Große                }                      // discard junk
1159493c275SMichael Große                if ($log['date'] < $trim_time) {
1169493c275SMichael Große                    $old_lines[$log['date'] . ".$i"] = $lines[$i];     // keep old lines for now (append .$i to prevent key collisions)
1179493c275SMichael Große                } else {
1189493c275SMichael Große                    $out_lines[$log['date'] . ".$i"] = $lines[$i];     // definitely keep these lines
1199493c275SMichael Große                }
1209493c275SMichael Große            }
1219493c275SMichael Große
1229493c275SMichael Große            if (count($lines) == count($out_lines)) {
1239493c275SMichael Große                // nothing to trim
1249493c275SMichael Große                @unlink($fn . '_tmp');
1259493c275SMichael Große                io_unlock($fn);
1269493c275SMichael Große                echo "runTrimRecentChanges($media_changes): finished" . NL;
1279493c275SMichael Große                return false;
1289493c275SMichael Große            }
1299493c275SMichael Große
1309493c275SMichael Große            // sort the final result, it shouldn't be necessary,
1319493c275SMichael Große            //   however the extra robustness in making the changelog cache self-correcting is worth it
1329493c275SMichael Große            ksort($out_lines);
1339493c275SMichael Große            $extra = $conf['recent'] - count($out_lines);        // do we need extra lines do bring us up to minimum
1349493c275SMichael Große            if ($extra > 0) {
1359493c275SMichael Große                ksort($old_lines);
1369493c275SMichael Große                $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines);
1379493c275SMichael Große            }
1389493c275SMichael Große
139b413fb0bSMichael Große            $eventData = [
140b413fb0bSMichael Große                'trimmedChangelogLines' => $out_lines,
141b413fb0bSMichael Große                'removedChangelogLines' => $extra > 0 ? array_slice($old_lines, 0, -$extra) : $old_lines,
142b413fb0bSMichael Große            ];
143b413fb0bSMichael Große            trigger_event('TRIM_RECENT_CHANGES', $eventData);
144b413fb0bSMichael Große            $out_lines = $eventData['trimmedChangelogLines'];
145b413fb0bSMichael Große
1469493c275SMichael Große            // save trimmed changelog
1479493c275SMichael Große            io_saveFile($fn . '_tmp', implode('', $out_lines));
1489493c275SMichael Große            @unlink($fn);
1499493c275SMichael Große            if (!rename($fn . '_tmp', $fn)) {
1509493c275SMichael Große                // rename failed so try another way...
1519493c275SMichael Große                io_unlock($fn);
1529493c275SMichael Große                io_saveFile($fn, implode('', $out_lines));
1539493c275SMichael Große                @unlink($fn . '_tmp');
1549493c275SMichael Große            } else {
1559493c275SMichael Große                io_unlock($fn);
1569493c275SMichael Große            }
1579493c275SMichael Große            echo "runTrimRecentChanges($media_changes): finished" . NL;
1589493c275SMichael Große            return true;
1599493c275SMichael Große        }
1609493c275SMichael Große
1619493c275SMichael Große        // nothing done
1629493c275SMichael Große        echo "runTrimRecentChanges($media_changes): finished" . NL;
1639493c275SMichael Große        return false;
1649493c275SMichael Große    }
1659493c275SMichael Große
1669493c275SMichael Große
1679493c275SMichael Große    /**
1689493c275SMichael Große     * Runs the indexer for the current page
1699493c275SMichael Große     *
1709493c275SMichael Große     * @author Andreas Gohr <andi@splitbrain.org>
1719493c275SMichael Große     */
172b5cf9c44SMichael Große    protected function runIndexer()
173b5cf9c44SMichael Große    {
1749493c275SMichael Große        global $ID;
1759493c275SMichael Große        global $conf;
176b5cf9c44SMichael Große        print 'runIndexer(): started' . NL;
1779493c275SMichael Große
178b5cf9c44SMichael Große        if (!$ID) {
179b5cf9c44SMichael Große            return false;
180b5cf9c44SMichael Große        }
1819493c275SMichael Große
1829493c275SMichael Große        // do the work
1839493c275SMichael Große        return idx_addPage($ID, true);
1849493c275SMichael Große    }
1859493c275SMichael Große
1869493c275SMichael Große    /**
1879493c275SMichael Große     * Builds a Google Sitemap of all public pages known to the indexer
1889493c275SMichael Große     *
1899493c275SMichael Große     * The map is placed in the root directory named sitemap.xml.gz - This
1909493c275SMichael Große     * file needs to be writable!
1919493c275SMichael Große     *
1929493c275SMichael Große     * @author Andreas Gohr
1939493c275SMichael Große     * @link   https://www.google.com/webmasters/sitemaps/docs/en/about.html
1949493c275SMichael Große     */
195b5cf9c44SMichael Große    protected function runSitemapper()
196b5cf9c44SMichael Große    {
197b5cf9c44SMichael Große        print 'runSitemapper(): started' . NL;
1989493c275SMichael Große        $result = Sitemapper::generate() && Sitemapper::pingSearchEngines();
1999493c275SMichael Große        print 'runSitemapper(): finished' . NL;
2009493c275SMichael Große        return $result;
2019493c275SMichael Große    }
2029493c275SMichael Große
2039493c275SMichael Große    /**
2049493c275SMichael Große     * Send digest and list mails for all subscriptions which are in effect for the
2059493c275SMichael Große     * current page
2069493c275SMichael Große     *
2079493c275SMichael Große     * @author Adrian Lang <lang@cosmocode.de>
2089493c275SMichael Große     */
209b5cf9c44SMichael Große    protected function sendDigest()
210b5cf9c44SMichael Große    {
2119493c275SMichael Große        global $conf;
2129493c275SMichael Große        global $ID;
2139493c275SMichael Große
2149493c275SMichael Große        echo 'sendDigest(): started' . NL;
2159493c275SMichael Große        if (!actionOK('subscribe')) {
2169493c275SMichael Große            echo 'sendDigest(): disabled' . NL;
2179493c275SMichael Große            return false;
2189493c275SMichael Große        }
2199493c275SMichael Große        $sub = new Subscription();
2209493c275SMichael Große        $sent = $sub->send_bulk($ID);
2219493c275SMichael Große
2229493c275SMichael Große        echo "sendDigest(): sent $sent mails" . NL;
2239493c275SMichael Große        echo 'sendDigest(): finished' . NL;
2249493c275SMichael Große        return (bool)$sent;
2259493c275SMichael Große    }
2269493c275SMichael Große}
227