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