xref: /dokuwiki/lib/exe/indexer.php (revision ba6984cf400bde3cbdd7e1f203602cb0d04eb7d2)
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',dirname(__FILE__).'/../../');
9define('DOKU_DISABLE_GZIP_OUTPUT',1);
10require_once(DOKU_INC.'inc/init.php');
11session_write_close();  //close session
12if(!defined('NL')) define('NL',"\n");
13
14// Version tag used to force rebuild on upgrade
15define('INDEXER_VERSION', 2);
16
17// keep running after browser closes connection
18@ignore_user_abort(true);
19
20// check if user abort worked, if yes send output early
21$defer = !@ignore_user_abort() || $conf['broken_iua'];
22if(!$defer){
23    sendGIF(); // send gif
24}
25
26$ID = cleanID($_REQUEST['id']);
27
28// Catch any possible output (e.g. errors)
29$output = isset($_REQUEST['debug']) && $conf['allowdebug'];
30if(!$output) ob_start();
31
32// run one of the jobs
33$tmp = array(); // No event data
34$evt = new Doku_Event('INDEXER_TASKS_RUN', $tmp);
35if ($evt->advise_before()) {
36  runIndexer() or
37  metaUpdate() or
38  runSitemapper() or
39  sendDigest() or
40  runTrimRecentChanges() or
41  runTrimRecentChanges(true) or
42  $evt->advise_after();
43}
44if($defer) sendGIF();
45
46if(!$output) ob_end_clean();
47exit;
48
49// --------------------------------------------------------------------
50
51/**
52 * Trims the recent changes cache (or imports the old changelog) as needed.
53 *
54 * @param media_changes If the media changelog shall be trimmed instead of
55 * the page changelog
56 *
57 * @author Ben Coburn <btcoburn@silicodon.net>
58 */
59function runTrimRecentChanges($media_changes = false) {
60    global $conf;
61
62    $fn = ($media_changes ? $conf['media_changelog'] : $conf['changelog']);
63
64    // Trim the Recent Changes
65    // Trims the recent changes cache to the last $conf['changes_days'] recent
66    // changes or $conf['recent'] items, which ever is larger.
67    // The trimming is only done once a day.
68    if (@file_exists($fn) &&
69        (@filemtime($fn.'.trimmed')+86400)<time() &&
70        !@file_exists($fn.'_tmp')) {
71            @touch($fn.'.trimmed');
72            io_lock($fn);
73            $lines = file($fn);
74            if (count($lines)<=$conf['recent']) {
75                // nothing to trim
76                io_unlock($fn);
77                return false;
78            }
79
80            io_saveFile($fn.'_tmp', '');          // presave tmp as 2nd lock
81            $trim_time = time() - $conf['recent_days']*86400;
82            $out_lines = array();
83
84            for ($i=0; $i<count($lines); $i++) {
85              $log = parseChangelogLine($lines[$i]);
86              if ($log === false) continue;                      // discard junk
87              if ($log['date'] < $trim_time) {
88                $old_lines[$log['date'].".$i"] = $lines[$i];     // keep old lines for now (append .$i to prevent key collisions)
89              } else {
90                $out_lines[$log['date'].".$i"] = $lines[$i];     // definitely keep these lines
91              }
92            }
93
94            if (count($lines)==count($out_lines)) {
95              // nothing to trim
96              @unlink($fn.'_tmp');
97              io_unlock($fn);
98              return false;
99            }
100
101            // sort the final result, it shouldn't be necessary,
102            //   however the extra robustness in making the changelog cache self-correcting is worth it
103            ksort($out_lines);
104            $extra = $conf['recent'] - count($out_lines);        // do we need extra lines do bring us up to minimum
105            if ($extra > 0) {
106              ksort($old_lines);
107              $out_lines = array_merge(array_slice($old_lines,-$extra),$out_lines);
108            }
109
110            // save trimmed changelog
111            io_saveFile($fn.'_tmp', implode('', $out_lines));
112            @unlink($fn);
113            if (!rename($fn.'_tmp', $fn)) {
114                // rename failed so try another way...
115                io_unlock($fn);
116                io_saveFile($fn, implode('', $out_lines));
117                @unlink($fn.'_tmp');
118            } else {
119                io_unlock($fn);
120            }
121            return true;
122    }
123
124    // nothing done
125    return false;
126}
127
128/**
129 * Runs the indexer for the current page
130 *
131 * @author Andreas Gohr <andi@splitbrain.org>
132 */
133function runIndexer(){
134    global $ID;
135    global $conf;
136    print "runIndexer(): started".NL;
137
138    if(!$ID) return false;
139
140    // check if indexing needed
141    $idxtag = metaFN($ID,'.indexed');
142    if(@file_exists($idxtag)){
143        if(io_readFile($idxtag) >= INDEXER_VERSION){
144            $last = @filemtime($idxtag);
145            if($last > @filemtime(wikiFN($ID))){
146                print "runIndexer(): index for $ID up to date".NL;
147                return false;
148            }
149        }
150    }
151
152    // try to aquire a lock
153    $lock = $conf['lockdir'].'/_indexer.lock';
154    while(!@mkdir($lock,$conf['dmode'])){
155        usleep(50);
156        if(time()-@filemtime($lock) > 60*5){
157            // looks like a stale lock - remove it
158            @rmdir($lock);
159            print "runIndexer(): stale lock removed".NL;
160        }else{
161            print "runIndexer(): indexer locked".NL;
162            return false;
163        }
164    }
165    if($conf['dperm']) chmod($lock, $conf['dperm']);
166
167    // do the work
168    idx_addPage($ID);
169
170    // we're finished - save and free lock
171    io_saveFile(metaFN($ID,'.indexed'),INDEXER_VERSION);
172    @rmdir($lock);
173    print "runIndexer(): finished".NL;
174    return true;
175}
176
177/**
178 * Will render the metadata for the page if not exists yet
179 *
180 * This makes sure pages which are created from outside DokuWiki will
181 * gain their data when viewed for the first time.
182 */
183function metaUpdate(){
184    global $ID;
185    print "metaUpdate(): started".NL;
186
187    if(!$ID) return false;
188    $file = metaFN($ID, '.meta');
189    echo "meta file: $file".NL;
190
191    // rendering needed?
192    if (@file_exists($file)) return false;
193    if (!page_exists($ID)) return false;
194
195    global $conf;
196
197    // gather some additional info from changelog
198    $info = io_grep($conf['changelog'],
199                    '/^(\d+)\t(\d+\.\d+\.\d+\.\d+)\t'.preg_quote($ID,'/').'\t([^\t]+)\t([^\t\n]+)/',
200                    0,true);
201
202    $meta = array();
203    if(!empty($info)){
204        $meta['date']['created'] = $info[0][1];
205        foreach($info as $item){
206            if($item[4] != '*'){
207                $meta['date']['modified'] = $item[1];
208                if($item[3]){
209                    $meta['contributor'][$item[3]] = $item[3];
210                }
211            }
212        }
213    }
214
215    $meta = p_render_metadata($ID, $meta);
216    p_save_metadata($ID, $meta);
217
218    echo "metaUpdate(): finished".NL;
219    return true;
220}
221
222/**
223 * Builds a Google Sitemap of all public pages known to the indexer
224 *
225 * The map is placed in the root directory named sitemap.xml.gz - This
226 * file needs to be writable!
227 *
228 * @author Andreas Gohr
229 * @link   https://www.google.com/webmasters/sitemaps/docs/en/about.html
230 */
231function runSitemapper(){
232    print "runSitemapper(): started".NL;
233    $result = Sitemapper::generate() && Sitemapper::pingSearchEngines();
234    print 'runSitemapper(): finished'.NL;
235    return $result;
236}
237
238/**
239 * Send digest and list mails for all subscriptions which are in effect for the
240 * current page
241 *
242 * @author Adrian Lang <lang@cosmocode.de>
243 */
244function sendDigest() {
245    echo 'sendDigest(): start'.NL;
246    global $ID;
247    global $conf;
248    if (!$conf['subscribers']) {
249        return;
250    }
251    $subscriptions = subscription_find($ID, array('style' => '(digest|list)',
252                                                  'escaped' => true));
253    global $auth;
254    global $lang;
255    global $conf;
256    global $USERINFO;
257
258    // remember current user info
259    $olduinfo = $USERINFO;
260    $olduser  = $_SERVER['REMOTE_USER'];
261
262    foreach($subscriptions as $id => $users) {
263        if (!subscription_lock($id)) {
264            continue;
265        }
266        foreach($users as $data) {
267            list($user, $style, $lastupdate) = $data;
268            $lastupdate = (int) $lastupdate;
269            if ($lastupdate + $conf['subscribe_time'] > time()) {
270                // Less than the configured time period passed since last
271                // update.
272                continue;
273            }
274
275            // Work as the user to make sure ACLs apply correctly
276            $USERINFO = $auth->getUserData($user);
277            $_SERVER['REMOTE_USER'] = $user;
278            if ($USERINFO === false) {
279                continue;
280            }
281
282            if (substr($id, -1, 1) === ':') {
283                // The subscription target is a namespace
284                $changes = getRecentsSince($lastupdate, null, getNS($id));
285            } else {
286                if(auth_quickaclcheck($id) < AUTH_READ) continue;
287
288                $meta = p_get_metadata($id);
289                $changes = array($meta['last_change']);
290            }
291
292            // Filter out pages only changed in small and own edits
293            $change_ids = array();
294            foreach($changes as $rev) {
295                $n = 0;
296                while (!is_null($rev) && $rev['date'] >= $lastupdate &&
297                       ($_SERVER['REMOTE_USER'] === $rev['user'] ||
298                        $rev['type'] === DOKU_CHANGE_TYPE_MINOR_EDIT)) {
299                    $rev = getRevisions($rev['id'], $n++, 1);
300                    $rev = (count($rev) > 0) ? $rev[0] : null;
301                }
302
303                if (!is_null($rev) && $rev['date'] >= $lastupdate) {
304                    // Some change was not a minor one and not by myself
305                    $change_ids[] = $rev['id'];
306                }
307            }
308
309            if ($style === 'digest') {
310                foreach($change_ids as $change_id) {
311                    subscription_send_digest($USERINFO['mail'], $change_id,
312                                             $lastupdate);
313                }
314            } elseif ($style === 'list') {
315                subscription_send_list($USERINFO['mail'], $change_ids, $id);
316            }
317            // TODO: Handle duplicate subscriptions.
318
319            // Update notification time.
320            subscription_set($user, $id, $style, time(), true);
321        }
322        subscription_unlock($id);
323    }
324
325    // restore current user info
326    $USERINFO = $olduinfo;
327    $_SERVER['REMOTE_USER'] = $olduser;
328}
329
330/**
331 * Just send a 1x1 pixel blank gif to the browser
332 *
333 * @author Andreas Gohr <andi@splitbrain.org>
334 * @author Harry Fuecks <fuecks@gmail.com>
335 */
336function sendGIF(){
337    if(isset($_REQUEST['debug'])){
338        header('Content-Type: text/plain');
339        return;
340    }
341    $img = base64_decode('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAEALAAAAAABAAEAAAIBTAA7');
342    header('Content-Type: image/gif');
343    header('Content-Length: '.strlen($img));
344    header('Connection: Close');
345    print $img;
346    flush();
347    // Browser should drop connection after this
348    // Thinks it's got the whole image
349}
350
351//Setup VIM: ex: et ts=4 enc=utf-8 :
352// No trailing PHP closing tag - no output please!
353// See Note at http://www.php.net/manual/en/language.basic-syntax.instruction-separation.php
354