3 $wgCommandLineMode = true;
5 define( 'MEDIAWIKI', true );
7 require 'includes/Defines.php';
8 require 'includes/ProfilerStub.php';
9 require 'LocalSettings.php';
10 require 'AdminSettings.php';
11 require 'includes/Setup.php';
13 function buildTestDatabase( $tables ) {
14 global $wgDBprefix, $wgDBserver, $wgDBadminuser, $wgDBadminpassword, $wgDBname, $wgDBtype;
15 $oldPrefix = $wgDBprefix;
16 $wgDBprefix = 'parsertest';
18 $db = wfGetDB ( DB_SLAVE );
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)");
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);
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" );
43 $db->query($create_tmp);
54 class SearchEngineTest {
57 function __construct( SearchEngine $search ){
58 $this->search = $search;
59 $this->db = $this->search->db;
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),
69 (5, 0, 'Unrelated_page', 5),
70 (6, 0, 'Another_page', 6),
73 (9, 0, 'Alan_Smithee', 9),
76 , $this->db->tableName( 'page' ) );
77 $this->db->safeQuery( <<<END
78 INSERT INTO ! (rev_id,rev_page)
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.'),
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')
118 , $this->db->tableName( 'searchindex' ) );
121 function fetchIds( $results ) {
123 while( $row = $results->next() ) {
124 $matches[] = $row->getTitle()->getPrefixedText();
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.
135 if( is_null( $this->db ) ){
136 fail( "Can't find a database to test with." );
140 $this->insertSearchData();
143 $exp = array( 'Smithee' );
144 $got = $this->fetchIds( $this->search->searchText( 'smithee' ) );
145 is( $got, $exp, "Plain search" );
147 $exp = array( 'Alan Smithee', 'Smithee' );
148 $got = $this->fetchIds( $this->search->searchTitle( 'smithee' ) );
149 is( $got, $exp, "Title search" );
151 $this->search->setNamespaces( array( 0, 1, 4 ) );
153 $exp = array( 'Smithee', 'Talk:Main Page', );
154 $got = $this->fetchIds( $this->search->searchText( 'smithee' ) );
155 is( $got, $exp, "Power search" );
157 $exp = array( 'Alan Smithee', 'Smithee', 'Talk:Smithee', );
158 $got = $this->fetchIds( $this->search->searchTitle( 'smithee' ) );
159 is( $got, $exp, "Title power search" );