19493c275SMichael Große<?php 29493c275SMichael Große 39493c275SMichael Großenamespace dokuwiki; 49493c275SMichael Große 59493c275SMichael Großeuse Doku_Event; 69493c275SMichael Großeuse Sitemapper; 79493c275SMichael Großeuse Subscription; 89493c275SMichael Große 9*3ad4c3cdSAndreas Gohr/** 10*3ad4c3cdSAndreas Gohr * Class TaskRunner 11*3ad4c3cdSAndreas Gohr * 12*3ad4c3cdSAndreas Gohr * Run an asynchronous task. 13*3ad4c3cdSAndreas Gohr */ 149493c275SMichael Großeclass TaskRunner 159493c275SMichael Große{ 16*3ad4c3cdSAndreas Gohr /** 17*3ad4c3cdSAndreas Gohr * Run the next task 18*3ad4c3cdSAndreas Gohr * 19*3ad4c3cdSAndreas Gohr * @todo refactor to remove dependencies on globals 20*3ad4c3cdSAndreas Gohr * @triggers INDEXER_TASKS_RUN 21*3ad4c3cdSAndreas Gohr */ 229493c275SMichael Große public function run() 239493c275SMichael Große { 243b58faf6SMichael Große global $INPUT, $conf, $ID; 253b58faf6SMichael Große 263b58faf6SMichael Große // keep running after browser closes connection 273b58faf6SMichael Große @ignore_user_abort(true); 283b58faf6SMichael Große 293b58faf6SMichael Große // check if user abort worked, if yes send output early 303b58faf6SMichael Große $defer = !@ignore_user_abort() || $conf['broken_iua']; 313b58faf6SMichael Große $output = $INPUT->has('debug') && $conf['allowdebug']; 323b58faf6SMichael Große if(!$defer && !$output){ 333b58faf6SMichael Große $this->sendGIF(); 343b58faf6SMichael Große } 353b58faf6SMichael Große 363b58faf6SMichael Große $ID = cleanID($INPUT->str('id')); 373b58faf6SMichael Große 383b58faf6SMichael Große // Catch any possible output (e.g. errors) 393b58faf6SMichael Große if(!$output) { 403b58faf6SMichael Große ob_start(); 413b58faf6SMichael Große } else { 423b58faf6SMichael Große header('Content-Type: text/plain'); 433b58faf6SMichael Große } 443b58faf6SMichael Große 459493c275SMichael Große // run one of the jobs 469493c275SMichael Große $tmp = []; // No event data 479493c275SMichael Große $evt = new Doku_Event('INDEXER_TASKS_RUN', $tmp); 489493c275SMichael Große if ($evt->advise_before()) { 499493c275SMichael Große $this->runIndexer() or 509493c275SMichael Große $this->runSitemapper() or 519493c275SMichael Große $this->sendDigest() or 529493c275SMichael Große $this->runTrimRecentChanges() or 539493c275SMichael Große $this->runTrimRecentChanges(true) or 549493c275SMichael Große $evt->advise_after(); 559493c275SMichael Große } 563b58faf6SMichael Große 573b58faf6SMichael Große if(!$output) { 583b58faf6SMichael Große ob_end_clean(); 593b58faf6SMichael Große if($defer) { 603b58faf6SMichael Große $this->sendGIF(); 613b58faf6SMichael Große } 623b58faf6SMichael Große } 633b58faf6SMichael Große } 643b58faf6SMichael Große 653b58faf6SMichael Große /** 663b58faf6SMichael Große * Just send a 1x1 pixel blank gif to the browser 673b58faf6SMichael Große * 683b58faf6SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 693b58faf6SMichael Große * @author Harry Fuecks <fuecks@gmail.com> 703b58faf6SMichael Große */ 71*3ad4c3cdSAndreas Gohr protected function sendGIF() 72*3ad4c3cdSAndreas Gohr { 733b58faf6SMichael Große $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7'); 743b58faf6SMichael Große header('Content-Type: image/gif'); 753b58faf6SMichael Große header('Content-Length: '.strlen($img)); 763b58faf6SMichael Große header('Connection: Close'); 773b58faf6SMichael Große print $img; 783b58faf6SMichael Große tpl_flush(); 793b58faf6SMichael Große // Browser should drop connection after this 803b58faf6SMichael Große // Thinks it's got the whole image 819493c275SMichael Große } 829493c275SMichael Große 839493c275SMichael Große /** 849493c275SMichael Große * Trims the recent changes cache (or imports the old changelog) as needed. 859493c275SMichael Große * 869493c275SMichael Große * @param bool $media_changes If the media changelog shall be trimmed instead of 879493c275SMichael Große * the page changelog 88b5cf9c44SMichael Große * 899493c275SMichael Große * @return bool 909493c275SMichael Große * 919493c275SMichael Große * @author Ben Coburn <btcoburn@silicodon.net> 929493c275SMichael Große */ 93b5cf9c44SMichael Große protected function runTrimRecentChanges($media_changes = false) 94b5cf9c44SMichael Große { 959493c275SMichael Große global $conf; 969493c275SMichael Große 979493c275SMichael Große echo "runTrimRecentChanges($media_changes): started" . NL; 989493c275SMichael Große 999493c275SMichael Große $fn = ($media_changes ? $conf['media_changelog'] : $conf['changelog']); 1009493c275SMichael Große 1019493c275SMichael Große // Trim the Recent Changes 1029493c275SMichael Große // Trims the recent changes cache to the last $conf['changes_days'] recent 1039493c275SMichael Große // changes or $conf['recent'] items, which ever is larger. 1049493c275SMichael Große // The trimming is only done once a day. 1059493c275SMichael Große if (file_exists($fn) && 1069493c275SMichael Große (@filemtime($fn . '.trimmed') + 86400) < time() && 1079493c275SMichael Große !file_exists($fn . '_tmp')) { 1089493c275SMichael Große @touch($fn . '.trimmed'); 1099493c275SMichael Große io_lock($fn); 1109493c275SMichael Große $lines = file($fn); 1119493c275SMichael Große if (count($lines) <= $conf['recent']) { 1129493c275SMichael Große // nothing to trim 1139493c275SMichael Große io_unlock($fn); 1149493c275SMichael Große echo "runTrimRecentChanges($media_changes): finished" . NL; 1159493c275SMichael Große return false; 1169493c275SMichael Große } 1179493c275SMichael Große 1189493c275SMichael Große io_saveFile($fn . '_tmp', ''); // presave tmp as 2nd lock 1199493c275SMichael Große $trim_time = time() - $conf['recent_days'] * 86400; 120b5cf9c44SMichael Große $out_lines = []; 121b5cf9c44SMichael Große $old_lines = []; 1229493c275SMichael Große for ($i = 0; $i < count($lines); $i++) { 1239493c275SMichael Große $log = parseChangelogLine($lines[$i]); 124b5cf9c44SMichael Große if ($log === false) { 125b5cf9c44SMichael Große continue; 126b5cf9c44SMichael Große } // discard junk 1279493c275SMichael Große if ($log['date'] < $trim_time) { 1289493c275SMichael Große $old_lines[$log['date'] . ".$i"] = $lines[$i]; // keep old lines for now (append .$i to prevent key collisions) 1299493c275SMichael Große } else { 1309493c275SMichael Große $out_lines[$log['date'] . ".$i"] = $lines[$i]; // definitely keep these lines 1319493c275SMichael Große } 1329493c275SMichael Große } 1339493c275SMichael Große 1349493c275SMichael Große if (count($lines) == count($out_lines)) { 1359493c275SMichael Große // nothing to trim 1369493c275SMichael Große @unlink($fn . '_tmp'); 1379493c275SMichael Große io_unlock($fn); 1389493c275SMichael Große echo "runTrimRecentChanges($media_changes): finished" . NL; 1399493c275SMichael Große return false; 1409493c275SMichael Große } 1419493c275SMichael Große 1429493c275SMichael Große // sort the final result, it shouldn't be necessary, 1439493c275SMichael Große // however the extra robustness in making the changelog cache self-correcting is worth it 1449493c275SMichael Große ksort($out_lines); 1459493c275SMichael Große $extra = $conf['recent'] - count($out_lines); // do we need extra lines do bring us up to minimum 1469493c275SMichael Große if ($extra > 0) { 1479493c275SMichael Große ksort($old_lines); 1489493c275SMichael Große $out_lines = array_merge(array_slice($old_lines, -$extra), $out_lines); 1499493c275SMichael Große } 1509493c275SMichael Große 151b413fb0bSMichael Große $eventData = [ 152b413fb0bSMichael Große 'trimmedChangelogLines' => $out_lines, 153b413fb0bSMichael Große 'removedChangelogLines' => $extra > 0 ? array_slice($old_lines, 0, -$extra) : $old_lines, 154b413fb0bSMichael Große ]; 155b413fb0bSMichael Große trigger_event('TRIM_RECENT_CHANGES', $eventData); 156b413fb0bSMichael Große $out_lines = $eventData['trimmedChangelogLines']; 157b413fb0bSMichael Große 1589493c275SMichael Große // save trimmed changelog 1599493c275SMichael Große io_saveFile($fn . '_tmp', implode('', $out_lines)); 1609493c275SMichael Große @unlink($fn); 1619493c275SMichael Große if (!rename($fn . '_tmp', $fn)) { 1629493c275SMichael Große // rename failed so try another way... 1639493c275SMichael Große io_unlock($fn); 1649493c275SMichael Große io_saveFile($fn, implode('', $out_lines)); 1659493c275SMichael Große @unlink($fn . '_tmp'); 1669493c275SMichael Große } else { 1679493c275SMichael Große io_unlock($fn); 1689493c275SMichael Große } 1699493c275SMichael Große echo "runTrimRecentChanges($media_changes): finished" . NL; 1709493c275SMichael Große return true; 1719493c275SMichael Große } 1729493c275SMichael Große 1739493c275SMichael Große // nothing done 1749493c275SMichael Große echo "runTrimRecentChanges($media_changes): finished" . NL; 1759493c275SMichael Große return false; 1769493c275SMichael Große } 1779493c275SMichael Große 1789493c275SMichael Große 1799493c275SMichael Große /** 1809493c275SMichael Große * Runs the indexer for the current page 1819493c275SMichael Große * 1829493c275SMichael Große * @author Andreas Gohr <andi@splitbrain.org> 1839493c275SMichael Große */ 184b5cf9c44SMichael Große protected function runIndexer() 185b5cf9c44SMichael Große { 1869493c275SMichael Große global $ID; 1879493c275SMichael Große global $conf; 188b5cf9c44SMichael Große print 'runIndexer(): started' . NL; 1899493c275SMichael Große 190b5cf9c44SMichael Große if (!$ID) { 191b5cf9c44SMichael Große return false; 192b5cf9c44SMichael Große } 1939493c275SMichael Große 1949493c275SMichael Große // do the work 1959493c275SMichael Große return idx_addPage($ID, true); 1969493c275SMichael Große } 1979493c275SMichael Große 1989493c275SMichael Große /** 1999493c275SMichael Große * Builds a Google Sitemap of all public pages known to the indexer 2009493c275SMichael Große * 2019493c275SMichael Große * The map is placed in the root directory named sitemap.xml.gz - This 2029493c275SMichael Große * file needs to be writable! 2039493c275SMichael Große * 2049493c275SMichael Große * @author Andreas Gohr 2059493c275SMichael Große * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html 2069493c275SMichael Große */ 207b5cf9c44SMichael Große protected function runSitemapper() 208b5cf9c44SMichael Große { 209b5cf9c44SMichael Große print 'runSitemapper(): started' . NL; 2109493c275SMichael Große $result = Sitemapper::generate() && Sitemapper::pingSearchEngines(); 2119493c275SMichael Große print 'runSitemapper(): finished' . NL; 2129493c275SMichael Große return $result; 2139493c275SMichael Große } 2149493c275SMichael Große 2159493c275SMichael Große /** 2169493c275SMichael Große * Send digest and list mails for all subscriptions which are in effect for the 2179493c275SMichael Große * current page 2189493c275SMichael Große * 2199493c275SMichael Große * @author Adrian Lang <lang@cosmocode.de> 2209493c275SMichael Große */ 221b5cf9c44SMichael Große protected function sendDigest() 222b5cf9c44SMichael Große { 2239493c275SMichael Große global $conf; 2249493c275SMichael Große global $ID; 2259493c275SMichael Große 2269493c275SMichael Große echo 'sendDigest(): started' . NL; 2279493c275SMichael Große if (!actionOK('subscribe')) { 2289493c275SMichael Große echo 'sendDigest(): disabled' . NL; 2299493c275SMichael Große return false; 2309493c275SMichael Große } 2319493c275SMichael Große $sub = new Subscription(); 2329493c275SMichael Große $sent = $sub->send_bulk($ID); 2339493c275SMichael Große 2349493c275SMichael Große echo "sendDigest(): sent $sent mails" . NL; 2359493c275SMichael Große echo 'sendDigest(): finished' . NL; 2369493c275SMichael Große return (bool)$sent; 2379493c275SMichael Große } 2389493c275SMichael Große} 239