update tests to match new is_ssl behaviour
[dokuwiki.git] / inc / TaskRunner.php
blobfea2fed572a1a39f15921d4b1df9dd3b37d9a9fa
1 <?php
3 namespace dokuwiki;
5 use dokuwiki\Extension\Event;
6 use dokuwiki\Sitemap\Mapper;
7 use dokuwiki\Subscriptions\BulkSubscriptionSender;
8 use dokuwiki\ChangeLog\ChangeLog;
10 /**
11 * Class TaskRunner
13 * Run an asynchronous task.
15 class TaskRunner
17 /**
18 * Run the next task
20 * @todo refactor to remove dependencies on globals
21 * @triggers INDEXER_TASKS_RUN
23 public function 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) {
34 $this->sendGIF();
37 $ID = cleanID($INPUT->str('id'));
39 // Catch any possible output (e.g. errors)
40 if (!$output) {
41 ob_start();
42 } else {
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 if (
52 $this->runIndexer() ||
53 $this->runSitemapper() ||
54 $this->sendDigest() ||
55 $this->runTrimRecentChanges() ||
56 $this->runTrimRecentChanges(true))
57 ) {
58 $evt->advise_after();
62 if (!$output) {
63 ob_end_clean();
64 if ($defer) {
65 $this->sendGIF();
70 /**
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');
82 echo $img;
83 tpl_flush();
84 // Browser should drop connection after this
85 // Thinks it's got the whole image
88 /**
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
92 * the page changelog
94 * @return bool
95 * @triggers TASK_RECENTCHANGES_TRIM
96 * @author Ben Coburn <btcoburn@silicodon.net>
98 protected function runTrimRecentChanges($media_changes = false)
100 global $conf;
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.
110 if (
111 file_exists($fn) &&
112 (@filemtime($fn . '.trimmed') + 86400) < time() &&
113 !file_exists($fn . '_tmp')
115 @touch($fn . '.trimmed');
116 io_lock($fn);
117 $lines = file($fn);
118 if (count($lines) <= $conf['recent']) {
119 // nothing to trim
120 io_unlock($fn);
121 echo "runTrimRecentChanges($media_changes): finished" . NL;
122 return false;
125 io_saveFile($fn . '_tmp', ''); // presave tmp as 2nd lock
126 $trim_time = time() - $conf['recent_days'] * 86400;
127 $out_lines = [];
128 $old_lines = [];
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];
139 } else {
140 // definitely keep these lines
141 $out_lines[$log['date'] . ".$i"] = $lines[$i];
145 if (count($lines) === count($out_lines)) {
146 // nothing to trim
147 @unlink($fn . '_tmp');
148 io_unlock($fn);
149 echo "runTrimRecentChanges($media_changes): finished" . NL;
150 return false;
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
155 ksort($out_lines);
156 $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum
157 if ($extra > 0) {
158 ksort($old_lines);
159 $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines);
162 $eventData = [
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));
172 @unlink($fn);
173 if (!rename($fn . '_tmp', $fn)) {
174 // rename failed so try another way...
175 io_unlock($fn);
176 io_saveFile($fn, implode('', $out_lines));
177 @unlink($fn . '_tmp');
178 } else {
179 io_unlock($fn);
181 echo "runTrimRecentChanges($media_changes): finished" . NL;
182 return true;
185 // nothing done
186 echo "runTrimRecentChanges($media_changes): finished" . NL;
187 return false;
192 * Runs the indexer for the current page
194 * @author Andreas Gohr <andi@splitbrain.org>
196 protected function runIndexer()
198 global $ID;
199 echo 'runIndexer(): started' . NL;
201 if ((string) $ID === '') {
202 return false;
205 // do the work
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;
223 return $result;
227 * Send digest and list mails for all subscriptions which are in effect for the
228 * current page
230 * @author Adrian Lang <lang@cosmocode.de>
232 protected function sendDigest()
234 global $ID;
236 echo 'sendDigest(): started' . NL;
237 if (!actionOK('subscribe')) {
238 echo 'sendDigest(): disabled' . NL;
239 return false;
241 $sub = new BulkSubscriptionSender();
242 $sent = $sub->sendBulk($ID);
244 echo "sendDigest(): sent $sent mails" . NL;
245 echo 'sendDigest(): finished' . NL;
246 return (bool)$sent;