xref: /dokuwiki/inc/TaskRunner.php (revision b413fb0be57e32e93f42b9e9582b10add858941a)
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
31b5cf9c44SMichael Große     *
329493c275SMichael Große     * @return bool
339493c275SMichael Große     *
349493c275SMichael Große     * @author Ben Coburn <btcoburn@silicodon.net>
359493c275SMichael Große     */
36b5cf9c44SMichael Große    protected function runTrimRecentChanges($media_changes = false)
37b5cf9c44SMichael 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;
63b5cf9c44SMichael Große            $out_lines = [];
64b5cf9c44SMichael Große            $old_lines = [];
659493c275SMichael Große            for ($i = 0; $i < count($lines); $i++) {
669493c275SMichael Große                $log = parseChangelogLine($lines[$i]);
67b5cf9c44SMichael Große                if ($log === false) {
68b5cf9c44SMichael Große                    continue;
69b5cf9c44SMichael 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
94*b413fb0bSMichael Große            $eventData = [
95*b413fb0bSMichael Große                'trimmedChangelogLines' => $out_lines,
96*b413fb0bSMichael Große                'removedChangelogLines' => $extra > 0 ? array_slice($old_lines, 0, -$extra) : $old_lines,
97*b413fb0bSMichael Große            ];
98*b413fb0bSMichael Große            trigger_event('TRIM_RECENT_CHANGES', $eventData);
99*b413fb0bSMichael Große            $out_lines = $eventData['trimmedChangelogLines'];
100*b413fb0bSMichael Große
1019493c275SMichael Große            // save trimmed changelog
1029493c275SMichael Große            io_saveFile($fn . '_tmp', implode('', $out_lines));
1039493c275SMichael Große            @unlink($fn);
1049493c275SMichael Große            if (!rename($fn . '_tmp', $fn)) {
1059493c275SMichael Große                // rename failed so try another way...
1069493c275SMichael Große                io_unlock($fn);
1079493c275SMichael Große                io_saveFile($fn, implode('', $out_lines));
1089493c275SMichael Große                @unlink($fn . '_tmp');
1099493c275SMichael Große            } else {
1109493c275SMichael Große                io_unlock($fn);
1119493c275SMichael Große            }
1129493c275SMichael Große            echo "runTrimRecentChanges($media_changes): finished" . NL;
1139493c275SMichael Große            return true;
1149493c275SMichael Große        }
1159493c275SMichael Große
1169493c275SMichael Große        // nothing done
1179493c275SMichael Große        echo "runTrimRecentChanges($media_changes): finished" . NL;
1189493c275SMichael Große        return false;
1199493c275SMichael Große    }
1209493c275SMichael Große
1219493c275SMichael Große
1229493c275SMichael Große    /**
1239493c275SMichael Große     * Runs the indexer for the current page
1249493c275SMichael Große     *
1259493c275SMichael Große     * @author Andreas Gohr <andi@splitbrain.org>
1269493c275SMichael Große     */
127b5cf9c44SMichael Große    protected function runIndexer()
128b5cf9c44SMichael Große    {
1299493c275SMichael Große        global $ID;
1309493c275SMichael Große        global $conf;
131b5cf9c44SMichael Große        print 'runIndexer(): started' . NL;
1329493c275SMichael Große
133b5cf9c44SMichael Große        if (!$ID) {
134b5cf9c44SMichael Große            return false;
135b5cf9c44SMichael Große        }
1369493c275SMichael Große
1379493c275SMichael Große        // do the work
1389493c275SMichael Große        return idx_addPage($ID, true);
1399493c275SMichael Große    }
1409493c275SMichael Große
1419493c275SMichael Große    /**
1429493c275SMichael Große     * Builds a Google Sitemap of all public pages known to the indexer
1439493c275SMichael Große     *
1449493c275SMichael Große     * The map is placed in the root directory named sitemap.xml.gz - This
1459493c275SMichael Große     * file needs to be writable!
1469493c275SMichael Große     *
1479493c275SMichael Große     * @author Andreas Gohr
1489493c275SMichael Große     * @link   https://www.google.com/webmasters/sitemaps/docs/en/about.html
1499493c275SMichael Große     */
150b5cf9c44SMichael Große    protected function runSitemapper()
151b5cf9c44SMichael Große    {
152b5cf9c44SMichael Große        print 'runSitemapper(): started' . NL;
1539493c275SMichael Große        $result = Sitemapper::generate() && Sitemapper::pingSearchEngines();
1549493c275SMichael Große        print 'runSitemapper(): finished' . NL;
1559493c275SMichael Große        return $result;
1569493c275SMichael Große    }
1579493c275SMichael Große
1589493c275SMichael Große    /**
1599493c275SMichael Große     * Send digest and list mails for all subscriptions which are in effect for the
1609493c275SMichael Große     * current page
1619493c275SMichael Große     *
1629493c275SMichael Große     * @author Adrian Lang <lang@cosmocode.de>
1639493c275SMichael Große     */
164b5cf9c44SMichael Große    protected function sendDigest()
165b5cf9c44SMichael Große    {
1669493c275SMichael Große        global $conf;
1679493c275SMichael Große        global $ID;
1689493c275SMichael Große
1699493c275SMichael Große        echo 'sendDigest(): started' . NL;
1709493c275SMichael Große        if (!actionOK('subscribe')) {
1719493c275SMichael Große            echo 'sendDigest(): disabled' . NL;
1729493c275SMichael Große            return false;
1739493c275SMichael Große        }
1749493c275SMichael Große        $sub = new Subscription();
1759493c275SMichael Große        $sent = $sub->send_bulk($ID);
1769493c275SMichael Große
1779493c275SMichael Große        echo "sendDigest(): sent $sent mails" . NL;
1789493c275SMichael Große        echo 'sendDigest(): finished' . NL;
1799493c275SMichael Große        return (bool)$sent;
1809493c275SMichael Große    }
1819493c275SMichael Große}
182