MediaWiki  REL1_24
ApiQueryPrefixSearch.php
Go to the documentation of this file.
00001 <?php
00025 class ApiQueryPrefixSearch extends ApiQueryGeneratorBase {
00026     public function __construct( $query, $moduleName ) {
00027         parent::__construct( $query, $moduleName, 'ps' );
00028     }
00029 
00030     public function execute() {
00031         $this->run();
00032     }
00033 
00034     public function executeGenerator( $resultPageSet ) {
00035         $this->run( $resultPageSet );
00036     }
00037 
00041     private function run( $resultPageSet = null ) {
00042         $params = $this->extractRequestParams();
00043         $search = $params['search'];
00044         $limit = $params['limit'];
00045         $namespaces = $params['namespace'];
00046 
00047         $searcher = new TitlePrefixSearch;
00048         $titles = $searcher->searchWithVariants( $search, $limit, $namespaces );
00049         if ( $resultPageSet ) {
00050             $resultPageSet->populateFromTitles( $titles );
00051         } else {
00052             $result = $this->getResult();
00053             foreach ( $titles as $title ) {
00054                 if ( !$limit-- ) {
00055                     break;
00056                 }
00057                 $vals = array(
00058                     'ns' => intval( $title->getNamespace() ),
00059                     'title' => $title->getPrefixedText(),
00060                 );
00061                 if ( $title->isSpecialPage() ) {
00062                     $vals['special'] = '';
00063                 } else {
00064                     $vals['pageid'] = intval( $title->getArticleId() );
00065                 }
00066                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00067                 if ( !$fit ) {
00068                     break;
00069                 }
00070             }
00071             $result->setIndexedTagName_internal(
00072                 array( 'query', $this->getModuleName() ), $this->getModulePrefix()
00073             );
00074         }
00075     }
00076 
00077     public function getCacheMode( $params ) {
00078         return 'public';
00079     }
00080 
00081     public function getAllowedParams() {
00082             return array(
00083                 'search' => array(
00084                     ApiBase::PARAM_TYPE => 'string',
00085                     ApiBase::PARAM_REQUIRED => true,
00086                 ),
00087                 'namespace' => array(
00088                     ApiBase::PARAM_DFLT => NS_MAIN,
00089                     ApiBase::PARAM_TYPE => 'namespace',
00090                     ApiBase::PARAM_ISMULTI => true,
00091                 ),
00092                 'limit' => array(
00093                     ApiBase::PARAM_DFLT => 10,
00094                     ApiBase::PARAM_TYPE => 'limit',
00095                     ApiBase::PARAM_MIN => 1,
00096                     // Non-standard value for compatibility with action=opensearch
00097                     ApiBase::PARAM_MAX => 100,
00098                     ApiBase::PARAM_MAX2 => 200,
00099                 ),
00100             );
00101     }
00102 
00103     public function getParamDescription() {
00104         return array(
00105             'search' => 'Search string',
00106             'limit' => 'Maximum amount of results to return',
00107             'namespace' => 'Namespaces to search',
00108         );
00109     }
00110 
00111     public function getDescription() {
00112         return 'Perform a prefix search for page titles';
00113     }
00114 
00115     public function getExamples() {
00116         return array(
00117             'api.php?action=query&list=prefixsearch&pssearch=meaning',
00118         );
00119     }
00120 
00121     public function getHelpUrls() {
00122         return 'https://www.mediawiki.org/wiki/API:Prefixsearch';
00123     }
00124 }