Merge "Special:Newpages feed now shows first revision instead of latest revision"
[lhc/web/wiklou.git] / includes / api / ApiQueryPagesWithProp.php
1 <?php
2 /**
3 * Created on December 31, 2012
4 *
5 * Copyright © 2012 Wikimedia Foundation and contributors
6 *
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.
11 *
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.
16 *
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
21 *
22 * @file
23 * @since 1.21
24 */
25
26 /**
27 * A query module to enumerate pages that use a particular prop
28 *
29 * @ingroup API
30 * @since 1.21
31 */
32 class ApiQueryPagesWithProp extends ApiQueryGeneratorBase {
33
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'pwp' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function getCacheMode( $params ) {
43 return 'public';
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
50 /**
51 * @param ApiPageSet $resultPageSet
52 * @return void
53 */
54 private function run( $resultPageSet = null ) {
55 $params = $this->extractRequestParams();
56
57 $prop = array_flip( $params['prop'] );
58 $fld_ids = isset( $prop['ids'] );
59 $fld_title = isset( $prop['title'] );
60 $fld_value = isset( $prop['value'] );
61
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 );
66 } else {
67 $this->addFields( $resultPageSet->getPageTableFields() );
68 }
69 $this->addTables( [ 'page_props', 'page' ] );
70 $this->addWhere( 'pp_page=page_id' );
71 $this->addWhereFld( 'pp_propname', $params['propname'] );
72
73 $dir = ( $params['dir'] == 'ascending' ) ? 'newer' : 'older';
74
75 if ( $params['continue'] ) {
76 $cont = explode( '|', $params['continue'] );
77 $this->dieContinueUsageIf( count( $cont ) != 1 );
78
79 // Add a WHERE clause
80 $from = (int)$cont[0];
81 $this->addWhereRange( 'pp_page', $dir, $from, null );
82 }
83
84 $sort = ( $params['dir'] === 'descending' ? ' DESC' : '' );
85 $this->addOption( 'ORDER BY', 'pp_page' . $sort );
86
87 $limit = $params['limit'];
88 $this->addOption( 'LIMIT', $limit + 1 );
89
90 $result = $this->getResult();
91 $count = 0;
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 );
97 break;
98 }
99
100 if ( $resultPageSet === null ) {
101 $vals = [
102 ApiResult::META_TYPE => 'assoc',
103 ];
104 if ( $fld_ids ) {
105 $vals['pageid'] = (int)$row->page_id;
106 }
107 if ( $fld_title ) {
108 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
109 ApiQueryBase::addTitleInfo( $vals, $title );
110 }
111 if ( $fld_value ) {
112 $vals['value'] = $row->pp_value;
113 }
114 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
115 if ( !$fit ) {
116 $this->setContinueEnumParameter( 'continue', $row->page_id );
117 break;
118 }
119 } else {
120 $resultPageSet->processDbRow( $row );
121 }
122 }
123
124 if ( $resultPageSet === null ) {
125 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'page' );
126 }
127 }
128
129 public function getAllowedParams() {
130 return [
131 'propname' => [
132 ApiBase::PARAM_TYPE => 'string',
133 ApiBase::PARAM_REQUIRED => true,
134 ],
135 'prop' => [
136 ApiBase::PARAM_DFLT => 'ids|title',
137 ApiBase::PARAM_ISMULTI => true,
138 ApiBase::PARAM_TYPE => [
139 'ids',
140 'title',
141 'value',
142 ],
143 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
144 ],
145 'continue' => [
146 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
147 ],
148 'limit' => [
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
154 ],
155 'dir' => [
156 ApiBase::PARAM_DFLT => 'ascending',
157 ApiBase::PARAM_TYPE => [
158 'ascending',
159 'descending',
160 ]
161 ],
162 ];
163 }
164
165 protected function getExamplesMessages() {
166 return [
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',
171 ];
172 }
173
174 public function getHelpUrls() {
175 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Pageswithprop';
176 }
177 }