Merge "logging: Deprecate LogEventsList::$mDefaultQuery member"
[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 $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
587
588 $page = null;
589 // Create a title for the revision if possible
590 // Rows from the hook may not include title information
591 if ( isset( $row->page_namespace ) && isset( $row->page_title ) ) {
592 $page = Title::newFromRow( $row );
593 }
594 $rev = $this->tryToCreateValidRevision( $row, $page );
595 if ( $rev ) {
596 $attribs['data-mw-revid'] = $rev->getId();
597
598 $link = $linkRenderer->makeLink(
599 $page,
600 $page->getPrefixedText(),
601 [ 'class' => 'mw-contributions-title' ],
602 $page->isRedirect() ? [ 'redirect' => 'no' ] : []
603 );
604 # Mark current revisions
605 $topmarktext = '';
606 $user = $this->getUser();
607
608 if ( $row->rev_id === $row->page_latest ) {
609 $topmarktext .= '<span class="mw-uctop">' . $this->messages['uctop'] . '</span>';
610 $classes[] = 'mw-contributions-current';
611 # Add rollback link
612 if ( !$row->page_is_new &&
613 $permissionManager->quickUserCan( 'rollback', $user, $page ) &&
614 $permissionManager->quickUserCan( 'edit', $user, $page )
615 ) {
616 $this->preventClickjacking();
617 $topmarktext .= ' ' . Linker::generateRollback( $rev, $this->getContext(),
618 [ 'noBrackets' ] );
619 }
620 }
621 # Is there a visible previous revision?
622 if ( $rev->userCan( RevisionRecord::DELETED_TEXT, $user ) && $rev->getParentId() !== 0 ) {
623 $difftext = $linkRenderer->makeKnownLink(
624 $page,
625 new HtmlArmor( $this->messages['diff'] ),
626 [ 'class' => 'mw-changeslist-diff' ],
627 [
628 'diff' => 'prev',
629 'oldid' => $row->rev_id
630 ]
631 );
632 } else {
633 $difftext = $this->messages['diff'];
634 }
635 $histlink = $linkRenderer->makeKnownLink(
636 $page,
637 new HtmlArmor( $this->messages['hist'] ),
638 [ 'class' => 'mw-changeslist-history' ],
639 [ 'action' => 'history' ]
640 );
641
642 if ( $row->rev_parent_id === null ) {
643 // For some reason rev_parent_id isn't populated for this row.
644 // Its rumoured this is true on wikipedia for some revisions (T36922).
645 // Next best thing is to have the total number of bytes.
646 $chardiff = ' <span class="mw-changeslist-separator"></span> ';
647 $chardiff .= Linker::formatRevisionSize( $row->rev_len );
648 $chardiff .= ' <span class="mw-changeslist-separator"></span> ';
649 } else {
650 $parentLen = 0;
651 if ( isset( $this->mParentLens[$row->rev_parent_id] ) ) {
652 $parentLen = $this->mParentLens[$row->rev_parent_id];
653 }
654
655 $chardiff = ' <span class="mw-changeslist-separator"></span> ';
656 $chardiff .= ChangesList::showCharacterDifference(
657 $parentLen,
658 $row->rev_len,
659 $this->getContext()
660 );
661 $chardiff .= ' <span class="mw-changeslist-separator"></span> ';
662 }
663
664 $lang = $this->getLanguage();
665 $comment = $lang->getDirMark() . Linker::revComment( $rev, false, true, false );
666 $d = ChangesList::revDateLink( $rev, $user, $lang, $page );
667
668 # When querying for an IP range, we want to always show user and user talk links.
669 $userlink = '';
670 if ( $this->isQueryableRange( $this->target ) ) {
671 $userlink = ' <span class="mw-changeslist-separator"></span> '
672 . $lang->getDirMark()
673 . Linker::userLink( $rev->getUser(), $rev->getUserText() );
674 $userlink .= ' ' . $this->msg( 'parentheses' )->rawParams(
675 Linker::userTalkLink( $rev->getUser(), $rev->getUserText() ) )->escaped() . ' ';
676 }
677
678 $flags = [];
679 if ( $rev->getParentId() === 0 ) {
680 $flags[] = ChangesList::flag( 'newpage' );
681 }
682
683 if ( $rev->isMinor() ) {
684 $flags[] = ChangesList::flag( 'minor' );
685 }
686
687 $del = Linker::getRevDeleteLink( $user, $rev, $page );
688 if ( $del !== '' ) {
689 $del .= ' ';
690 }
691
692 // While it might be tempting to use a list here
693 // this would result in clutter and slows down navigating the content
694 // in assistive technology.
695 // See https://phabricator.wikimedia.org/T205581#4734812
696 $diffHistLinks = Html::rawElement( 'span',
697 [ 'class' => 'mw-changeslist-links' ],
698 // The spans are needed to ensure the dividing '|' elements are not
699 // themselves styled as links.
700 Html::rawElement( 'span', [], $difftext ) .
701 ' ' . // Space needed for separating two words.
702 Html::rawElement( 'span', [], $histlink )
703 );
704
705 # Tags, if any.
706 list( $tagSummary, $newClasses ) = ChangeTags::formatSummaryRow(
707 $row->ts_tags,
708 'contributions',
709 $this->getContext()
710 );
711 $classes = array_merge( $classes, $newClasses );
712
713 Hooks::run( 'SpecialContributions::formatRow::flags', [ $this->getContext(), $row, &$flags ] );
714
715 $templateParams = [
716 'del' => $del,
717 'timestamp' => $d,
718 'diffHistLinks' => $diffHistLinks,
719 'charDifference' => $chardiff,
720 'flags' => $flags,
721 'articleLink' => $link,
722 'userlink' => $userlink,
723 'logText' => $comment,
724 'topmarktext' => $topmarktext,
725 'tagSummary' => $tagSummary,
726 ];
727
728 # Denote if username is redacted for this edit
729 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
730 $templateParams['rev-deleted-user-contribs'] =
731 $this->msg( 'rev-deleted-user-contribs' )->escaped();
732 }
733
734 $ret = $this->templateParser->processTemplate(
735 'SpecialContributionsLine',
736 $templateParams
737 );
738 }
739
740 // Let extensions add data
741 Hooks::run( 'ContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] );
742 $attribs = array_filter( $attribs,
743 [ Sanitizer::class, 'isReservedDataAttribute' ],
744 ARRAY_FILTER_USE_KEY
745 );
746
747 // TODO: Handle exceptions in the catch block above. Do any extensions rely on
748 // receiving empty rows?
749
750 if ( $classes === [] && $attribs === [] && $ret === '' ) {
751 wfDebug( "Dropping Special:Contribution row that could not be formatted\n" );
752 return "<!-- Could not format Special:Contribution row. -->\n";
753 }
754 $attribs['class'] = $classes;
755
756 // FIXME: The signature of the ContributionsLineEnding hook makes it
757 // very awkward to move this LI wrapper into the template.
758 return Html::rawElement( 'li', $attribs, $ret ) . "\n";
759 }
760
761 /**
762 * Overwrite Pager function and return a helpful comment
763 * @return string
764 */
765 function getSqlComment() {
766 if ( $this->namespace || $this->deletedOnly ) {
767 // potentially slow, see CR r58153
768 return 'contributions page filtered for namespace or RevisionDeleted edits';
769 } else {
770 return 'contributions page unfiltered';
771 }
772 }
773
774 protected function preventClickjacking() {
775 $this->preventClickjacking = true;
776 }
777
778 /**
779 * @return bool
780 */
781 public function getPreventClickjacking() {
782 return $this->preventClickjacking;
783 }
784
785 /**
786 * Set up date filter options, given request data.
787 *
788 * @param array $opts Options array
789 * @return array Options array with processed start and end date filter options
790 */
791 public static function processDateFilter( array $opts ) {
792 $start = $opts['start'] ?? '';
793 $end = $opts['end'] ?? '';
794 $year = $opts['year'] ?? '';
795 $month = $opts['month'] ?? '';
796
797 if ( $start !== '' && $end !== '' && $start > $end ) {
798 $temp = $start;
799 $start = $end;
800 $end = $temp;
801 }
802
803 // If year/month legacy filtering options are set, convert them to display the new stamp
804 if ( $year !== '' || $month !== '' ) {
805 // Reuse getDateCond logic, but subtract a day because
806 // the endpoints of our date range appear inclusive
807 // but the internal end offsets are always exclusive
808 $legacyTimestamp = ReverseChronologicalPager::getOffsetDate( $year, $month );
809 $legacyDateTime = new DateTime( $legacyTimestamp->getTimestamp( TS_ISO_8601 ) );
810 $legacyDateTime = $legacyDateTime->modify( '-1 day' );
811
812 // Clear the new timestamp range options if used and
813 // replace with the converted legacy timestamp
814 $start = '';
815 $end = $legacyDateTime->format( 'Y-m-d' );
816 }
817
818 $opts['start'] = $start;
819 $opts['end'] = $end;
820
821 return $opts;
822 }
823 }