1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
4 if (function_exists('date_default_timezone_set')) {
5 date_default_timezone_set('UTC');
8 include dirname(__FILE__) . '/../inc/common.php';
9 MTrackSearchDB::setBatchMode();
11 $vardir = MTrackConfig::get('core', 'vardir');
13 /* only allow one instance to run concurrently */
14 $fp = fopen("$vardir/.indexer.lock", 'w');
18 if (!flock($fp, LOCK_EX|LOCK_NB)) {
19 echo "Another instance is already running\n";
22 /* "leak" $fp, so that the lock is held while we continue to run */
24 /* log to a file in the var dir */
25 function log_output($buffer)
28 fwrite($log_file, $buffer);
31 $log_file = fopen("$vardir/indexer.log", 'w');
33 ob_start('log_output');
35 function log_flush() {
42 echo "Indexing started at " . date('c') . "\n";
45 $last = '1990-01-01T00:00:00';
47 foreach (MTrackDB::q('select last_run from search_engine_state')->fetchAll()
52 $LATEST = strtotime($last);
57 function index_and_measure($object)
60 if (isset($DONE[$object])) {
63 $DONE[$object] = true;
65 echo "Examine: $object\n";
68 $res = MTrackSearchDB::index_object($object);
69 $elapsed = time() - $start;
70 printf("Indexed $object in %f seconds\n", $elapsed);
75 function index_items($lower)
83 /* do the work here */
85 foreach (MTrackDB::q('select object, max(changedate) from changes where changedate > ? group by object order by max(changedate)', $lower)->fetchAll(PDO::FETCH_NUM)
88 if ($LATEST > ($FIRST + 3) && time() - $start_time > 280) {
89 // Step back 1 second on the next run, otherwise we may miss out
90 // a couple of items from the current second
95 list($object, $when) = $row;
99 $res = index_and_measure($object);
103 if ($res === false) {
104 echo "Don't know how to index $object\n";
106 echo "Processed $object $when > $lower\n";
108 $t = strtotime($when);
116 // walk all the wiki pages, in case someone checked in against the
117 // wiki repo outside of the app
119 $root = MTrackWikiItem::getRepoAndRoot($repo);
120 $suf = MTrackConfig::get('core', 'wikifilenamesuffix');
121 function walk_wiki($repo, $dir, $suf)
125 $items = $repo->readdir($dir);
126 foreach ($items as $file) {
128 walk_wiki($repo, $file->name, $suf);
130 if (!strlen($suf) || substr($file->name, -strlen($suf)) == $suf) {
131 //echo "Going to index wiki:$file->name\n";
132 $object = "wiki:$file->name";
133 index_and_measure($object);
135 //echo "NO: wiki:$file->name\n";
140 walk_wiki($repo, $root, $suf);
145 $db = MTrackDB::get();
146 $db->beginTransaction();
147 $db->exec("delete from search_engine_state");
148 $insert = $db->prepare("insert into search_engine_state (last_run) values (?)");
149 $insert->execute(array(MTrackDB::unixtime($LATEST)));
153 MTrackSearchDB::commit();
157 $elapsed = $end_time - $start_time;
158 echo "$ITEMS items processed (in $elapsed seconds)\n";