]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQueryWatchlist.php
MediaWiki 1.17.4
[autoinstalls/mediawiki.git] / includes / api / ApiQueryWatchlist.php
1 <?php
2 /**
3  * API for MediaWiki 1.8+
4  *
5  * Created on Sep 25, 2006
6  *
7  * Copyright © 2006 Yuri Astrakhan <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 if ( !defined( 'MEDIAWIKI' ) ) {
28         // Eclipse helper - will be ignored in production
29         require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33  * This query action allows clients to retrieve a list of recently modified pages
34  * that are part of the logged-in user's watchlist.
35  *
36  * @ingroup API
37  */
38 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
39
40         public function __construct( $query, $moduleName ) {
41                 parent::__construct( $query, $moduleName, 'wl' );
42         }
43
44         public function execute() {
45                 $this->run();
46         }
47
48         public function executeGenerator( $resultPageSet ) {
49                 $this->run( $resultPageSet );
50         }
51
52         private $fld_ids = false, $fld_title = false, $fld_patrol = false, $fld_flags = false,
53                         $fld_timestamp = false, $fld_user = false, $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
54                         $fld_notificationtimestamp = false, $fld_userid = false;
55
56         private function run( $resultPageSet = null ) {
57                 $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
58
59                 $params = $this->extractRequestParams();
60
61                 $user = $this->getWatchlistUser( $params );
62
63                 if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
64                         $prop = array_flip( $params['prop'] );
65
66                         $this->fld_ids = isset( $prop['ids'] );
67                         $this->fld_title = isset( $prop['title'] );
68                         $this->fld_flags = isset( $prop['flags'] );
69                         $this->fld_user = isset( $prop['user'] );
70                         $this->fld_userid = isset( $prop['userid'] );
71                         $this->fld_comment = isset( $prop['comment'] );
72                         $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
73                         $this->fld_timestamp = isset( $prop['timestamp'] );
74                         $this->fld_sizes = isset( $prop['sizes'] );
75                         $this->fld_patrol = isset( $prop['patrol'] );
76                         $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
77
78                         if ( $this->fld_patrol ) {
79                                 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
80                                         $this->dieUsage( 'patrol property is not available', 'patrol' );
81                                 }
82                         }
83                 }
84
85                 $this->addFields( array(
86                         'rc_namespace',
87                         'rc_title',
88                         'rc_timestamp'
89                 ) );
90
91                 if ( is_null( $resultPageSet ) ) {
92                         $this->addFields( array(
93                                 'rc_cur_id',
94                                 'rc_this_oldid'
95                         ) );
96
97                         $this->addFieldsIf( 'rc_new', $this->fld_flags );
98                         $this->addFieldsIf( 'rc_minor', $this->fld_flags );
99                         $this->addFieldsIf( 'rc_bot', $this->fld_flags );
100                         $this->addFieldsIf( 'rc_user', $this->fld_user || $this->fld_userid );
101                         $this->addFieldsIf( 'rc_user_text', $this->fld_user );
102                         $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
103                         $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol );
104                         $this->addFieldsIf( 'rc_old_len', $this->fld_sizes );
105                         $this->addFieldsIf( 'rc_new_len', $this->fld_sizes );
106                         $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
107                 } elseif ( $params['allrev'] ) {
108                         $this->addFields( 'rc_this_oldid' );
109                 } else {
110                         $this->addFields( 'rc_cur_id' );
111                 }
112
113                 $this->addTables( array(
114                         'watchlist',
115                         'page',
116                         'recentchanges'
117                 ) );
118
119                 $userId = $user->getId();
120                 $this->addWhere( array(
121                         'wl_namespace = rc_namespace',
122                         'wl_title = rc_title',
123                         'rc_cur_id = page_id',
124                         'wl_user' => $userId,
125                         'rc_deleted' => 0,
126                 ) );
127                 
128                 $db = $this->getDB();
129
130                 $this->addWhereRange( 'rc_timestamp', $params['dir'], 
131                         $db->timestamp( $params['start'] ), 
132                         $db->timestamp( $params['end'] ) );
133                 $this->addWhereFld( 'wl_namespace', $params['namespace'] );
134                 $this->addWhereIf( 'rc_this_oldid=page_latest', !$params['allrev'] );
135
136                 if ( !is_null( $params['show'] ) ) {
137                         $show = array_flip( $params['show'] );
138
139                         /* Check for conflicting parameters. */
140                         if ( ( isset ( $show['minor'] ) && isset ( $show['!minor'] ) )
141                                         || ( isset ( $show['bot'] ) && isset ( $show['!bot'] ) )
142                                         || ( isset ( $show['anon'] ) && isset ( $show['!anon'] ) )
143                                         || ( isset ( $show['patrolled'] ) && isset ( $show['!patrolled'] ) )
144                         )
145                         {
146                                 $this->dieUsageMsg( array( 'show' ) );
147                         }
148
149                         // Check permissions.
150                         if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
151                                 global $wgUser;
152                                 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
153                                         $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
154                                 }
155                         }
156
157                         /* Add additional conditions to query depending upon parameters. */
158                         $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
159                         $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
160                         $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
161                         $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
162                         $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
163                         $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
164                         $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
165                         $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
166                 }
167
168                 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
169                         $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
170                 }
171                 if ( !is_null( $params['user'] ) ) {
172                         $this->addWhereFld( 'rc_user_text', $params['user'] );
173                 }
174                 if ( !is_null( $params['excludeuser'] ) ) {
175                         $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
176                 }
177
178                 
179
180                 // This is an index optimization for mysql, as done in the Special:Watchlist page
181                 $this->addWhereIf( "rc_timestamp > ''", !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql' );
182
183                 $this->addOption( 'LIMIT', $params['limit'] + 1 );
184
185                 $ids = array();
186                 $count = 0;
187                 $res = $this->select( __METHOD__ );
188
189                 foreach ( $res as $row ) {
190                         if ( ++ $count > $params['limit'] ) {
191                                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
192                                 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
193                                 break;
194                         }
195
196                         if ( is_null( $resultPageSet ) ) {
197                                 $vals = $this->extractRowInfo( $row );
198                                 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
199                                 if ( !$fit ) {
200                                         $this->setContinueEnumParameter( 'start',
201                                                         wfTimestamp( TS_ISO_8601, $row->rc_timestamp ) );
202                                         break;
203                                 }
204                         } else {
205                                 if ( $params['allrev'] ) {
206                                         $ids[] = intval( $row->rc_this_oldid );
207                                 } else {
208                                         $ids[] = intval( $row->rc_cur_id );
209                                 }
210                         }
211                 }
212
213                 if ( is_null( $resultPageSet ) ) {
214                         $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
215                 } elseif ( $params['allrev'] ) {
216                         $resultPageSet->populateFromRevisionIDs( $ids );
217                 } else {
218                         $resultPageSet->populateFromPageIDs( $ids );
219                 }
220         }
221
222         private function extractRowInfo( $row ) {
223                 $vals = array();
224
225                 if ( $this->fld_ids ) {
226                         $vals['pageid'] = intval( $row->rc_cur_id );
227                         $vals['revid'] = intval( $row->rc_this_oldid );
228                 }
229
230                 $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
231
232                 if ( $this->fld_title ) {
233                         ApiQueryBase::addTitleInfo( $vals, $title );
234                 }
235
236                 if ( $this->fld_user || $this->fld_userid ) {
237
238                         if ( $this->fld_user ) {
239                                 $vals['user'] = $row->rc_user_text;
240                         }
241
242                         if ( $this->fld_userid ) {
243                                 $vals['user'] = $row->rc_user;  
244                         }
245
246                         if ( !$row->rc_user ) {
247                                 $vals['anon'] = '';
248                         }
249                 }
250
251                 if ( $this->fld_flags ) {
252                         if ( $row->rc_new ) {
253                                 $vals['new'] = '';
254                         }
255                         if ( $row->rc_minor ) {
256                                 $vals['minor'] = '';
257                         }
258                         if ( $row->rc_bot ) {
259                                 $vals['bot'] = '';
260                         }
261                 }
262
263                 if ( $this->fld_patrol && isset( $row->rc_patrolled ) ) {
264                         $vals['patrolled'] = '';
265                 }
266
267                 if ( $this->fld_timestamp ) {
268                         $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
269                 }
270
271                 if ( $this->fld_sizes ) {
272                         $vals['oldlen'] = intval( $row->rc_old_len );
273                         $vals['newlen'] = intval( $row->rc_new_len );
274                 }
275
276                 if ( $this->fld_notificationtimestamp ) {
277                         $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp == null )
278                                 ? ''
279                                 : wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
280                 }
281
282                 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
283                         $vals['comment'] = $row->rc_comment;
284                 }
285
286                 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
287                         global $wgUser;
288                         $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rc_comment, $title );
289                 }
290
291                 return $vals;
292         }
293
294         public function getAllowedParams() {
295                 return array(
296                         'allrev' => false,
297                         'start' => array(
298                                 ApiBase::PARAM_TYPE => 'timestamp'
299                         ),
300                         'end' => array(
301                                 ApiBase::PARAM_TYPE => 'timestamp'
302                         ),
303                         'namespace' => array (
304                                 ApiBase::PARAM_ISMULTI => true,
305                                 ApiBase::PARAM_TYPE => 'namespace'
306                         ),
307                         'user' => array(
308                                 ApiBase::PARAM_TYPE => 'user',
309                         ),
310                         'excludeuser' => array(
311                                 ApiBase::PARAM_TYPE => 'user',
312                         ),
313                         'dir' => array(
314                                 ApiBase::PARAM_DFLT => 'older',
315                                 ApiBase::PARAM_TYPE => array(
316                                         'newer',
317                                         'older'
318                                 )
319                         ),
320                         'limit' => array(
321                                 ApiBase::PARAM_DFLT => 10,
322                                 ApiBase::PARAM_TYPE => 'limit',
323                                 ApiBase::PARAM_MIN => 1,
324                                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
325                                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
326                         ),
327                         'prop' => array(
328                                 ApiBase::PARAM_ISMULTI => true,
329                                 ApiBase::PARAM_DFLT => 'ids|title|flags',
330                                 ApiBase::PARAM_TYPE => array(
331                                         'ids',
332                                         'title',
333                                         'flags',
334                                         'user',
335                                         'userid',
336                                         'comment',
337                                         'parsedcomment',
338                                         'timestamp',
339                                         'patrol',
340                                         'sizes',
341                                         'notificationtimestamp'
342                                 )
343                         ),
344                         'show' => array(
345                                 ApiBase::PARAM_ISMULTI => true,
346                                 ApiBase::PARAM_TYPE => array(
347                                         'minor',
348                                         '!minor',
349                                         'bot',
350                                         '!bot',
351                                         'anon',
352                                         '!anon',
353                                         'patrolled',
354                                         '!patrolled',
355                                 )
356                         ),
357                         'owner' => array(
358                                 ApiBase::PARAM_TYPE => 'user'
359                         ),
360                         'token' => array(
361                                 ApiBase::PARAM_TYPE => 'string'
362                         )
363                 );
364         }
365
366         public function getParamDescription() {
367                 return array(
368                         'allrev' => 'Include multiple revisions of the same page within given timeframe',
369                         'start' => 'The timestamp to start enumerating from',
370                         'end' => 'The timestamp to end enumerating',
371                         'namespace' => 'Filter changes to only the given namespace(s)',
372                         'user' => 'Only list changes by this user',
373                         'excludeuser' => 'Don\'t list changes by this user',
374                         'dir' => 'In which direction to enumerate pages',
375                         'limit' => 'How many total results to return per request',
376                         'prop' => array(
377                                 'Which additional items to get (non-generator mode only).',
378                                 ' ids                    - Adds revision ids and page ids',
379                                 ' title                  - Adds title of the page',
380                                 ' flags                  - Adds flags for the edit',
381                                 ' user                   - Adds the user who made the edit',
382                                 ' userid                 - Adds user id of whom made the edit',
383                                 ' comment                - Adds comment of the edit',
384                                 ' parsedcomment          - Adds parsed comment of the edit',
385                                 ' timestamp              - Adds timestamp of the edit',
386                                 ' patrol                 - Tags edits that are patrolled',
387                                 ' size                   - Adds the old and new lengths of the page',
388                                 ' notificationtimestamp  - Adds timestamp of when the user was last notified about the edit',
389                         ),
390                         'show' => array(
391                                 'Show only items that meet this criteria.',
392                                 "For example, to see only minor edits done by logged-in users, set {$this->getModulePrefix()}show=minor|!anon"
393                         ),
394                         'owner' => 'The name of the user whose watchlist you\'d like to access',
395                         'token' => 'Give a security token (settable in preferences) to allow access to another user\'s watchlist'
396                 );
397         }
398
399         public function getDescription() {
400                 return "Get all recent changes to pages in the logged in user's watchlist";
401         }
402
403         public function getPossibleErrors() {
404                 return array_merge( parent::getPossibleErrors(), array(
405                         array( 'code' => 'bad_wlowner', 'info' => 'Specified user does not exist' ),
406                         array( 'code' => 'bad_wltoken', 'info' => 'Incorrect watchlist token provided -- please set a correct token in Special:Preferences' ),
407                         array( 'code' => 'notloggedin', 'info' => 'You must be logged-in to have a watchlist' ),
408                         array( 'code' => 'patrol', 'info' => 'patrol property is not available' ),
409                         array( 'show' ),
410                         array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
411                         array( 'code' => 'user-excludeuser', 'info' => 'user and excludeuser cannot be used together' ),
412                 ) );
413         }
414
415         protected function getExamples() {
416                 return array(
417                         'api.php?action=query&list=watchlist',
418                         'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
419                         'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
420                         'api.php?action=query&generator=watchlist&prop=info',
421                         'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
422                         'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=d8d562e9725ea1512894cdab28e5ceebc7f20237'
423                 );
424         }
425
426         public function getVersion() {
427                 return __CLASS__ . ': $Id$';
428         }
429 }