target is ignored then. Defaults to "users". */ private $contribs; /** * @var string|int A single namespace number, or an empty string for all namespaces */ private $namespace = ''; /** * @var string|false Name of tag to filter, or false to ignore tags */ private $tagFilter; /** * @var bool Set to true to invert the namespace selection */ private $nsInvert; /** * @var bool Set to true to show both the subject and talk namespace, no matter which got * selected */ private $associated; /** * @var bool Set to true to show only deleted revisions */ private $deletedOnly; /** * @var bool Set to true to show only latest (a.k.a. current) revisions */ private $topOnly; /** * @var bool Set to true to show only new pages */ private $newOnly; /** * @var bool Set to true to hide edits marked as minor by the user */ private $hideMinor; private $preventClickjacking = false; /** @var IDatabase */ private $mDbSecondary; /** * @var array */ private $mParentLens; /** * @var TemplateParser */ private $templateParser; public function __construct( IContextSource $context, array $options ) { parent::__construct( $context ); $msgs = [ 'diff', 'hist', 'pipe-separator', 'uctop' ]; foreach ( $msgs as $msg ) { $this->messages[$msg] = $this->msg( $msg )->escaped(); } $this->target = $options['target'] ?? ''; $this->contribs = $options['contribs'] ?? 'users'; $this->namespace = $options['namespace'] ?? ''; $this->tagFilter = $options['tagfilter'] ?? false; $this->nsInvert = $options['nsInvert'] ?? false; $this->associated = $options['associated'] ?? false; $this->deletedOnly = !empty( $options['deletedOnly'] ); $this->topOnly = !empty( $options['topOnly'] ); $this->newOnly = !empty( $options['newOnly'] ); $this->hideMinor = !empty( $options['hideMinor'] ); // Date filtering: use timestamp if available $startTimestamp = ''; $endTimestamp = ''; if ( $options['start'] ) { $startTimestamp = $options['start'] . ' 00:00:00'; } if ( $options['end'] ) { $endTimestamp = $options['end'] . ' 23:59:59'; } $this->getDateRangeCond( $startTimestamp, $endTimestamp ); // Most of this code will use the 'contributions' group DB, which can map to replica DBs // with extra user based indexes or partioning by user. The additional metadata // queries should use a regular replica DB since the lookup pattern is not all by user. $this->mDbSecondary = wfGetDB( DB_REPLICA ); // any random replica DB $this->mDb = wfGetDB( DB_REPLICA, 'contributions' ); $this->templateParser = new TemplateParser(); } function getDefaultQuery() { $query = parent::getDefaultQuery(); $query['target'] = $this->target; return $query; } /** * Wrap the navigation bar in a p element with identifying class. * In future we may want to change the `p` tag to a `div` and upstream * this to the parent class. * * @return string HTML */ function getNavigationBar() { return Html::rawElement( 'p', [ 'class' => 'mw-pager-navigation-bar' ], parent::getNavigationBar() ); } /** * This method basically executes the exact same code as the parent class, though with * a hook added, to allow extensions to add additional queries. * * @param string $offset Index offset, inclusive * @param int $limit Exact query limit * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING * @return IResultWrapper */ function reallyDoQuery( $offset, $limit, $order ) { list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo( $offset, $limit, $order ); /* * This hook will allow extensions to add in additional queries, so they can get their data * in My Contributions as well. Extensions should append their results to the $data array. * * Extension queries have to implement the navbar requirement as well. They should * - have a column aliased as $pager->getIndexField() * - have LIMIT set * - have a WHERE-clause that compares the $pager->getIndexField()-equivalent column to the offset * - have the ORDER BY specified based upon the details provided by the navbar * * See includes/Pager.php buildQueryInfo() method on how to build LIMIT, WHERE & ORDER BY * * &$data: an array of results of all contribs queries * $pager: the ContribsPager object hooked into * $offset: see phpdoc above * $limit: see phpdoc above * $descending: see phpdoc above */ $data = [ $this->mDb->select( $tables, $fields, $conds, $fname, $options, $join_conds ) ]; Hooks::run( 'ContribsPager::reallyDoQuery', [ &$data, $this, $offset, $limit, $order ] ); $result = []; // loop all results and collect them in an array foreach ( $data as $query ) { foreach ( $query as $i => $row ) { // use index column as key, allowing us to easily sort in PHP $result[$row->{$this->getIndexField()} . "-$i"] = $row; } } // sort results if ( $order === self::QUERY_ASCENDING ) { ksort( $result ); } else { krsort( $result ); } // enforce limit $result = array_slice( $result, 0, $limit ); // get rid of array keys $result = array_values( $result ); return new FakeResultWrapper( $result ); } function getQueryInfo() { $revQuery = Revision::getQueryInfo( [ 'page', 'user' ] ); $queryInfo = [ 'tables' => $revQuery['tables'], 'fields' => array_merge( $revQuery['fields'], [ 'page_is_new' ] ), 'conds' => [], 'options' => [], 'join_conds' => $revQuery['joins'], ]; if ( $this->contribs == 'newbie' ) { $max = $this->mDb->selectField( 'user', 'max(user_id)', '', __METHOD__ ); $queryInfo['conds'][] = $revQuery['fields']['rev_user'] . ' >' . (int)( $max - $max / 100 ); # ignore local groups with the bot right # @todo FIXME: Global groups may have 'bot' rights $groupsWithBotPermission = User::getGroupsWithPermission( 'bot' ); if ( count( $groupsWithBotPermission ) ) { $queryInfo['tables'][] = 'user_groups'; $queryInfo['conds'][] = 'ug_group IS NULL'; $queryInfo['join_conds']['user_groups'] = [ 'LEFT JOIN', [ 'ug_user = ' . $revQuery['fields']['rev_user'], 'ug_group' => $groupsWithBotPermission, 'ug_expiry IS NULL OR ug_expiry >= ' . $this->mDb->addQuotes( $this->mDb->timestamp() ) ] ]; } // (T140537) Disallow looking too far in the past for 'newbies' queries. If the user requested // a timestamp offset far in the past such that there are no edits by users with user_ids in // the range, we would end up scanning all revisions from that offset until start of time. $queryInfo['conds'][] = 'rev_timestamp > ' . $this->mDb->addQuotes( $this->mDb->timestamp( wfTimestamp() - 30 * 24 * 60 * 60 ) ); } else { $user = User::newFromName( $this->target, false ); $ipRangeConds = $user->isAnon() ? $this->getIpRangeConds( $this->mDb, $this->target ) : null; if ( $ipRangeConds ) { $queryInfo['tables'][] = 'ip_changes'; /** * These aliases make `ORDER BY rev_timestamp, rev_id` from {@see getIndexField} and * {@see getExtraSortFields} use the replicated `ipc_rev_timestamp` and `ipc_rev_id` * columns from the `ip_changes` table, for more efficient queries. * @see https://phabricator.wikimedia.org/T200259#4832318 */ $queryInfo['fields'] = array_merge( [ 'rev_timestamp' => 'ipc_rev_timestamp', 'rev_id' => 'ipc_rev_id', ], array_diff( $queryInfo['fields'], [ 'rev_timestamp', 'rev_id', ] ) ); $queryInfo['join_conds']['ip_changes'] = [ 'LEFT JOIN', [ 'ipc_rev_id = rev_id' ] ]; $queryInfo['conds'][] = $ipRangeConds; } else { // tables and joins are already handled by Revision::getQueryInfo() $conds = ActorMigration::newMigration()->getWhere( $this->mDb, 'rev_user', $user ); $queryInfo['conds'][] = $conds['conds']; // Force the appropriate index to avoid bad query plans (T189026) if ( isset( $conds['orconds']['actor'] ) ) { // @todo: This will need changing when revision_comment_temp goes away $queryInfo['options']['USE INDEX']['temp_rev_user'] = 'actor_timestamp'; // Alias 'rev_timestamp' => 'revactor_timestamp' and 'rev_id' => 'revactor_rev' so // "ORDER BY rev_timestamp, rev_id" is interpreted to use denormalized revision_actor_temp // fields instead. $queryInfo['fields'] = array_merge( array_diff( $queryInfo['fields'], [ 'rev_timestamp', 'rev_id' ] ), [ 'rev_timestamp' => 'revactor_timestamp', 'rev_id' => 'revactor_rev' ] ); } else { $queryInfo['options']['USE INDEX']['revision'] = isset( $conds['orconds']['userid'] ) ? 'user_timestamp' : 'usertext_timestamp'; } } } if ( $this->deletedOnly ) { $queryInfo['conds'][] = 'rev_deleted != 0'; } if ( $this->topOnly ) { $queryInfo['conds'][] = 'rev_id = page_latest'; } if ( $this->newOnly ) { $queryInfo['conds'][] = 'rev_parent_id = 0'; } if ( $this->hideMinor ) { $queryInfo['conds'][] = 'rev_minor_edit = 0'; } $user = $this->getUser(); $queryInfo['conds'] = array_merge( $queryInfo['conds'], $this->getNamespaceCond() ); // Paranoia: avoid brute force searches (T19342) if ( !$user->isAllowed( 'deletedhistory' ) ) { $queryInfo['conds'][] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0'; } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) { $queryInfo['conds'][] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) . ' != ' . Revision::SUPPRESSED_USER; } // For IPv6, we use ipc_rev_timestamp on ip_changes as the index field, // which will be referenced when parsing the results of a query. if ( self::isQueryableRange( $this->target ) ) { $queryInfo['fields'][] = 'ipc_rev_timestamp'; } ChangeTags::modifyDisplayQuery( $queryInfo['tables'], $queryInfo['fields'], $queryInfo['conds'], $queryInfo['join_conds'], $queryInfo['options'], $this->tagFilter ); // Avoid PHP 7.1 warning from passing $this by reference $pager = $this; Hooks::run( 'ContribsPager::getQueryInfo', [ &$pager, &$queryInfo ] ); return $queryInfo; } function getNamespaceCond() { if ( $this->namespace !== '' ) { $selectedNS = $this->mDb->addQuotes( $this->namespace ); $eq_op = $this->nsInvert ? '!=' : '='; $bool_op = $this->nsInvert ? 'AND' : 'OR'; if ( !$this->associated ) { return [ "page_namespace $eq_op $selectedNS" ]; } $associatedNS = $this->mDb->addQuotes( MWNamespace::getAssociated( $this->namespace ) ); return [ "page_namespace $eq_op $selectedNS " . $bool_op . " page_namespace $eq_op $associatedNS" ]; } return []; } /** * Get SQL conditions for an IP range, if applicable * @param IDatabase $db * @param string $ip The IP address or CIDR * @return string|false SQL for valid IP ranges, false if invalid */ private function getIpRangeConds( $db, $ip ) { // First make sure it is a valid range and they are not outside the CIDR limit if ( !$this->isQueryableRange( $ip ) ) { return false; } list( $start, $end ) = IP::parseRange( $ip ); return 'ipc_hex BETWEEN ' . $db->addQuotes( $start ) . ' AND ' . $db->addQuotes( $end ); } /** * Is the given IP a range and within the CIDR limit? * * @param string $ipRange * @return bool True if it is valid * @since 1.30 */ public function isQueryableRange( $ipRange ) { $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' ); $bits = IP::parseCIDR( $ipRange )[1]; if ( ( $bits === false ) || ( IP::isIPv4( $ipRange ) && $bits < $limits['IPv4'] ) || ( IP::isIPv6( $ipRange ) && $bits < $limits['IPv6'] ) ) { return false; } return true; } /** * @return string */ public function getIndexField() { // Note this is run via parent::__construct() *before* $this->target is set! return 'rev_timestamp'; } /** * @return false|string */ public function getTagFilter() { return $this->tagFilter; } /** * @return string */ public function getContribs() { return $this->contribs; } /** * @return string */ public function getTarget() { return $this->target; } /** * @return bool */ public function isNewOnly() { return $this->newOnly; } /** * @return int|string */ public function getNamespace() { return $this->namespace; } /** * @return string[] */ protected function getExtraSortFields() { // Note this is run via parent::__construct() *before* $this->target is set! return [ 'rev_id' ]; } protected function doBatchLookups() { # Do a link batch query $this->mResult->seek( 0 ); $parentRevIds = []; $this->mParentLens = []; $batch = new LinkBatch(); $isIpRange = $this->isQueryableRange( $this->target ); # Give some pointers to make (last) links foreach ( $this->mResult as $row ) { if ( isset( $row->rev_parent_id ) && $row->rev_parent_id ) { $parentRevIds[] = $row->rev_parent_id; } if ( isset( $row->rev_id ) ) { $this->mParentLens[$row->rev_id] = $row->rev_len; if ( $this->contribs === 'newbie' ) { // multiple users $batch->add( NS_USER, $row->user_name ); $batch->add( NS_USER_TALK, $row->user_name ); } elseif ( $isIpRange ) { // If this is an IP range, batch the IP's talk page $batch->add( NS_USER_TALK, $row->rev_user_text ); } $batch->add( $row->page_namespace, $row->page_title ); } } # Fetch rev_len for revisions not already scanned above $this->mParentLens += Revision::getParentLengths( $this->mDbSecondary, array_diff( $parentRevIds, array_keys( $this->mParentLens ) ) ); $batch->execute(); $this->mResult->seek( 0 ); } /** * @return string */ protected function getStartBody() { return "