messages[$msg] = $this->msg( $msg )->escaped(); } $this->target = isset( $options['target'] ) ? $options['target'] : ''; $this->contribs = isset( $options['contribs'] ) ? $options['contribs'] : 'users'; $this->namespace = isset( $options['namespace'] ) ? $options['namespace'] : ''; $this->tagFilter = isset( $options['tagfilter'] ) ? $options['tagfilter'] : false; $this->nsInvert = isset( $options['nsInvert'] ) ? $options['nsInvert'] : false; $this->associated = isset( $options['associated'] ) ? $options['associated'] : false; $this->deletedOnly = !empty( $options['deletedOnly'] ); $this->topOnly = !empty( $options['topOnly'] ); $this->newOnly = !empty( $options['newOnly'] ); $this->hideMinor = !empty( $options['hideMinor'] ); $year = isset( $options['year'] ) ? $options['year'] : false; $month = isset( $options['month'] ) ? $options['month'] : false; $this->getDateCond( $year, $month ); // 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' ); } function getDefaultQuery() { $query = parent::getDefaultQuery(); $query['target'] = $this->target; return $query; } /** * 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 $descending Query direction, false for ascending, true for descending * @return ResultWrapper */ function reallyDoQuery( $offset, $limit, $descending ) { list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo( $offset, $limit, $descending ); /* * 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, $descending ] ); $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 ( $descending ) { 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() { list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond(); $user = $this->getUser(); $conds = array_merge( $userCond, $this->getNamespaceCond() ); // Paranoia: avoid brute force searches (T19342) if ( !$user->isAllowed( 'deletedhistory' ) ) { $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0'; } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) { $conds[] = $this->mDb->bitAnd( 'rev_deleted', Revision::SUPPRESSED_USER ) . ' != ' . Revision::SUPPRESSED_USER; } # Don't include orphaned revisions $join_cond['page'] = Revision::pageJoinCond(); # Get the current user name for accounts $join_cond['user'] = Revision::userJoinCond(); $options = []; if ( $index ) { $options['USE INDEX'] = [ 'revision' => $index ]; } $queryInfo = [ 'tables' => $tables, 'fields' => array_merge( Revision::selectFields(), Revision::selectUserFields(), [ 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'page_is_redirect', 'page_len' ] ), 'conds' => $conds, 'options' => $options, 'join_conds' => $join_cond ]; 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 getUserCond() { $condition = []; $join_conds = []; $tables = [ 'revision', 'page', 'user' ]; $index = false; if ( $this->contribs == 'newbie' ) { $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ ); $condition[] = '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 ) ) { $tables[] = 'user_groups'; $condition[] = 'ug_group IS NULL'; $join_conds['user_groups'] = [ 'LEFT JOIN', [ 'ug_user = rev_user', 'ug_group' => $groupsWithBotPermission, $this->getConfig()->get( 'DisableUserGroupExpiry' ) ? '1' : '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. $condition[] = 'rev_timestamp > ' . $this->mDb->addQuotes( $this->mDb->timestamp( wfTimestamp() - 30 * 24 * 60 * 60 ) ); } else { $uid = User::idFromName( $this->target ); if ( $uid ) { $condition['rev_user'] = $uid; $index = 'user_timestamp'; } else { $condition['rev_user_text'] = $this->target; $index = 'usertext_timestamp'; } } if ( $this->deletedOnly ) { $condition[] = 'rev_deleted != 0'; } if ( $this->topOnly ) { $condition[] = 'rev_id = page_latest'; } if ( $this->newOnly ) { $condition[] = 'rev_parent_id = 0'; } if ( $this->hideMinor ) { $condition[] = 'rev_minor_edit = 0'; } return [ $tables, $index, $condition, $join_conds ]; } 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 []; } function getIndexField() { return 'rev_timestamp'; } function doBatchLookups() { # Do a link batch query $this->mResult->seek( 0 ); $parentRevIds = []; $this->mParentLens = []; $batch = new LinkBatch(); # 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 ); } $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 */ function getStartBody() { return "