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()) {
50 $this->runIndexer() or
51 $this->runSitemapper() or
52 $this->sendDigest() or
53 $this->runTrimRecentChanges() or
54 $this->runTrimRecentChanges(true) or
67 * Just send a 1x1 pixel blank gif to the browser
69 * @author Andreas Gohr <andi@splitbrain.org>
70 * @author Harry Fuecks <fuecks@gmail.com>
72 protected function sendGIF()
74 $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
75 header('Content-Type: image/gif');
76 header('Content-Length: '.strlen($img));
77 header('Connection: Close');
80 // Browser should drop connection after this
81 // Thinks it's got the whole image
85 * Trims the recent changes cache (or imports the old changelog) as needed.
87 * @param bool $media_changes If the media changelog shall be trimmed instead of
91 * @triggers TASK_RECENTCHANGES_TRIM
92 * @author Ben Coburn <btcoburn@silicodon.net>
94 protected function runTrimRecentChanges($media_changes = false)
98 echo "runTrimRecentChanges($media_changes): started" . NL
;
100 $fn = ($media_changes ?
$conf['media_changelog'] : $conf['changelog']);
102 // Trim the Recent Changes
103 // Trims the recent changes cache to the last $conf['changes_days'] recent
104 // changes or $conf['recent'] items, which ever is larger.
105 // The trimming is only done once a day.
106 if (file_exists($fn) &&
107 (@filemtime
($fn . '.trimmed') +
86400) < time() &&
108 !file_exists($fn . '_tmp')) {
109 @touch
($fn . '.trimmed');
112 if (count($lines) <= $conf['recent']) {
115 echo "runTrimRecentChanges($media_changes): finished" . NL
;
119 io_saveFile($fn . '_tmp', ''); // presave tmp as 2nd lock
120 $trim_time = time() - $conf['recent_days'] * 86400;
123 for ($i = 0; $i < count($lines); $i++
) {
124 $log = ChangeLog
::parseLogLine($lines[$i]);
125 if ($log === false) {
126 continue; // discard junk
129 if ($log['date'] < $trim_time) {
130 // keep old lines for now (append .$i to prevent key collisions)
131 $old_lines[$log['date'] . ".$i"] = $lines[$i];
133 // definitely keep these lines
134 $out_lines[$log['date'] . ".$i"] = $lines[$i];
138 if (count($lines) == count($out_lines)) {
140 @unlink
($fn . '_tmp');
142 echo "runTrimRecentChanges($media_changes): finished" . NL
;
146 // sort the final result, it shouldn't be necessary,
147 // however the extra robustness in making the changelog cache self-correcting is worth it
149 $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum
152 $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines);
156 'isMedia' => $media_changes,
157 'trimmedChangelogLines' => $out_lines,
158 'removedChangelogLines' => $extra > 0 ?
array_slice($old_lines, 0, -$extra) : $old_lines,
160 Event
::createAndTrigger('TASK_RECENTCHANGES_TRIM', $eventData);
161 $out_lines = $eventData['trimmedChangelogLines'];
163 // save trimmed changelog
164 io_saveFile($fn . '_tmp', implode('', $out_lines));
166 if (!rename($fn . '_tmp', $fn)) {
167 // rename failed so try another way...
169 io_saveFile($fn, implode('', $out_lines));
170 @unlink
($fn . '_tmp');
174 echo "runTrimRecentChanges($media_changes): finished" . NL
;
179 echo "runTrimRecentChanges($media_changes): finished" . NL
;
185 * Runs the indexer for the current page
187 * @author Andreas Gohr <andi@splitbrain.org>
189 protected function runIndexer()
192 print 'runIndexer(): started' . NL
;
194 if ((string) $ID === '') {
199 return idx_addPage($ID, true);
203 * Builds a Google Sitemap of all public pages known to the indexer
205 * The map is placed in the root directory named sitemap.xml.gz - This
206 * file needs to be writable!
208 * @author Andreas Gohr
209 * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html
211 protected function runSitemapper()
213 print 'runSitemapper(): started' . NL
;
214 $result = Mapper
::generate() && Mapper
::pingSearchEngines();
215 print 'runSitemapper(): finished' . NL
;
220 * Send digest and list mails for all subscriptions which are in effect for the
223 * @author Adrian Lang <lang@cosmocode.de>
225 protected function sendDigest()
229 echo 'sendDigest(): started' . NL
;
230 if (!actionOK('subscribe')) {
231 echo 'sendDigest(): disabled' . NL
;
234 $sub = new BulkSubscriptionSender();
235 $sent = $sub->sendBulk($ID);
237 echo "sendDigest(): sent $sent mails" . NL
;
238 echo 'sendDigest(): finished' . NL
;