3 $wgCommandLineMode = true;
5 define( 'MEDIAWIKI', true );
7 require 'includes/Defines.php';
8 require 'includes/ProfilerStub.php';
9 require 'LocalSettings.php';
10 require 'includes/Setup.php';
12 function buildTestDatabase( $tables ) {
13 global $wgDBprefix, $wgDBserver, $wgDBname, $wgDBtype;
14 $oldPrefix = $wgDBprefix;
15 $wgDBprefix = 'parsertest';
17 $db = wfGetDB ( DB_SLAVE );
20 if ( !( stristr( $db->getSoftwareLink(), 'MySQL') && version_compare( $db->getServerVersion(), '4.1', '<' ) ) ) {
21 # Database that supports CREATE TABLE ... LIKE
22 foreach ($tables as $tbl) {
23 $newTableName = $db->tableName( $tbl );
24 $tableName = $oldPrefix . $tbl;
25 $db->query("CREATE TEMPORARY TABLE $newTableName (LIKE $tableName)");
28 # Hack for MySQL versions < 4.1, which don't support
29 # "CREATE TABLE ... LIKE". Note that
30 # "CREATE TEMPORARY TABLE ... SELECT * FROM ... LIMIT 0"
31 # would not create the indexes we need....
32 foreach ($tables as $tbl) {
33 $res = $db->query("SHOW CREATE TABLE $tbl");
34 $row = $db->fetchRow($res);
36 $create_tmp = preg_replace('/CREATE TABLE `(.*?)`/', 'CREATE TEMPORARY TABLE `'
37 . $wgDBprefix . '\\1`', $create);
38 if ($create === $create_tmp) {
39 # Couldn't do replacement
40 wfDie( "could not create temporary table $tbl" );
42 $db->query($create_tmp);
53 class SearchEngineTest {
56 function __construct( SearchEngine $search ){
57 $this->search = $search;
58 $this->db = $this->search->db;
61 function insertSearchData() {
62 $this->db->safeQuery( <<<END
63 INSERT INTO ! (page_id,page_namespace,page_title,page_latest)
64 VALUES (1, 0, 'Main_Page', 1),
65 (2, 1, 'Main_Page', 2),
68 (5, 0, 'Unrelated_page', 5),
69 (6, 0, 'Another_page', 6),
72 (9, 0, 'Alan_Smithee', 9),
75 , $this->db->tableName( 'page' ) );
76 $this->db->safeQuery( <<<END
77 INSERT INTO ! (rev_id,rev_page)
89 , $this->db->tableName( 'revision' ) );
90 $this->db->safeQuery( <<<END
91 INSERT INTO ! (old_id,old_text)
92 VALUES (1, 'This is a main page'),
93 (2, 'This is a talk page to the main page, see [[smithee]]'),
94 (3, 'A smithee is one who smiths. See also [[Alan Smithee]]'),
95 (4, 'This article sucks.'),
96 (5, 'Nothing in this page is about the S word.'),
97 (6, 'This page also is unrelated.'),
103 , $this->db->tableName( 'text' ) );
104 $this->db->safeQuery( <<<END
105 INSERT INTO ! (si_page,si_title,si_text)
106 VALUES (1, 'main page', 'this is a main page'),
107 (2, 'main page', 'this is a talk page to the main page, see smithee'),
108 (3, 'smithee', 'a smithee is one who smiths see also alan smithee'),
109 (4, 'smithee', 'this article sucks'),
110 (5, 'unrelated page', 'nothing in this page is about the s word'),
111 (6, 'another page', 'this page also is unrelated'),
112 (7, 'help', 'help me'),
113 (8, 'thppt', 'blah blah'),
114 (9, 'alan smithee', 'yum'),
115 (10, 'pages', 'are food')
117 , $this->db->tableName( 'searchindex' ) );
120 function fetchIds( $results ) {
122 while( $row = $results->next() ) {
123 $matches[] = $row->getTitle()->getPrefixedText();
126 # Search is not guaranteed to return results in a certain order;
127 # sort them numerically so we will compare simply that we received
128 # the expected matches.
134 if( is_null( $this->db ) ){
135 fail( "Can't find a database to test with." );
139 $this->insertSearchData();
142 $exp = array( 'Smithee' );
143 $got = $this->fetchIds( $this->search->searchText( 'smithee' ) );
144 is( $got, $exp, "Plain search" );
146 $exp = array( 'Alan Smithee', 'Smithee' );
147 $got = $this->fetchIds( $this->search->searchTitle( 'smithee' ) );
148 is( $got, $exp, "Title search" );
150 $this->search->setNamespaces( array( 0, 1, 4 ) );
152 $exp = array( 'Smithee', 'Talk:Main Page', );
153 $got = $this->fetchIds( $this->search->searchText( 'smithee' ) );
154 is( $got, $exp, "Power search" );
156 $exp = array( 'Alan Smithee', 'Smithee', 'Talk:Smithee', );
157 $got = $this->fetchIds( $this->search->searchTitle( 'smithee' ) );
158 is( $got, $exp, "Title power search" );