1<?php
2
3/**
4 * SubjectIndex plugin indexer
5 *
6 * @author  Symon Bent
7 */
8
9if(!defined('DOKU_INC')) die();
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'action.php');
12
13
14
15class action_plugin_subjectindex_indexer extends DokuWiki_Action_Plugin {
16
17    /**
18     * Register its handlers with the DokuWiki's event controller
19     */
20    function register(Doku_Event_Handler $controller) {
21        $controller->register_hook('INDEXER_PAGE_ADD', 'AFTER', $this, 'handle');
22    }
23
24
25    function handle(&$event, $param) {
26        require_once(DOKU_INC . 'inc/indexer.php');
27        require_once(DOKU_PLUGIN . 'subjectindex/inc/common.php');
28        require_once(DOKU_PLUGIN . 'subjectindex/inc/matcher.php');
29
30        if (isset($event->data['page'])) {
31            $page = $event->data['page'];
32        }
33        if (empty($page)) return;   // get out if no such wiki-page
34
35        $raw_page = rawWiki($page);
36
37        $all_pages = idx_getIndex('page', '');
38        $indexer = new SI_Indexer();
39
40        // first remove any entries that reference non-existant files (currently once a day!)
41        $indexer->cleanup($all_pages);
42
43        // now get all marked up entries for this wiki page
44        $matched_entries = array();
45        if ( ! $this->_skip_index($raw_page)) {
46            $matcher = new SI_MatchEntry();
47            if ($matcher->match($raw_page) === true) {
48                $matched_entries = $matcher->all;
49            }
50        }
51
52        // get page id--this corresponds to line number in page.idx file
53        $doku_indexer = idx_get_indexer();
54        $page_id = $doku_indexer->getPID($page);
55
56        $indexer->update($page_id, $matched_entries)->save();
57    }
58
59
60    private function _skip_index($page) {
61        $skip_index = (preg_match('`~~NOSUBJECTINDEX~~`i', $page) == 1);
62        return $skip_index;
63    }
64}