MediaWiki  REL1_24
ApiQueryWatchlist.php
Go to the documentation of this file.
00001 <?php
00033 class ApiQueryWatchlist extends ApiQueryGeneratorBase {
00034 
00035     public function __construct( ApiQuery $query, $moduleName ) {
00036         parent::__construct( $query, $moduleName, 'wl' );
00037     }
00038 
00039     public function execute() {
00040         $this->run();
00041     }
00042 
00043     public function executeGenerator( $resultPageSet ) {
00044         $this->run( $resultPageSet );
00045     }
00046 
00047     private $fld_ids = false, $fld_title = false, $fld_patrol = false,
00048         $fld_flags = false, $fld_timestamp = false, $fld_user = false,
00049         $fld_comment = false, $fld_parsedcomment = false, $fld_sizes = false,
00050         $fld_notificationtimestamp = false, $fld_userid = false,
00051         $fld_loginfo = false;
00052 
00057     private function run( $resultPageSet = null ) {
00058         $this->selectNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
00059 
00060         $params = $this->extractRequestParams();
00061 
00062         $user = $this->getUser();
00063         $wlowner = $this->getWatchlistUser( $params );
00064 
00065         if ( !is_null( $params['prop'] ) && is_null( $resultPageSet ) ) {
00066             $prop = array_flip( $params['prop'] );
00067 
00068             $this->fld_ids = isset( $prop['ids'] );
00069             $this->fld_title = isset( $prop['title'] );
00070             $this->fld_flags = isset( $prop['flags'] );
00071             $this->fld_user = isset( $prop['user'] );
00072             $this->fld_userid = isset( $prop['userid'] );
00073             $this->fld_comment = isset( $prop['comment'] );
00074             $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
00075             $this->fld_timestamp = isset( $prop['timestamp'] );
00076             $this->fld_sizes = isset( $prop['sizes'] );
00077             $this->fld_patrol = isset( $prop['patrol'] );
00078             $this->fld_notificationtimestamp = isset( $prop['notificationtimestamp'] );
00079             $this->fld_loginfo = isset( $prop['loginfo'] );
00080 
00081             if ( $this->fld_patrol ) {
00082                 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
00083                     $this->dieUsage( 'patrol property is not available', 'patrol' );
00084                 }
00085             }
00086         }
00087 
00088         $this->addFields( array(
00089             'rc_id',
00090             'rc_namespace',
00091             'rc_title',
00092             'rc_timestamp',
00093             'rc_type',
00094             'rc_deleted',
00095         ) );
00096 
00097         if ( is_null( $resultPageSet ) ) {
00098             $this->addFields( array(
00099                 'rc_cur_id',
00100                 'rc_this_oldid',
00101                 'rc_last_oldid',
00102             ) );
00103 
00104             $this->addFieldsIf( array( 'rc_type', 'rc_minor', 'rc_bot' ), $this->fld_flags );
00105             $this->addFieldsIf( 'rc_user', $this->fld_user || $this->fld_userid );
00106             $this->addFieldsIf( 'rc_user_text', $this->fld_user );
00107             $this->addFieldsIf( 'rc_comment', $this->fld_comment || $this->fld_parsedcomment );
00108             $this->addFieldsIf( 'rc_patrolled', $this->fld_patrol );
00109             $this->addFieldsIf( array( 'rc_old_len', 'rc_new_len' ), $this->fld_sizes );
00110             $this->addFieldsIf( 'wl_notificationtimestamp', $this->fld_notificationtimestamp );
00111             $this->addFieldsIf(
00112                 array( 'rc_logid', 'rc_log_type', 'rc_log_action', 'rc_params' ),
00113                 $this->fld_loginfo
00114             );
00115         } elseif ( $params['allrev'] ) {
00116             $this->addFields( 'rc_this_oldid' );
00117         } else {
00118             $this->addFields( 'rc_cur_id' );
00119         }
00120 
00121         $this->addTables( array(
00122             'recentchanges',
00123             'watchlist',
00124         ) );
00125 
00126         $userId = $wlowner->getId();
00127         $this->addJoinConds( array( 'watchlist' => array( 'INNER JOIN',
00128             array(
00129                 'wl_user' => $userId,
00130                 'wl_namespace=rc_namespace',
00131                 'wl_title=rc_title'
00132             )
00133         ) ) );
00134 
00135         $db = $this->getDB();
00136 
00137         $this->addTimestampWhereRange( 'rc_timestamp', $params['dir'],
00138             $params['start'], $params['end'] );
00139         // Include in ORDER BY for uniqueness
00140         $this->addWhereRange( 'rc_id', $params['dir'], null, null );
00141 
00142         if ( !is_null( $params['continue'] ) ) {
00143             $cont = explode( '|', $params['continue'] );
00144             $this->dieContinueUsageIf( count( $cont ) != 2 );
00145             $op = ( $params['dir'] === 'newer' ? '>' : '<' );
00146             $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
00147             $continueId = (int)$cont[1];
00148             $this->dieContinueUsageIf( $continueId != $cont[1] );
00149             $this->addWhere( "rc_timestamp $op $continueTimestamp OR " .
00150                 "(rc_timestamp = $continueTimestamp AND " .
00151                 "rc_id $op= $continueId)"
00152             );
00153         }
00154 
00155         $this->addWhereFld( 'wl_namespace', $params['namespace'] );
00156 
00157         if ( !$params['allrev'] ) {
00158             $this->addTables( 'page' );
00159             $this->addJoinConds( array( 'page' => array( 'LEFT JOIN', 'rc_cur_id=page_id' ) ) );
00160             $this->addWhere( 'rc_this_oldid=page_latest OR rc_type=' . RC_LOG );
00161         }
00162 
00163         if ( !is_null( $params['show'] ) ) {
00164             $show = array_flip( $params['show'] );
00165 
00166             /* Check for conflicting parameters. */
00167             if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
00168                 || ( isset( $show['bot'] ) && isset( $show['!bot'] ) )
00169                 || ( isset( $show['anon'] ) && isset( $show['!anon'] ) )
00170                 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
00171                 || ( isset( $show['unread'] ) && isset( $show['!unread'] ) )
00172             ) {
00173                 $this->dieUsageMsg( 'show' );
00174             }
00175 
00176             // Check permissions.
00177             if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
00178                 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
00179                     $this->dieUsage(
00180                         'You need the patrol right to request the patrolled flag',
00181                         'permissiondenied'
00182                     );
00183                 }
00184             }
00185 
00186             /* Add additional conditions to query depending upon parameters. */
00187             $this->addWhereIf( 'rc_minor = 0', isset( $show['!minor'] ) );
00188             $this->addWhereIf( 'rc_minor != 0', isset( $show['minor'] ) );
00189             $this->addWhereIf( 'rc_bot = 0', isset( $show['!bot'] ) );
00190             $this->addWhereIf( 'rc_bot != 0', isset( $show['bot'] ) );
00191             $this->addWhereIf( 'rc_user = 0', isset( $show['anon'] ) );
00192             $this->addWhereIf( 'rc_user != 0', isset( $show['!anon'] ) );
00193             $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
00194             $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
00195             $this->addWhereIf( 'wl_notificationtimestamp IS NOT NULL', isset( $show['unread'] ) );
00196             $this->addWhereIf( 'wl_notificationtimestamp IS NULL', isset( $show['!unread'] ) );
00197         }
00198 
00199         if ( !is_null( $params['type'] ) ) {
00200             try {
00201                 $this->addWhereFld( 'rc_type', RecentChange::parseToRCType( $params['type'] ) );
00202             } catch ( MWException $e ) {
00203                 ApiBase::dieDebug( __METHOD__, $e->getMessage() );
00204             }
00205         }
00206 
00207         if ( !is_null( $params['user'] ) && !is_null( $params['excludeuser'] ) ) {
00208             $this->dieUsage( 'user and excludeuser cannot be used together', 'user-excludeuser' );
00209         }
00210         if ( !is_null( $params['user'] ) ) {
00211             $this->addWhereFld( 'rc_user_text', $params['user'] );
00212         }
00213         if ( !is_null( $params['excludeuser'] ) ) {
00214             $this->addWhere( 'rc_user_text != ' . $db->addQuotes( $params['excludeuser'] ) );
00215         }
00216 
00217         // This is an index optimization for mysql, as done in the Special:Watchlist page
00218         $this->addWhereIf(
00219             "rc_timestamp > ''",
00220             !isset( $params['start'] ) && !isset( $params['end'] ) && $db->getType() == 'mysql'
00221         );
00222 
00223         // Paranoia: avoid brute force searches (bug 17342)
00224         if ( !is_null( $params['user'] ) || !is_null( $params['excludeuser'] ) ) {
00225             if ( !$user->isAllowed( 'deletedhistory' ) ) {
00226                 $bitmask = Revision::DELETED_USER;
00227             } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
00228                 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
00229             } else {
00230                 $bitmask = 0;
00231             }
00232             if ( $bitmask ) {
00233                 $this->addWhere( $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask" );
00234             }
00235         }
00236 
00237         // LogPage::DELETED_ACTION hides the affected page, too. So hide those
00238         // entirely from the watchlist, or someone could guess the title.
00239         if ( !$user->isAllowed( 'deletedhistory' ) ) {
00240             $bitmask = LogPage::DELETED_ACTION;
00241         } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
00242             $bitmask = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
00243         } else {
00244             $bitmask = 0;
00245         }
00246         if ( $bitmask ) {
00247             $this->addWhere( $this->getDB()->makeList( array(
00248                 'rc_type != ' . RC_LOG,
00249                 $this->getDB()->bitAnd( 'rc_deleted', $bitmask ) . " != $bitmask",
00250             ), LIST_OR ) );
00251         }
00252 
00253         $this->addOption( 'LIMIT', $params['limit'] + 1 );
00254 
00255         $ids = array();
00256         $count = 0;
00257         $res = $this->select( __METHOD__ );
00258 
00259         foreach ( $res as $row ) {
00260             if ( ++$count > $params['limit'] ) {
00261                 // We've reached the one extra which shows that there are
00262                 // additional pages to be had. Stop here...
00263                 $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
00264                 break;
00265             }
00266 
00267             if ( is_null( $resultPageSet ) ) {
00268                 $vals = $this->extractRowInfo( $row );
00269                 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00270                 if ( !$fit ) {
00271                     $this->setContinueEnumParameter( 'continue', "$row->rc_timestamp|$row->rc_id" );
00272                     break;
00273                 }
00274             } else {
00275                 if ( $params['allrev'] ) {
00276                     $ids[] = intval( $row->rc_this_oldid );
00277                 } else {
00278                     $ids[] = intval( $row->rc_cur_id );
00279                 }
00280             }
00281         }
00282 
00283         if ( is_null( $resultPageSet ) ) {
00284             $this->getResult()->setIndexedTagName_internal(
00285                 array( 'query', $this->getModuleName() ),
00286                 'item'
00287             );
00288         } elseif ( $params['allrev'] ) {
00289             $resultPageSet->populateFromRevisionIDs( $ids );
00290         } else {
00291             $resultPageSet->populateFromPageIDs( $ids );
00292         }
00293     }
00294 
00295     private function extractRowInfo( $row ) {
00296         /* Determine the title of the page that has been changed. */
00297         $title = Title::makeTitle( $row->rc_namespace, $row->rc_title );
00298         $user = $this->getUser();
00299 
00300         /* Our output data. */
00301         $vals = array();
00302         $type = intval( $row->rc_type );
00303         $vals['type'] = RecentChange::parseFromRCType( $type );
00304         $anyHidden = false;
00305 
00306         /* Create a new entry in the result for the title. */
00307         if ( $this->fld_title || $this->fld_ids ) {
00308             // These should already have been filtered out of the query, but just in case.
00309             if ( $type === RC_LOG && ( $row->rc_deleted & LogPage::DELETED_ACTION ) ) {
00310                 $vals['actionhidden'] = '';
00311                 $anyHidden = true;
00312             }
00313             if ( $type !== RC_LOG ||
00314                 LogEventsList::userCanBitfield( $row->rc_deleted, LogPage::DELETED_ACTION, $user )
00315             ) {
00316                 if ( $this->fld_title ) {
00317                     ApiQueryBase::addTitleInfo( $vals, $title );
00318                 }
00319                 if ( $this->fld_ids ) {
00320                     $vals['pageid'] = intval( $row->rc_cur_id );
00321                     $vals['revid'] = intval( $row->rc_this_oldid );
00322                     $vals['old_revid'] = intval( $row->rc_last_oldid );
00323                 }
00324             }
00325         }
00326 
00327         /* Add user data and 'anon' flag, if user is anonymous. */
00328         if ( $this->fld_user || $this->fld_userid ) {
00329             if ( $row->rc_deleted & Revision::DELETED_USER ) {
00330                 $vals['userhidden'] = '';
00331                 $anyHidden = true;
00332             }
00333             if ( Revision::userCanBitfield( $row->rc_deleted, Revision::DELETED_USER, $user ) ) {
00334                 if ( $this->fld_userid ) {
00335                     $vals['userid'] = $row->rc_user;
00336                     // for backwards compatibility
00337                     $vals['user'] = $row->rc_user;
00338                 }
00339 
00340                 if ( $this->fld_user ) {
00341                     $vals['user'] = $row->rc_user_text;
00342                 }
00343 
00344                 if ( !$row->rc_user ) {
00345                     $vals['anon'] = '';
00346                 }
00347             }
00348         }
00349 
00350         /* Add flags, such as new, minor, bot. */
00351         if ( $this->fld_flags ) {
00352             if ( $row->rc_bot ) {
00353                 $vals['bot'] = '';
00354             }
00355             if ( $row->rc_type == RC_NEW ) {
00356                 $vals['new'] = '';
00357             }
00358             if ( $row->rc_minor ) {
00359                 $vals['minor'] = '';
00360             }
00361         }
00362 
00363         /* Add sizes of each revision. (Only available on 1.10+) */
00364         if ( $this->fld_sizes ) {
00365             $vals['oldlen'] = intval( $row->rc_old_len );
00366             $vals['newlen'] = intval( $row->rc_new_len );
00367         }
00368 
00369         /* Add the timestamp. */
00370         if ( $this->fld_timestamp ) {
00371             $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rc_timestamp );
00372         }
00373 
00374         if ( $this->fld_notificationtimestamp ) {
00375             $vals['notificationtimestamp'] = ( $row->wl_notificationtimestamp == null )
00376                 ? ''
00377                 : wfTimestamp( TS_ISO_8601, $row->wl_notificationtimestamp );
00378         }
00379 
00380         /* Add edit summary / log summary. */
00381         if ( $this->fld_comment || $this->fld_parsedcomment ) {
00382             if ( $row->rc_deleted & Revision::DELETED_COMMENT ) {
00383                 $vals['commenthidden'] = '';
00384                 $anyHidden = true;
00385             }
00386             if ( Revision::userCanBitfield( $row->rc_deleted, Revision::DELETED_COMMENT, $user ) ) {
00387                 if ( $this->fld_comment && isset( $row->rc_comment ) ) {
00388                     $vals['comment'] = $row->rc_comment;
00389                 }
00390 
00391                 if ( $this->fld_parsedcomment && isset( $row->rc_comment ) ) {
00392                     $vals['parsedcomment'] = Linker::formatComment( $row->rc_comment, $title );
00393                 }
00394             }
00395         }
00396 
00397         /* Add the patrolled flag */
00398         if ( $this->fld_patrol && $row->rc_patrolled == 1 ) {
00399             $vals['patrolled'] = '';
00400         }
00401 
00402         if ( $this->fld_patrol && ChangesList::isUnpatrolled( $row, $user ) ) {
00403             $vals['unpatrolled'] = '';
00404         }
00405 
00406         if ( $this->fld_loginfo && $row->rc_type == RC_LOG ) {
00407             if ( $row->rc_deleted & LogPage::DELETED_ACTION ) {
00408                 $vals['actionhidden'] = '';
00409                 $anyHidden = true;
00410             }
00411             if ( LogEventsList::userCanBitfield( $row->rc_deleted, LogPage::DELETED_ACTION, $user ) ) {
00412                 $vals['logid'] = intval( $row->rc_logid );
00413                 $vals['logtype'] = $row->rc_log_type;
00414                 $vals['logaction'] = $row->rc_log_action;
00415                 $logEntry = DatabaseLogEntry::newFromRow( (array)$row );
00416                 ApiQueryLogEvents::addLogParams(
00417                     $this->getResult(),
00418                     $vals,
00419                     $logEntry->getParameters(),
00420                     $logEntry->getType(),
00421                     $logEntry->getSubtype(),
00422                     $logEntry->getTimestamp()
00423                 );
00424             }
00425         }
00426 
00427         if ( $anyHidden && ( $row->rc_deleted & Revision::DELETED_RESTRICTED ) ) {
00428             $vals['suppressed'] = '';
00429         }
00430 
00431         return $vals;
00432     }
00433 
00434     public function getAllowedParams() {
00435         return array(
00436             'allrev' => false,
00437             'start' => array(
00438                 ApiBase::PARAM_TYPE => 'timestamp'
00439             ),
00440             'end' => array(
00441                 ApiBase::PARAM_TYPE => 'timestamp'
00442             ),
00443             'namespace' => array(
00444                 ApiBase::PARAM_ISMULTI => true,
00445                 ApiBase::PARAM_TYPE => 'namespace'
00446             ),
00447             'user' => array(
00448                 ApiBase::PARAM_TYPE => 'user',
00449             ),
00450             'excludeuser' => array(
00451                 ApiBase::PARAM_TYPE => 'user',
00452             ),
00453             'dir' => array(
00454                 ApiBase::PARAM_DFLT => 'older',
00455                 ApiBase::PARAM_TYPE => array(
00456                     'newer',
00457                     'older'
00458                 )
00459             ),
00460             'limit' => array(
00461                 ApiBase::PARAM_DFLT => 10,
00462                 ApiBase::PARAM_TYPE => 'limit',
00463                 ApiBase::PARAM_MIN => 1,
00464                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00465                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00466             ),
00467             'prop' => array(
00468                 ApiBase::PARAM_ISMULTI => true,
00469                 ApiBase::PARAM_DFLT => 'ids|title|flags',
00470                 ApiBase::PARAM_TYPE => array(
00471                     'ids',
00472                     'title',
00473                     'flags',
00474                     'user',
00475                     'userid',
00476                     'comment',
00477                     'parsedcomment',
00478                     'timestamp',
00479                     'patrol',
00480                     'sizes',
00481                     'notificationtimestamp',
00482                     'loginfo',
00483                 )
00484             ),
00485             'show' => array(
00486                 ApiBase::PARAM_ISMULTI => true,
00487                 ApiBase::PARAM_TYPE => array(
00488                     'minor',
00489                     '!minor',
00490                     'bot',
00491                     '!bot',
00492                     'anon',
00493                     '!anon',
00494                     'patrolled',
00495                     '!patrolled',
00496                     'unread',
00497                     '!unread',
00498                 )
00499             ),
00500             'type' => array(
00501                 ApiBase::PARAM_ISMULTI => true,
00502                 ApiBase::PARAM_TYPE => array(
00503                     'edit',
00504                     'external',
00505                     'new',
00506                     'log',
00507                 )
00508             ),
00509             'owner' => array(
00510                 ApiBase::PARAM_TYPE => 'user'
00511             ),
00512             'token' => array(
00513                 ApiBase::PARAM_TYPE => 'string'
00514             ),
00515             'continue' => null,
00516         );
00517     }
00518 
00519     public function getParamDescription() {
00520         $p = $this->getModulePrefix();
00521 
00522         return array(
00523             'allrev' => 'Include multiple revisions of the same page within given timeframe',
00524             'start' => 'The timestamp to start enumerating from',
00525             'end' => 'The timestamp to end enumerating',
00526             'namespace' => 'Filter changes to only the given namespace(s)',
00527             'user' => 'Only list changes by this user',
00528             'excludeuser' => 'Don\'t list changes by this user',
00529             'dir' => $this->getDirectionDescription( $p ),
00530             'limit' => 'How many total results to return per request',
00531             'prop' => array(
00532                 'Which additional items to get (non-generator mode only).',
00533                 ' ids                    - Adds revision ids and page ids',
00534                 ' title                  - Adds title of the page',
00535                 ' flags                  - Adds flags for the edit',
00536                 ' user                   - Adds the user who made the edit',
00537                 ' userid                 - Adds user id of whom made the edit',
00538                 ' comment                - Adds comment of the edit',
00539                 ' parsedcomment          - Adds parsed comment of the edit',
00540                 ' timestamp              - Adds timestamp of the edit',
00541                 ' patrol                 - Tags edits that are patrolled',
00542                 ' sizes                  - Adds the old and new lengths of the page',
00543                 ' notificationtimestamp  - Adds timestamp of when the user was last notified about the edit',
00544                 ' loginfo                - Adds log information where appropriate',
00545             ),
00546             'show' => array(
00547                 'Show only items that meet this criteria.',
00548                 "For example, to see only minor edits done by logged-in users, set {$p}show=minor|!anon"
00549             ),
00550             'type' => array(
00551                 'Which types of changes to show',
00552                 ' edit           - Regular page edits',
00553                 ' external       - External changes',
00554                 ' new            - Page creations',
00555                 ' log            - Log entries',
00556             ),
00557             'owner' => 'The name of the user whose watchlist you\'d like to access',
00558             'token' => 'Give a security token (settable in preferences) to ' .
00559                 'allow access to another user\'s watchlist',
00560             'continue' => 'When more results are available, use this to continue',
00561         );
00562     }
00563 
00564     public function getDescription() {
00565         return "Get all recent changes to pages in the logged in user's watchlist.";
00566     }
00567 
00568     public function getExamples() {
00569         return array(
00570             'api.php?action=query&list=watchlist',
00571             'api.php?action=query&list=watchlist&wlprop=ids|title|timestamp|user|comment',
00572             'api.php?action=query&list=watchlist&wlallrev=&wlprop=ids|title|timestamp|user|comment',
00573             'api.php?action=query&generator=watchlist&prop=info',
00574             'api.php?action=query&generator=watchlist&gwlallrev=&prop=revisions&rvprop=timestamp|user',
00575             'api.php?action=query&list=watchlist&wlowner=Bob_Smith&wltoken=123ABC'
00576         );
00577     }
00578 
00579     public function getHelpUrls() {
00580         return 'https://www.mediawiki.org/wiki/API:Watchlist';
00581     }
00582 }