MediaWiki
REL1_24
|
00001 <?php 00033 class ApiQueryPagesWithProp extends ApiQueryGeneratorBase { 00034 00035 public function __construct( ApiQuery $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 00096 // additional pages to be had. Stop here... 00097 $this->setContinueEnumParameter( 'continue', $row->page_id ); 00098 break; 00099 } 00100 00101 if ( $resultPageSet === null ) { 00102 $vals = array(); 00103 if ( $fld_ids ) { 00104 $vals['pageid'] = (int)$row->page_id; 00105 } 00106 if ( $fld_title ) { 00107 $title = Title::makeTitle( $row->page_namespace, $row->page_title ); 00108 ApiQueryBase::addTitleInfo( $vals, $title ); 00109 } 00110 if ( $fld_value ) { 00111 $vals['value'] = $row->pp_value; 00112 } 00113 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals ); 00114 if ( !$fit ) { 00115 $this->setContinueEnumParameter( 'continue', $row->page_id ); 00116 break; 00117 } 00118 } else { 00119 $resultPageSet->processDbRow( $row ); 00120 } 00121 } 00122 00123 if ( $resultPageSet === null ) { 00124 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' ); 00125 } 00126 } 00127 00128 public function getAllowedParams() { 00129 return array( 00130 'propname' => array( 00131 ApiBase::PARAM_TYPE => 'string', 00132 ApiBase::PARAM_REQUIRED => true, 00133 ), 00134 'prop' => array( 00135 ApiBase::PARAM_DFLT => 'ids|title', 00136 ApiBase::PARAM_ISMULTI => true, 00137 ApiBase::PARAM_TYPE => array( 00138 'ids', 00139 'title', 00140 'value', 00141 ) 00142 ), 00143 'continue' => null, 00144 'limit' => array( 00145 ApiBase::PARAM_TYPE => 'limit', 00146 ApiBase::PARAM_DFLT => 10, 00147 ApiBase::PARAM_MIN => 1, 00148 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1, 00149 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 00150 ), 00151 'dir' => array( 00152 ApiBase::PARAM_DFLT => 'ascending', 00153 ApiBase::PARAM_TYPE => array( 00154 'ascending', 00155 'descending', 00156 ) 00157 ), 00158 ); 00159 } 00160 00161 public function getParamDescription() { 00162 return array( 00163 'propname' => 'Page prop for which to enumerate pages', 00164 'prop' => array( 00165 'What pieces of information to include', 00166 ' ids - Adds the page ID', 00167 ' title - Adds the title and namespace ID of the page', 00168 ' value - Adds the value of the page prop', 00169 ), 00170 'dir' => 'In which direction to sort', 00171 'continue' => 'When more results are available, use this to continue', 00172 'limit' => 'The maximum number of pages to return', 00173 ); 00174 } 00175 00176 public function getDescription() { 00177 return 'List all pages using a given page prop.'; 00178 } 00179 00180 public function getExamples() { 00181 return array( 00182 'api.php?action=query&list=pageswithprop&pwppropname=displaytitle&pwpprop=ids|title|value' 00183 => 'Get first 10 pages using {{DISPLAYTITLE:}}', 00184 'api.php?action=query&generator=pageswithprop&gpwppropname=notoc&prop=info' 00185 => 'Get page info about first 10 pages using __NOTOC__', 00186 ); 00187 } 00188 00189 public function getHelpUrls() { 00190 return 'https://www.mediawiki.org/wiki/API:Pageswithprop'; 00191 } 00192 }