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