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