Fix regression from 36662: broke the external link icon totally, external links weren...
[lhc/web/wiklou.git] / t / Search.inc
1 <?php
2
3 $wgCommandLineMode = true;
4 $self = 'Search.t';
5 define( 'MEDIAWIKI', true );
6 require 't/Test.php';
7 require 'includes/Defines.php';
8 require 'includes/ProfilerStub.php';
9 require 'LocalSettings.php';
10 require 'AdminSettings.php';
11 require 'includes/Setup.php';
12
13 function buildTestDatabase( $tables ) {
14 global $wgDBprefix, $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname, $wgDBtype;
15 $oldPrefix = $wgDBprefix;
16 $wgDBprefix = 'parsertest';
17 $class = 'Database' . ucfirst( $wgDBtype );
18 $db = new $class (
19 $wgDBserver,
20 $wgDBadminuser,
21 $wgDBadminpassword,
22 $wgDBname );
23 if( $db->isOpen() ) {
24 if ( !( stristr( $db->getSoftwareLink(), 'MySQL') && version_compare( $db->getServerVersion(), '4.1', '<' ) ) ) {
25 # Database that supports CREATE TABLE ... LIKE
26 foreach ($tables as $tbl) {
27 $newTableName = $db->tableName( $tbl );
28 #$tableName = $oldPrefix . $tbl;
29 $tableName = $tbl;
30 $db->query("CREATE TEMPORARY TABLE $newTableName (LIKE $tableName)");
31 }
32 } else {
33 # Hack for MySQL versions < 4.1, which don't support
34 # "CREATE TABLE ... LIKE". Note that
35 # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0"
36 # would not create the indexes we need....
37 foreach ($tables as $tbl) {
38 $res = $db->query("SHOW CREATE TABLE $tbl");
39 $row = $db->fetchRow($res);
40 $create = $row[1];
41 $create_tmp = preg_replace('/CREATE TABLE `(.*?)`/', 'CREATE TEMPORARY TABLE `'
42 . $wgDBprefix . '\\1`', $create);
43 if ($create === $create_tmp) {
44 # Couldn't do replacement
45 wfDie( "could not create temporary table $tbl" );
46 }
47 $db->query($create_tmp);
48 }
49
50 }
51 return $db;
52 } else {
53 // Something amiss
54 return null;
55 }
56 }
57
58 class SearchEngineTest {
59 var $db, $search;
60
61 function __construct( SearchEngine $search ){
62 $this->search = $search;
63 $this->db = $this->search->db;
64 }
65
66 function insertSearchData() {
67 $this->db->safeQuery( <<<END
68 INSERT INTO ! (page_id,page_namespace,page_title,page_latest)
69 VALUES (1, 0, 'Main_Page', 1),
70 (2, 1, 'Main_Page', 2),
71 (3, 0, 'Smithee', 3),
72 (4, 1, 'Smithee', 4),
73 (5, 0, 'Unrelated_page', 5),
74 (6, 0, 'Another_page', 6),
75 (7, 4, 'Help', 7),
76 (8, 0, 'Thppt', 8),
77 (9, 0, 'Alan_Smithee', 9),
78 (10, 0, 'Pages', 10)
79 END
80 , $this->db->tableName( 'page' ) );
81 $this->db->safeQuery( <<<END
82 INSERT INTO ! (rev_id,rev_page)
83 VALUES (1, 1),
84 (2, 2),
85 (3, 3),
86 (4, 4),
87 (5, 5),
88 (6, 6),
89 (7, 7),
90 (8, 8),
91 (9, 9),
92 (10, 10)
93 END
94 , $this->db->tableName( 'revision' ) );
95 $this->db->safeQuery( <<<END
96 INSERT INTO ! (old_id,old_text)
97 VALUES (1, 'This is a main page'),
98 (2, 'This is a talk page to the main page, see [[smithee]]'),
99 (3, 'A smithee is one who smiths. See also [[Alan Smithee]]'),
100 (4, 'This article sucks.'),
101 (5, 'Nothing in this page is about the S word.'),
102 (6, 'This page also is unrelated.'),
103 (7, 'Help me!'),
104 (8, 'Blah blah'),
105 (9, 'yum'),
106 (10,'are food')
107 END
108 , $this->db->tableName( 'text' ) );
109 $this->db->safeQuery( <<<END
110 INSERT INTO ! (si_page,si_title,si_text)
111 VALUES (1, 'main page', 'this is a main page'),
112 (2, 'main page', 'this is a talk page to the main page, see smithee'),
113 (3, 'smithee', 'a smithee is one who smiths see also alan smithee'),
114 (4, 'smithee', 'this article sucks'),
115 (5, 'unrelated page', 'nothing in this page is about the s word'),
116 (6, 'another page', 'this page also is unrelated'),
117 (7, 'help', 'help me'),
118 (8, 'thppt', 'blah blah'),
119 (9, 'alan smithee', 'yum'),
120 (10, 'pages', 'are food')
121 END
122 , $this->db->tableName( 'searchindex' ) );
123 }
124
125 function fetchIds( $results ) {
126 $matches = array();
127 while( $row = $results->next() ) {
128 $matches[] = $row->getTitle()->getPrefixedText();
129 }
130 $results->free();
131 # Search is not guaranteed to return results in a certain order;
132 # sort them numerically so we will compare simply that we received
133 # the expected matches.
134 sort( $matches );
135 return $matches;
136 }
137
138 function run(){
139 if( is_null( $this->db ) ){
140 fail( "Can't find a database to test with." );
141 return;
142 }
143
144 $this->insertSearchData();
145 plan( 4 );
146
147 $exp = array( 'Smithee' );
148 $got = $this->fetchIds( $this->search->searchText( 'smithee' ) );
149 is( $got, $exp, "Plain search" );
150
151 $exp = array( 'Alan Smithee', 'Smithee' );
152 $got = $this->fetchIds( $this->search->searchTitle( 'smithee' ) );
153 is( $got, $exp, "Title search" );
154
155 $this->search->setNamespaces( array( 0, 1, 4 ) );
156
157 $exp = array( 'Smithee', 'Talk:Main Page', );
158 $got = $this->fetchIds( $this->search->searchText( 'smithee' ) );
159 is( $got, $exp, "Power search" );
160
161 $exp = array( 'Alan Smithee', 'Smithee', 'Talk:Smithee', );
162 $got = $this->fetchIds( $this->search->searchTitle( 'smithee' ) );
163 is( $got, $exp, "Title power search" );
164 }
165 }