ContribsPage: bring back getContribs() method
[lhc/web/wiklou.git] / includes / specials / pagers / ContribsPager.php
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @ingroup Pager
20 */
21
22 /**
23 * Pager for Special:Contributions
24 * @ingroup Pager
25 */
26 use MediaWiki\MediaWikiServices;
27 use MediaWiki\Storage\RevisionRecord;
28 use Wikimedia\Rdbms\IResultWrapper;
29 use Wikimedia\Rdbms\FakeResultWrapper;
30 use Wikimedia\Rdbms\IDatabase;
31
32 class ContribsPager extends RangeChronologicalPager {
33
34 /**
35 * @var string[] Local cache for escaped messages
36 */
37 private $messages;
38
39 /**
40 * @var string User name, or a string describing an IP address range
41 */
42 private $target;
43
44 /**
45 * @var string|int A single namespace number, or an empty string for all namespaces
46 */
47 private $namespace = '';
48
49 /**
50 * @var string|false Name of tag to filter, or false to ignore tags
51 */
52 private $tagFilter;
53
54 /**
55 * @var bool Set to true to invert the namespace selection
56 */
57 private $nsInvert;
58
59 /**
60 * @var bool Set to true to show both the subject and talk namespace, no matter which got
61 * selected
62 */
63 private $associated;
64
65 /**
66 * @var bool Set to true to show only deleted revisions
67 */
68 private $deletedOnly;
69
70 /**
71 * @var bool Set to true to show only latest (a.k.a. current) revisions
72 */
73 private $topOnly;
74
75 /**
76 * @var bool Set to true to show only new pages
77 */
78 private $newOnly;
79
80 /**
81 * @var bool Set to true to hide edits marked as minor by the user
82 */
83 private $hideMinor;
84
85 private $preventClickjacking = false;
86
87 /** @var IDatabase */
88 private $mDbSecondary;
89
90 /**
91 * @var array
92 */
93 private $mParentLens;
94
95 /**
96 * @var TemplateParser
97 */
98 private $templateParser;
99
100 public function __construct( IContextSource $context, array $options ) {
101 // Set ->target before calling parent::__construct() so
102 // parent can call $this->getIndexField() and get the right result. Set
103 // the rest too just to keep things simple.
104 $this->target = $options['target'] ?? '';
105 $this->namespace = $options['namespace'] ?? '';
106 $this->tagFilter = $options['tagfilter'] ?? false;
107 $this->nsInvert = $options['nsInvert'] ?? false;
108 $this->associated = $options['associated'] ?? false;
109
110 $this->deletedOnly = !empty( $options['deletedOnly'] );
111 $this->topOnly = !empty( $options['topOnly'] );
112 $this->newOnly = !empty( $options['newOnly'] );
113 $this->hideMinor = !empty( $options['hideMinor'] );
114
115 parent::__construct( $context );
116
117 $msgs = [
118 'diff',
119 'hist',
120 'pipe-separator',
121 'uctop'
122 ];
123
124 foreach ( $msgs as $msg ) {
125 $this->messages[$msg] = $this->msg( $msg )->escaped();
126 }
127
128 // Date filtering: use timestamp if available
129 $startTimestamp = '';
130 $endTimestamp = '';
131 if ( $options['start'] ) {
132 $startTimestamp = $options['start'] . ' 00:00:00';
133 }
134 if ( $options['end'] ) {
135 $endTimestamp = $options['end'] . ' 23:59:59';
136 }
137 $this->getDateRangeCond( $startTimestamp, $endTimestamp );
138
139 // Most of this code will use the 'contributions' group DB, which can map to replica DBs
140 // with extra user based indexes or partioning by user. The additional metadata
141 // queries should use a regular replica DB since the lookup pattern is not all by user.
142 $this->mDbSecondary = wfGetDB( DB_REPLICA ); // any random replica DB
143 $this->mDb = wfGetDB( DB_REPLICA, 'contributions' );
144 $this->templateParser = new TemplateParser();
145 }
146
147 function getDefaultQuery() {
148 $query = parent::getDefaultQuery();
149 $query['target'] = $this->target;
150
151 return $query;
152 }
153
154 /**
155 * Wrap the navigation bar in a p element with identifying class.
156 * In future we may want to change the `p` tag to a `div` and upstream
157 * this to the parent class.
158 *
159 * @return string HTML
160 */
161 function getNavigationBar() {
162 return Html::rawElement( 'p', [ 'class' => 'mw-pager-navigation-bar' ],
163 parent::getNavigationBar()
164 );
165 }
166
167 /**
168 * This method basically executes the exact same code as the parent class, though with
169 * a hook added, to allow extensions to add additional queries.
170 *
171 * @param string $offset Index offset, inclusive
172 * @param int $limit Exact query limit
173 * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING
174 * @return IResultWrapper
175 */
176 function reallyDoQuery( $offset, $limit, $order ) {
177 list( $tables, $fields, $conds, $fname, $options, $join_conds ) = $this->buildQueryInfo(
178 $offset,
179 $limit,
180 $order
181 );
182
183 /*
184 * This hook will allow extensions to add in additional queries, so they can get their data
185 * in My Contributions as well. Extensions should append their results to the $data array.
186 *
187 * Extension queries have to implement the navbar requirement as well. They should
188 * - have a column aliased as $pager->getIndexField()
189 * - have LIMIT set
190 * - have a WHERE-clause that compares the $pager->getIndexField()-equivalent column to the offset
191 * - have the ORDER BY specified based upon the details provided by the navbar
192 *
193 * See includes/Pager.php buildQueryInfo() method on how to build LIMIT, WHERE & ORDER BY
194 *
195 * &$data: an array of results of all contribs queries
196 * $pager: the ContribsPager object hooked into
197 * $offset: see phpdoc above
198 * $limit: see phpdoc above
199 * $descending: see phpdoc above
200 */
201 $data = [ $this->mDb->select(
202 $tables, $fields, $conds, $fname, $options, $join_conds
203 ) ];
204 Hooks::run(
205 'ContribsPager::reallyDoQuery',
206 [ &$data, $this, $offset, $limit, $order ]
207 );
208
209 $result = [];
210
211 // loop all results and collect them in an array
212 foreach ( $data as $query ) {
213 foreach ( $query as $i => $row ) {
214 // use index column as key, allowing us to easily sort in PHP
215 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
216 }
217 }
218
219 // sort results
220 if ( $order === self::QUERY_ASCENDING ) {
221 ksort( $result );
222 } else {
223 krsort( $result );
224 }
225
226 // enforce limit
227 $result = array_slice( $result, 0, $limit );
228
229 // get rid of array keys
230 $result = array_values( $result );
231
232 return new FakeResultWrapper( $result );
233 }
234
235 /**
236 * Return the table targeted for ordering and continuation
237 *
238 * See T200259 and T221380.
239 *
240 * @warning Keep this in sync with self::getQueryInfo()!
241 *
242 * @return string
243 */
244 private function getTargetTable() {
245 $user = User::newFromName( $this->target, false );
246 $ipRangeConds = $user->isAnon() ? $this->getIpRangeConds( $this->mDb, $this->target ) : null;
247 if ( $ipRangeConds ) {
248 return 'ip_changes';
249 } else {
250 $conds = ActorMigration::newMigration()->getWhere( $this->mDb, 'rev_user', $user );
251 if ( isset( $conds['orconds']['actor'] ) ) {
252 // @todo: This will need changing when revision_actor_temp goes away
253 return 'revision_actor_temp';
254 }
255 }
256
257 return 'revision';
258 }
259
260 function getQueryInfo() {
261 $revQuery = Revision::getQueryInfo( [ 'page', 'user' ] );
262 $queryInfo = [
263 'tables' => $revQuery['tables'],
264 'fields' => array_merge( $revQuery['fields'], [ 'page_is_new' ] ),
265 'conds' => [],
266 'options' => [],
267 'join_conds' => $revQuery['joins'],
268 ];
269
270 // WARNING: Keep this in sync with getTargetTable()!
271 $user = User::newFromName( $this->target, false );
272 $ipRangeConds = $user->isAnon() ? $this->getIpRangeConds( $this->mDb, $this->target ) : null;
273 if ( $ipRangeConds ) {
274 $queryInfo['tables'][] = 'ip_changes';
275 $queryInfo['join_conds']['ip_changes'] = [
276 'LEFT JOIN', [ 'ipc_rev_id = rev_id' ]
277 ];
278 $queryInfo['conds'][] = $ipRangeConds;
279 } else {
280 // tables and joins are already handled by Revision::getQueryInfo()
281 $conds = ActorMigration::newMigration()->getWhere( $this->mDb, 'rev_user', $user );
282 $queryInfo['conds'][] = $conds['conds'];
283 // Force the appropriate index to avoid bad query plans (T189026)
284 if ( isset( $conds['orconds']['actor'] ) ) {
285 // @todo: This will need changing when revision_actor_temp goes away
286 $queryInfo['options']['USE INDEX']['temp_rev_user'] = 'actor_timestamp';
287 } else {
288 $queryInfo['options']['USE INDEX']['revision'] =
289 isset( $conds['orconds']['userid'] ) ? 'user_timestamp' : 'usertext_timestamp';
290 }
291 }
292
293 if ( $this->deletedOnly ) {
294 $queryInfo['conds'][] = 'rev_deleted != 0';
295 }
296
297 if ( $this->topOnly ) {
298 $queryInfo['conds'][] = 'rev_id = page_latest';
299 }
300
301 if ( $this->newOnly ) {
302 $queryInfo['conds'][] = 'rev_parent_id = 0';
303 }
304
305 if ( $this->hideMinor ) {
306 $queryInfo['conds'][] = 'rev_minor_edit = 0';
307 }
308
309 $user = $this->getUser();
310 $queryInfo['conds'] = array_merge( $queryInfo['conds'], $this->getNamespaceCond() );
311
312 // Paranoia: avoid brute force searches (T19342)
313 if ( !$user->isAllowed( 'deletedhistory' ) ) {
314 $queryInfo['conds'][] = $this->mDb->bitAnd(
315 'rev_deleted', RevisionRecord::DELETED_USER
316 ) . ' = 0';
317 } elseif ( !MediaWikiServices::getInstance()
318 ->getPermissionManager()
319 ->userHasAnyRight( $user, 'suppressrevision', 'viewsuppressed' )
320 ) {
321 $queryInfo['conds'][] = $this->mDb->bitAnd(
322 'rev_deleted', RevisionRecord::SUPPRESSED_USER
323 ) . ' != ' . RevisionRecord::SUPPRESSED_USER;
324 }
325
326 // $this->getIndexField() must be in the result rows, as reallyDoQuery() tries to access it.
327 $indexField = $this->getIndexField();
328 if ( $indexField !== 'rev_timestamp' ) {
329 $queryInfo['fields'][] = $indexField;
330 }
331
332 ChangeTags::modifyDisplayQuery(
333 $queryInfo['tables'],
334 $queryInfo['fields'],
335 $queryInfo['conds'],
336 $queryInfo['join_conds'],
337 $queryInfo['options'],
338 $this->tagFilter
339 );
340
341 // Avoid PHP 7.1 warning from passing $this by reference
342 $pager = $this;
343 Hooks::run( 'ContribsPager::getQueryInfo', [ &$pager, &$queryInfo ] );
344
345 return $queryInfo;
346 }
347
348 function getNamespaceCond() {
349 if ( $this->namespace !== '' ) {
350 $selectedNS = $this->mDb->addQuotes( $this->namespace );
351 $eq_op = $this->nsInvert ? '!=' : '=';
352 $bool_op = $this->nsInvert ? 'AND' : 'OR';
353
354 if ( !$this->associated ) {
355 return [ "page_namespace $eq_op $selectedNS" ];
356 }
357
358 $associatedNS = $this->mDb->addQuotes(
359 MediaWikiServices::getInstance()->getNamespaceInfo()->getAssociated( $this->namespace )
360 );
361
362 return [
363 "page_namespace $eq_op $selectedNS " .
364 $bool_op .
365 " page_namespace $eq_op $associatedNS"
366 ];
367 }
368
369 return [];
370 }
371
372 /**
373 * Get SQL conditions for an IP range, if applicable
374 * @param IDatabase $db
375 * @param string $ip The IP address or CIDR
376 * @return string|false SQL for valid IP ranges, false if invalid
377 */
378 private function getIpRangeConds( $db, $ip ) {
379 // First make sure it is a valid range and they are not outside the CIDR limit
380 if ( !$this->isQueryableRange( $ip ) ) {
381 return false;
382 }
383
384 list( $start, $end ) = IP::parseRange( $ip );
385
386 return 'ipc_hex BETWEEN ' . $db->addQuotes( $start ) . ' AND ' . $db->addQuotes( $end );
387 }
388
389 /**
390 * Is the given IP a range and within the CIDR limit?
391 *
392 * @param string $ipRange
393 * @return bool True if it is valid
394 * @since 1.30
395 */
396 public function isQueryableRange( $ipRange ) {
397 $limits = $this->getConfig()->get( 'RangeContributionsCIDRLimit' );
398
399 $bits = IP::parseCIDR( $ipRange )[1];
400 if (
401 ( $bits === false ) ||
402 ( IP::isIPv4( $ipRange ) && $bits < $limits['IPv4'] ) ||
403 ( IP::isIPv6( $ipRange ) && $bits < $limits['IPv6'] )
404 ) {
405 return false;
406 }
407
408 return true;
409 }
410
411 /**
412 * @return string
413 */
414 public function getIndexField() {
415 // The returned column is used for sorting and continuation, so we need to
416 // make sure to use the right denormalized column depending on which table is
417 // being targeted by the query to avoid bad query plans.
418 // See T200259, T204669, T220991, and T221380.
419 $target = $this->getTargetTable();
420 switch ( $target ) {
421 case 'revision':
422 return 'rev_timestamp';
423 case 'ip_changes':
424 return 'ipc_rev_timestamp';
425 case 'revision_actor_temp':
426 return 'revactor_timestamp';
427 default:
428 wfWarn(
429 __METHOD__ . ": Unknown value '$target' from " . static::class . '::getTargetTable()', 0
430 );
431 return 'rev_timestamp';
432 }
433 }
434
435 /**
436 * @return false|string
437 */
438 public function getTagFilter() {
439 return $this->tagFilter;
440 }
441
442 /**
443 * @deprecated since 1.34, redundant.
444 *
445 * @return string "users"
446 */
447 public function getContribs() {
448 // Brought back for backwards compatibility, see T231540.
449 return 'users';
450 }
451
452 /**
453 * @return string
454 */
455 public function getTarget() {
456 return $this->target;
457 }
458
459 /**
460 * @return bool
461 */
462 public function isNewOnly() {
463 return $this->newOnly;
464 }
465
466 /**
467 * @return int|string
468 */
469 public function getNamespace() {
470 return $this->namespace;
471 }
472
473 /**
474 * @return string[]
475 */
476 protected function getExtraSortFields() {
477 // The returned columns are used for sorting, so we need to make sure
478 // to use the right denormalized column depending on which table is
479 // being targeted by the query to avoid bad query plans.
480 // See T200259, T204669, T220991, and T221380.
481 $target = $this->getTargetTable();
482 switch ( $target ) {
483 case 'revision':
484 return [ 'rev_id' ];
485 case 'ip_changes':
486 return [ 'ipc_rev_id' ];
487 case 'revision_actor_temp':
488 return [ 'revactor_rev' ];
489 default:
490 wfWarn(
491 __METHOD__ . ": Unknown value '$target' from " . static::class . '::getTargetTable()', 0
492 );
493 return [ 'rev_id' ];
494 }
495 }
496
497 protected function doBatchLookups() {
498 # Do a link batch query
499 $this->mResult->seek( 0 );
500 $parentRevIds = [];
501 $this->mParentLens = [];
502 $batch = new LinkBatch();
503 $isIpRange = $this->isQueryableRange( $this->target );
504 # Give some pointers to make (last) links
505 foreach ( $this->mResult as $row ) {
506 if ( isset( $row->rev_parent_id ) && $row->rev_parent_id ) {
507 $parentRevIds[] = $row->rev_parent_id;
508 }
509 if ( isset( $row->rev_id ) ) {
510 $this->mParentLens[$row->rev_id] = $row->rev_len;
511 if ( $isIpRange ) {
512 // If this is an IP range, batch the IP's talk page
513 $batch->add( NS_USER_TALK, $row->rev_user_text );
514 }
515 $batch->add( $row->page_namespace, $row->page_title );
516 }
517 }
518 # Fetch rev_len for revisions not already scanned above
519 $this->mParentLens += Revision::getParentLengths(
520 $this->mDbSecondary,
521 array_diff( $parentRevIds, array_keys( $this->mParentLens ) )
522 );
523 $batch->execute();
524 $this->mResult->seek( 0 );
525 }
526
527 /**
528 * @return string
529 */
530 protected function getStartBody() {
531 return "<ul class=\"mw-contributions-list\">\n";
532 }
533
534 /**
535 * @return string
536 */
537 protected function getEndBody() {
538 return "</ul>\n";
539 }
540
541 /**
542 * Check whether the revision associated is valid for formatting. If has no associated revision
543 * id then null is returned.
544 *
545 * @param object $row
546 * @param Title|null $title
547 * @return Revision|null
548 */
549 public function tryToCreateValidRevision( $row, $title = null ) {
550 /*
551 * There may be more than just revision rows. To make sure that we'll only be processing
552 * revisions here, let's _try_ to build a revision out of our row (without displaying
553 * notices though) and then trying to grab data from the built object. If we succeed,
554 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
555 * to extensions to subscribe to the hook to parse the row.
556 */
557 Wikimedia\suppressWarnings();
558 try {
559 $rev = new Revision( $row, 0, $title );
560 $validRevision = (bool)$rev->getId();
561 } catch ( Exception $e ) {
562 $validRevision = false;
563 }
564 Wikimedia\restoreWarnings();
565 return $validRevision ? $rev : null;
566 }
567
568 /**
569 * Generates each row in the contributions list.
570 *
571 * Contributions which are marked "top" are currently on top of the history.
572 * For these contributions, a [rollback] link is shown for users with roll-
573 * back privileges. The rollback link restores the most recent version that
574 * was not written by the target user.
575 *
576 * @todo This would probably look a lot nicer in a table.
577 * @param object $row
578 * @return string
579 */
580 function formatRow( $row ) {
581 $ret = '';
582 $classes = [];
583 $attribs = [];
584
585 $linkRenderer = $this->getLinkRenderer();
586
587 $page = null;
588 // Create a title for the revision if possible
589 // Rows from the hook may not include title information
590 if ( isset( $row->page_namespace ) && isset( $row->page_title ) ) {
591 $page = Title::newFromRow( $row );
592 }
593 $rev = $this->tryToCreateValidRevision( $row, $page );
594 if ( $rev ) {
595 $attribs['data-mw-revid'] = $rev->getId();
596
597 $link = $linkRenderer->makeLink(
598 $page,
599 $page->getPrefixedText(),
600 [ 'class' => 'mw-contributions-title' ],
601 $page->isRedirect() ? [ 'redirect' => 'no' ] : []
602 );
603 # Mark current revisions
604 $topmarktext = '';
605 $user = $this->getUser();
606
607 if ( $row->rev_id === $row->page_latest ) {
608 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
609 $classes[] = 'mw-contributions-current';
610 # Add rollback link
611 if ( !$row->page_is_new && $page->quickUserCan( 'rollback', $user )
612 && $page->quickUserCan( 'edit', $user )
613 ) {
614 $this->preventClickjacking();
615 $topmarktext .= ' ' . Linker::generateRollback( $rev, $this->getContext(),
616 [ 'noBrackets' ] );
617 }
618 }
619 # Is there a visible previous revision?
620 if ( $rev->userCan( RevisionRecord::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
621 $difftext = $linkRenderer->makeKnownLink(
622 $page,
623 new HtmlArmor( $this->messages['diff'] ),
624 [ 'class' => 'mw-changeslist-diff' ],
625 [
626 'diff' => 'prev',
627 'oldid' => $row->rev_id
628 ]
629 );
630 } else {
631 $difftext = $this->messages['diff'];
632 }
633 $histlink = $linkRenderer->makeKnownLink(
634 $page,
635 new HtmlArmor( $this->messages['hist'] ),
636 [ 'class' => 'mw-changeslist-history' ],
637 [ 'action' => 'history' ]
638 );
639
640 if ( $row->rev_parent_id === null ) {
641 // For some reason rev_parent_id isn't populated for this row.
642 // Its rumoured this is true on wikipedia for some revisions (T36922).
643 // Next best thing is to have the total number of bytes.
644 $chardiff = ' <span class="mw-changeslist-separator"></span> ';
645 $chardiff .= Linker::formatRevisionSize( $row->rev_len );
646 $chardiff .= ' <span class="mw-changeslist-separator"></span> ';
647 } else {
648 $parentLen = 0;
649 if ( isset( $this->mParentLens[$row->rev_parent_id] ) ) {
650 $parentLen = $this->mParentLens[$row->rev_parent_id];
651 }
652
653 $chardiff = ' <span class="mw-changeslist-separator"></span> ';
654 $chardiff .= ChangesList::showCharacterDifference(
655 $parentLen,
656 $row->rev_len,
657 $this->getContext()
658 );
659 $chardiff .= ' <span class="mw-changeslist-separator"></span> ';
660 }
661
662 $lang = $this->getLanguage();
663 $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true, false );
664 $d = ChangesList::revDateLink( $rev, $user, $lang, $page );
665
666 # When querying for an IP range, we want to always show user and user talk links.
667 $userlink = '';
668 if ( $this->isQueryableRange( $this->target ) ) {
669 $userlink = ' <span class="mw-changeslist-separator"></span> '
670 . $lang->getDirMark()
671 . Linker::userLink( $rev->getUser(), $rev->getUserText() );
672 $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
673 Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
674 }
675
676 $flags = [];
677 if ( $rev->getParentId() === 0 ) {
678 $flags[] = ChangesList::flag( 'newpage' );
679 }
680
681 if ( $rev->isMinor() ) {
682 $flags[] = ChangesList::flag( 'minor' );
683 }
684
685 $del = Linker::getRevDeleteLink( $user, $rev, $page );
686 if ( $del !== '' ) {
687 $del .= ' ';
688 }
689
690 // While it might be tempting to use a list here
691 // this would result in clutter and slows down navigating the content
692 // in assistive technology.
693 // See https://phabricator.wikimedia.org/T205581#4734812
694 $diffHistLinks = Html::rawElement( 'span',
695 [ 'class' => 'mw-changeslist-links' ],
696 // The spans are needed to ensure the dividing '|' elements are not
697 // themselves styled as links.
698 Html::rawElement( 'span', [], $difftext ) .
699 ' ' . // Space needed for separating two words.
700 Html::rawElement( 'span', [], $histlink )
701 );
702
703 # Tags, if any.
704 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
705 $row->ts_tags,
706 'contributions',
707 $this->getContext()
708 );
709 $classes = array_merge( $classes, $newClasses );
710
711 Hooks::run( 'SpecialContributions::formatRow::flags', [ $this->getContext(), $row, &$flags ] );
712
713 $templateParams = [
714 'del' => $del,
715 'timestamp' => $d,
716 'diffHistLinks' => $diffHistLinks,
717 'charDifference' => $chardiff,
718 'flags' => $flags,
719 'articleLink' => $link,
720 'userlink' => $userlink,
721 'logText' => $comment,
722 'topmarktext' => $topmarktext,
723 'tagSummary' => $tagSummary,
724 ];
725
726 # Denote if username is redacted for this edit
727 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
728 $templateParams['rev-deleted-user-contribs'] =
729 $this->msg( 'rev-deleted-user-contribs' )->escaped();
730 }
731
732 $ret = $this->templateParser->processTemplate(
733 'SpecialContributionsLine',
734 $templateParams
735 );
736 }
737
738 // Let extensions add data
739 Hooks::run( 'ContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] );
740 $attribs = array_filter( $attribs,
741 [ Sanitizer::class, 'isReservedDataAttribute' ],
742 ARRAY_FILTER_USE_KEY
743 );
744
745 // TODO: Handle exceptions in the catch block above. Do any extensions rely on
746 // receiving empty rows?
747
748 if ( $classes === [] && $attribs === [] && $ret === '' ) {
749 wfDebug( "Dropping Special:Contribution row that could not be formatted\n" );
750 return "<!-- Could not format Special:Contribution row. -->\n";
751 }
752 $attribs['class'] = $classes;
753
754 // FIXME: The signature of the ContributionsLineEnding hook makes it
755 // very awkward to move this LI wrapper into the template.
756 return Html::rawElement( 'li', $attribs, $ret ) . "\n";
757 }
758
759 /**
760 * Overwrite Pager function and return a helpful comment
761 * @return string
762 */
763 function getSqlComment() {
764 if ( $this->namespace || $this->deletedOnly ) {
765 // potentially slow, see CR r58153
766 return 'contributions page filtered for namespace or RevisionDeleted edits';
767 } else {
768 return 'contributions page unfiltered';
769 }
770 }
771
772 protected function preventClickjacking() {
773 $this->preventClickjacking = true;
774 }
775
776 /**
777 * @return bool
778 */
779 public function getPreventClickjacking() {
780 return $this->preventClickjacking;
781 }
782
783 /**
784 * Set up date filter options, given request data.
785 *
786 * @param array $opts Options array
787 * @return array Options array with processed start and end date filter options
788 */
789 public static function processDateFilter( array $opts ) {
790 $start = $opts['start'] ?? '';
791 $end = $opts['end'] ?? '';
792 $year = $opts['year'] ?? '';
793 $month = $opts['month'] ?? '';
794
795 if ( $start !== '' && $end !== '' && $start > $end ) {
796 $temp = $start;
797 $start = $end;
798 $end = $temp;
799 }
800
801 // If year/month legacy filtering options are set, convert them to display the new stamp
802 if ( $year !== '' || $month !== '' ) {
803 // Reuse getDateCond logic, but subtract a day because
804 // the endpoints of our date range appear inclusive
805 // but the internal end offsets are always exclusive
806 $legacyTimestamp = ReverseChronologicalPager::getOffsetDate( $year, $month );
807 $legacyDateTime = new DateTime( $legacyTimestamp->getTimestamp( TS_ISO_8601 ) );
808 $legacyDateTime = $legacyDateTime->modify( '-1 day' );
809
810 // Clear the new timestamp range options if used and
811 // replace with the converted legacy timestamp
812 $start = '';
813 $end = $legacyDateTime->format( 'Y-m-d' );
814 }
815
816 $opts['start'] = $start;
817 $opts['end'] = $end;
818
819 return $opts;
820 }
821 }