5 * Created on Oct 19, 2006
7 * Copyright © 2006 Yuri Astrakhan "<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 * A query action to enumerate the recent changes that were done to the wiki.
29 * Various filters are supported.
33 class ApiQueryRecentChanges
extends ApiQueryGeneratorBase
{
35 public function __construct( ApiQuery
$query, $moduleName ) {
36 parent
::__construct( $query, $moduleName, 'rc' );
39 private $fld_comment = false, $fld_parsedcomment = false, $fld_user = false, $fld_userid = false,
40 $fld_flags = false, $fld_timestamp = false, $fld_title = false, $fld_ids = false,
41 $fld_sizes = false, $fld_redirect = false, $fld_patrolled = false, $fld_loginfo = false,
42 $fld_tags = false, $fld_sha1 = false, $token = array();
44 private $tokenFunctions;
47 * Get an array mapping token names to their handler functions.
48 * The prototype for a token function is func($pageid, $title, $rc)
49 * it should return a token or false (permission denied)
50 * @deprecated since 1.24
51 * @return array Array(tokenname => function)
53 protected function getTokenFunctions() {
54 // Don't call the hooks twice
55 if ( isset( $this->tokenFunctions
) ) {
56 return $this->tokenFunctions
;
59 // If we're in a mode that breaks the same-origin policy, no tokens can
61 if ( $this->lacksSameOriginSecurity() ) {
65 $this->tokenFunctions
= array(
66 'patrol' => array( 'ApiQueryRecentChanges', 'getPatrolToken' )
68 Hooks
::run( 'APIQueryRecentChangesTokens', array( &$this->tokenFunctions
) );
70 return $this->tokenFunctions
;
74 * @deprecated since 1.24
77 * @param RecentChange|null $rc
80 public static function getPatrolToken( $pageid, $title, $rc = null ) {
83 $validTokenUser = false;
86 if ( ( $wgUser->useRCPatrol() && $rc->getAttribute( 'rc_type' ) == RC_EDIT
) ||
87 ( $wgUser->useNPPatrol() && $rc->getAttribute( 'rc_type' ) == RC_NEW
)
89 $validTokenUser = true;
91 } elseif ( $wgUser->useRCPatrol() ||
$wgUser->useNPPatrol() ) {
92 $validTokenUser = true;
95 if ( $validTokenUser ) {
96 // The patrol token is always the same, let's exploit that
97 static $cachedPatrolToken = null;
99 if ( is_null( $cachedPatrolToken ) ) {
100 $cachedPatrolToken = $wgUser->getEditToken( 'patrol' );
103 return $cachedPatrolToken;
110 * Sets internal state to include the desired properties in the output.
111 * @param array $prop Associative array of properties, only keys are used here
113 public function initProperties( $prop ) {
114 $this->fld_comment
= isset( $prop['comment'] );
115 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
116 $this->fld_user
= isset( $prop['user'] );
117 $this->fld_userid
= isset( $prop['userid'] );
118 $this->fld_flags
= isset( $prop['flags'] );
119 $this->fld_timestamp
= isset( $prop['timestamp'] );
120 $this->fld_title
= isset( $prop['title'] );
121 $this->fld_ids
= isset( $prop['ids'] );
122 $this->fld_sizes
= isset( $prop['sizes'] );
123 $this->fld_redirect
= isset( $prop['redirect'] );
124 $this->fld_patrolled
= isset( $prop['patrolled'] );
125 $this->fld_loginfo
= isset( $prop['loginfo'] );
126 $this->fld_tags
= isset( $prop['tags'] );
127 $this->fld_sha1
= isset( $prop['sha1'] );
130 public function execute() {
134 public function executeGenerator( $resultPageSet ) {
135 $this->run( $resultPageSet );
139 * Generates and outputs the result of this query based upon the provided parameters.
141 * @param ApiPageSet $resultPageSet
143 public function run( $resultPageSet = null ) {
144 $user = $this->getUser();
145 /* Get the parameters of the request. */
146 $params = $this->extractRequestParams();
148 /* Build our basic query. Namely, something along the lines of:
149 * SELECT * FROM recentchanges WHERE rc_timestamp > $start
150 * AND rc_timestamp < $end AND rc_namespace = $namespace
152 $this->addTables( 'recentchanges' );
153 $index = array( 'recentchanges' => 'rc_timestamp' ); // May change
154 $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'], $params['start'], $params['end'] );
156 if ( !is_null( $params['continue'] ) ) {
157 $cont = explode( '|', $params['continue'] );
158 $this->dieContinueUsageIf( count( $cont ) != 2 );
159 $db = $this->getDB();
160 $timestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
161 $id = intval( $cont[1] );
162 $this->dieContinueUsageIf( $id != $cont[1] );
163 $op = $params['dir'] === 'older' ?
'<' : '>';
165 "rc_timestamp $op $timestamp OR " .
166 "(rc_timestamp = $timestamp AND " .
171 $order = $params['dir'] === 'older' ?
'DESC' : 'ASC';
172 $this->addOption( 'ORDER BY', array(
173 "rc_timestamp $order",
177 $this->addWhereFld( 'rc_namespace', $params['namespace'] );
179 if ( !is_null( $params['type'] ) ) {
181 $this->addWhereFld( 'rc_type', RecentChange
::parseToRCType( $params['type'] ) );
182 } catch ( Exception
$e ) {
183 ApiBase
::dieDebug( __METHOD__
, $e->getMessage() );
187 if ( !is_null( $params['show'] ) ) {
188 $show = array_flip( $params['show'] );
190 /* Check for conflicting parameters. */
191 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
192 ||
( isset( $show['bot'] ) && isset( $show['!bot'] ) )
193 ||
( isset( $show['anon'] ) && isset( $show['!anon'] ) )
194 ||
( isset( $show['redirect'] ) && isset( $show['!redirect'] ) )
195 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
196 ||
( isset( $show['patrolled'] ) && isset( $show['unpatrolled'] ) )
197 ||
( isset( $show['!patrolled'] ) && isset( $show['unpatrolled'] ) )
199 $this->dieUsageMsg( 'show' );
203 if ( isset( $show['patrolled'] )
204 ||
isset( $show['!patrolled'] )
205 ||
isset( $show['unpatrolled'] )
207 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
209 'You need the patrol right to request the patrolled flag',
215 /* Add additional conditions to query depending upon parameters. */
216 $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
217 $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
218 $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
219 $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
220 $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
221 $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
222 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
223 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
224 $this->addWhereIf( 'page_is_redirect = 1', isset( $show['redirect'] ) );
226 if ( isset( $show['unpatrolled'] ) ) {
227 // See ChangesList:isUnpatrolled
228 if ( $user->useRCPatrol() ) {
229 $this->addWhere( 'rc_patrolled = 0' );
230 } elseif ( $user->useNPPatrol() ) {
231 $this->addWhere( 'rc_patrolled = 0' );
232 $this->addWhereFld( 'rc_type', RC_NEW
);
236 // Don't throw log entries out the window here
238 'page_is_redirect = 0 OR page_is_redirect IS NULL',
239 isset( $show['!redirect'] )
243 if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
244 $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
247 if ( !is_null( $params['user'] ) ) {
248 $this->addWhereFld( 'rc_user_text', $params['user'] );
249 $index['recentchanges'] = 'rc_user_text';
252 if ( !is_null( $params['excludeuser'] ) ) {
253 // We don't use the rc_user_text index here because
254 // * it would require us to sort by rc_user_text before rc_timestamp
255 // * the != condition doesn't throw out too many rows anyway
256 $this->addWhere( 'rc_user_text != ' . $this->getDB()->addQuotes( $params['excludeuser'] ) );
259 /* Add the fields we're concerned with to our query. */
260 $this->addFields( array(
270 $showRedirects = false;
271 /* Determine what properties we need to display. */
272 if ( !is_null( $params['prop'] ) ) {
273 $prop = array_flip( $params['prop'] );
275 /* Set up internal members based upon params. */
276 $this->initProperties( $prop );
278 if ( $this->fld_patrolled
&& !$user->useRCPatrol() && !$user->useNPPatrol() ) {
280 'You need the patrol right to request the patrolled flag',
285 /* Add fields to our query if they are specified as a needed parameter. */
286 $this->addFieldsIf( array( 'rc_this_oldid', 'rc_last_oldid' ), $this->fld_ids
);
287 $this->addFieldsIf( 'rc_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
288 $this->addFieldsIf( 'rc_user', $this->fld_user ||
$this->fld_userid
);
289 $this->addFieldsIf( 'rc_user_text', $this->fld_user
);
290 $this->addFieldsIf( array( 'rc_minor', 'rc_type', 'rc_bot' ), $this->fld_flags
);
291 $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes
);
292 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
294 array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ),
297 $showRedirects = $this->fld_redirect ||
isset( $show['redirect'] )
298 ||
isset( $show['!redirect'] );
301 if ( $this->fld_tags
) {
302 $this->addTables( 'tag_summary' );
303 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rc_id=ts_rc_id' ) ) ) );
304 $this->addFields( 'ts_tags' );
307 if ( $this->fld_sha1
) {
308 $this->addTables( 'revision' );
309 $this->addJoinConds( array( 'revision' => array( 'LEFT JOIN',
310 array( 'rc_this_oldid=rev_id' ) ) ) );
311 $this->addFields( array( 'rev_sha1', 'rev_deleted' ) );
314 if ( $params['toponly'] ||
$showRedirects ) {
315 $this->addTables( 'page' );
316 $this->addJoinConds( array( 'page' => array( 'LEFT JOIN',
317 array( 'rc_namespace=page_namespace', 'rc_title=page_title' ) ) ) );
318 $this->addFields( 'page_is_redirect' );
320 if ( $params['toponly'] ) {
321 $this->addWhere( 'rc_this_oldid = page_latest' );
325 if ( !is_null( $params['tag'] ) ) {
326 $this->addTables( 'change_tag' );
327 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rc_id=ct_rc_id' ) ) ) );
328 $this->addWhereFld( 'ct_tag', $params['tag'] );
331 // Paranoia: avoid brute force searches (bug 17342)
332 if ( !is_null( $params['user'] ) ||
!is_null( $params['excludeuser'] ) ) {
333 if ( !$user->isAllowed( 'deletedhistory' ) ) {
334 $bitmask = Revision
::DELETED_USER
;
335 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
336 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
341 $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" );
344 if ( $this->getRequest()->getCheck( 'namespace' ) ) {
345 // LogPage::DELETED_ACTION hides the affected page, too.
346 if ( !$user->isAllowed( 'deletedhistory' ) ) {
347 $bitmask = LogPage
::DELETED_ACTION
;
348 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
349 $bitmask = LogPage
::DELETED_ACTION | LogPage
::DELETED_RESTRICTED
;
354 $this->addWhere( $this->getDB()->makeList( array(
355 'rc_type != ' . RC_LOG
,
356 $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
361 $this->token
= $params['token'];
362 $this->addOption( 'LIMIT', $params['limit'] +
1 );
363 $this->addOption( 'USE INDEX', $index );
366 /* Perform the actual query. */
367 $res = $this->select( __METHOD__
);
371 $result = $this->getResult();
373 /* Iterate through the rows, adding data extracted from them to our query result. */
374 foreach ( $res as $row ) {
375 if ( ++
$count > $params['limit'] ) {
376 // We've reached the one extra which shows that there are
377 // additional pages to be had. Stop here...
378 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
382 if ( is_null( $resultPageSet ) ) {
383 /* Extract the data from a single row. */
384 $vals = $this->extractRowInfo( $row );
386 /* Add that row's data to our final output. */
390 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
392 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
396 $titles[] = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
400 if ( is_null( $resultPageSet ) ) {
401 /* Format the result */
402 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'rc' );
404 $resultPageSet->populateFromTitles( $titles );
409 * Extracts from a single sql row the data needed to describe one recent change.
411 * @param stdClass $row The row from which to extract the data.
412 * @return array An array mapping strings (descriptors) to their respective string values.
415 public function extractRowInfo( $row ) {
416 /* Determine the title of the page that has been changed. */
417 $title = Title
::makeTitle( $row->rc_namespace
, $row->rc_title
);
418 $user = $this->getUser();
420 /* Our output data. */
423 $type = intval( $row->rc_type
);
424 $vals['type'] = RecentChange
::parseFromRCType( $type );
428 /* Create a new entry in the result for the title. */
429 if ( $this->fld_title ||
$this->fld_ids
) {
430 if ( $type === RC_LOG
&& ( $row->rc_deleted
& LogPage
::DELETED_ACTION
) ) {
431 $vals['actionhidden'] = '';
434 if ( $type !== RC_LOG ||
435 LogEventsList
::userCanBitfield( $row->rc_deleted
, LogPage
::DELETED_ACTION
, $user )
437 if ( $this->fld_title
) {
438 ApiQueryBase
::addTitleInfo( $vals, $title );
440 if ( $this->fld_ids
) {
441 $vals['pageid'] = intval( $row->rc_cur_id
);
442 $vals['revid'] = intval( $row->rc_this_oldid
);
443 $vals['old_revid'] = intval( $row->rc_last_oldid
);
448 if ( $this->fld_ids
) {
449 $vals['rcid'] = intval( $row->rc_id
);
452 /* Add user data and 'anon' flag, if user is anonymous. */
453 if ( $this->fld_user ||
$this->fld_userid
) {
454 if ( $row->rc_deleted
& Revision
::DELETED_USER
) {
455 $vals['userhidden'] = '';
458 if ( Revision
::userCanBitfield( $row->rc_deleted
, Revision
::DELETED_USER
, $user ) ) {
459 if ( $this->fld_user
) {
460 $vals['user'] = $row->rc_user_text
;
463 if ( $this->fld_userid
) {
464 $vals['userid'] = $row->rc_user
;
467 if ( !$row->rc_user
) {
473 /* Add flags, such as new, minor, bot. */
474 if ( $this->fld_flags
) {
475 if ( $row->rc_bot
) {
478 if ( $row->rc_type
== RC_NEW
) {
481 if ( $row->rc_minor
) {
486 /* Add sizes of each revision. (Only available on 1.10+) */
487 if ( $this->fld_sizes
) {
488 $vals['oldlen'] = intval( $row->rc_old_len
);
489 $vals['newlen'] = intval( $row->rc_new_len
);
492 /* Add the timestamp. */
493 if ( $this->fld_timestamp
) {
494 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rc_timestamp
);
497 /* Add edit summary / log summary. */
498 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
499 if ( $row->rc_deleted
& Revision
::DELETED_COMMENT
) {
500 $vals['commenthidden'] = '';
503 if ( Revision
::userCanBitfield( $row->rc_deleted
, Revision
::DELETED_COMMENT
, $user ) ) {
504 if ( $this->fld_comment
&& isset( $row->rc_comment
) ) {
505 $vals['comment'] = $row->rc_comment
;
508 if ( $this->fld_parsedcomment
&& isset( $row->rc_comment
) ) {
509 $vals['parsedcomment'] = Linker
::formatComment( $row->rc_comment
, $title );
514 if ( $this->fld_redirect
) {
515 if ( $row->page_is_redirect
) {
516 $vals['redirect'] = '';
520 /* Add the patrolled flag */
521 if ( $this->fld_patrolled
&& $row->rc_patrolled
== 1 ) {
522 $vals['patrolled'] = '';
525 if ( $this->fld_patrolled
&& ChangesList
::isUnpatrolled( $row, $user ) ) {
526 $vals['unpatrolled'] = '';
529 if ( $this->fld_loginfo
&& $row->rc_type
== RC_LOG
) {
530 if ( $row->rc_deleted
& LogPage
::DELETED_ACTION
) {
531 $vals['actionhidden'] = '';
534 if ( LogEventsList
::userCanBitfield( $row->rc_deleted
, LogPage
::DELETED_ACTION
, $user ) ) {
535 $vals['logid'] = intval( $row->rc_logid
);
536 $vals['logtype'] = $row->rc_log_type
;
537 $vals['logaction'] = $row->rc_log_action
;
538 $logEntry = DatabaseLogEntry
::newFromRow( $row );
539 ApiQueryLogEvents
::addLogParams(
542 $logEntry->getParameters(),
543 $logEntry->getType(),
544 $logEntry->getSubtype(),
545 $logEntry->getTimestamp()
550 if ( $this->fld_tags
) {
551 if ( $row->ts_tags
) {
552 $tags = explode( ',', $row->ts_tags
);
553 $this->getResult()->setIndexedTagName( $tags, 'tag' );
554 $vals['tags'] = $tags;
556 $vals['tags'] = array();
560 if ( $this->fld_sha1
&& $row->rev_sha1
!== null ) {
561 if ( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
562 $vals['sha1hidden'] = '';
565 if ( Revision
::userCanBitfield( $row->rev_deleted
, Revision
::DELETED_TEXT
, $user ) ) {
566 if ( $row->rev_sha1
!== '' ) {
567 $vals['sha1'] = wfBaseConvert( $row->rev_sha1
, 36, 16, 40 );
574 if ( !is_null( $this->token
) ) {
575 $tokenFunctions = $this->getTokenFunctions();
576 foreach ( $this->token
as $t ) {
577 $val = call_user_func( $tokenFunctions[$t], $row->rc_cur_id
,
578 $title, RecentChange
::newFromRow( $row ) );
579 if ( $val === false ) {
580 $this->setWarning( "Action '$t' is not allowed for the current user" );
582 $vals[$t . 'token'] = $val;
587 if ( $anyHidden && ( $row->rc_deleted
& Revision
::DELETED_RESTRICTED
) ) {
588 $vals['suppressed'] = '';
594 public function getCacheMode( $params ) {
595 if ( isset( $params['show'] ) ) {
596 foreach ( $params['show'] as $show ) {
597 if ( $show === 'patrolled' ||
$show === '!patrolled' ) {
602 if ( isset( $params['token'] ) ) {
605 if ( $this->userCanSeeRevDel() ) {
608 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
609 // formatComment() calls wfMessage() among other things
610 return 'anon-public-user-private';
616 public function getAllowedParams() {
619 ApiBase
::PARAM_TYPE
=> 'timestamp'
622 ApiBase
::PARAM_TYPE
=> 'timestamp'
625 ApiBase
::PARAM_DFLT
=> 'older',
626 ApiBase
::PARAM_TYPE
=> array(
630 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
632 'namespace' => array(
633 ApiBase
::PARAM_ISMULTI
=> true,
634 ApiBase
::PARAM_TYPE
=> 'namespace'
637 ApiBase
::PARAM_TYPE
=> 'user'
639 'excludeuser' => array(
640 ApiBase
::PARAM_TYPE
=> 'user'
644 ApiBase
::PARAM_ISMULTI
=> true,
645 ApiBase
::PARAM_DFLT
=> 'title|timestamp|ids',
646 ApiBase
::PARAM_TYPE
=> array(
664 ApiBase
::PARAM_DEPRECATED
=> true,
665 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
666 ApiBase
::PARAM_ISMULTI
=> true
669 ApiBase
::PARAM_ISMULTI
=> true,
670 ApiBase
::PARAM_TYPE
=> array(
685 ApiBase
::PARAM_DFLT
=> 10,
686 ApiBase
::PARAM_TYPE
=> 'limit',
687 ApiBase
::PARAM_MIN
=> 1,
688 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
689 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
692 ApiBase
::PARAM_DFLT
=> 'edit|new|log',
693 ApiBase
::PARAM_ISMULTI
=> true,
694 ApiBase
::PARAM_TYPE
=> array(
703 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
708 protected function getExamplesMessages() {
710 'action=query&list=recentchanges'
711 => 'apihelp-query+recentchanges-example-simple',
712 'action=query&generator=recentchanges&grcshow=!patrolled&prop=info'
713 => 'apihelp-query+recentchanges-example-generator',
717 public function getHelpUrls() {
718 return 'https://www.mediawiki.org/wiki/API:Recentchanges';