1<?php 2/** 3 * DokuWiki indexer 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/'); 9require_once(DOKU_INC.'inc/init.php'); 10require_once(DOKU_INC.'inc/auth.php'); 11session_write_close(); //close session 12if(!defined('NL')) define('NL',"\n"); 13 14// keep running after browser closes connection 15@ignore_user_abort(true); 16 17// send gif 18sendGIF(); 19 20// Catch any possible output (e.g. errors) 21if(!$_REQUEST['debug']) ob_start(); 22 23// run one of the jobs 24runIndexer() or runSitemapper(); 25 26if(!$_REQUEST['debug']) ob_end_clean(); 27exit; 28 29// -------------------------------------------------------------------- 30 31/** 32 * Runs the indexer for the current page 33 * 34 * @author Andreas Gohr <andi@splitbrain.org> 35 */ 36function runIndexer(){ 37 global $conf; 38 print "runIndexer(): started".NL; 39 40 $ID = cleanID($_REQUEST['id']); 41 if(!$ID) return false; 42 43 // check if indexing needed 44 $last = @filemtime(metaFN($ID,'.indexed')); 45 if($last > @filemtime(wikiFN($ID))){ 46 print "runIndexer(): index for $ID up to date".NL; 47 return false; 48 } 49 50 // try to aquire a lock 51 $lock = $conf['lockdir'].'/_indexer.lock'; 52 while(!@mkdir($lock,$conf['dmode'])){ 53 usleep(50); 54 if(time()-@filemtime($lock) > 60*5){ 55 // looks like a stale lock - remove it 56 @rmdir($lock); 57 print "runIndexer(): stale lock removed".NL; 58 }else{ 59 print "runIndexer(): indexer locked".NL; 60 return false; 61 } 62 } 63 if(isset($conf['dmask'])) { chmod($lock, $conf['dmask']); } 64 65 require_once(DOKU_INC.'inc/indexer.php'); 66 67 // do the work 68 idx_addPage($ID); 69 70 // we're finished - save and free lock 71 io_saveFile(metaFN($ID,'.indexed'),' '); 72 @rmdir($lock); 73 print "runIndexer(): finished".NL; 74 return true; 75} 76 77/** 78 * Builds a Google Sitemap of all public pages known to the indexer 79 * 80 * The map is placed in the root directory named sitemap.xml.gz - This 81 * file needs to be writable! 82 * 83 * @author Andreas Gohr 84 * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html 85 */ 86function runSitemapper(){ 87 global $conf; 88 print "runSitemapper(): started".NL; 89 if(!$conf['sitemap']) return false; 90 91 if($conf['usegzip']){ 92 $sitemap = DOKU_INC.'sitemap.xml.gz'; 93 }else{ 94 $sitemap = DOKU_INC.'sitemap.xml'; 95 } 96 print "runSitemapper(): using $sitemap".NL; 97 98 if(!is_writable($sitemap)) return false; 99 if(@filesize($sitemap) && 100 @filemtime($sitemap) > (time()-($conf['sitemap']*60*60*24))){ 101 print 'runSitemapper(): Sitemap up to date'.NL; 102 return false; 103 } 104 105 $pages = file($conf['cachedir'].'/page.idx'); 106 print 'runSitemapper(): creating sitemap using '.count($pages).' pages'.NL; 107 108 // build the sitemap 109 ob_start(); 110 print '<?xml version="1.0" encoding="UTF-8"?>'.NL; 111 print '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">'.NL; 112 foreach($pages as $id){ 113 $id = trim($id); 114 $file = wikiFN($id); 115 116 //skip hidden, non existing and restricted files 117 if(isHiddenPage($id)) return false; 118 $date = @filemtime($file); 119 if(!$date) continue; 120 if(auth_aclcheck($id,'','') < AUTH_READ) continue; 121 122 print ' <url>'.NL; 123 print ' <loc>'.wl($id,'',true).'</loc>'.NL; 124 print ' <lastmod>'.date_iso8601($date).'</lastmod>'.NL; 125 print ' </url>'.NL; 126 } 127 print '</urlset>'.NL; 128 $data = ob_get_contents(); 129 ob_end_clean(); 130 131 //save the new sitemap 132 io_saveFile($sitemap,$data); 133 134 print 'runSitemapper(): pinging google'.NL; 135 //ping google 136 $url = 'http://www.google.com/webmasters/sitemaps/ping?sitemap='; 137 $url .= urlencode(DOKU_URL.$sitemap); 138 $http = new DokuHTTPClient(); 139 $http->get($url); 140 if($http->error) print 'runSitemapper(): '.$http->error.NL; 141 142 print 'runSitemapper(): finished'.NL; 143 return true; 144} 145 146/** 147 * Formats a timestamp as ISO 8601 date 148 * 149 * @author <ungu at terong dot com> 150 * @link http://www.php.net/manual/en/function.date.php#54072 151 */ 152function date_iso8601($int_date) { 153 //$int_date: current date in UNIX timestamp 154 $date_mod = date('Y-m-d\TH:i:s', $int_date); 155 $pre_timezone = date('O', $int_date); 156 $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 157 $date_mod .= $time_zone; 158 return $date_mod; 159} 160 161/** 162 * Just send a 1x1 pixel blank gif to the browser 163 * 164 * @author Andreas Gohr <andi@splitbrain.org> 165 * @author Harry Fuecks <fuecks@gmail.com> 166 */ 167function sendGIF(){ 168 if($_REQUEST['debug']){ 169 header('Content-Type: text/plain'); 170 return; 171 } 172 $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7'); 173 header('Content-Type: image/gif'); 174 header('Content-Length: '.strlen($img)); 175 header('Connection: Close'); 176 print $img; 177 flush(); 178 // Browser should drop connection after this 179 // Thinks it's got the whole image 180} 181 182//Setup VIM: ex: et ts=4 enc=utf-8 : 183// No trailing PHP closing tag - no output please! 184// See Note at http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php 185