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.
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.
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
25 use MediaWiki\MediaWikiServices
;
26 use MediaWiki\Storage\RevisionRecord
;
27 use Wikimedia\Rdbms\IDatabase
;
28 use Wikimedia\Rdbms\IResultWrapper
;
29 use Wikimedia\Rdbms\FakeResultWrapper
;
31 class DeletedContribsPager
extends IndexPager
{
34 * @var bool Default direction for pager
36 public $mDefaultDirection = IndexPager
::DIR_DESCENDING
;
39 * @var string[] Local cache for escaped messages
44 * @var string User name, or a string describing an IP address range
49 * @var string|int A single namespace number, or an empty string for all namespaces
51 public $namespace = '';
59 * @var string Navigation bar with paging links.
61 protected $mNavigationBar;
63 public function __construct( IContextSource
$context, $target, $namespace = false ) {
64 parent
::__construct( $context );
65 $msgs = [ 'deletionlog', 'undeleteviewlink', 'diff' ];
66 foreach ( $msgs as $msg ) {
67 $this->messages
[$msg] = $this->msg( $msg )->text();
69 $this->target
= $target;
70 $this->namespace = $namespace;
71 $this->mDb
= wfGetDB( DB_REPLICA
, 'contributions' );
74 function getDefaultQuery() {
75 $query = parent
::getDefaultQuery();
76 $query['target'] = $this->target
;
81 function getQueryInfo() {
83 // ->getJoin() below takes care of any joins needed
84 ActorMigration
::newMigration()->getWhere(
85 wfGetDB( DB_REPLICA
), 'ar_user', User
::newFromName( $this->target
, false ), false
88 $conds = array_merge( $userCond, $this->getNamespaceCond() );
89 $user = $this->getUser();
90 // Paranoia: avoid brute force searches (T19792)
91 if ( !$user->isAllowed( 'deletedhistory' ) ) {
92 $conds[] = $this->mDb
->bitAnd( 'ar_deleted', RevisionRecord
::DELETED_USER
) . ' = 0';
93 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
94 $conds[] = $this->mDb
->bitAnd( 'ar_deleted', RevisionRecord
::SUPPRESSED_USER
) .
95 ' != ' . RevisionRecord
::SUPPRESSED_USER
;
98 $commentQuery = CommentStore
::getStore()->getJoin( 'ar_comment' );
99 $actorQuery = ActorMigration
::newMigration()->getJoin( 'ar_user' );
102 'tables' => [ 'archive' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
104 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp',
105 'ar_minor_edit', 'ar_deleted'
106 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
109 'join_conds' => $commentQuery['joins'] +
$actorQuery['joins'],
114 * This method basically executes the exact same code as the parent class, though with
115 * a hook added, to allow extensions to add additional queries.
117 * @param string $offset Index offset, inclusive
118 * @param int $limit Exact query limit
119 * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING
120 * @return IResultWrapper
122 function reallyDoQuery( $offset, $limit, $order ) {
123 $data = [ parent
::reallyDoQuery( $offset, $limit, $order ) ];
125 // This hook will allow extensions to add in additional queries, nearly
126 // identical to ContribsPager::reallyDoQuery.
128 'DeletedContribsPager::reallyDoQuery',
129 [ &$data, $this, $offset, $limit, $order ]
134 // loop all results and collect them in an array
135 foreach ( $data as $query ) {
136 foreach ( $query as $i => $row ) {
137 // use index column as key, allowing us to easily sort in PHP
138 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
143 if ( $order === self
::QUERY_ASCENDING
) {
150 $result = array_slice( $result, 0, $limit );
152 // get rid of array keys
153 $result = array_values( $result );
155 return new FakeResultWrapper( $result );
158 function getIndexField() {
159 return 'ar_timestamp';
165 public function getTarget() {
166 return $this->target
;
172 public function getNamespace() {
173 return $this->namespace;
176 protected function getStartBody() {
180 protected function getEndBody() {
184 function getNavigationBar() {
185 if ( isset( $this->mNavigationBar
) ) {
186 return $this->mNavigationBar
;
190 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit
)->escaped(),
191 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit
)->escaped(),
192 'first' => $this->msg( 'histlast' )->escaped(),
193 'last' => $this->msg( 'histfirst' )->escaped()
196 $pagingLinks = $this->getPagingLinks( $linkTexts );
197 $limitLinks = $this->getLimitLinks();
198 $lang = $this->getLanguage();
199 $limits = $lang->pipeList( $limitLinks );
201 $firstLast = $lang->pipeList( [ $pagingLinks['first'], $pagingLinks['last'] ] );
202 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
203 $prevNext = $this->msg( 'viewprevnext' )
205 $pagingLinks['prev'],
206 $pagingLinks['next'],
209 $separator = $this->msg( 'word-separator' )->escaped();
210 $this->mNavigationBar
= $firstLast . $separator . $prevNext;
212 return $this->mNavigationBar
;
215 function getNamespaceCond() {
216 if ( $this->namespace !== '' ) {
217 return [ 'ar_namespace' => (int)$this->namespace ];
224 * Generates each row in the contributions list.
226 * @todo This would probably look a lot nicer in a table.
227 * @param stdClass $row
230 function formatRow( $row ) {
236 * There may be more than just revision rows. To make sure that we'll only be processing
237 * revisions here, let's _try_ to build a revision out of our row (without displaying
238 * notices though) and then trying to grab data from the built object. If we succeed,
239 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
240 * to extensions to subscribe to the hook to parse the row.
242 Wikimedia\
suppressWarnings();
244 $rev = Revision
::newFromArchiveRow( $row );
245 $validRevision = (bool)$rev->getId();
246 } catch ( Exception
$e ) {
247 $validRevision = false;
249 Wikimedia\restoreWarnings
();
251 if ( $validRevision ) {
252 $attribs['data-mw-revid'] = $rev->getId();
253 $ret = $this->formatRevisionRow( $row );
256 // Let extensions add data
257 Hooks
::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] );
258 $attribs = array_filter( $attribs,
259 [ Sanitizer
::class, 'isReservedDataAttribute' ],
263 if ( $classes === [] && $attribs === [] && $ret === '' ) {
264 wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
265 $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
267 $attribs['class'] = $classes;
268 $ret = Html
::rawElement( 'li', $attribs, $ret ) . "\n";
275 * Generates each row in the contributions list for archive entries.
277 * Contributions which are marked "top" are currently on top of the history.
278 * For these contributions, a [rollback] link is shown for users with sysop
279 * privileges. The rollback link restores the most recent version that was not
280 * written by the target user.
282 * @todo This would probably look a lot nicer in a table.
283 * @param stdClass $row
286 function formatRevisionRow( $row ) {
287 $page = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
289 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
291 $rev = new Revision( [
293 'id' => $row->ar_rev_id
,
294 'comment' => CommentStore
::getStore()->getComment( 'ar_comment', $row )->text
,
295 'user' => $row->ar_user
,
296 'user_text' => $row->ar_user_text
,
297 'actor' => $row->ar_actor ??
null,
298 'timestamp' => $row->ar_timestamp
,
299 'minor_edit' => $row->ar_minor_edit
,
300 'deleted' => $row->ar_deleted
,
303 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
305 $logs = SpecialPage
::getTitleFor( 'Log' );
306 $dellog = $linkRenderer->makeKnownLink(
308 $this->messages
['deletionlog'],
312 'page' => $page->getPrefixedText()
316 $reviewlink = $linkRenderer->makeKnownLink(
317 SpecialPage
::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
318 $this->messages
['undeleteviewlink']
321 $user = $this->getUser();
323 if ( $user->isAllowed( 'deletedtext' ) ) {
324 $last = $linkRenderer->makeKnownLink(
326 $this->messages
['diff'],
329 'target' => $page->getPrefixedText(),
330 'timestamp' => $rev->getTimestamp(),
335 $last = htmlspecialchars( $this->messages
['diff'] );
338 $comment = Linker
::revComment( $rev );
339 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
341 if ( !$user->isAllowed( 'undelete' ) ||
!$rev->userCan( RevisionRecord
::DELETED_TEXT
, $user ) ) {
342 $link = htmlspecialchars( $date ); // unusable link
344 $link = $linkRenderer->makeKnownLink(
347 [ 'class' => 'mw-changeslist-date' ],
349 'target' => $page->getPrefixedText(),
350 'timestamp' => $rev->getTimestamp()
354 // Style deleted items
355 if ( $rev->isDeleted( RevisionRecord
::DELETED_TEXT
) ) {
356 $link = '<span class="history-deleted">' . $link . '</span>';
359 $pagelink = $linkRenderer->makeLink(
362 [ 'class' => 'mw-changeslist-title' ]
365 if ( $rev->isMinor() ) {
366 $mflag = ChangesList
::flag( 'minor' );
371 // Revision delete link
372 $del = Linker
::getRevDeleteLink( $user, $rev, $page );
377 $tools = Html
::rawElement(
379 [ 'class' => 'mw-deletedcontribs-tools' ],
380 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
381 [ $last, $dellog, $reviewlink ] ) )->escaped()
384 $separator = '<span class="mw-changeslist-separator">. .</span>';
385 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
387 # Denote if username is redacted for this edit
388 if ( $rev->isDeleted( RevisionRecord
::DELETED_USER
) ) {
389 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
396 * Get the Database object in use
400 public function getDatabase() {