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\Linker\LinkRenderer
;
26 use MediaWiki\MediaWikiServices
;
27 use MediaWiki\Revision\RevisionRecord
;
28 use Wikimedia\Rdbms\IDatabase
;
29 use Wikimedia\Rdbms\IResultWrapper
;
30 use Wikimedia\Rdbms\FakeResultWrapper
;
32 class DeletedContribsPager
extends IndexPager
{
35 * @var bool Default direction for pager
37 public $mDefaultDirection = IndexPager
::DIR_DESCENDING
;
40 * @var string[] Local cache for escaped messages
45 * @var string User name, or a string describing an IP address range
50 * @var string|int A single namespace number, or an empty string for all namespaces
52 public $namespace = '';
60 * @var string Navigation bar with paging links.
62 protected $mNavigationBar;
64 public function __construct( IContextSource
$context, $target, $namespace = false,
65 LinkRenderer
$linkRenderer
67 parent
::__construct( $context, $linkRenderer );
68 $msgs = [ 'deletionlog', 'undeleteviewlink', 'diff' ];
69 foreach ( $msgs as $msg ) {
70 $this->messages
[$msg] = $this->msg( $msg )->text();
72 $this->target
= $target;
73 $this->namespace = $namespace;
74 $this->mDb
= wfGetDB( DB_REPLICA
, 'contributions' );
77 function getDefaultQuery() {
78 $query = parent
::getDefaultQuery();
79 $query['target'] = $this->target
;
84 function getQueryInfo() {
86 // ->getJoin() below takes care of any joins needed
87 ActorMigration
::newMigration()->getWhere(
88 wfGetDB( DB_REPLICA
), 'ar_user', User
::newFromName( $this->target
, false ), false
91 $conds = array_merge( $userCond, $this->getNamespaceCond() );
92 $user = $this->getUser();
93 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
94 // Paranoia: avoid brute force searches (T19792)
95 if ( !$permissionManager->userHasRight( $user, 'deletedhistory' ) ) {
96 $conds[] = $this->mDb
->bitAnd( 'ar_deleted', RevisionRecord
::DELETED_USER
) . ' = 0';
97 } elseif ( !$permissionManager->userHasAnyRight( $user, 'suppressrevision', 'viewsuppressed' ) ) {
98 $conds[] = $this->mDb
->bitAnd( 'ar_deleted', RevisionRecord
::SUPPRESSED_USER
) .
99 ' != ' . RevisionRecord
::SUPPRESSED_USER
;
102 $commentQuery = CommentStore
::getStore()->getJoin( 'ar_comment' );
103 $actorQuery = ActorMigration
::newMigration()->getJoin( 'ar_user' );
106 'tables' => [ 'archive' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
108 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp',
109 'ar_minor_edit', 'ar_deleted'
110 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
113 'join_conds' => $commentQuery['joins'] +
$actorQuery['joins'],
118 * This method basically executes the exact same code as the parent class, though with
119 * a hook added, to allow extensions to add additional queries.
121 * @param string $offset Index offset, inclusive
122 * @param int $limit Exact query limit
123 * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING
124 * @return IResultWrapper
126 function reallyDoQuery( $offset, $limit, $order ) {
127 $data = [ parent
::reallyDoQuery( $offset, $limit, $order ) ];
129 // This hook will allow extensions to add in additional queries, nearly
130 // identical to ContribsPager::reallyDoQuery.
132 'DeletedContribsPager::reallyDoQuery',
133 [ &$data, $this, $offset, $limit, $order ]
138 // loop all results and collect them in an array
139 foreach ( $data as $query ) {
140 foreach ( $query as $i => $row ) {
141 // use index column as key, allowing us to easily sort in PHP
142 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
147 if ( $order === self
::QUERY_ASCENDING
) {
154 $result = array_slice( $result, 0, $limit );
156 // get rid of array keys
157 $result = array_values( $result );
159 return new FakeResultWrapper( $result );
162 function getIndexField() {
163 return 'ar_timestamp';
169 public function getTarget() {
170 return $this->target
;
176 public function getNamespace() {
177 return $this->namespace;
180 protected function getStartBody() {
184 protected function getEndBody() {
188 function getNavigationBar() {
189 if ( isset( $this->mNavigationBar
) ) {
190 return $this->mNavigationBar
;
194 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit
)->escaped(),
195 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit
)->escaped(),
196 'first' => $this->msg( 'histlast' )->escaped(),
197 'last' => $this->msg( 'histfirst' )->escaped()
200 $pagingLinks = $this->getPagingLinks( $linkTexts );
201 $limitLinks = $this->getLimitLinks();
202 $lang = $this->getLanguage();
203 $limits = $lang->pipeList( $limitLinks );
205 $firstLast = $lang->pipeList( [ $pagingLinks['first'], $pagingLinks['last'] ] );
206 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
207 $prevNext = $this->msg( 'viewprevnext' )
209 $pagingLinks['prev'],
210 $pagingLinks['next'],
213 $separator = $this->msg( 'word-separator' )->escaped();
214 $this->mNavigationBar
= $firstLast . $separator . $prevNext;
216 return $this->mNavigationBar
;
219 function getNamespaceCond() {
220 if ( $this->namespace !== '' ) {
221 return [ 'ar_namespace' => (int)$this->namespace ];
228 * Generates each row in the contributions list.
230 * @todo This would probably look a lot nicer in a table.
231 * @param stdClass $row
234 function formatRow( $row ) {
240 * There may be more than just revision rows. To make sure that we'll only be processing
241 * revisions here, let's _try_ to build a revision out of our row (without displaying
242 * notices though) and then trying to grab data from the built object. If we succeed,
243 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
244 * to extensions to subscribe to the hook to parse the row.
246 Wikimedia\
suppressWarnings();
248 $rev = Revision
::newFromArchiveRow( $row );
249 $validRevision = (bool)$rev->getId();
250 } catch ( Exception
$e ) {
251 $validRevision = false;
253 Wikimedia\restoreWarnings
();
255 if ( $validRevision ) {
256 $attribs['data-mw-revid'] = $rev->getId();
257 $ret = $this->formatRevisionRow( $row );
260 // Let extensions add data
261 Hooks
::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] );
262 $attribs = array_filter( $attribs,
263 [ Sanitizer
::class, 'isReservedDataAttribute' ],
267 if ( $classes === [] && $attribs === [] && $ret === '' ) {
268 wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
269 $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
271 $attribs['class'] = $classes;
272 $ret = Html
::rawElement( 'li', $attribs, $ret ) . "\n";
279 * Generates each row in the contributions list for archive entries.
281 * Contributions which are marked "top" are currently on top of the history.
282 * For these contributions, a [rollback] link is shown for users with sysop
283 * privileges. The rollback link restores the most recent version that was not
284 * written by the target user.
286 * @todo This would probably look a lot nicer in a table.
287 * @param stdClass $row
290 function formatRevisionRow( $row ) {
291 $page = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
293 $linkRenderer = $this->getLinkRenderer();
295 $rev = new Revision( [
297 'id' => $row->ar_rev_id
,
298 'comment' => CommentStore
::getStore()->getComment( 'ar_comment', $row )->text
,
299 'user' => $row->ar_user
,
300 'user_text' => $row->ar_user_text
,
301 'actor' => $row->ar_actor ??
null,
302 'timestamp' => $row->ar_timestamp
,
303 'minor_edit' => $row->ar_minor_edit
,
304 'deleted' => $row->ar_deleted
,
307 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
309 $logs = SpecialPage
::getTitleFor( 'Log' );
310 $dellog = $linkRenderer->makeKnownLink(
312 $this->messages
['deletionlog'],
316 'page' => $page->getPrefixedText()
320 $reviewlink = $linkRenderer->makeKnownLink(
321 SpecialPage
::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
322 $this->messages
['undeleteviewlink']
325 $user = $this->getUser();
326 $permissionManager = MediaWikiServices
::getInstance()->getPermissionManager();
328 if ( $permissionManager->userHasRight( $user, 'deletedtext' ) ) {
329 $last = $linkRenderer->makeKnownLink(
331 $this->messages
['diff'],
334 'target' => $page->getPrefixedText(),
335 'timestamp' => $rev->getTimestamp(),
340 $last = htmlspecialchars( $this->messages
['diff'] );
343 $comment = Linker
::revComment( $rev );
344 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
346 if ( !$permissionManager->userHasRight( $user, 'undelete' ) ||
347 !$rev->userCan( RevisionRecord
::DELETED_TEXT
, $user )
349 $link = htmlspecialchars( $date ); // unusable link
351 $link = $linkRenderer->makeKnownLink(
354 [ 'class' => 'mw-changeslist-date' ],
356 'target' => $page->getPrefixedText(),
357 'timestamp' => $rev->getTimestamp()
361 // Style deleted items
362 if ( $rev->isDeleted( RevisionRecord
::DELETED_TEXT
) ) {
363 $link = '<span class="history-deleted">' . $link . '</span>';
366 $pagelink = $linkRenderer->makeLink(
369 [ 'class' => 'mw-changeslist-title' ]
372 if ( $rev->isMinor() ) {
373 $mflag = ChangesList
::flag( 'minor' );
378 // Revision delete link
379 $del = Linker
::getRevDeleteLink( $user, $rev, $page );
384 $tools = Html
::rawElement(
386 [ 'class' => 'mw-deletedcontribs-tools' ],
387 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
388 [ $last, $dellog, $reviewlink ] ) )->escaped()
391 $separator = '<span class="mw-changeslist-separator">. .</span>';
392 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
394 # Denote if username is redacted for this edit
395 if ( $rev->isDeleted( RevisionRecord
::DELETED_USER
) ) {
396 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
403 * Get the Database object in use
407 public function getDatabase() {