19493c275SMichael Große<?php 29493c275SMichael Große 39493c275SMichael Großenamespace dokuwiki; 49493c275SMichael Große 5e1d9dcc8SAndreas Gohruse dokuwiki\Extension\Event; 6432adb37SAndreas Gohruse dokuwiki\Sitemap\Mapper; 7704a815fSMichael Großeuse dokuwiki\Subscriptions\BulkSubscriptionSender; 81d11f1d3SSatoshi Saharause dokuwiki\ChangeLog\ChangeLog; 99493c275SMichael Große 103ad4c3cdSAndreas Gohr/** 113ad4c3cdSAndreas Gohr * Class TaskRunner 123ad4c3cdSAndreas Gohr * 133ad4c3cdSAndreas Gohr * Run an asynchronous task. 143ad4c3cdSAndreas Gohr */ 159493c275SMichael Großeclass TaskRunner 169493c275SMichael Große{ 173ad4c3cdSAndreas Gohr /** 183ad4c3cdSAndreas Gohr * Run the next task 193ad4c3cdSAndreas Gohr * 203ad4c3cdSAndreas Gohr * @todo refactor to remove dependencies on globals 213ad4c3cdSAndreas Gohr * @triggers INDEXER_TASKS_RUN 223ad4c3cdSAndreas Gohr */ 239493c275SMichael Große public function run() 249493c275SMichael Große { 253b58faf6SMichael Große global $INPUT, $conf, $ID; 263b58faf6SMichael Große 273b58faf6SMichael Große // keep running after browser closes connection 283b58faf6SMichael Große @ignore_user_abort(true); 293b58faf6SMichael Große 303b58faf6SMichael Große // check if user abort worked, if yes send output early 313b58faf6SMichael Große $defer = !@ignore_user_abort() || $conf['broken_iua']; 323b58faf6SMichael Große $output = $INPUT->has('debug') && $conf['allowdebug']; 333b58faf6SMichael Große if(!$defer && !$output){ 343b58faf6SMichael Große $this->sendGIF(); 353b58faf6SMichael Große } 363b58faf6SMichael Große 373b58faf6SMichael Große $ID = cleanID($INPUT->str('id')); 383b58faf6SMichael Große 393b58faf6SMichael Große // Catch any possible output (e.g. errors) 403b58faf6SMichael Große if(!$output) { 413b58faf6SMichael Große ob_start(); 423b58faf6SMichael Große } else { 433b58faf6SMichael Große header('Content-Type: text/plain'); 443b58faf6SMichael Große } 453b58faf6SMichael Große 469493c275SMichael Große // run one of the jobs 479493c275SMichael Große $tmp = []; // No event data 48e1d9dcc8SAndreas Gohr $evt = new Event('INDEXER_TASKS_RUN', $tmp); 499493c275SMichael Große if ($evt->advise_before()) { 50*24870174SAndreas Gohr if (!( 51*24870174SAndreas Gohr $this->runIndexer() || 52*24870174SAndreas Gohr $this->runSitemapper() || 53*24870174SAndreas Gohr $this->sendDigest() || 54*24870174SAndreas Gohr $this->runTrimRecentChanges() || 55*24870174SAndreas Gohr $this->runTrimRecentChanges(true)) 56*24870174SAndreas Gohr ) { 579493c275SMichael Große $evt->advise_after(); 589493c275SMichael Große } 59*24870174SAndreas Gohr } 603b58faf6SMichael Große 613b58faf6SMichael Große if(!$output) { 623b58faf6SMichael Große ob_end_clean(); 633b58faf6SMichael Große if($defer) { 643b58faf6SMichael Große $this->sendGIF(); 653b58faf6SMichael Große } 663b58faf6SMichael Große } 673b58faf6SMichael Große } 683b58faf6SMichael Große 693b58faf6SMichael Große /** 703b58faf6SMichael Große * Just send a 1x1 pixel blank gif to the browser 713b58faf6SMichael Große * 723b58faf6SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 733b58faf6SMichael Große * @author Harry Fuecks <fuecks@gmail.com> 743b58faf6SMichael Große */ 753ad4c3cdSAndreas Gohr protected function sendGIF() 763ad4c3cdSAndreas Gohr { 773b58faf6SMichael Große $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7'); 783b58faf6SMichael Große header('Content-Type: image/gif'); 793b58faf6SMichael Große header('Content-Length: '.strlen($img)); 803b58faf6SMichael Große header('Connection: Close'); 813b58faf6SMichael Große print $img; 823b58faf6SMichael Große tpl_flush(); 833b58faf6SMichael Große // Browser should drop connection after this 843b58faf6SMichael Große // Thinks it's got the whole image 859493c275SMichael Große } 869493c275SMichael Große 879493c275SMichael Große /** 889493c275SMichael Große * Trims the recent changes cache (or imports the old changelog) as needed. 899493c275SMichael Große * 909493c275SMichael Große * @param bool $media_changes If the media changelog shall be trimmed instead of 919493c275SMichael Große * the page changelog 92b5cf9c44SMichael Große * 939493c275SMichael Große * @return bool 9450d9e958SAndreas Gohr * @triggers TASK_RECENTCHANGES_TRIM 959493c275SMichael Große * @author Ben Coburn <btcoburn@silicodon.net> 969493c275SMichael Große */ 97b5cf9c44SMichael Große protected function runTrimRecentChanges($media_changes = false) 98b5cf9c44SMichael Große { 999493c275SMichael Große global $conf; 1009493c275SMichael Große 1019493c275SMichael Große echo "runTrimRecentChanges($media_changes): started" . NL; 1029493c275SMichael Große 1039493c275SMichael Große $fn = ($media_changes ? $conf['media_changelog'] : $conf['changelog']); 1049493c275SMichael Große 1059493c275SMichael Große // Trim the Recent Changes 1069493c275SMichael Große // Trims the recent changes cache to the last $conf['changes_days'] recent 1079493c275SMichael Große // changes or $conf['recent'] items, which ever is larger. 1089493c275SMichael Große // The trimming is only done once a day. 1099493c275SMichael Große if (file_exists($fn) && 1109493c275SMichael Große (@filemtime($fn . '.trimmed') + 86400) < time() && 1119493c275SMichael Große !file_exists($fn . '_tmp')) { 1129493c275SMichael Große @touch($fn . '.trimmed'); 1139493c275SMichael Große io_lock($fn); 1149493c275SMichael Große $lines = file($fn); 1159493c275SMichael Große if (count($lines) <= $conf['recent']) { 1169493c275SMichael Große // nothing to trim 1179493c275SMichael Große io_unlock($fn); 1189493c275SMichael Große echo "runTrimRecentChanges($media_changes): finished" . NL; 1199493c275SMichael Große return false; 1209493c275SMichael Große } 1219493c275SMichael Große 1229493c275SMichael Große io_saveFile($fn . '_tmp', ''); // presave tmp as 2nd lock 1239493c275SMichael Große $trim_time = time() - $conf['recent_days'] * 86400; 124b5cf9c44SMichael Große $out_lines = []; 125b5cf9c44SMichael Große $old_lines = []; 126*24870174SAndreas Gohr $counter = count($lines); 127*24870174SAndreas Gohr for ($i = 0; $i < $counter; $i++) { 1281d11f1d3SSatoshi Sahara $log = ChangeLog::parseLogLine($lines[$i]); 129b5cf9c44SMichael Große if ($log === false) { 130e24a74c0SAndreas Gohr continue; // discard junk 131e24a74c0SAndreas Gohr } 132e24a74c0SAndreas Gohr 1339493c275SMichael Große if ($log['date'] < $trim_time) { 134e24a74c0SAndreas Gohr // keep old lines for now (append .$i to prevent key collisions) 135e24a74c0SAndreas Gohr $old_lines[$log['date'] . ".$i"] = $lines[$i]; 1369493c275SMichael Große } else { 137e24a74c0SAndreas Gohr // definitely keep these lines 138e24a74c0SAndreas Gohr $out_lines[$log['date'] . ".$i"] = $lines[$i]; 1399493c275SMichael Große } 1409493c275SMichael Große } 1419493c275SMichael Große 1429493c275SMichael Große if (count($lines) == count($out_lines)) { 1439493c275SMichael Große // nothing to trim 1449493c275SMichael Große @unlink($fn . '_tmp'); 1459493c275SMichael Große io_unlock($fn); 1469493c275SMichael Große echo "runTrimRecentChanges($media_changes): finished" . NL; 1479493c275SMichael Große return false; 1489493c275SMichael Große } 1499493c275SMichael Große 1509493c275SMichael Große // sort the final result, it shouldn't be necessary, 1519493c275SMichael Große // however the extra robustness in making the changelog cache self-correcting is worth it 1529493c275SMichael Große ksort($out_lines); 1539493c275SMichael Große $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum 1549493c275SMichael Große if ($extra > 0) { 1559493c275SMichael Große ksort($old_lines); 1569493c275SMichael Große $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines); 1579493c275SMichael Große } 1589493c275SMichael Große 159b413fb0bSMichael Große $eventData = [ 160eb787020SMichael Große 'isMedia' => $media_changes, 161b413fb0bSMichael Große 'trimmedChangelogLines' => $out_lines, 162b413fb0bSMichael Große 'removedChangelogLines' => $extra > 0 ? array_slice($old_lines, 0, -$extra) : $old_lines, 163b413fb0bSMichael Große ]; 164cbb44eabSAndreas Gohr Event::createAndTrigger('TASK_RECENTCHANGES_TRIM', $eventData); 165b413fb0bSMichael Große $out_lines = $eventData['trimmedChangelogLines']; 166b413fb0bSMichael Große 1679493c275SMichael Große // save trimmed changelog 1689493c275SMichael Große io_saveFile($fn . '_tmp', implode('', $out_lines)); 1699493c275SMichael Große @unlink($fn); 1709493c275SMichael Große if (!rename($fn . '_tmp', $fn)) { 1719493c275SMichael Große // rename failed so try another way... 1729493c275SMichael Große io_unlock($fn); 1739493c275SMichael Große io_saveFile($fn, implode('', $out_lines)); 1749493c275SMichael Große @unlink($fn . '_tmp'); 1759493c275SMichael Große } else { 1769493c275SMichael Große io_unlock($fn); 1779493c275SMichael Große } 1789493c275SMichael Große echo "runTrimRecentChanges($media_changes): finished" . NL; 1799493c275SMichael Große return true; 1809493c275SMichael Große } 1819493c275SMichael Große 1829493c275SMichael Große // nothing done 1839493c275SMichael Große echo "runTrimRecentChanges($media_changes): finished" . NL; 1849493c275SMichael Große return false; 1859493c275SMichael Große } 1869493c275SMichael Große 1879493c275SMichael Große 1889493c275SMichael Große /** 1899493c275SMichael Große * Runs the indexer for the current page 1909493c275SMichael Große * 1919493c275SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 1929493c275SMichael Große */ 193b5cf9c44SMichael Große protected function runIndexer() 194b5cf9c44SMichael Große { 1959493c275SMichael Große global $ID; 196b5cf9c44SMichael Große print 'runIndexer(): started' . NL; 1979493c275SMichael Große 19873f05217SPhy if ((string) $ID === '') { 199b5cf9c44SMichael Große return false; 200b5cf9c44SMichael Große } 2019493c275SMichael Große 2029493c275SMichael Große // do the work 2039493c275SMichael Große return idx_addPage($ID, true); 2049493c275SMichael Große } 2059493c275SMichael Große 2069493c275SMichael Große /** 2079493c275SMichael Große * Builds a Google Sitemap of all public pages known to the indexer 2089493c275SMichael Große * 2099493c275SMichael Große * The map is placed in the root directory named sitemap.xml.gz - This 2109493c275SMichael Große * file needs to be writable! 2119493c275SMichael Große * 2129493c275SMichael Große * @author Andreas Gohr 2139493c275SMichael Große * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html 2149493c275SMichael Große */ 215b5cf9c44SMichael Große protected function runSitemapper() 216b5cf9c44SMichael Große { 217b5cf9c44SMichael Große print 'runSitemapper(): started' . NL; 218432adb37SAndreas Gohr $result = Mapper::generate() && Mapper::pingSearchEngines(); 2199493c275SMichael Große print 'runSitemapper(): finished' . NL; 2209493c275SMichael Große return $result; 2219493c275SMichael Große } 2229493c275SMichael Große 2239493c275SMichael Große /** 2249493c275SMichael Große * Send digest and list mails for all subscriptions which are in effect for the 2259493c275SMichael Große * current page 2269493c275SMichael Große * 2279493c275SMichael Große * @author Adrian Lang <lang@cosmocode.de> 2289493c275SMichael Große */ 229b5cf9c44SMichael Große protected function sendDigest() 230b5cf9c44SMichael Große { 2319493c275SMichael Große global $ID; 2329493c275SMichael Große 2339493c275SMichael Große echo 'sendDigest(): started' . NL; 2349493c275SMichael Große if (!actionOK('subscribe')) { 2359493c275SMichael Große echo 'sendDigest(): disabled' . NL; 2369493c275SMichael Große return false; 2379493c275SMichael Große } 238704a815fSMichael Große $sub = new BulkSubscriptionSender(); 23975d66495SMichael Große $sent = $sub->sendBulk($ID); 2409493c275SMichael Große 2419493c275SMichael Große echo "sendDigest(): sent $sent mails" . NL; 2429493c275SMichael Große echo 'sendDigest(): finished' . NL; 2439493c275SMichael Große return (bool)$sent; 2449493c275SMichael Große } 2459493c275SMichael Große} 246