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