3 * Created on December 31, 2012
5 * Copyright © 2012 Wikimedia Foundation and contributors
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * http://www.gnu.org/copyleft/gpl.html
27 * A query module to enumerate pages that use a particular prop
32 class ApiQueryPagesWithProp
extends ApiQueryGeneratorBase
{
34 public function __construct( ApiQuery
$query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'pwp' );
38 public function execute() {
42 public function getCacheMode( $params ) {
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
51 * @param ApiPageSet $resultPageSet
54 private function run( $resultPageSet = null ) {
55 $params = $this->extractRequestParams();
57 $prop = array_flip( $params['prop'] );
58 $fld_ids = isset( $prop['ids'] );
59 $fld_title = isset( $prop['title'] );
60 $fld_value = isset( $prop['value'] );
62 if ( $resultPageSet === null ) {
63 $this->addFields( [ 'page_id' ] );
64 $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
65 $this->addFieldsIf( 'pp_value', $fld_value );
67 $this->addFields( $resultPageSet->getPageTableFields() );
69 $this->addTables( [ 'page_props', 'page' ] );
70 $this->addWhere( 'pp_page=page_id' );
71 $this->addWhereFld( 'pp_propname', $params['propname'] );
73 $dir = ( $params['dir'] == 'ascending' ) ?
'newer' : 'older';
75 if ( $params['continue'] ) {
76 $cont = explode( '|', $params['continue'] );
77 $this->dieContinueUsageIf( count( $cont ) != 1 );
80 $from = (int)$cont[0];
81 $this->addWhereRange( 'pp_page', $dir, $from, null );
84 $sort = ( $params['dir'] === 'descending' ?
' DESC' : '' );
85 $this->addOption( 'ORDER BY', 'pp_page' . $sort );
87 $limit = $params['limit'];
88 $this->addOption( 'LIMIT', $limit +
1 );
90 $result = $this->getResult();
92 foreach ( $this->select( __METHOD__
) as $row ) {
93 if ( ++
$count > $limit ) {
94 // We've reached the one extra which shows that there are
95 // additional pages to be had. Stop here...
96 $this->setContinueEnumParameter( 'continue', $row->page_id
);
100 if ( $resultPageSet === null ) {
102 ApiResult
::META_TYPE
=> 'assoc',
105 $vals['pageid'] = (int)$row->page_id
;
108 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
109 ApiQueryBase
::addTitleInfo( $vals, $title );
112 $vals['value'] = $row->pp_value
;
114 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
116 $this->setContinueEnumParameter( 'continue', $row->page_id
);
120 $resultPageSet->processDbRow( $row );
124 if ( $resultPageSet === null ) {
125 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
129 public function getAllowedParams() {
132 ApiBase
::PARAM_TYPE
=> 'string',
133 ApiBase
::PARAM_REQUIRED
=> true,
136 ApiBase
::PARAM_DFLT
=> 'ids|title',
137 ApiBase
::PARAM_ISMULTI
=> true,
138 ApiBase
::PARAM_TYPE
=> [
143 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
146 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
149 ApiBase
::PARAM_TYPE
=> 'limit',
150 ApiBase
::PARAM_DFLT
=> 10,
151 ApiBase
::PARAM_MIN
=> 1,
152 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
153 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
156 ApiBase
::PARAM_DFLT
=> 'ascending',
157 ApiBase
::PARAM_TYPE
=> [
165 protected function getExamplesMessages() {
167 'action=query&list=pageswithprop&pwppropname=displaytitle&pwpprop=ids|title|value'
168 => 'apihelp-query+pageswithprop-example-simple',
169 'action=query&generator=pageswithprop&gpwppropname=notoc&prop=info'
170 => 'apihelp-query+pageswithprop-example-generator',
174 public function getHelpUrls() {
175 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageswithprop';