3 * Created on December 31, 2012
5 * Copyright © 2012 Brad Jorsch <bjorsch@wikimedia.org>
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
28 * A query module to enumerate pages that use a particular prop
33 class ApiQueryPagesWithProp
extends ApiQueryGeneratorBase
{
35 public function __construct( ApiQuery
$query, $moduleName ) {
36 parent
::__construct( $query, $moduleName, 'pwp' );
39 public function execute() {
43 public function getCacheMode( $params ) {
47 public function executeGenerator( $resultPageSet ) {
48 $this->run( $resultPageSet );
52 * @param ApiPageSet $resultPageSet
55 private function run( $resultPageSet = null ) {
56 $params = $this->extractRequestParams();
58 $prop = array_flip( $params['prop'] );
59 $fld_ids = isset( $prop['ids'] );
60 $fld_title = isset( $prop['title'] );
61 $fld_value = isset( $prop['value'] );
63 if ( $resultPageSet === null ) {
64 $this->addFields( [ 'page_id' ] );
65 $this->addFieldsIf( [ 'page_title', 'page_namespace' ], $fld_title );
66 $this->addFieldsIf( 'pp_value', $fld_value );
68 $this->addFields( $resultPageSet->getPageTableFields() );
70 $this->addTables( [ 'page_props', 'page' ] );
71 $this->addWhere( 'pp_page=page_id' );
72 $this->addWhereFld( 'pp_propname', $params['propname'] );
74 $dir = ( $params['dir'] == 'ascending' ) ?
'newer' : 'older';
76 if ( $params['continue'] ) {
77 $cont = explode( '|', $params['continue'] );
78 $this->dieContinueUsageIf( count( $cont ) != 1 );
81 $from = (int)$cont[0];
82 $this->addWhereRange( 'pp_page', $dir, $from, null );
85 $sort = ( $params['dir'] === 'descending' ?
' DESC' : '' );
86 $this->addOption( 'ORDER BY', 'pp_page' . $sort );
88 $limit = $params['limit'];
89 $this->addOption( 'LIMIT', $limit +
1 );
91 $result = $this->getResult();
93 foreach ( $this->select( __METHOD__
) as $row ) {
94 if ( ++
$count > $limit ) {
95 // We've reached the one extra which shows that there are
96 // additional pages to be had. Stop here...
97 $this->setContinueEnumParameter( 'continue', $row->page_id
);
101 if ( $resultPageSet === null ) {
103 ApiResult
::META_TYPE
=> 'assoc',
106 $vals['pageid'] = (int)$row->page_id
;
109 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
110 ApiQueryBase
::addTitleInfo( $vals, $title );
113 $vals['value'] = $row->pp_value
;
115 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
117 $this->setContinueEnumParameter( 'continue', $row->page_id
);
121 $resultPageSet->processDbRow( $row );
125 if ( $resultPageSet === null ) {
126 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
130 public function getAllowedParams() {
133 ApiBase
::PARAM_TYPE
=> 'string',
134 ApiBase
::PARAM_REQUIRED
=> true,
137 ApiBase
::PARAM_DFLT
=> 'ids|title',
138 ApiBase
::PARAM_ISMULTI
=> true,
139 ApiBase
::PARAM_TYPE
=> [
144 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
147 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
150 ApiBase
::PARAM_TYPE
=> 'limit',
151 ApiBase
::PARAM_DFLT
=> 10,
152 ApiBase
::PARAM_MIN
=> 1,
153 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
154 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
157 ApiBase
::PARAM_DFLT
=> 'ascending',
158 ApiBase
::PARAM_TYPE
=> [
166 protected function getExamplesMessages() {
168 'action=query&list=pageswithprop&pwppropname=displaytitle&pwpprop=ids|title|value'
169 => 'apihelp-query+pageswithprop-example-simple',
170 'action=query&generator=pageswithprop&gpwppropname=notoc&prop=info'
171 => 'apihelp-query+pageswithprop-example-generator',
175 public function getHelpUrls() {
176 return 'https://www.mediawiki.org/wiki/API:Pageswithprop';