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 metaUpdate() 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($conf['dperm']) chmod($lock, $conf['dperm']); 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 * Will render the metadata for the page if not exists yet 79 * 80 * This makes sure pages which are created from outside DokuWiki will 81 * gain their data when viewed for the first time. 82 */ 83function metaUpdate(){ 84 print "metaUpdate(): started".NL; 85 86 $ID = cleanID($_REQUEST['id']); 87 if(!$ID) return false; 88 $file = metaFN($ID, '.meta'); 89 90 // rendering needed? 91 if (@file_exists($file)) return false; 92 93 require_once(DOKU_INC.'inc/parserutils.php'); 94 95 $meta = array(); 96 $meta = p_render_metadata($ID, $meta); 97 io_saveFile($file, serialize($meta)); 98 99 print "metaUpdate(): finished".NL; 100 return true; 101} 102 103/** 104 * Builds a Google Sitemap of all public pages known to the indexer 105 * 106 * The map is placed in the root directory named sitemap.xml.gz - This 107 * file needs to be writable! 108 * 109 * @author Andreas Gohr 110 * @link https://www.google.com/webmasters/sitemaps/docs/en/about.html 111 */ 112function runSitemapper(){ 113 global $conf; 114 print "runSitemapper(): started".NL; 115 if(!$conf['sitemap']) return false; 116 117 if($conf['usegzip']){ 118 $sitemap = DOKU_INC.'sitemap.xml.gz'; 119 }else{ 120 $sitemap = DOKU_INC.'sitemap.xml'; 121 } 122 print "runSitemapper(): using $sitemap".NL; 123 124 if(!is_writable($sitemap)) return false; 125 if(@filesize($sitemap) && 126 @filemtime($sitemap) > (time()-($conf['sitemap']*60*60*24))){ 127 print 'runSitemapper(): Sitemap up to date'.NL; 128 return false; 129 } 130 131 $pages = file($conf['cachedir'].'/page.idx'); 132 print 'runSitemapper(): creating sitemap using '.count($pages).' pages'.NL; 133 134 // build the sitemap 135 ob_start(); 136 print '<?xml version="1.0" encoding="UTF-8"?>'.NL; 137 print '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">'.NL; 138 foreach($pages as $id){ 139 $id = trim($id); 140 $file = wikiFN($id); 141 142 //skip hidden, non existing and restricted files 143 if(isHiddenPage($id)) return false; 144 $date = @filemtime($file); 145 if(!$date) continue; 146 if(auth_aclcheck($id,'','') < AUTH_READ) continue; 147 148 print ' <url>'.NL; 149 print ' <loc>'.wl($id,'',true).'</loc>'.NL; 150 print ' <lastmod>'.date_iso8601($date).'</lastmod>'.NL; 151 print ' </url>'.NL; 152 } 153 print '</urlset>'.NL; 154 $data = ob_get_contents(); 155 ob_end_clean(); 156 157 //save the new sitemap 158 io_saveFile($sitemap,$data); 159 160 print 'runSitemapper(): pinging google'.NL; 161 //ping google 162 $url = 'http://www.google.com/webmasters/sitemaps/ping?sitemap='; 163 $url .= urlencode(DOKU_URL.$sitemap); 164 $http = new DokuHTTPClient(); 165 $http->get($url); 166 if($http->error) print 'runSitemapper(): '.$http->error.NL; 167 168 print 'runSitemapper(): finished'.NL; 169 return true; 170} 171 172/** 173 * Formats a timestamp as ISO 8601 date 174 * 175 * @author <ungu at terong dot com> 176 * @link http://www.php.net/manual/en/function.date.php#54072 177 */ 178function date_iso8601($int_date) { 179 //$int_date: current date in UNIX timestamp 180 $date_mod = date('Y-m-d\TH:i:s', $int_date); 181 $pre_timezone = date('O', $int_date); 182 $time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2); 183 $date_mod .= $time_zone; 184 return $date_mod; 185} 186 187/** 188 * Just send a 1x1 pixel blank gif to the browser 189 * 190 * @author Andreas Gohr <andi@splitbrain.org> 191 * @author Harry Fuecks <fuecks@gmail.com> 192 */ 193function sendGIF(){ 194 if($_REQUEST['debug']){ 195 header('Content-Type: text/plain'); 196 return; 197 } 198 $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7'); 199 header('Content-Type: image/gif'); 200 header('Content-Length: '.strlen($img)); 201 header('Connection: Close'); 202 print $img; 203 flush(); 204 // Browser should drop connection after this 205 // Thinks it's got the whole image 206} 207 208//Setup VIM: ex: et ts=4 enc=utf-8 : 209// No trailing PHP closing tag - no output please! 210// See Note at http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php 211