MediaWiki  master
ContributionsLookup.php
Go to the documentation of this file.
1 <?php
2 
4 
5 use ContribsPager;
6 use FauxRequest;
9 use User;
10 
15 
19  private $revisionStore;
20 
27  $this->revisionStore = $revisionStore;
28  }
29 
41  private function getPagerParams( int $limit, string $segment ) {
42  $dir = 'next';
43  $seg = explode( '|', $segment, 2 );
44  if ( count( $seg ) > 1 ) {
45  if ( $seg[0] === 'after' ) {
46  $dir = 'prev';
47  $segment = $seg[1];
48  } elseif ( $seg[0] == 'before' ) {
49  $dir = 'next';
50  $segment = $seg[1];
51  } else {
52  $dir = null;
53  $segment = null;
54  }
55  } else {
56  $segment = null;
57  }
58  return [
59  'limit' => $limit,
60  'offset' => $segment,
61  'dir' => $dir
62  ];
63  }
64 
75  public function getContributions(
76  UserIdentity $target,
77  int $limit,
78  User $performer,
79  string $segment = '',
80  string $tag = null
82  $context = new RequestContext();
83  $context->setUser( $performer );
84 
85  $paramArr = $this->getPagerParams( $limit, $segment );
86  $context->setRequest( new FauxRequest( $paramArr ) );
87 
88  // TODO: explore moving this to factory method for testing
89  $pager = new ContribsPager( $context, [
90  'target' => $target->getName(),
91  'tagfilter' => $tag,
92  'revisionsOnly' => true
93  ] );
94  $revisions = [];
95  $tags = [];
96  $count = 0;
97  if ( $pager->getNumRows() > 0 ) {
98  foreach ( $pager->mResult as $row ) {
99  // We retrieve and ignore one extra record to see if we are on the oldest segment.
100  if ( ++$count > $limit ) {
101  break;
102  }
103 
104  // TODO: pre-load title batch?
105  $revision = $this->revisionStore->newRevisionFromRow( $row, 0 );
106  $revisions[] = $revision;
107  $tags[ $row->rev_id ] =
108  $row->ts_tags ? explode( ',', $row->ts_tags ) : [];
109  }
110  }
111 
112  $deltas = $this->getContributionDeltas( $revisions );
113 
114  $flags = [
115  'newest' => $pager->mIsFirst,
116  'oldest' => $pager->mIsLast,
117  ];
118 
119  // TODO: Make me an option in IndexPager
120  $pager->mIsFirst = false; // XXX: nasty...
121  $pagingQueries = $pager->getPagingQueries();
122 
123  $prev = $pagingQueries['prev']['offset'] ?? null;
124  $next = $pagingQueries['next']['offset'] ?? null;
125 
126  $after = $prev ? 'after|' . $prev : null; // later in time
127  $before = $next ? 'before|' . $next : null; // earlier in time
128 
129  // TODO: Possibly return public $pager properties to segment for populating URLS ($mIsFirst, $mIsLast)
130  // HACK: Force result set order to be descending. Sorting logic in ContribsPager::reallyDoQuery is confusing.
131  if ( $paramArr['dir'] === 'prev' ) {
132  $revisions = array_reverse( $revisions );
133  }
134  return new ContributionsSegment( $revisions, $tags, $before, $after, $deltas, $flags );
135  }
136 
144  private function getContributionDeltas( $revisions ) {
145  // SpecialContributions uses the size of the revision if the parent revision is unknown. Cases include:
146  // - revision has been deleted
147  // - parent rev id has not been populated (this is the case for very old revisions)
148  $parentIds = [];
149  foreach ( $revisions as $revision ) {
150  $revId = $revision->getId();
151  $parentIds[$revId] = $revision->getParentId();
152  }
153  $parentSizes = $this->revisionStore->getRevisionSizes( $parentIds );
154  $deltas = [];
155  foreach ( $revisions as $revision ) {
156  $parentId = $revision->getParentId();
157  if ( $parentId === 0 ) { // first revision on a page
158  $delta = $revision->getSize();
159  } elseif ( !isset( $parentSizes[$parentId] ) ) { // parent revision is either deleted or untracked
160  $delta = null;
161  } else {
162  $delta = $revision->getSize() - $parentSizes[$parentId];
163  }
164  $deltas[ $revision->getId() ] = $delta;
165  }
166  return $deltas;
167  }
168 
178  public function getContributionCount( UserIdentity $user, User $performer, $tag = null ): int {
179  $context = new RequestContext();
180  $context->setUser( $performer );
181  $context->setRequest( new FauxRequest( [] ) );
182 
183  // TODO: explore moving this to factory method for testing
184  $pager = new ContribsPager( $context, [
185  'target' => $user->getName(),
186  'tagfilter' => $tag,
187  ] );
188 
189  $query = $pager->getQueryInfo();
190 
191  $count = $pager->mDb->selectField(
192  $query['tables'],
193  'COUNT(*)',
194  $query['conds'],
195  __METHOD__,
196  [],
197  $query['join_conds']
198  );
199 
200  return (int)$count;
201  }
202 }
Revision\ContributionsLookup\getPagerParams
getPagerParams(int $limit, string $segment)
Constructs fake query parameters to be passed to ContribsPager.
Definition: ContributionsLookup.php:41
Revision\ContributionsLookup\__construct
__construct(RevisionStore $revisionStore)
ContributionsLookup constructor.
Definition: ContributionsLookup.php:26
FauxRequest
WebRequest clone which takes values from a provided array.
Definition: FauxRequest.php:35
Revision\ContributionsLookup\getContributions
getContributions(UserIdentity $target, int $limit, User $performer, string $segment='', string $tag=null)
Definition: ContributionsLookup.php:75
Revision\ContributionsLookup\$revisionStore
RevisionStore $revisionStore
Definition: ContributionsLookup.php:19
Revision\RevisionStore
Service for looking up page revisions.
Definition: RevisionStore.php:81
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:32
MediaWiki\Revision
Definition: ContributionsLookup.php:3
Revision\ContributionsSegment
@newable
Definition: ContributionsSegment.php:9
RequestContext
Group all the pieces relevant to the context of a request into one instance @newable.
Definition: RequestContext.php:38
Revision\ContributionsLookup\getContributionDeltas
getContributionDeltas( $revisions)
Gets size deltas of a revision and its parent revision.
Definition: ContributionsLookup.php:144
ContribsPager
Definition: ContribsPager.php:34
Revision\ContributionsLookup
Definition: ContributionsLookup.php:14
MediaWiki\$context
IContextSource $context
Definition: MediaWiki.php:40
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
Revision\ContributionsLookup\getContributionCount
getContributionCount(UserIdentity $user, User $performer, $tag=null)
Returns the number of edits by the given user.
Definition: ContributionsLookup.php:178