1<?php 2 3namespace dokuwiki; 4 5use dokuwiki\Extension\Event; 6use dokuwiki\Logger; 7use dokuwiki\Search\Indexer; 8use dokuwiki\Sitemap\Mapper; 9use dokuwiki\Subscriptions\BulkSubscriptionSender; 10use dokuwiki\ChangeLog\ChangeLog; 11 12/** 13 * Class TaskRunner 14 * 15 * Run an asynchronous task. 16 */ 17class TaskRunner 18{ 19 /** 20 * Run the next task 21 * 22 * @todo refactor to remove dependencies on globals 23 * @triggers INDEXER_TASKS_RUN 24 */ 25 public function run() 26 { 27 global $INPUT, $conf, $ID; 28 29 // keep running after browser closes connection 30 @ignore_user_abort(true); 31 32 // check if user abort worked, if yes send output early 33 $defer = !@ignore_user_abort() || $conf['broken_iua']; 34 $output = $INPUT->has('debug') && $conf['allowdebug']; 35 if (!$defer && !$output) { 36 $this->sendGIF(); 37 } 38 39 $ID = cleanID($INPUT->str('id')); 40 41 // Catch any possible output (e.g. errors) 42 if (!$output) { 43 ob_start(); 44 } else { 45 header('Content-Type: text/plain'); 46 } 47 48 // run one of the jobs 49 $tmp = []; // No event data 50 $evt = new Event('INDEXER_TASKS_RUN', $tmp); 51 if ($evt->advise_before()) { 52 if ( 53 !( 54 $this->runIndexer() || 55 $this->runSitemapper() || 56 $this->sendDigest() || 57 $this->runTrimRecentChanges() || 58 $this->runTrimRecentChanges(true)) 59 ) { 60 $evt->advise_after(); 61 } 62 } 63 64 if (!$output) { 65 ob_end_clean(); 66 if ($defer) { 67 $this->sendGIF(); 68 } 69 } 70 } 71 72 /** 73 * Just send a 1x1 pixel blank gif to the browser 74 * 75 * @author Andreas Gohr <andi@splitbrain.org> 76 * @author Harry Fuecks <fuecks@gmail.com> 77 */ 78 protected function sendGIF() 79 { 80 $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7'); 81 header('Content-Type: image/gif'); 82 header('Content-Length: ' . strlen($img)); 83 header('Connection: Close'); 84 echo $img; 85 tpl_flush(); 86 // Browser should drop connection after this 87 // Thinks it's got the whole image 88 } 89 90 /** 91 * Trims the recent changes cache (or imports the old changelog) as needed. 92 * 93 * @param bool $media_changes If the media changelog shall be trimmed instead of 94 * the page changelog 95 * 96 * @return bool 97 * @triggers TASK_RECENTCHANGES_TRIM 98 * @author Ben Coburn <btcoburn@silicodon.net> 99 */ 100 protected function runTrimRecentChanges($media_changes = false) 101 { 102 global $conf; 103 104 echo "runTrimRecentChanges($media_changes): started" . NL; 105 106 $fn = ($media_changes ? $conf['media_changelog'] : $conf['changelog']); 107 108 // Trim the Recent Changes 109 // Trims the recent changes cache to the last $conf['changes_days'] recent 110 // changes or $conf['recent'] items, which ever is larger. 111 // The trimming is only done once a day. 112 if ( 113 file_exists($fn) && 114 (@filemtime($fn . '.trimmed') + 86400) < time() && 115 !file_exists($fn . '_tmp') 116 ) { 117 @touch($fn . '.trimmed'); 118 io_lock($fn); 119 $lines = file($fn); 120 if (count($lines) <= $conf['recent']) { 121 // nothing to trim 122 io_unlock($fn); 123 echo "runTrimRecentChanges($media_changes): finished" . NL; 124 return false; 125 } 126 127 io_saveFile($fn . '_tmp', ''); // presave tmp as 2nd lock 128 $trim_time = time() - $conf['recent_days'] * 86400; 129 $out_lines = []; 130 $old_lines = []; 131 $counter = count($lines); 132 for ($i = 0; $i < $counter; $i++) { 133 $log = ChangeLog::parseLogLine($lines[$i]); 134 if ($log === false) { 135 continue; // discard junk 136 } 137 138 if ($log['date'] < $trim_time) { 139 // keep old lines for now (append .$i to prevent key collisions) 140 $old_lines[$log['date'] . ".$i"] = $lines[$i]; 141 } else { 142 // definitely keep these lines 143 $out_lines[$log['date'] . ".$i"] = $lines[$i]; 144 } 145 } 146 147 if (count($lines) === count($out_lines)) { 148 // nothing to trim 149 @unlink($fn . '_tmp'); 150 io_unlock($fn); 151 echo "runTrimRecentChanges($media_changes): finished" . NL; 152 return false; 153 } 154 155 // sort the final result, it shouldn't be necessary, 156 // however the extra robustness in making the changelog cache self-correcting is worth it 157 ksort($out_lines); 158 $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum 159 if ($extra > 0) { 160 ksort($old_lines); 161 $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines); 162 } 163 164 $eventData = [ 165 'isMedia' => $media_changes, 166 'trimmedChangelogLines' => $out_lines, 167 'removedChangelogLines' => $extra > 0 ? array_slice($old_lines, 0, -$extra) : $old_lines, 168 ]; 169 Event::createAndTrigger('TASK_RECENTCHANGES_TRIM', $eventData); 170 $out_lines = $eventData['trimmedChangelogLines']; 171 172 // save trimmed changelog 173 io_saveFile($fn . '_tmp', implode('', $out_lines)); 174 @unlink($fn); 175 if (!rename($fn . '_tmp', $fn)) { 176 // rename failed so try another way... 177 io_unlock($fn); 178 io_saveFile($fn, implode('', $out_lines)); 179 @unlink($fn . '_tmp'); 180 } else { 181 io_unlock($fn); 182 } 183 echo "runTrimRecentChanges($media_changes): finished" . NL; 184 return true; 185 } 186 187 // nothing done 188 echo "runTrimRecentChanges($media_changes): finished" . NL; 189 return false; 190 } 191 192 193 /** 194 * Runs the indexer for the current page 195 * 196 * @author Andreas Gohr <andi@splitbrain.org> 197 */ 198 protected function runIndexer() 199 { 200 global $ID; 201 echo 'runIndexer(): started' . NL; 202 203 if ((string) $ID === '') { 204 return false; 205 } 206 207 // do the work 208 try { 209 return (new Indexer($ID))->dispatch(true); 210 } catch (Search\Exception\SearchException $e) { 211 $msg = get_class($e) .' : '. $e->getMessage(); 212 echo $msg; 213 Logger::debug($msg); 214 return false; 215 } 216 } 217 218 /** 219 * Builds a Google Sitemap of all public pages known to the indexer 220 * 221 * The map is placed in the root directory named sitemap.xml.gz - This 222 * file needs to be writable! 223 * 224 * @author Andreas Gohr 225 * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html 226 */ 227 protected function runSitemapper() 228 { 229 echo 'runSitemapper(): started' . NL; 230 $result = Mapper::generate() && Mapper::pingSearchEngines(); 231 echo 'runSitemapper(): finished' . NL; 232 return $result; 233 } 234 235 /** 236 * Send digest and list mails for all subscriptions which are in effect for the 237 * current page 238 * 239 * @author Adrian Lang <lang@cosmocode.de> 240 */ 241 protected function sendDigest() 242 { 243 global $ID; 244 245 echo 'sendDigest(): started' . NL; 246 if (!actionOK('subscribe')) { 247 echo 'sendDigest(): disabled' . NL; 248 return false; 249 } 250 $sub = new BulkSubscriptionSender(); 251 $sent = $sub->sendBulk($ID); 252 253 echo "sendDigest(): sent $sent mails" . NL; 254 echo 'sendDigest(): finished' . NL; 255 return (bool)$sent; 256 } 257} 258