5 use dokuwiki\Extension\Event
;
6 use dokuwiki\Sitemap\Mapper
;
7 use dokuwiki\Subscriptions\BulkSubscriptionSender
;
8 use dokuwiki\ChangeLog\ChangeLog
;
13 * Run an asynchronous task.
20 * @todo refactor to remove dependencies on globals
21 * @triggers INDEXER_TASKS_RUN
25 global $INPUT, $conf, $ID;
27 // keep running after browser closes connection
28 @ignore_user_abort
(true);
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) {
37 $ID = cleanID($INPUT->str('id'));
39 // Catch any possible output (e.g. errors)
43 header('Content-Type: text/plain');
46 // run one of the jobs
47 $tmp = []; // No event data
48 $evt = new Event('INDEXER_TASKS_RUN', $tmp);
49 if ($evt->advise_before()) {
52 $this->runIndexer() ||
53 $this->runSitemapper() ||
54 $this->sendDigest() ||
55 $this->runTrimRecentChanges() ||
56 $this->runTrimRecentChanges(true))
71 * Just send a 1x1 pixel blank gif to the browser
73 * @author Andreas Gohr <andi@splitbrain.org>
74 * @author Harry Fuecks <fuecks@gmail.com>
76 protected function sendGIF()
78 $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
79 header('Content-Type: image/gif');
80 header('Content-Length: ' . strlen($img));
81 header('Connection: Close');
84 // Browser should drop connection after this
85 // Thinks it's got the whole image
89 * Trims the recent changes cache (or imports the old changelog) as needed.
91 * @param bool $media_changes If the media changelog shall be trimmed instead of
95 * @triggers TASK_RECENTCHANGES_TRIM
96 * @author Ben Coburn <btcoburn@silicodon.net>
98 protected function runTrimRecentChanges($media_changes = false)
102 echo "runTrimRecentChanges($media_changes): started" . NL
;
104 $fn = ($media_changes ?
$conf['media_changelog'] : $conf['changelog']);
106 // Trim the Recent Changes
107 // Trims the recent changes cache to the last $conf['changes_days'] recent
108 // changes or $conf['recent'] items, which ever is larger.
109 // The trimming is only done once a day.
112 (@filemtime
($fn . '.trimmed') +
86400) < time() &&
113 !file_exists($fn . '_tmp')
115 @touch
($fn . '.trimmed');
118 if (count($lines) <= $conf['recent']) {
121 echo "runTrimRecentChanges($media_changes): finished" . NL
;
125 io_saveFile($fn . '_tmp', ''); // presave tmp as 2nd lock
126 $trim_time = time() - $conf['recent_days'] * 86400;
129 $counter = count($lines);
130 for ($i = 0; $i < $counter; $i++
) {
131 $log = ChangeLog
::parseLogLine($lines[$i]);
132 if ($log === false) {
133 continue; // discard junk
136 if ($log['date'] < $trim_time) {
137 // keep old lines for now (append .$i to prevent key collisions)
138 $old_lines[$log['date'] . ".$i"] = $lines[$i];
140 // definitely keep these lines
141 $out_lines[$log['date'] . ".$i"] = $lines[$i];
145 if (count($lines) === count($out_lines)) {
147 @unlink
($fn . '_tmp');
149 echo "runTrimRecentChanges($media_changes): finished" . NL
;
153 // sort the final result, it shouldn't be necessary,
154 // however the extra robustness in making the changelog cache self-correcting is worth it
156 $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum
159 $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines);
163 'isMedia' => $media_changes,
164 'trimmedChangelogLines' => $out_lines,
165 'removedChangelogLines' => $extra > 0 ?
array_slice($old_lines, 0, -$extra) : $old_lines,
167 Event
::createAndTrigger('TASK_RECENTCHANGES_TRIM', $eventData);
168 $out_lines = $eventData['trimmedChangelogLines'];
170 // save trimmed changelog
171 io_saveFile($fn . '_tmp', implode('', $out_lines));
173 if (!rename($fn . '_tmp', $fn)) {
174 // rename failed so try another way...
176 io_saveFile($fn, implode('', $out_lines));
177 @unlink
($fn . '_tmp');
181 echo "runTrimRecentChanges($media_changes): finished" . NL
;
186 echo "runTrimRecentChanges($media_changes): finished" . NL
;
192 * Runs the indexer for the current page
194 * @author Andreas Gohr <andi@splitbrain.org>
196 protected function runIndexer()
199 echo 'runIndexer(): started' . NL
;
201 if ((string) $ID === '') {
206 return idx_addPage($ID, true);
210 * Builds a Google Sitemap of all public pages known to the indexer
212 * The map is placed in the root directory named sitemap.xml.gz - This
213 * file needs to be writable!
215 * @author Andreas Gohr
216 * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html
218 protected function runSitemapper()
220 echo 'runSitemapper(): started' . NL
;
221 $result = Mapper
::generate() && Mapper
::pingSearchEngines();
222 echo 'runSitemapper(): finished' . NL
;
227 * Send digest and list mails for all subscriptions which are in effect for the
230 * @author Adrian Lang <lang@cosmocode.de>
232 protected function sendDigest()
236 echo 'sendDigest(): started' . NL
;
237 if (!actionOK('subscribe')) {
238 echo 'sendDigest(): disabled' . NL
;
241 $sub = new BulkSubscriptionSender();
242 $sent = $sub->sendBulk($ID);
244 echo "sendDigest(): sent $sent mails" . NL
;
245 echo 'sendDigest(): finished' . NL
;