5 * Created on Feb 13, 2009
7 * Copyright © 2009 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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.
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.
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
28 * Query module to enumerate all create-protected pages.
32 class ApiQueryProtectedTitles
extends ApiQueryGeneratorBase
{
34 public function __construct( $query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'pt' );
38 public function execute() {
42 public function executeGenerator( $resultPageSet ) {
43 $this->run( $resultPageSet );
47 * @param $resultPageSet ApiPageSet
50 private function run( $resultPageSet = null ) {
51 $params = $this->extractRequestParams();
53 $this->addTables( 'protected_titles' );
54 $this->addFields( array( 'pt_namespace', 'pt_title', 'pt_timestamp' ) );
56 $prop = array_flip( $params['prop'] );
57 $this->addFieldsIf( 'pt_user', isset( $prop['user'] ) ||
isset( $prop['userid'] ) );
58 $this->addFieldsIf( 'pt_reason', isset( $prop['comment'] ) ||
isset( $prop['parsedcomment'] ) );
59 $this->addFieldsIf( 'pt_expiry', isset( $prop['expiry'] ) );
60 $this->addFieldsIf( 'pt_create_perm', isset( $prop['level'] ) );
62 $this->addTimestampWhereRange( 'pt_timestamp', $params['dir'], $params['start'], $params['end'] );
63 $this->addWhereFld( 'pt_namespace', $params['namespace'] );
64 $this->addWhereFld( 'pt_create_perm', $params['level'] );
66 if ( isset( $prop['user'] ) ) {
67 $this->addTables( 'user' );
68 $this->addFields( 'user_name' );
69 $this->addJoinConds( array( 'user' => array( 'LEFT JOIN',
74 $this->addOption( 'LIMIT', $params['limit'] +
1 );
75 $res = $this->select( __METHOD__
);
78 $result = $this->getResult();
82 foreach ( $res as $row ) {
83 if ( ++
$count > $params['limit'] ) {
84 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
85 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->pt_timestamp
) );
89 $title = Title
::makeTitle( $row->pt_namespace
, $row->pt_title
);
90 if ( is_null( $resultPageSet ) ) {
92 ApiQueryBase
::addTitleInfo( $vals, $title );
93 if ( isset( $prop['timestamp'] ) ) {
94 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->pt_timestamp
);
97 if ( isset( $prop['user'] ) && !is_null( $row->user_name
) ) {
98 $vals['user'] = $row->user_name
;
101 if ( isset( $prop['userid'] ) ||
/*B/C*/isset( $prop['user'] ) ) {
102 $vals['userid'] = $row->pt_user
;
105 if ( isset( $prop['comment'] ) ) {
106 $vals['comment'] = $row->pt_reason
;
109 if ( isset( $prop['parsedcomment'] ) ) {
110 $vals['parsedcomment'] = Linker
::formatComment( $row->pt_reason
, $title );
113 if ( isset( $prop['expiry'] ) ) {
115 $vals['expiry'] = $wgContLang->formatExpiry( $row->pt_expiry
, TS_ISO_8601
);
118 if ( isset( $prop['level'] ) ) {
119 $vals['level'] = $row->pt_create_perm
;
122 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
124 $this->setContinueEnumParameter( 'start',
125 wfTimestamp( TS_ISO_8601
, $row->pt_timestamp
) );
133 if ( is_null( $resultPageSet ) ) {
134 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), $this->getModulePrefix() );
136 $resultPageSet->populateFromTitles( $titles );
140 public function getCacheMode( $params ) {
141 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
142 // formatComment() calls wfMessage() among other things
143 return 'anon-public-user-private';
149 public function getAllowedParams() {
150 global $wgRestrictionLevels;
152 'namespace' => array(
153 ApiBase
::PARAM_ISMULTI
=> true,
154 ApiBase
::PARAM_TYPE
=> 'namespace',
157 ApiBase
::PARAM_ISMULTI
=> true,
158 ApiBase
::PARAM_TYPE
=> array_diff( $wgRestrictionLevels, array( '' ) )
161 ApiBase
::PARAM_DFLT
=> 10,
162 ApiBase
::PARAM_TYPE
=> 'limit',
163 ApiBase
::PARAM_MIN
=> 1,
164 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
165 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
168 ApiBase
::PARAM_DFLT
=> 'older',
169 ApiBase
::PARAM_TYPE
=> array(
175 ApiBase
::PARAM_TYPE
=> 'timestamp'
178 ApiBase
::PARAM_TYPE
=> 'timestamp'
181 ApiBase
::PARAM_ISMULTI
=> true,
182 ApiBase
::PARAM_DFLT
=> 'timestamp|level',
183 ApiBase
::PARAM_TYPE
=> array(
196 public function getParamDescription() {
198 'namespace' => 'Only list titles in these namespaces',
199 'start' => 'Start listing at this protection timestamp',
200 'end' => 'Stop listing at this protection timestamp',
201 'dir' => $this->getDirectionDescription( $this->getModulePrefix() ),
202 'limit' => 'How many total pages to return',
204 'Which properties to get',
205 ' timestamp - Adds the timestamp of when protection was added',
206 ' user - Adds the user that added the protection',
207 ' userid - Adds the user id that added the protection',
208 ' comment - Adds the comment for the protection',
209 ' parsedcomment - Adds the parsed comment for the protection',
210 ' expiry - Adds the timestamp of when the protection will be lifted',
211 ' level - Adds the protection level',
213 'level' => 'Only list titles with these protection levels',
217 public function getResultProperties() {
218 global $wgRestrictionLevels;
224 'timestamp' => array(
225 'timestamp' => 'timestamp'
229 ApiBase
::PROP_TYPE
=> 'string',
230 ApiBase
::PROP_NULLABLE
=> true
232 'userid' => 'integer'
235 'userid' => 'integer'
238 'comment' => 'string'
240 'parsedcomment' => array(
241 'parsedcomment' => 'string'
244 'expiry' => 'timestamp'
248 ApiBase
::PROP_TYPE
=> array_diff( $wgRestrictionLevels, array( '' ) )
254 public function getDescription() {
255 return 'List all titles protected from creation';
258 public function getExamples() {
260 'api.php?action=query&list=protectedtitles',
264 public function getHelpUrls() {
265 return 'https://www.mediawiki.org/wiki/API:Protectedtitles';