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