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