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