2010-12-14 16:26:35 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* @group Search
|
2011-06-28 22:26:22 +00:00
|
|
|
|
* @group Database
|
2010-12-14 16:26:35 +00:00
|
|
|
|
*/
|
2012-11-11 07:13:21 +00:00
|
|
|
|
class SearchEngineTest extends MediaWikiLangTestCase {
|
2011-06-28 22:26:22 +00:00
|
|
|
|
protected $search, $pageList;
|
|
|
|
|
|
|
2011-10-26 03:45:13 +00:00
|
|
|
|
/**
|
2010-12-14 16:26:35 +00:00
|
|
|
|
* Checks for database type & version.
|
|
|
|
|
|
* Will skip current test if DB does not support search.
|
|
|
|
|
|
*/
|
2012-10-08 10:56:20 +00:00
|
|
|
|
protected function setUp() {
|
2011-06-28 22:26:22 +00:00
|
|
|
|
parent::setUp();
|
2012-11-11 07:13:21 +00:00
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
|
// Search tests require MySQL or SQLite with FTS
|
|
|
|
|
|
# Get database type and version
|
2011-06-16 20:57:31 +00:00
|
|
|
|
$dbType = $this->db->getType();
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$dbSupported =
|
2013-02-15 10:24:31 +00:00
|
|
|
|
( $dbType === 'mysql' )
|
|
|
|
|
|
|| ( $dbType === 'sqlite' && $this->db->getFulltextSearchModule() == 'FTS3' );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
2013-02-15 10:24:31 +00:00
|
|
|
|
if ( !$dbSupported ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$this->markTestSkipped( "MySQL or SQLite with FTS3 only" );
|
|
|
|
|
|
}
|
2011-06-28 22:26:22 +00:00
|
|
|
|
|
|
|
|
|
|
$searchType = $this->db->getSearchEngine();
|
|
|
|
|
|
$this->search = new $searchType( $this->db );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-08 10:56:20 +00:00
|
|
|
|
protected function tearDown() {
|
|
|
|
|
|
unset( $this->search );
|
2012-12-31 12:54:06 +00:00
|
|
|
|
|
|
|
|
|
|
parent::tearDown();
|
2012-10-08 10:56:20 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
|
function pageExists( $title ) {
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-06-28 22:26:22 +00:00
|
|
|
|
function addDBData() {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
if ( $this->pageExists( 'Not_Main_Page' ) ) {
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2012-10-12 11:09:08 +00:00
|
|
|
|
|
|
|
|
|
|
if ( !$this->isWikitextNS( NS_MAIN ) ) {
|
2013-05-15 01:12:35 +00:00
|
|
|
|
// @todo cover the case of non-wikitext content in the main namespace
|
2012-10-12 11:09:08 +00:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-02-15 10:24:31 +00:00
|
|
|
|
$this->insertPage( "Not_Main_Page", "This is not a main page", 0 );
|
|
|
|
|
|
$this->insertPage( 'Talk:Not_Main_Page', 'This is not a talk page to the main page, see [[smithee]]', 1 );
|
|
|
|
|
|
$this->insertPage( 'Smithee', 'A smithee is one who smiths. See also [[Alan Smithee]]', 0 );
|
|
|
|
|
|
$this->insertPage( 'Talk:Smithee', 'This article sucks.', 1 );
|
|
|
|
|
|
$this->insertPage( 'Unrelated_page', 'Nothing in this page is about the S word.', 0 );
|
|
|
|
|
|
$this->insertPage( 'Another_page', 'This page also is unrelated.', 0 );
|
|
|
|
|
|
$this->insertPage( 'Help:Help', 'Help me!', 4 );
|
|
|
|
|
|
$this->insertPage( 'Thppt', 'Blah blah', 0 );
|
|
|
|
|
|
$this->insertPage( 'Alan_Smithee', 'yum', 0 );
|
|
|
|
|
|
$this->insertPage( 'Pages', 'are\'food', 0 );
|
|
|
|
|
|
$this->insertPage( 'HalfOneUp', 'AZ', 0 );
|
|
|
|
|
|
$this->insertPage( 'FullOneUp', 'AZ', 0 );
|
|
|
|
|
|
$this->insertPage( 'HalfTwoLow', 'az', 0 );
|
|
|
|
|
|
$this->insertPage( 'FullTwoLow', 'az', 0 );
|
|
|
|
|
|
$this->insertPage( 'HalfNumbers', '1234567890', 0 );
|
|
|
|
|
|
$this->insertPage( 'FullNumbers', '1234567890', 0 );
|
|
|
|
|
|
$this->insertPage( 'DomainName', 'example.com', 0 );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function fetchIds( $results ) {
|
2012-10-12 11:09:08 +00:00
|
|
|
|
if ( !$this->isWikitextNS( NS_MAIN ) ) {
|
|
|
|
|
|
$this->markTestIncomplete( __CLASS__ . " does no yet support non-wikitext content "
|
2013-02-15 10:24:31 +00:00
|
|
|
|
. "in the main namespace" );
|
2012-10-12 11:09:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$this->assertTrue( is_object( $results ) );
|
|
|
|
|
|
|
|
|
|
|
|
$matches = array();
|
2011-10-16 03:27:12 +00:00
|
|
|
|
$row = $results->next();
|
|
|
|
|
|
while ( $row ) {
|
2010-12-14 16:26:35 +00:00
|
|
|
|
$matches[] = $row->getTitle()->getPrefixedText();
|
2011-10-16 03:27:12 +00:00
|
|
|
|
$row = $results->next();
|
2010-12-14 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
$results->free();
|
|
|
|
|
|
# Search is not guaranteed to return results in a certain order;
|
|
|
|
|
|
# sort them numerically so we will compare simply that we received
|
|
|
|
|
|
# the expected matches.
|
|
|
|
|
|
sort( $matches );
|
2013-04-26 12:00:22 +00:00
|
|
|
|
|
2010-12-14 16:26:35 +00:00
|
|
|
|
return $matches;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2011-05-13 09:30:19 +00:00
|
|
|
|
/**
|
|
|
|
|
|
* Insert a new page
|
|
|
|
|
|
*
|
|
|
|
|
|
* @param $pageName String: page name
|
|
|
|
|
|
* @param $text String: page's content
|
|
|
|
|
|
* @param $n Integer: unused
|
|
|
|
|
|
*/
|
2010-12-14 16:26:35 +00:00
|
|
|
|
function insertPage( $pageName, $text, $ns ) {
|
2012-10-12 11:09:08 +00:00
|
|
|
|
$title = Title::newFromText( $pageName, $ns );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
2011-06-16 20:57:31 +00:00
|
|
|
|
$user = User::newFromName( 'WikiSysop' );
|
|
|
|
|
|
$comment = 'Search Test';
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
2011-06-16 20:57:31 +00:00
|
|
|
|
// avoid memory leak...?
|
2011-12-21 18:46:39 +00:00
|
|
|
|
LinkCache::singleton()->clear();
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
2012-02-06 11:19:14 +00:00
|
|
|
|
$page = WikiPage::factory( $title );
|
2012-08-28 13:58:36 +00:00
|
|
|
|
$page->doEditContent( ContentHandler::makeContent( $text, $title ), $comment, 0, false, $user );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
2012-02-06 11:19:14 +00:00
|
|
|
|
$this->pageList[] = array( $title, $page->getId() );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
2011-06-16 20:57:31 +00:00
|
|
|
|
return true;
|
|
|
|
|
|
}
|
2010-12-14 16:26:35 +00:00
|
|
|
|
|
|
|
|
|
|
function testFullWidth() {
|
2011-06-16 20:57:31 +00:00
|
|
|
|
$this->assertEquals(
|
|
|
|
|
|
array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
|
|
|
|
|
|
$this->fetchIds( $this->search->searchText( 'AZ' ) ),
|
|
|
|
|
|
"Search for normalized from Half-width Upper" );
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
|
array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
|
|
|
|
|
|
$this->fetchIds( $this->search->searchText( 'az' ) ),
|
|
|
|
|
|
"Search for normalized from Half-width Lower" );
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
|
array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
|
|
|
|
|
|
$this->fetchIds( $this->search->searchText( 'AZ' ) ),
|
|
|
|
|
|
"Search for normalized from Full-width Upper" );
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
|
array( 'FullOneUp', 'FullTwoLow', 'HalfOneUp', 'HalfTwoLow' ),
|
|
|
|
|
|
$this->fetchIds( $this->search->searchText( 'az' ) ),
|
|
|
|
|
|
"Search for normalized from Full-width Lower" );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testTextSearch() {
|
|
|
|
|
|
$this->assertEquals(
|
2013-02-15 10:24:31 +00:00
|
|
|
|
array( 'Smithee' ),
|
|
|
|
|
|
$this->fetchIds( $this->search->searchText( 'smithee' ) ),
|
|
|
|
|
|
"Plain search failed" );
|
2010-12-14 16:26:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testTextPowerSearch() {
|
|
|
|
|
|
$this->search->setNamespaces( array( 0, 1, 4 ) );
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
|
array(
|
|
|
|
|
|
'Smithee',
|
|
|
|
|
|
'Talk:Not Main Page',
|
|
|
|
|
|
),
|
|
|
|
|
|
$this->fetchIds( $this->search->searchText( 'smithee' ) ),
|
|
|
|
|
|
"Power search failed" );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testTitleSearch() {
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
|
array(
|
|
|
|
|
|
'Alan Smithee',
|
|
|
|
|
|
'Smithee',
|
|
|
|
|
|
),
|
|
|
|
|
|
$this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
|
|
|
|
|
|
"Title search failed" );
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function testTextTitlePowerSearch() {
|
|
|
|
|
|
$this->search->setNamespaces( array( 0, 1, 4 ) );
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
|
|
array(
|
|
|
|
|
|
'Alan Smithee',
|
|
|
|
|
|
'Smithee',
|
|
|
|
|
|
'Talk:Smithee',
|
|
|
|
|
|
),
|
|
|
|
|
|
$this->fetchIds( $this->search->searchTitle( 'smithee' ) ),
|
|
|
|
|
|
"Title power search failed" );
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|