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