From a8dba4523d2ecd09dd69a68a36673eaf5c009c57 Mon Sep 17 00:00:00 2001
From: Michael Hamann <michael@content-space.de>
Date: Sat, 19 Oct 2013 16:09:15 +0200
Subject: [PATCH] Fix FS#2854: Treat numerically different keys as different

---
 _test/tests/inc/indexer_indexing.test.php | 45 +++++++++++++++++++++++
 inc/indexer.php                           | 22 +++++------
 2 files changed, 56 insertions(+), 11 deletions(-)
 create mode 100644 _test/tests/inc/indexer_indexing.test.php

diff --git a/_test/tests/inc/indexer_indexing.test.php b/_test/tests/inc/indexer_indexing.test.php
new file mode 100644
index 000000000..8600cf156
--- /dev/null
+++ b/_test/tests/inc/indexer_indexing.test.php
@@ -0,0 +1,45 @@
+<?php
+/**
+ * Tests the indexing functionality of the indexer
+ *
+ * @author Michael Hamann <michael@content-space.de>
+ */
+class indexer_indexing_test extends DokuWikiTest {
+    public function setUp() {
+        parent::setUp();
+        saveWikiText('testpage', 'Foo bar baz.', 'Test initialization');
+        saveWikiText('notfound', 'Foon barn bazn.', 'Test initialization');
+        idx_addPage('testpage');
+        idx_addPage('notfound');
+    }
+
+    public function test_words() {
+        $indexer = idx_get_indexer();
+        $query = array('baz', 'foo');
+        $this->assertEquals(array('baz' => array('testpage' => 1), 'foo' => array('testpage' => 1)), $indexer->lookup($query));
+    }
+
+    public function test_numerically_identical_words() {
+        $indexer = idx_get_indexer();
+        $indexer->addPageWords('testpage', '0x1 002');
+        $indexer->addPageWords('notfound', '0x2');
+        $query = array('001', '002');
+        $this->assertEquals(array('001' => array('notfound' => 1), '002' => array('testpage' => 1)), $indexer->lookup($query));
+    }
+
+    public function test_meta() {
+        $indexer = idx_get_indexer();
+        $indexer->addMetaKeys('testpage', 'testkey', 'testvalue');
+        $indexer->addMetaKeys('notfound', 'testkey', 'notvalue');
+        $query = 'testvalue';
+        $this->assertEquals(array('testpage'), $indexer->lookupKey('testkey', $query));
+    }
+
+    public function test_numerically_identical_meta_values() {
+        $indexer = idx_get_indexer();
+        $indexer->addMetaKeys('testpage', 'numkey', array('0001', '01'));
+        $indexer->addMetaKeys('notfound', 'numkey', array('00001', '000001'));
+        $query = array('001', '01');
+        $this->assertEquals(array('001' => array(), '01' => array('testpage')), $indexer->lookupKey('numkey', $query));
+    }
+}
\ No newline at end of file
diff --git a/inc/indexer.php b/inc/indexer.php
index 8f0ba7ec6..378abb360 100644
--- a/inc/indexer.php
+++ b/inc/indexer.php
@@ -10,7 +10,7 @@
 if(!defined('DOKU_INC')) die('meh.');
 
 // Version tag used to force rebuild on upgrade
-define('INDEXER_VERSION', 6);
+define('INDEXER_VERSION', 7);
 
 // set the minimum token length to use in the index (note, this doesn't apply to numeric tokens)
 if (!defined('IDX_MINWORDLENGTH')) define('IDX_MINWORDLENGTH',2);
@@ -215,7 +215,7 @@ class Doku_Indexer {
         foreach (array_keys($words) as $wlen) {
             $word_idx = $this->getIndex('w', $wlen);
             foreach ($words[$wlen] as $word => $freq) {
-                $wid = array_search($word, $word_idx);
+                $wid = array_search($word, $word_idx, true);
                 if ($wid === false) {
                     $wid = count($word_idx);
                     $word_idx[] = $word;
@@ -296,7 +296,7 @@ class Doku_Indexer {
             foreach ($values as $val) {
                 $val = (string)$val;
                 if ($val !== "") {
-                    $id = array_search($val, $metawords);
+                    $id = array_search($val, $metawords, true);
                     if ($id === false) {
                         $id = count($metawords);
                         $metawords[$id] = $val;
@@ -352,13 +352,13 @@ class Doku_Indexer {
 
         $pages = $this->getPages();
 
-        $id = array_search($oldpage, $pages);
+        $id = array_search($oldpage, $pages, true);
         if ($id === false) {
             $this->unlock();
             return 'page is not in index';
         }
 
-        $new_id = array_search($newpage, $pages);
+        $new_id = array_search($newpage, $pages, true);
         if ($new_id !== false) {
             // make sure the page is not in the index anymore
             if ($this->deletePageNoLock($newpage) !== true) {
@@ -397,9 +397,9 @@ class Doku_Indexer {
 
         // change the relation references index
         $metavalues = $this->getIndex($key, '_w');
-        $oldid = array_search($oldvalue, $metavalues);
+        $oldid = array_search($oldvalue, $metavalues, true);
         if ($oldid !== false) {
-            $newid = array_search($newvalue, $metavalues);
+            $newid = array_search($newvalue, $metavalues, true);
             if ($newid !== false) {
                 // free memory
                 unset ($metavalues);
@@ -600,7 +600,7 @@ class Doku_Indexer {
 
         foreach ($wordlist as $i => $word) {
             if ((!is_numeric($word) && strlen($word) < IDX_MINWORDLENGTH)
-              || array_search($word, $stopwords) !== false)
+              || array_search($word, $stopwords, true) !== false)
                 unset($wordlist[$i]);
         }
         return array_values($wordlist);
@@ -771,7 +771,7 @@ class Doku_Indexer {
                     foreach(array_keys(preg_grep('/'.$re.'/', $words)) as $i)
                         $value_ids[$i][] = $val;
                 } else {
-                    if (($i = array_search($val, $words)) !== false)
+                    if (($i = array_search($val, $words, true)) !== false)
                         $value_ids[$i][] = $val;
                 }
             }
@@ -874,7 +874,7 @@ class Doku_Indexer {
             // handle exact search
             if (isset($tokenlength[$ixlen])) {
                 foreach ($tokenlength[$ixlen] as $xword) {
-                    $wid = array_search($xword, $word_idx);
+                    $wid = array_search($xword, $word_idx, true);
                     if ($wid !== false) {
                         $wids[$ixlen][] = $wid;
                         foreach ($tokens[$xword] as $w)
@@ -1152,7 +1152,7 @@ class Doku_Indexer {
      */
     protected function addIndexKey($idx, $suffix, $value) {
         $index = $this->getIndex($idx, $suffix);
-        $id = array_search($value, $index);
+        $id = array_search($value, $index, true);
         if ($id === false) {
             $id = count($index);
             $index[$id] = $value;
-- 
GitLab