MediaWiki  REL1_21
ApiQueryPagesWithProp.php
Go to the documentation of this file.
00001 <?php
00033 class ApiQueryPagesWithProp extends ApiQueryGeneratorBase {
00034 
00035         public function __construct( $query, $moduleName ) {
00036                 parent::__construct( $query, $moduleName, 'pwp' );
00037         }
00038 
00039         public function execute() {
00040                 $this->run();
00041         }
00042 
00043         public function getCacheMode( $params ) {
00044                 return 'public';
00045         }
00046 
00047         public function executeGenerator( $resultPageSet ) {
00048                 $this->run( $resultPageSet );
00049         }
00050 
00055         private function run( $resultPageSet = null ) {
00056                 $params = $this->extractRequestParams();
00057 
00058                 $prop = array_flip( $params['prop'] );
00059                 $fld_ids = isset( $prop['ids'] );
00060                 $fld_title = isset( $prop['title'] );
00061                 $fld_value = isset( $prop['value'] );
00062 
00063                 if ( $resultPageSet === null ) {
00064                         $this->addFields( array( 'page_id' ) );
00065                         $this->addFieldsIf( array( 'page_title', 'page_namespace' ), $fld_title );
00066                         $this->addFieldsIf( 'pp_value', $fld_value );
00067                 } else {
00068                         $this->addFields( $resultPageSet->getPageTableFields() );
00069                 }
00070                 $this->addTables( array( 'page_props', 'page' ) );
00071                 $this->addWhere( 'pp_page=page_id' );
00072                 $this->addWhereFld( 'pp_propname', $params['propname'] );
00073 
00074                 $dir = ( $params['dir'] == 'ascending' ) ? 'newer' : 'older';
00075 
00076                 if ( $params['continue'] ) {
00077                         $cont = explode( '|', $params['continue'] );
00078                         $this->dieContinueUsageIf( count( $cont ) != 1 );
00079 
00080                         // Add a WHERE clause
00081                         $from = (int)$cont[0];
00082                         $this->addWhereRange( 'pp_page', $dir, $from, null );
00083                 }
00084 
00085                 $sort = ( $params['dir'] === 'descending' ? ' DESC' : '' );
00086                 $this->addOption( 'ORDER BY', 'pp_page' . $sort );
00087 
00088                 $limit = $params['limit'];
00089                 $this->addOption( 'LIMIT', $limit + 1 );
00090 
00091                 $result = $this->getResult();
00092                 $count = 0;
00093                 foreach ( $this->select( __METHOD__ ) as $row ) {
00094                         if ( ++$count > $limit ) {
00095                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
00096                                 $this->setContinueEnumParameter( 'continue', $row->page_id );
00097                                 break;
00098                         }
00099 
00100                         if ( $resultPageSet === null ) {
00101                                 $vals = array();
00102                                 if ( $fld_ids ) {
00103                                         $vals['pageid'] = (int)$row->page_id;
00104                                 }
00105                                 if ( $fld_title ) {
00106                                         $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00107                                         ApiQueryBase::addTitleInfo( $vals, $title );
00108                                 }
00109                                 if ( $fld_value ) {
00110                                         $vals['value'] = $row->pp_value;
00111                                 }
00112                                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00113                                 if ( !$fit ) {
00114                                         $this->setContinueEnumParameter( 'continue', $row->page_id );
00115                                         break;
00116                                 }
00117                         } else {
00118                                 $resultPageSet->processDbRow( $row );
00119                         }
00120                 }
00121 
00122                 if ( $resultPageSet === null ) {
00123                         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
00124                 }
00125         }
00126 
00127         public function getAllowedParams() {
00128                 return array(
00129                         'propname' => array(
00130                                 ApiBase::PARAM_TYPE => 'string',
00131                                 ApiBase::PARAM_REQUIRED => true,
00132                         ),
00133                         'prop' => array(
00134                                 ApiBase::PARAM_DFLT => 'ids|title',
00135                                 ApiBase::PARAM_ISMULTI => true,
00136                                 ApiBase::PARAM_TYPE => array (
00137                                         'ids',
00138                                         'title',
00139                                         'value',
00140                                 )
00141                         ),
00142                         'continue' => null,
00143                         'limit' => array(
00144                                 ApiBase::PARAM_TYPE => 'limit',
00145                                 ApiBase::PARAM_DFLT => 10,
00146                                 ApiBase::PARAM_MIN => 1,
00147                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00148                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00149                         ),
00150                         'dir' => array(
00151                                 ApiBase::PARAM_DFLT => 'ascending',
00152                                 ApiBase::PARAM_TYPE => array(
00153                                         'ascending',
00154                                         'descending',
00155                                 )
00156                         ),
00157                 );
00158         }
00159 
00160         public function getParamDescription() {
00161                 return array(
00162                         'propname' => 'Page prop for which to enumerate pages',
00163                         'prop' => array(
00164                                 'What pieces of information to include',
00165                                 ' ids   - Adds the page ID',
00166                                 ' title - Adds the title and namespace ID of the page',
00167                                 ' value - Adds the value of the page prop',
00168                         ),
00169                         'dir' => 'In which direction to sort',
00170                         'continue' => 'When more results are available, use this to continue',
00171                         'limit' => 'The maximum number of pages to return',
00172                 );
00173         }
00174 
00175         public function getDescription() {
00176                 return 'List all pages using a given page prop';
00177         }
00178 
00179         public function getExamples() {
00180                 return array(
00181                         'api.php?action=query&list=pageswithprop&pwppropname=displaytitle&pwpprop=ids|title|value' => 'Get first 10 pages using {{DISPLAYTITLE:}}',
00182                         'api.php?action=query&generator=pageswithprop&gpwppropname=notoc&prop=info' => 'Get page info about first 10 pages using __NOTOC__',
00183                 );
00184         }
00185 
00186         public function getHelpUrls() {
00187                 return 'https://www.mediawiki.org/wiki/API:Pageswithprop';
00188         }
00189 }