New API module, list=prefixsearch
It's pretty much like action=opensearch but can be used as a generator which can be handy when you need to retrieve both the list of pages and some information about them. Change-Id: Iaffe30a0f7402e1316c4885a805692a34bbe1a6a
This commit is contained in:
parent
6fc8831652
commit
af6d9aba6d
6 changed files with 264 additions and 60 deletions
|
|
@ -235,6 +235,8 @@ production.
|
|||
* (bug 42026) Deprecated uctoponly in favor of ucshow=top.
|
||||
* list=search no longer has a "srredirects" parameter. Redirects are now
|
||||
included in all searches.
|
||||
* Added list=prefixsearch that works like action=opensearch but can be used as
|
||||
a generator.
|
||||
|
||||
=== Languages updated in 1.23 ===
|
||||
|
||||
|
|
|
|||
|
|
@ -198,6 +198,7 @@ $wgAutoloadLocalClasses = array(
|
|||
'StatCounter' => 'includes/StatCounter.php',
|
||||
'Status' => 'includes/Status.php',
|
||||
'StreamFile' => 'includes/StreamFile.php',
|
||||
'StringPrefixSearch' => 'includes/PrefixSearch.php',
|
||||
'StubContLang' => 'includes/StubObject.php',
|
||||
'StubObject' => 'includes/StubObject.php',
|
||||
'StubUserLang' => 'includes/StubObject.php',
|
||||
|
|
@ -207,6 +208,7 @@ $wgAutoloadLocalClasses = array(
|
|||
'Title' => 'includes/Title.php',
|
||||
'TitleArray' => 'includes/TitleArray.php',
|
||||
'TitleArrayFromResult' => 'includes/TitleArrayFromResult.php',
|
||||
'TitlePrefixSearch' => 'includes/PrefixSearch.php',
|
||||
'UnlistedSpecialPage' => 'includes/specialpage/UnlistedSpecialPage.php',
|
||||
'UploadSourceAdapter' => 'includes/Import.php',
|
||||
'UppercaseCollation' => 'includes/Collation.php',
|
||||
|
|
@ -342,6 +344,7 @@ $wgAutoloadLocalClasses = array(
|
|||
'ApiQueryPageProps' => 'includes/api/ApiQueryPageProps.php',
|
||||
'ApiQueryPagesWithProp' => 'includes/api/ApiQueryPagesWithProp.php',
|
||||
'ApiQueryPagePropNames' => 'includes/api/ApiQueryPagePropNames.php',
|
||||
'ApiQueryPrefixSearch' => 'includes/api/ApiQueryPrefixSearch.php',
|
||||
'ApiQueryProtectedTitles' => 'includes/api/ApiQueryProtectedTitles.php',
|
||||
'ApiQueryQueryPage' => 'includes/api/ApiQueryQueryPage.php',
|
||||
'ApiQueryRandom' => 'includes/api/ApiQueryRandom.php',
|
||||
|
|
|
|||
|
|
@ -26,9 +26,10 @@
|
|||
*
|
||||
* @ingroup Search
|
||||
*/
|
||||
class PrefixSearch {
|
||||
abstract class PrefixSearch {
|
||||
/**
|
||||
* Do a prefix search of titles and return a list of matching page names.
|
||||
* @deprecated: Since 1.23, use TitlePrefixSearch or StringPrefixSearch classes
|
||||
*
|
||||
* @param $search String
|
||||
* @param $limit Integer
|
||||
|
|
@ -36,11 +37,24 @@ class PrefixSearch {
|
|||
* @return Array of strings
|
||||
*/
|
||||
public static function titleSearch( $search, $limit, $namespaces = array() ) {
|
||||
$search = new StringPrefixSearch;
|
||||
return $search->search( $search, $limit, $namespaces );
|
||||
}
|
||||
|
||||
/**
|
||||
* Do a prefix search of titles and return a list of matching page names.
|
||||
*
|
||||
* @param $search String
|
||||
* @param $limit Integer
|
||||
* @param array $namespaces used if query is not explicitly prefixed
|
||||
* @return Array of strings or Title objects
|
||||
*/
|
||||
public function search( $search, $limit, $namespaces = array() ) {
|
||||
$search = trim( $search );
|
||||
if ( $search == '' ) {
|
||||
return array(); // Return empty result
|
||||
}
|
||||
$namespaces = self::validateNamespaces( $namespaces );
|
||||
$namespaces = $this->validateNamespaces( $namespaces );
|
||||
|
||||
// Find a Title which is not an interwiki and is in NS_MAIN
|
||||
$title = Title::newFromText( $search );
|
||||
|
|
@ -49,7 +63,7 @@ class PrefixSearch {
|
|||
if ( $ns[0] == NS_MAIN ) {
|
||||
$ns = $namespaces; // no explicit prefix, use default namespaces
|
||||
}
|
||||
return self::searchBackend(
|
||||
return $this->searchBackend(
|
||||
$ns, $title->getText(), $limit );
|
||||
}
|
||||
|
||||
|
|
@ -57,14 +71,68 @@ class PrefixSearch {
|
|||
$title = Title::newFromText( $search . 'Dummy' );
|
||||
if ( $title && $title->getText() == 'Dummy'
|
||||
&& $title->getNamespace() != NS_MAIN
|
||||
&& !$title->isExternal() ) {
|
||||
return self::searchBackend(
|
||||
array( $title->getNamespace() ), '', $limit );
|
||||
&& !$title->isExternal() )
|
||||
{
|
||||
$namespaces = array( $title->getNamespace() );
|
||||
$search = '';
|
||||
}
|
||||
|
||||
return self::searchBackend( $namespaces, $search, $limit );
|
||||
return $this->searchBackend( $namespaces, $search, $limit );
|
||||
}
|
||||
|
||||
/**
|
||||
* Do a prefix search for all possible variants of the prefix
|
||||
* @param $search String
|
||||
* @param $limit Integer
|
||||
* @param array $namespaces
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function searchWithVariants( $search, $limit, array $namespaces ) {
|
||||
wfProfileIn( __METHOD__ );
|
||||
$searches = $this->search( $search, $limit, $namespaces );
|
||||
|
||||
// if the content language has variants, try to retrieve fallback results
|
||||
$fallbackLimit = $limit - count( $searches );
|
||||
if ( $fallbackLimit > 0 ) {
|
||||
global $wgContLang;
|
||||
|
||||
$fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
|
||||
$fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
|
||||
|
||||
foreach ( $fallbackSearches as $fbs ) {
|
||||
$fallbackSearchResult = $this->search( $fbs, $fallbackLimit, $namespaces );
|
||||
$searches = array_merge( $searches, $fallbackSearchResult );
|
||||
$fallbackLimit -= count( $fallbackSearchResult );
|
||||
|
||||
if ( $fallbackLimit == 0 ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
wfProfileOut( __METHOD__ );
|
||||
return $searches;
|
||||
}
|
||||
|
||||
/**
|
||||
* When implemented in a descendant class, receives an array of Title objects and returns
|
||||
* either an unmodified array or an array of strings corresponding to titles passed to it.
|
||||
*
|
||||
* @param array $titles
|
||||
* @return array
|
||||
*/
|
||||
protected abstract function titles( array $titles );
|
||||
|
||||
/**
|
||||
* When implemented in a descendant class, receives an array of titles as strings and returns
|
||||
* either an unmodified array or an array of Title objects corresponding to strings received.
|
||||
*
|
||||
* @param array $strings
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected abstract function strings( array $strings );
|
||||
|
||||
/**
|
||||
* Do a prefix search of titles and return a list of matching page names.
|
||||
* @param $namespaces Array
|
||||
|
|
@ -72,20 +140,20 @@ class PrefixSearch {
|
|||
* @param $limit Integer
|
||||
* @return Array of strings
|
||||
*/
|
||||
protected static function searchBackend( $namespaces, $search, $limit ) {
|
||||
protected function searchBackend( $namespaces, $search, $limit ) {
|
||||
if ( count( $namespaces ) == 1 ) {
|
||||
$ns = $namespaces[0];
|
||||
if ( $ns == NS_MEDIA ) {
|
||||
$namespaces = array( NS_FILE );
|
||||
} elseif ( $ns == NS_SPECIAL ) {
|
||||
return self::specialSearch( $search, $limit );
|
||||
return $this->titles( $this->specialSearch( $search, $limit ) );
|
||||
}
|
||||
}
|
||||
$srchres = array();
|
||||
if ( wfRunHooks( 'PrefixSearchBackend', array( $namespaces, $search, $limit, &$srchres ) ) ) {
|
||||
return self::defaultSearchBackend( $namespaces, $search, $limit );
|
||||
return $this->titles( $this->defaultSearchBackend( $namespaces, $search, $limit ) );
|
||||
}
|
||||
return $srchres;
|
||||
return $this->strings( $srchres );
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -95,7 +163,7 @@ class PrefixSearch {
|
|||
* @param $limit Integer: max number of items to return
|
||||
* @return Array
|
||||
*/
|
||||
protected static function specialSearch( $search, $limit ) {
|
||||
protected function specialSearch( $search, $limit ) {
|
||||
global $wgContLang;
|
||||
|
||||
# normalize searchKey, so aliases with spaces can be found - bug 25675
|
||||
|
|
@ -129,7 +197,7 @@ class PrefixSearch {
|
|||
// localizes its input leading to searches for e.g. Special:All
|
||||
// returning Spezial:MediaWiki-Systemnachrichten and returning
|
||||
// Spezial:Alle_Seiten twice when $wgLanguageCode == 'de'
|
||||
$srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page )->getPrefixedText();
|
||||
$srchres[] = Title::makeTitleSafe( NS_SPECIAL, $page );
|
||||
wfRestoreWarnings();
|
||||
}
|
||||
|
||||
|
|
@ -150,38 +218,30 @@ class PrefixSearch {
|
|||
* @param array $namespaces namespaces to search in
|
||||
* @param string $search term
|
||||
* @param $limit Integer: max number of items to return
|
||||
* @return Array of title strings
|
||||
* @return Array of Title objects
|
||||
*/
|
||||
protected static function defaultSearchBackend( $namespaces, $search, $limit ) {
|
||||
protected function defaultSearchBackend( $namespaces, $search, $limit ) {
|
||||
$ns = array_shift( $namespaces ); // support only one namespace
|
||||
if ( in_array( NS_MAIN, $namespaces ) ) {
|
||||
$ns = NS_MAIN; // if searching on many always default to main
|
||||
}
|
||||
|
||||
// Prepare nested request
|
||||
$req = new FauxRequest( array(
|
||||
'action' => 'query',
|
||||
'list' => 'allpages',
|
||||
'apnamespace' => $ns,
|
||||
'aplimit' => $limit,
|
||||
'apprefix' => $search
|
||||
));
|
||||
|
||||
// Execute
|
||||
$module = new ApiMain( $req );
|
||||
$module->execute();
|
||||
|
||||
// Get resulting data
|
||||
$data = $module->getResultData();
|
||||
|
||||
// Reformat useful data for future printing by JSON engine
|
||||
$t = Title::newFromText( $search, $ns );
|
||||
$prefix = $t ? $t->getDBkey() : '';
|
||||
$dbr = wfGetDB( DB_SLAVE );
|
||||
$res = $dbr->select( 'page',
|
||||
array( 'page_id', 'page_namespace', 'page_title' ),
|
||||
array(
|
||||
'page_namespace' => $ns,
|
||||
'page_title ' . $dbr->buildLike( $prefix, $dbr->anyString() )
|
||||
),
|
||||
__METHOD__,
|
||||
array( 'LIMIT' => $limit, 'ORDER BY' => 'page_title' )
|
||||
);
|
||||
$srchres = array();
|
||||
foreach ( (array)$data['query']['allpages'] as $pageinfo ) {
|
||||
// Note: this data will no be printable by the xml engine
|
||||
// because it does not support lists of unnamed items
|
||||
$srchres[] = $pageinfo['title'];
|
||||
foreach ( $res as $row ) {
|
||||
$srchres[] = Title::newFromRow( $row );
|
||||
}
|
||||
|
||||
return $srchres;
|
||||
}
|
||||
|
||||
|
|
@ -191,7 +251,7 @@ class PrefixSearch {
|
|||
* @param $namespaces Array
|
||||
* @return Array (default: contains only NS_MAIN)
|
||||
*/
|
||||
protected static function validateNamespaces( $namespaces ) {
|
||||
protected function validateNamespaces( $namespaces ) {
|
||||
global $wgContLang;
|
||||
|
||||
// We will look at each given namespace against wgContLang namespaces
|
||||
|
|
@ -211,3 +271,37 @@ class PrefixSearch {
|
|||
return array( NS_MAIN );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs prefix search, returning Title objects
|
||||
* @ingroup Search
|
||||
*/
|
||||
class TitlePrefixSearch extends PrefixSearch {
|
||||
|
||||
protected function titles( array $titles ) {
|
||||
return $titles;
|
||||
}
|
||||
|
||||
protected function strings( array $strings ) {
|
||||
$titles = array_map( 'Title::newFromText', $strings );
|
||||
$lb = new LinkBatch( $titles );
|
||||
$lb->setCaller( __METHOD__ );
|
||||
$lb->execute();
|
||||
return $titles;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs prefix search, returning strings
|
||||
* @ingroup Search
|
||||
*/
|
||||
class StringPrefixSearch extends PrefixSearch {
|
||||
|
||||
protected function titles( array $titles ) {
|
||||
return array_map( function( Title $t ) { return $t->getPrefixedText(); }, $titles );
|
||||
}
|
||||
|
||||
protected function strings( array $strings ) {
|
||||
return $strings;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -60,28 +60,8 @@ class ApiOpenSearch extends ApiBase {
|
|||
$this->getMain()->setCacheMaxAge( $wgSearchSuggestCacheExpiry );
|
||||
$this->getMain()->setCacheMode( 'public' );
|
||||
|
||||
$searches = PrefixSearch::titleSearch( $search, $limit,
|
||||
$namespaces );
|
||||
|
||||
// if the content language has variants, try to retrieve fallback results
|
||||
$fallbackLimit = $limit - count( $searches );
|
||||
if ( $fallbackLimit > 0 ) {
|
||||
global $wgContLang;
|
||||
|
||||
$fallbackSearches = $wgContLang->autoConvertToAllVariants( $search );
|
||||
$fallbackSearches = array_diff( array_unique( $fallbackSearches ), array( $search ) );
|
||||
|
||||
foreach ( $fallbackSearches as $fbs ) {
|
||||
$fallbackSearchResult = PrefixSearch::titleSearch( $fbs, $fallbackLimit,
|
||||
$namespaces );
|
||||
$searches = array_merge( $searches, $fallbackSearchResult );
|
||||
$fallbackLimit -= count( $fallbackSearchResult );
|
||||
|
||||
if ( $fallbackLimit == 0 ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
$searcher = new StringPrefixSearch;
|
||||
$searches = $searcher->searchWithVariants( $search, $limit, $namespaces );
|
||||
}
|
||||
// Set top level elements
|
||||
$result = $this->getResult();
|
||||
|
|
|
|||
|
|
@ -86,6 +86,7 @@ class ApiQuery extends ApiBase {
|
|||
'logevents' => 'ApiQueryLogEvents',
|
||||
'pageswithprop' => 'ApiQueryPagesWithProp',
|
||||
'pagepropnames' => 'ApiQueryPagePropNames',
|
||||
'prefixsearch' => 'ApiQueryPrefixSearch',
|
||||
'protectedtitles' => 'ApiQueryProtectedTitles',
|
||||
'querypage' => 'ApiQueryQueryPage',
|
||||
'random' => 'ApiQueryRandom',
|
||||
|
|
|
|||
124
includes/api/ApiQueryPrefixSearch.php
Normal file
124
includes/api/ApiQueryPrefixSearch.php
Normal file
|
|
@ -0,0 +1,124 @@
|
|||
<?php
|
||||
/**
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 2 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
||||
* http://www.gnu.org/copyleft/gpl.html
|
||||
*
|
||||
* @file
|
||||
* @since 1.23
|
||||
*/
|
||||
|
||||
/**
|
||||
* @ingroup API
|
||||
*/
|
||||
class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
|
||||
public function __construct( $query, $moduleName ) {
|
||||
parent::__construct( $query, $moduleName, 'ps' );
|
||||
}
|
||||
|
||||
public function execute() {
|
||||
$this->run();
|
||||
}
|
||||
|
||||
public function executeGenerator( $resultPageSet ) {
|
||||
$this->run( $resultPageSet );
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $resultPageSet ApiPageSet
|
||||
*/
|
||||
private function run( $resultPageSet = null ) {
|
||||
$params = $this->extractRequestParams();
|
||||
$search = $params['search'];
|
||||
$limit = $params['limit'];
|
||||
$namespaces = $params['namespace'];
|
||||
|
||||
$searcher = new TitlePrefixSearch;
|
||||
$titles = $searcher->searchWithVariants( $search, $limit, $namespaces );
|
||||
if ( $resultPageSet ) {
|
||||
$resultPageSet->populateFromTitles( $titles );
|
||||
} else {
|
||||
$result = $this->getResult();
|
||||
foreach ( $titles as $title ) {
|
||||
if ( !$limit-- ) {
|
||||
break;
|
||||
}
|
||||
$vals = array(
|
||||
'ns' => intval( $title->getNamespace() ),
|
||||
'title' => $title->getPrefixedText(),
|
||||
);
|
||||
if ( $title->isSpecialPage() ) {
|
||||
$vals['special'] = '';
|
||||
} else {
|
||||
$vals['pageid'] = intval( $title->getArticleId() );
|
||||
}
|
||||
$fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
|
||||
if ( !$fit ) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
$result->setIndexedTagName_internal(
|
||||
array( 'query', $this->getModuleName() ), $this->getModulePrefix()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function getCacheMode( $params ) {
|
||||
return 'public';
|
||||
}
|
||||
|
||||
public function getAllowedParams() {
|
||||
return array(
|
||||
'search' => array(
|
||||
ApiBase::PARAM_TYPE => 'string',
|
||||
ApiBase::PARAM_REQUIRED => true,
|
||||
),
|
||||
'namespace' => array(
|
||||
ApiBase::PARAM_DFLT => NS_MAIN,
|
||||
ApiBase::PARAM_TYPE => 'namespace',
|
||||
ApiBase::PARAM_ISMULTI => true,
|
||||
),
|
||||
'limit' => array(
|
||||
ApiBase::PARAM_DFLT => 10,
|
||||
ApiBase::PARAM_TYPE => 'limit',
|
||||
ApiBase::PARAM_MIN => 1,
|
||||
ApiBase::PARAM_MAX => 100, // Non-standard value for compatibility
|
||||
// with action=opensearch
|
||||
ApiBase::PARAM_MAX2 => 200,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function getParamDescription() {
|
||||
return array(
|
||||
'search' => 'Search string',
|
||||
'limit' => 'Maximum amount of results to return',
|
||||
'namespace' => 'Namespaces to search',
|
||||
);
|
||||
}
|
||||
|
||||
public function getDescription() {
|
||||
return 'Perform a prefix search for page titles';
|
||||
}
|
||||
|
||||
public function getExamples() {
|
||||
return array(
|
||||
'api.php?action=query&list=prefixsearch&pssearch=meaning',
|
||||
);
|
||||
}
|
||||
|
||||
public function getHelpUrls() {
|
||||
return 'https://www.mediawiki.org/wiki/API:Prefixsearch';
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue