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,0777)){ 53 if(time()-@filemtime($lock) > 60*5){ 54 // looks like a stale lock - remove it 55 @rmdir($lock); 56 print "runIndexer(): stale lock removed".NL; 57 }else{ 58 print "runIndexer(): indexer locked".NL; 59 return false; 60 } 61 } 62 63 require_once(DOKU_INC.'inc/indexer.php'); 64 65 // do the work 66 idx_addPage($ID); 67 68 // we're finished - save and free lock 69 io_saveFile(metaFN($ID,'.indexed'),' '); 70 @rmdir($lock); 71 print "runIndexer(): finished".NL; 72 return true; 73} 74 75/** 76 * Builds a Google Sitemap of all public pages known to the indexer 77 * 78 * The map is placed in the root directory named sitemap.xml.gz - This 79 * file needs to be writable! 80 * 81 * @author Andreas Gohr 82 * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html 83 */ 84function runSitemapper(){ 85 global $conf; 86 print "runSitemapper(): started".NL; 87 if(!$conf['sitemap']) return false; 88 89 if($conf['usegzip']){ 90 $sitemap = DOKU_INC.'sitemap.xml.gz'; 91 }else{ 92 $sitemap = DOKU_INC.'sitemap.xml'; 93 } 94 print "runSitemapper(): using $sitemap".NL; 95 96 if(!is_writable($sitemap)) return false; 97 if(@filesize($sitemap) && 98 @filemtime($sitemap) > (time()-($conf['sitemap']*60*60*24))){ 99 print 'runSitemapper(): Sitemap up to date'.NL; 100 return false; 101 } 102 103 $pages = file($conf['cachedir'].'/page.idx'); 104 print 'runSitemapper(): creating sitemap using '.count($pages).' pages'.NL; 105 106 // build the sitemap 107 ob_start(); 108 print '<?xml version="1.0" encoding="UTF-8"?>'.NL; 109 print '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">'.NL; 110 foreach($pages as $id){ 111 $id = trim($id); 112 $file = wikiFN($id); 113 114 //skip hidden, non existing and restricted files 115 if(isHiddenPage($id)) return false; 116 $date = @filemtime($file); 117 if(!$date) continue; 118 if(auth_aclcheck($id,'','') < AUTH_READ) continue; 119 120 print ' <url>'.NL; 121 print ' <loc>'.wl($id,'',true).'</loc>'.NL; 122 print ' <lastmod>'.date_iso8601($date).'</lastmod>'.NL; 123 print ' </url>'.NL; 124 } 125 print '</urlset>'.NL; 126 $data = ob_get_contents(); 127 ob_end_clean(); 128 129 //save the new sitemap 130 io_saveFile($sitemap,$data); 131 132 print 'runSitemapper(): pinging google'.NL; 133 //ping google 134 $url = 'http://www.google.com/webmasters/sitemaps/ping?sitemap='; 135 $url .= urlencode(DOKU_URL.$sitemap); 136 $http = new DokuHTTPClient(); 137 $http->get($url); 138 if($http->error) print 'runSitemapper(): '.$http->error.NL; 139 140 print 'runSitemapper(): finished'.NL; 141 return true; 142} 143 144/** 145 * Formats a timestamp as ISO 8601 date 146 * 147 * @author <ungu at terong dot com> 148 * @link http://www.php.net/manual/en/function.date.php#54072 149 */ 150function date_iso8601($int_date) { 151 //$int_date: current date in UNIX timestamp 152 $date_mod = date('Y-m-d\TH:i:s', $int_date); 153 $pre_timezone = date('O', $int_date); 154 $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 155 $date_mod .= $time_zone; 156 return $date_mod; 157} 158 159/** 160 * Just send a 1x1 pixel blank gif to the browser 161 * 162 * @author Andreas Gohr <andi@splitbrain.org> 163 * @author Harry Fuecks <fuecks@gmail.com> 164 */ 165function sendGIF(){ 166 if($_REQUEST['debug']){ 167 header('Content-Type: text/plain'); 168 return; 169 } 170 $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7'); 171 header('Content-Type: image/gif'); 172 header('Content-Length: '.strlen($img)); 173 header('Connection: Close'); 174 print $img; 175 flush(); 176 // Browser should drop connection after this 177 // Thinks it's got the whole image 178} 179 180//Setup VIM: ex: et ts=4 enc=utf-8 : 181// No trailing PHP closing tag - no output please! 182// See Note at http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php 183