]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryProtectedTitles.php
MediaWiki 1.30.2-scripts2
[autoinstalls/mediawiki.git] / includes / api / ApiQueryProtectedTitles.php
1 <?php
2 /**
3  *
4  *
5  * Created on Feb 13, 2009
6  *
7  * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  * http://www.gnu.org/copyleft/gpl.html
23  *
24  * @file
25  */
26
27 /**
28  * Query module to enumerate all create-protected pages.
29  *
30  * @ingroup API
31  */
32 class ApiQueryProtectedTitles extends ApiQueryGeneratorBase {
33
34         public function __construct( ApiQuery $query, $moduleName ) {
35                 parent::__construct( $query, $moduleName, 'pt' );
36         }
37
38         public function execute() {
39                 $this->run();
40         }
41
42         public function executeGenerator( $resultPageSet ) {
43                 $this->run( $resultPageSet );
44         }
45
46         /**
47          * @param ApiPageSet $resultPageSet
48          * @return void
49          */
50         private function run( $resultPageSet = null ) {
51                 $params = $this->extractRequestParams();
52
53                 $this->addTables( 'protected_titles' );
54                 $this->addFields( [ 'pt_namespace', 'pt_title', 'pt_timestamp' ] );
55
56                 $prop = array_flip( $params['prop'] );
57                 $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) || isset( $prop['userid'] ) );
58                 $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) );
59                 $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );
60
61                 if ( isset( $prop['comment'] ) || isset( $prop['parsedcomment'] ) ) {
62                         $commentStore = new CommentStore( 'pt_reason' );
63                         $commentQuery = $commentStore->getJoin();
64                         $this->addTables( $commentQuery['tables'] );
65                         $this->addFields( $commentQuery['fields'] );
66                         $this->addJoinConds( $commentQuery['joins'] );
67                 }
68
69                 $this->addTimestampWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] );
70                 $this->addWhereFld( 'pt_namespace', $params['namespace'] );
71                 $this->addWhereFld( 'pt_create_perm', $params['level'] );
72
73                 // Include in ORDER BY for uniqueness
74                 $this->addWhereRange( 'pt_namespace', $params['dir'], null, null );
75                 $this->addWhereRange( 'pt_title', $params['dir'], null, null );
76
77                 if ( !is_null( $params['continue'] ) ) {
78                         $cont = explode( '|', $params['continue'] );
79                         $this->dieContinueUsageIf( count( $cont ) != 3 );
80                         $op = ( $params['dir'] === 'newer' ? '>' : '<' );
81                         $db = $this->getDB();
82                         $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
83                         $continueNs = (int)$cont[1];
84                         $this->dieContinueUsageIf( $continueNs != $cont[1] );
85                         $continueTitle = $db->addQuotes( $cont[2] );
86                         $this->addWhere( "pt_timestamp $op $continueTimestamp OR " .
87                                 "(pt_timestamp = $continueTimestamp AND " .
88                                 "(pt_namespace $op $continueNs OR " .
89                                 "(pt_namespace = $continueNs AND " .
90                                 "pt_title $op= $continueTitle)))"
91                         );
92                 }
93
94                 if ( isset( $prop['user'] ) ) {
95                         $this->addTables( 'user' );
96                         $this->addFields( 'user_name' );
97                         $this->addJoinConds( [ 'user' => [ 'LEFT JOIN',
98                                 'user_id=pt_user'
99                         ] ] );
100                 }
101
102                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
103                 $res = $this->select( __METHOD__ );
104
105                 $count = 0;
106                 $result = $this->getResult();
107
108                 $titles = [];
109
110                 foreach ( $res as $row ) {
111                         if ( ++$count > $params['limit'] ) {
112                                 // We've reached the one extra which shows that there are
113                                 // additional pages to be had. Stop here...
114                                 $this->setContinueEnumParameter( 'continue',
115                                         "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
116                                 );
117                                 break;
118                         }
119
120                         $title = Title::makeTitle( $row->pt_namespace, $row->pt_title );
121                         if ( is_null( $resultPageSet ) ) {
122                                 $vals = [];
123                                 ApiQueryBase::addTitleInfo( $vals, $title );
124                                 if ( isset( $prop['timestamp'] ) ) {
125                                         $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->pt_timestamp );
126                                 }
127
128                                 if ( isset( $prop['user'] ) && !is_null( $row->user_name ) ) {
129                                         $vals['user'] = $row->user_name;
130                                 }
131
132                                 if ( isset( $prop['userid'] ) || /*B/C*/isset( $prop['user'] ) ) {
133                                         $vals['userid'] = (int)$row->pt_user;
134                                 }
135
136                                 if ( isset( $prop['comment'] ) ) {
137                                         $vals['comment'] = $commentStore->getComment( $row )->text;
138                                 }
139
140                                 if ( isset( $prop['parsedcomment'] ) ) {
141                                         $vals['parsedcomment'] = Linker::formatComment(
142                                                 $commentStore->getComment( $row )->text, $titles
143                                         );
144                                 }
145
146                                 if ( isset( $prop['expiry'] ) ) {
147                                         $vals['expiry'] = ApiResult::formatExpiry( $row->pt_expiry );
148                                 }
149
150                                 if ( isset( $prop['level'] ) ) {
151                                         $vals['level'] = $row->pt_create_perm;
152                                 }
153
154                                 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
155                                 if ( !$fit ) {
156                                         $this->setContinueEnumParameter( 'continue',
157                                                 "$row->pt_timestamp|$row->pt_namespace|$row->pt_title"
158                                         );
159                                         break;
160                                 }
161                         } else {
162                                 $titles[] = $title;
163                         }
164                 }
165
166                 if ( is_null( $resultPageSet ) ) {
167                         $result->addIndexedTagName(
168                                 [ 'query', $this->getModuleName() ],
169                                 $this->getModulePrefix()
170                         );
171                 } else {
172                         $resultPageSet->populateFromTitles( $titles );
173                 }
174         }
175
176         public function getCacheMode( $params ) {
177                 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
178                         // formatComment() calls wfMessage() among other things
179                         return 'anon-public-user-private';
180                 } else {
181                         return 'public';
182                 }
183         }
184
185         public function getAllowedParams() {
186                 return [
187                         'namespace' => [
188                                 ApiBase::PARAM_ISMULTI => true,
189                                 ApiBase::PARAM_TYPE => 'namespace',
190                         ],
191                         'level' => [
192                                 ApiBase::PARAM_ISMULTI => true,
193                                 ApiBase::PARAM_TYPE => array_diff( $this->getConfig()->get( 'RestrictionLevels' ), [ '' ] )
194                         ],
195                         'limit' => [
196                                 ApiBase::PARAM_DFLT => 10,
197                                 ApiBase::PARAM_TYPE => 'limit',
198                                 ApiBase::PARAM_MIN => 1,
199                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
200                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
201                         ],
202                         'dir' => [
203                                 ApiBase::PARAM_DFLT => 'older',
204                                 ApiBase::PARAM_TYPE => [
205                                         'newer',
206                                         'older'
207                                 ],
208                                 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
209                         ],
210                         'start' => [
211                                 ApiBase::PARAM_TYPE => 'timestamp'
212                         ],
213                         'end' => [
214                                 ApiBase::PARAM_TYPE => 'timestamp'
215                         ],
216                         'prop' => [
217                                 ApiBase::PARAM_ISMULTI => true,
218                                 ApiBase::PARAM_DFLT => 'timestamp|level',
219                                 ApiBase::PARAM_TYPE => [
220                                         'timestamp',
221                                         'user',
222                                         'userid',
223                                         'comment',
224                                         'parsedcomment',
225                                         'expiry',
226                                         'level'
227                                 ],
228                                 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
229                         ],
230                         'continue' => [
231                                 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
232                         ],
233                 ];
234         }
235
236         protected function getExamplesMessages() {
237                 return [
238                         'action=query&list=protectedtitles'
239                                 => 'apihelp-query+protectedtitles-example-simple',
240                         'action=query&generator=protectedtitles&gptnamespace=0&prop=linkshere'
241                                 => 'apihelp-query+protectedtitles-example-generator',
242                 ];
243         }
244
245         public function getHelpUrls() {
246                 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Protectedtitles';
247         }
248 }