19493c275SMichael Große<?php 29493c275SMichael Große 39493c275SMichael Großenamespace dokuwiki; 49493c275SMichael Große 5e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 63df1553dSSatoshi Saharause dokuwiki\Logger; 74027a91aSSatoshi Saharause dokuwiki\Search\Indexer; 8432adb37SAndreas Gohruse dokuwiki\Sitemap\Mapper; 9704a815fSMichael Großeuse dokuwiki\Subscriptions\BulkSubscriptionSender; 101d11f1d3SSatoshi Saharause dokuwiki\ChangeLog\ChangeLog; 119493c275SMichael Große 123ad4c3cdSAndreas Gohr/** 133ad4c3cdSAndreas Gohr * Class TaskRunner 143ad4c3cdSAndreas Gohr * 153ad4c3cdSAndreas Gohr * Run an asynchronous task. 163ad4c3cdSAndreas Gohr */ 179493c275SMichael Großeclass TaskRunner 189493c275SMichael Große{ 193ad4c3cdSAndreas Gohr /** 203ad4c3cdSAndreas Gohr * Run the next task 213ad4c3cdSAndreas Gohr * 223ad4c3cdSAndreas Gohr * @todo refactor to remove dependencies on globals 233ad4c3cdSAndreas Gohr * @triggers INDEXER_TASKS_RUN 243ad4c3cdSAndreas Gohr */ 259493c275SMichael Große public function run() 269493c275SMichael Große { 273b58faf6SMichael Große global $INPUT, $conf, $ID; 283b58faf6SMichael Große 293b58faf6SMichael Große // keep running after browser closes connection 303b58faf6SMichael Große @ignore_user_abort(true); 313b58faf6SMichael Große 323b58faf6SMichael Große // check if user abort worked, if yes send output early 333b58faf6SMichael Große $defer = !@ignore_user_abort() || $conf['broken_iua']; 343b58faf6SMichael Große $output = $INPUT->has('debug') && $conf['allowdebug']; 353b58faf6SMichael Große if (!$defer && !$output) { 363b58faf6SMichael Große $this->sendGIF(); 373b58faf6SMichael Große } 383b58faf6SMichael Große 393b58faf6SMichael Große $ID = cleanID($INPUT->str('id')); 403b58faf6SMichael Große 413b58faf6SMichael Große // Catch any possible output (e.g. errors) 423b58faf6SMichael Große if (!$output) { 433b58faf6SMichael Große ob_start(); 443b58faf6SMichael Große } else { 453b58faf6SMichael Große header('Content-Type: text/plain'); 463b58faf6SMichael Große } 473b58faf6SMichael Große 489493c275SMichael Große // run one of the jobs 499493c275SMichael Große $tmp = []; // No event data 50e1d9dcc8SAndreas Gohr $evt = new Event('INDEXER_TASKS_RUN', $tmp); 519493c275SMichael Große if ($evt->advise_before()) { 527d34963bSAndreas Gohr if ( 537d34963bSAndreas Gohr !( 5424870174SAndreas Gohr $this->runIndexer() || 5524870174SAndreas Gohr $this->runSitemapper() || 5624870174SAndreas Gohr $this->sendDigest() || 5724870174SAndreas Gohr $this->runTrimRecentChanges() || 5824870174SAndreas Gohr $this->runTrimRecentChanges(true)) 5924870174SAndreas Gohr ) { 609493c275SMichael Große $evt->advise_after(); 619493c275SMichael Große } 6224870174SAndreas Gohr } 633b58faf6SMichael Große 643b58faf6SMichael Große if (!$output) { 653b58faf6SMichael Große ob_end_clean(); 663b58faf6SMichael Große if ($defer) { 673b58faf6SMichael Große $this->sendGIF(); 683b58faf6SMichael Große } 693b58faf6SMichael Große } 703b58faf6SMichael Große } 713b58faf6SMichael Große 723b58faf6SMichael Große /** 733b58faf6SMichael Große * Just send a 1x1 pixel blank gif to the browser 743b58faf6SMichael Große * 753b58faf6SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 763b58faf6SMichael Große * @author Harry Fuecks <fuecks@gmail.com> 773b58faf6SMichael Große */ 783ad4c3cdSAndreas Gohr protected function sendGIF() 793ad4c3cdSAndreas Gohr { 803b58faf6SMichael Große $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7'); 813b58faf6SMichael Große header('Content-Type: image/gif'); 823b58faf6SMichael Große header('Content-Length: ' . strlen($img)); 833b58faf6SMichael Große header('Connection: Close'); 8426dfc232SAndreas Gohr echo $img; 853b58faf6SMichael Große tpl_flush(); 863b58faf6SMichael Große // Browser should drop connection after this 873b58faf6SMichael Große // Thinks it's got the whole image 889493c275SMichael Große } 899493c275SMichael Große 909493c275SMichael Große /** 919493c275SMichael Große * Trims the recent changes cache (or imports the old changelog) as needed. 929493c275SMichael Große * 939493c275SMichael Große * @param bool $media_changes If the media changelog shall be trimmed instead of 949493c275SMichael Große * the page changelog 95b5cf9c44SMichael Große * 969493c275SMichael Große * @return bool 9750d9e958SAndreas Gohr * @triggers TASK_RECENTCHANGES_TRIM 989493c275SMichael Große * @author Ben Coburn <btcoburn@silicodon.net> 999493c275SMichael Große */ 100b5cf9c44SMichael Große protected function runTrimRecentChanges($media_changes = false) 101b5cf9c44SMichael Große { 1029493c275SMichael Große global $conf; 1039493c275SMichael Große 1049493c275SMichael Große echo "runTrimRecentChanges($media_changes): started" . NL; 1059493c275SMichael Große 1069493c275SMichael Große $fn = ($media_changes ? $conf['media_changelog'] : $conf['changelog']); 1079493c275SMichael Große 1089493c275SMichael Große // Trim the Recent Changes 1099493c275SMichael Große // Trims the recent changes cache to the last $conf['changes_days'] recent 1109493c275SMichael Große // changes or $conf['recent'] items, which ever is larger. 1119493c275SMichael Große // The trimming is only done once a day. 1127d34963bSAndreas Gohr if ( 1137d34963bSAndreas Gohr file_exists($fn) && 1149493c275SMichael Große (@filemtime($fn . '.trimmed') + 86400) < time() && 1157d34963bSAndreas Gohr !file_exists($fn . '_tmp') 1167d34963bSAndreas Gohr ) { 1179493c275SMichael Große @touch($fn . '.trimmed'); 1189493c275SMichael Große io_lock($fn); 1199493c275SMichael Große $lines = file($fn); 1209493c275SMichael Große if (count($lines) <= $conf['recent']) { 1219493c275SMichael Große // nothing to trim 1229493c275SMichael Große io_unlock($fn); 1239493c275SMichael Große echo "runTrimRecentChanges($media_changes): finished" . NL; 1249493c275SMichael Große return false; 1259493c275SMichael Große } 1269493c275SMichael Große 1279493c275SMichael Große io_saveFile($fn . '_tmp', ''); // presave tmp as 2nd lock 1289493c275SMichael Große $trim_time = time() - $conf['recent_days'] * 86400; 129b5cf9c44SMichael Große $out_lines = []; 130b5cf9c44SMichael Große $old_lines = []; 13124870174SAndreas Gohr $counter = count($lines); 13224870174SAndreas Gohr for ($i = 0; $i < $counter; $i++) { 1331d11f1d3SSatoshi Sahara $log = ChangeLog::parseLogLine($lines[$i]); 134b5cf9c44SMichael Große if ($log === false) { 135e24a74c0SAndreas Gohr continue; // discard junk 136e24a74c0SAndreas Gohr } 137e24a74c0SAndreas Gohr 1389493c275SMichael Große if ($log['date'] < $trim_time) { 139e24a74c0SAndreas Gohr // keep old lines for now (append .$i to prevent key collisions) 140e24a74c0SAndreas Gohr $old_lines[$log['date'] . ".$i"] = $lines[$i]; 1419493c275SMichael Große } else { 142e24a74c0SAndreas Gohr // definitely keep these lines 143e24a74c0SAndreas Gohr $out_lines[$log['date'] . ".$i"] = $lines[$i]; 1449493c275SMichael Große } 1459493c275SMichael Große } 1469493c275SMichael Große 14728a6ee9aSKlap-in if (count($lines) === count($out_lines)) { 1489493c275SMichael Große // nothing to trim 1499493c275SMichael Große @unlink($fn . '_tmp'); 1509493c275SMichael Große io_unlock($fn); 1519493c275SMichael Große echo "runTrimRecentChanges($media_changes): finished" . NL; 1529493c275SMichael Große return false; 1539493c275SMichael Große } 1549493c275SMichael Große 1559493c275SMichael Große // sort the final result, it shouldn't be necessary, 1569493c275SMichael Große // however the extra robustness in making the changelog cache self-correcting is worth it 1579493c275SMichael Große ksort($out_lines); 1589493c275SMichael Große $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum 1599493c275SMichael Große if ($extra > 0) { 1609493c275SMichael Große ksort($old_lines); 1619493c275SMichael Große $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines); 1629493c275SMichael Große } 1639493c275SMichael Große 164b413fb0bSMichael Große $eventData = [ 165eb787020SMichael Große 'isMedia' => $media_changes, 166b413fb0bSMichael Große 'trimmedChangelogLines' => $out_lines, 167b413fb0bSMichael Große 'removedChangelogLines' => $extra > 0 ? array_slice($old_lines, 0, -$extra) : $old_lines, 168b413fb0bSMichael Große ]; 169cbb44eabSAndreas Gohr Event::createAndTrigger('TASK_RECENTCHANGES_TRIM', $eventData); 170b413fb0bSMichael Große $out_lines = $eventData['trimmedChangelogLines']; 171b413fb0bSMichael Große 1729493c275SMichael Große // save trimmed changelog 1739493c275SMichael Große io_saveFile($fn . '_tmp', implode('', $out_lines)); 1749493c275SMichael Große @unlink($fn); 1759493c275SMichael Große if (!rename($fn . '_tmp', $fn)) { 1769493c275SMichael Große // rename failed so try another way... 1779493c275SMichael Große io_unlock($fn); 1789493c275SMichael Große io_saveFile($fn, implode('', $out_lines)); 1799493c275SMichael Große @unlink($fn . '_tmp'); 1809493c275SMichael Große } else { 1819493c275SMichael Große io_unlock($fn); 1829493c275SMichael Große } 1839493c275SMichael Große echo "runTrimRecentChanges($media_changes): finished" . NL; 1849493c275SMichael Große return true; 1859493c275SMichael Große } 1869493c275SMichael Große 1879493c275SMichael Große // nothing done 1889493c275SMichael Große echo "runTrimRecentChanges($media_changes): finished" . NL; 1899493c275SMichael Große return false; 1909493c275SMichael Große } 1919493c275SMichael Große 1929493c275SMichael Große 1939493c275SMichael Große /** 1949493c275SMichael Große * Runs the indexer for the current page 1959493c275SMichael Große * 1969493c275SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 1979493c275SMichael Große */ 198b5cf9c44SMichael Große protected function runIndexer() 199b5cf9c44SMichael Große { 2009493c275SMichael Große global $ID; 20126dfc232SAndreas Gohr echo 'runIndexer(): started' . NL; 2029493c275SMichael Große 20373f05217SPhy if ((string) $ID === '') { 204b5cf9c44SMichael Große return false; 205b5cf9c44SMichael Große } 2069493c275SMichael Große 2079493c275SMichael Große // do the work 20815f699acSAndreas Gohr try { 209*83b3acccSAndreas Gohr $indexer = (new Indexer())->setLogger(function ($msg) { echo $msg . NL; }); 210*83b3acccSAndreas Gohr if (!page_exists($ID)) { 211*83b3acccSAndreas Gohr $indexer->deletePage($ID, true); 212*83b3acccSAndreas Gohr } else { 213*83b3acccSAndreas Gohr $indexer->addPage($ID, true); 214*83b3acccSAndreas Gohr } 215*83b3acccSAndreas Gohr return true; 21615f699acSAndreas Gohr } catch (Search\Exception\SearchException $e) { 21772ebc99bSSatoshi Sahara $msg = get_class($e) .' : '. $e->getMessage(); 21872ebc99bSSatoshi Sahara echo $msg; 2193df1553dSSatoshi Sahara Logger::debug($msg); 22015f699acSAndreas Gohr return false; 22115f699acSAndreas Gohr } 2229493c275SMichael Große } 2239493c275SMichael Große 2249493c275SMichael Große /** 2259493c275SMichael Große * Builds a Google Sitemap of all public pages known to the indexer 2269493c275SMichael Große * 2279493c275SMichael Große * The map is placed in the root directory named sitemap.xml.gz - This 2289493c275SMichael Große * file needs to be writable! 2299493c275SMichael Große * 2309493c275SMichael Große * @author Andreas Gohr 2319493c275SMichael Große * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html 2329493c275SMichael Große */ 233b5cf9c44SMichael Große protected function runSitemapper() 234b5cf9c44SMichael Große { 23526dfc232SAndreas Gohr echo 'runSitemapper(): started' . NL; 236432adb37SAndreas Gohr $result = Mapper::generate() && Mapper::pingSearchEngines(); 23726dfc232SAndreas Gohr echo 'runSitemapper(): finished' . NL; 2389493c275SMichael Große return $result; 2399493c275SMichael Große } 2409493c275SMichael Große 2419493c275SMichael Große /** 2429493c275SMichael Große * Send digest and list mails for all subscriptions which are in effect for the 2439493c275SMichael Große * current page 2449493c275SMichael Große * 2459493c275SMichael Große * @author Adrian Lang <lang@cosmocode.de> 2469493c275SMichael Große */ 247b5cf9c44SMichael Große protected function sendDigest() 248b5cf9c44SMichael Große { 2499493c275SMichael Große global $ID; 2509493c275SMichael Große 2519493c275SMichael Große echo 'sendDigest(): started' . NL; 2529493c275SMichael Große if (!actionOK('subscribe')) { 2539493c275SMichael Große echo 'sendDigest(): disabled' . NL; 2549493c275SMichael Große return false; 2559493c275SMichael Große } 256704a815fSMichael Große $sub = new BulkSubscriptionSender(); 25775d66495SMichael Große $sent = $sub->sendBulk($ID); 2589493c275SMichael Große 2599493c275SMichael Große echo "sendDigest(): sent $sent mails" . NL; 2609493c275SMichael Große echo 'sendDigest(): finished' . NL; 2619493c275SMichael Große return (bool)$sent; 2629493c275SMichael Große } 2639493c275SMichael Große} 264