xref: /dokuwiki/inc/TaskRunner.php (revision b5cf9c44d4749147db308f747f8dafd443e86c35)
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    {
139493c275SMichael Große        // run one of the jobs
149493c275SMichael Große        $tmp = []; // No event data
159493c275SMichael Große        $evt = new Doku_Event('INDEXER_TASKS_RUN', $tmp);
169493c275SMichael Große        if ($evt->advise_before()) {
179493c275SMichael Große            $this->runIndexer() or
189493c275SMichael Große            $this->runSitemapper() or
199493c275SMichael Große            $this->sendDigest() or
209493c275SMichael Große            $this->runTrimRecentChanges() or
219493c275SMichael Große            $this->runTrimRecentChanges(true) or
229493c275SMichael Große            $evt->advise_after();
239493c275SMichael Große        }
249493c275SMichael Große    }
259493c275SMichael Große
269493c275SMichael Große    /**
279493c275SMichael Große     * Trims the recent changes cache (or imports the old changelog) as needed.
289493c275SMichael Große     *
299493c275SMichael Große     * @param bool $media_changes   If the media changelog shall be trimmed instead of
309493c275SMichael Große     *                              the page changelog
31*b5cf9c44SMichael Große     *
329493c275SMichael Große     * @return bool
339493c275SMichael Große     *
349493c275SMichael Große     * @author Ben Coburn <btcoburn@silicodon.net>
359493c275SMichael Große     */
36*b5cf9c44SMichael Große    protected function runTrimRecentChanges($media_changes = false)
37*b5cf9c44SMichael Große    {
389493c275SMichael Große        global $conf;
399493c275SMichael Große
409493c275SMichael Große        echo "runTrimRecentChanges($media_changes): started" . NL;
419493c275SMichael Große
429493c275SMichael Große        $fn = ($media_changes ? $conf['media_changelog'] : $conf['changelog']);
439493c275SMichael Große
449493c275SMichael Große        // Trim the Recent Changes
459493c275SMichael Große        // Trims the recent changes cache to the last $conf['changes_days'] recent
469493c275SMichael Große        // changes or $conf['recent'] items, which ever is larger.
479493c275SMichael Große        // The trimming is only done once a day.
489493c275SMichael Große        if (file_exists($fn) &&
499493c275SMichael Große            (@filemtime($fn . '.trimmed') + 86400) < time() &&
509493c275SMichael Große            !file_exists($fn . '_tmp')) {
519493c275SMichael Große            @touch($fn . '.trimmed');
529493c275SMichael Große            io_lock($fn);
539493c275SMichael Große            $lines = file($fn);
549493c275SMichael Große            if (count($lines) <= $conf['recent']) {
559493c275SMichael Große                // nothing to trim
569493c275SMichael Große                io_unlock($fn);
579493c275SMichael Große                echo "runTrimRecentChanges($media_changes): finished" . NL;
589493c275SMichael Große                return false;
599493c275SMichael Große            }
609493c275SMichael Große
619493c275SMichael Große            io_saveFile($fn . '_tmp', '');          // presave tmp as 2nd lock
629493c275SMichael Große            $trim_time = time() - $conf['recent_days'] * 86400;
63*b5cf9c44SMichael Große            $out_lines = [];
64*b5cf9c44SMichael Große            $old_lines = [];
659493c275SMichael Große            for ($i = 0; $i < count($lines); $i++) {
669493c275SMichael Große                $log = parseChangelogLine($lines[$i]);
67*b5cf9c44SMichael Große                if ($log === false) {
68*b5cf9c44SMichael Große                    continue;
69*b5cf9c44SMichael Große                }                      // discard junk
709493c275SMichael Große                if ($log['date'] < $trim_time) {
719493c275SMichael Große                    $old_lines[$log['date'] . ".$i"] = $lines[$i];     // keep old lines for now (append .$i to prevent key collisions)
729493c275SMichael Große                } else {
739493c275SMichael Große                    $out_lines[$log['date'] . ".$i"] = $lines[$i];     // definitely keep these lines
749493c275SMichael Große                }
759493c275SMichael Große            }
769493c275SMichael Große
779493c275SMichael Große            if (count($lines) == count($out_lines)) {
789493c275SMichael Große                // nothing to trim
799493c275SMichael Große                @unlink($fn . '_tmp');
809493c275SMichael Große                io_unlock($fn);
819493c275SMichael Große                echo "runTrimRecentChanges($media_changes): finished" . NL;
829493c275SMichael Große                return false;
839493c275SMichael Große            }
849493c275SMichael Große
859493c275SMichael Große            // sort the final result, it shouldn't be necessary,
869493c275SMichael Große            //   however the extra robustness in making the changelog cache self-correcting is worth it
879493c275SMichael Große            ksort($out_lines);
889493c275SMichael Große            $extra = $conf['recent'] - count($out_lines);        // do we need extra lines do bring us up to minimum
899493c275SMichael Große            if ($extra > 0) {
909493c275SMichael Große                ksort($old_lines);
919493c275SMichael Große                $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines);
929493c275SMichael Große            }
939493c275SMichael Große
949493c275SMichael Große            // save trimmed changelog
959493c275SMichael Große            io_saveFile($fn . '_tmp', implode('', $out_lines));
969493c275SMichael Große            @unlink($fn);
979493c275SMichael Große            if (!rename($fn . '_tmp', $fn)) {
989493c275SMichael Große                // rename failed so try another way...
999493c275SMichael Große                io_unlock($fn);
1009493c275SMichael Große                io_saveFile($fn, implode('', $out_lines));
1019493c275SMichael Große                @unlink($fn . '_tmp');
1029493c275SMichael Große            } else {
1039493c275SMichael Große                io_unlock($fn);
1049493c275SMichael Große            }
1059493c275SMichael Große            echo "runTrimRecentChanges($media_changes): finished" . NL;
1069493c275SMichael Große            return true;
1079493c275SMichael Große        }
1089493c275SMichael Große
1099493c275SMichael Große        // nothing done
1109493c275SMichael Große        echo "runTrimRecentChanges($media_changes): finished" . NL;
1119493c275SMichael Große        return false;
1129493c275SMichael Große    }
1139493c275SMichael Große
1149493c275SMichael Große
1159493c275SMichael Große    /**
1169493c275SMichael Große     * Runs the indexer for the current page
1179493c275SMichael Große     *
1189493c275SMichael Große     * @author Andreas Gohr <andi@splitbrain.org>
1199493c275SMichael Große     */
120*b5cf9c44SMichael Große    protected function runIndexer()
121*b5cf9c44SMichael Große    {
1229493c275SMichael Große        global $ID;
1239493c275SMichael Große        global $conf;
124*b5cf9c44SMichael Große        print 'runIndexer(): started' . NL;
1259493c275SMichael Große
126*b5cf9c44SMichael Große        if (!$ID) {
127*b5cf9c44SMichael Große            return false;
128*b5cf9c44SMichael Große        }
1299493c275SMichael Große
1309493c275SMichael Große        // do the work
1319493c275SMichael Große        return idx_addPage($ID, true);
1329493c275SMichael Große    }
1339493c275SMichael Große
1349493c275SMichael Große    /**
1359493c275SMichael Große     * Builds a Google Sitemap of all public pages known to the indexer
1369493c275SMichael Große     *
1379493c275SMichael Große     * The map is placed in the root directory named sitemap.xml.gz - This
1389493c275SMichael Große     * file needs to be writable!
1399493c275SMichael Große     *
1409493c275SMichael Große     * @author Andreas Gohr
1419493c275SMichael Große     * @link   https://www.google.com/webmasters/sitemaps/docs/en/about.html
1429493c275SMichael Große     */
143*b5cf9c44SMichael Große    protected function runSitemapper()
144*b5cf9c44SMichael Große    {
145*b5cf9c44SMichael Große        print 'runSitemapper(): started' . NL;
1469493c275SMichael Große        $result = Sitemapper::generate() && Sitemapper::pingSearchEngines();
1479493c275SMichael Große        print 'runSitemapper(): finished' . NL;
1489493c275SMichael Große        return $result;
1499493c275SMichael Große    }
1509493c275SMichael Große
1519493c275SMichael Große    /**
1529493c275SMichael Große     * Send digest and list mails for all subscriptions which are in effect for the
1539493c275SMichael Große     * current page
1549493c275SMichael Große     *
1559493c275SMichael Große     * @author Adrian Lang <lang@cosmocode.de>
1569493c275SMichael Große     */
157*b5cf9c44SMichael Große    protected function sendDigest()
158*b5cf9c44SMichael Große    {
1599493c275SMichael Große        global $conf;
1609493c275SMichael Große        global $ID;
1619493c275SMichael Große
1629493c275SMichael Große        echo 'sendDigest(): started' . NL;
1639493c275SMichael Große        if (!actionOK('subscribe')) {
1649493c275SMichael Große            echo 'sendDigest(): disabled' . NL;
1659493c275SMichael Große            return false;
1669493c275SMichael Große        }
1679493c275SMichael Große        $sub = new Subscription();
1689493c275SMichael Große        $sent = $sub->send_bulk($ID);
1699493c275SMichael Große
1709493c275SMichael Große        echo "sendDigest(): sent $sent mails" . NL;
1719493c275SMichael Große        echo 'sendDigest(): finished' . NL;
1729493c275SMichael Große        return (bool)$sent;
1739493c275SMichael Große    }
1749493c275SMichael Große}
175