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 Wikimedia\Rdbms\IDatabase
;
27 use Wikimedia\Rdbms\IResultWrapper
;
28 use Wikimedia\Rdbms\FakeResultWrapper
;
30 class DeletedContribsPager
extends IndexPager
{
33 * @var bool Default direction for pager
35 public $mDefaultDirection = IndexPager
::DIR_DESCENDING
;
38 * @var string[] Local cache for escaped messages
43 * @var string User name, or a string describing an IP address range
48 * @var string|int A single namespace number, or an empty string for all namespaces
50 public $namespace = '';
53 * @var \Wikimedia\Rdbms\Database
58 * @var string Navigation bar with paging links.
60 protected $mNavigationBar;
62 public function __construct( IContextSource
$context, $target, $namespace = false ) {
63 parent
::__construct( $context );
64 $msgs = [ 'deletionlog', 'undeleteviewlink', 'diff' ];
65 foreach ( $msgs as $msg ) {
66 $this->messages
[$msg] = $this->msg( $msg )->text();
68 $this->target
= $target;
69 $this->namespace = $namespace;
70 $this->mDb
= wfGetDB( DB_REPLICA
, 'contributions' );
73 function getDefaultQuery() {
74 $query = parent
::getDefaultQuery();
75 $query['target'] = $this->target
;
80 function getQueryInfo() {
82 // ->getJoin() below takes care of any joins needed
83 ActorMigration
::newMigration()->getWhere(
84 wfGetDB( DB_REPLICA
), 'ar_user', User
::newFromName( $this->target
, false ), false
87 $conds = array_merge( $userCond, $this->getNamespaceCond() );
88 $user = $this->getUser();
89 // Paranoia: avoid brute force searches (T19792)
90 if ( !$user->isAllowed( 'deletedhistory' ) ) {
91 $conds[] = $this->mDb
->bitAnd( 'ar_deleted', Revision
::DELETED_USER
) . ' = 0';
92 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
93 $conds[] = $this->mDb
->bitAnd( 'ar_deleted', Revision
::SUPPRESSED_USER
) .
94 ' != ' . Revision
::SUPPRESSED_USER
;
97 $commentQuery = CommentStore
::getStore()->getJoin( 'ar_comment' );
98 $actorQuery = ActorMigration
::newMigration()->getJoin( 'ar_user' );
101 'tables' => [ 'archive' ] +
$commentQuery['tables'] +
$actorQuery['tables'],
103 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp',
104 'ar_minor_edit', 'ar_deleted'
105 ] +
$commentQuery['fields'] +
$actorQuery['fields'],
108 'join_conds' => $commentQuery['joins'] +
$actorQuery['joins'],
113 * This method basically executes the exact same code as the parent class, though with
114 * a hook added, to allow extensions to add additional queries.
116 * @param string $offset Index offset, inclusive
117 * @param int $limit Exact query limit
118 * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING
119 * @return IResultWrapper
121 function reallyDoQuery( $offset, $limit, $order ) {
122 $data = [ parent
::reallyDoQuery( $offset, $limit, $order ) ];
124 // This hook will allow extensions to add in additional queries, nearly
125 // identical to ContribsPager::reallyDoQuery.
127 'DeletedContribsPager::reallyDoQuery',
128 [ &$data, $this, $offset, $limit, $order ]
133 // loop all results and collect them in an array
134 foreach ( $data as $query ) {
135 foreach ( $query as $i => $row ) {
136 // use index column as key, allowing us to easily sort in PHP
137 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
142 if ( $order === self
::QUERY_ASCENDING
) {
149 $result = array_slice( $result, 0, $limit );
151 // get rid of array keys
152 $result = array_values( $result );
154 return new FakeResultWrapper( $result );
157 function getIndexField() {
158 return 'ar_timestamp';
164 public function getTarget() {
165 return $this->target
;
171 public function getNamespace() {
172 return $this->namespace;
175 protected function getStartBody() {
179 protected function getEndBody() {
183 function getNavigationBar() {
184 if ( isset( $this->mNavigationBar
) ) {
185 return $this->mNavigationBar
;
189 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit
)->escaped(),
190 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit
)->escaped(),
191 'first' => $this->msg( 'histlast' )->escaped(),
192 'last' => $this->msg( 'histfirst' )->escaped()
195 $pagingLinks = $this->getPagingLinks( $linkTexts );
196 $limitLinks = $this->getLimitLinks();
197 $lang = $this->getLanguage();
198 $limits = $lang->pipeList( $limitLinks );
200 $firstLast = $lang->pipeList( [ $pagingLinks['first'], $pagingLinks['last'] ] );
201 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
202 $prevNext = $this->msg( 'viewprevnext' )
204 $pagingLinks['prev'],
205 $pagingLinks['next'],
208 $separator = $this->msg( 'word-separator' )->escaped();
209 $this->mNavigationBar
= $firstLast . $separator . $prevNext;
211 return $this->mNavigationBar
;
214 function getNamespaceCond() {
215 if ( $this->namespace !== '' ) {
216 return [ 'ar_namespace' => (int)$this->namespace ];
223 * Generates each row in the contributions list.
225 * @todo This would probably look a lot nicer in a table.
226 * @param stdClass $row
229 function formatRow( $row ) {
235 * There may be more than just revision rows. To make sure that we'll only be processing
236 * revisions here, let's _try_ to build a revision out of our row (without displaying
237 * notices though) and then trying to grab data from the built object. If we succeed,
238 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
239 * to extensions to subscribe to the hook to parse the row.
241 Wikimedia\
suppressWarnings();
243 $rev = Revision
::newFromArchiveRow( $row );
244 $validRevision = (bool)$rev->getId();
245 } catch ( Exception
$e ) {
246 $validRevision = false;
248 Wikimedia\restoreWarnings
();
250 if ( $validRevision ) {
251 $attribs['data-mw-revid'] = $rev->getId();
252 $ret = $this->formatRevisionRow( $row );
255 // Let extensions add data
256 Hooks
::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] );
257 $attribs = array_filter( $attribs,
258 [ Sanitizer
::class, 'isReservedDataAttribute' ],
262 if ( $classes === [] && $attribs === [] && $ret === '' ) {
263 wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
264 $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
266 $attribs['class'] = $classes;
267 $ret = Html
::rawElement( 'li', $attribs, $ret ) . "\n";
274 * Generates each row in the contributions list for archive entries.
276 * Contributions which are marked "top" are currently on top of the history.
277 * For these contributions, a [rollback] link is shown for users with sysop
278 * privileges. The rollback link restores the most recent version that was not
279 * written by the target user.
281 * @todo This would probably look a lot nicer in a table.
282 * @param stdClass $row
285 function formatRevisionRow( $row ) {
286 $page = Title
::makeTitle( $row->ar_namespace
, $row->ar_title
);
288 $linkRenderer = MediaWikiServices
::getInstance()->getLinkRenderer();
290 $rev = new Revision( [
292 'id' => $row->ar_rev_id
,
293 'comment' => CommentStore
::getStore()->getComment( 'ar_comment', $row )->text
,
294 'user' => $row->ar_user
,
295 'user_text' => $row->ar_user_text
,
296 'actor' => $row->ar_actor ??
null,
297 'timestamp' => $row->ar_timestamp
,
298 'minor_edit' => $row->ar_minor_edit
,
299 'deleted' => $row->ar_deleted
,
302 $undelete = SpecialPage
::getTitleFor( 'Undelete' );
304 $logs = SpecialPage
::getTitleFor( 'Log' );
305 $dellog = $linkRenderer->makeKnownLink(
307 $this->messages
['deletionlog'],
311 'page' => $page->getPrefixedText()
315 $reviewlink = $linkRenderer->makeKnownLink(
316 SpecialPage
::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
317 $this->messages
['undeleteviewlink']
320 $user = $this->getUser();
322 if ( $user->isAllowed( 'deletedtext' ) ) {
323 $last = $linkRenderer->makeKnownLink(
325 $this->messages
['diff'],
328 'target' => $page->getPrefixedText(),
329 'timestamp' => $rev->getTimestamp(),
334 $last = htmlspecialchars( $this->messages
['diff'] );
337 $comment = Linker
::revComment( $rev );
338 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
340 if ( !$user->isAllowed( 'undelete' ) ||
!$rev->userCan( Revision
::DELETED_TEXT
, $user ) ) {
341 $link = htmlspecialchars( $date ); // unusable link
343 $link = $linkRenderer->makeKnownLink(
346 [ 'class' => 'mw-changeslist-date' ],
348 'target' => $page->getPrefixedText(),
349 'timestamp' => $rev->getTimestamp()
353 // Style deleted items
354 if ( $rev->isDeleted( Revision
::DELETED_TEXT
) ) {
355 $link = '<span class="history-deleted">' . $link . '</span>';
358 $pagelink = $linkRenderer->makeLink(
361 [ 'class' => 'mw-changeslist-title' ]
364 if ( $rev->isMinor() ) {
365 $mflag = ChangesList
::flag( 'minor' );
370 // Revision delete link
371 $del = Linker
::getRevDeleteLink( $user, $rev, $page );
376 $tools = Html
::rawElement(
378 [ 'class' => 'mw-deletedcontribs-tools' ],
379 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
380 [ $last, $dellog, $reviewlink ] ) )->escaped()
383 $separator = '<span class="mw-changeslist-separator">. .</span>';
384 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
386 # Denote if username is redacted for this edit
387 if ( $rev->isDeleted( Revision
::DELETED_USER
) ) {
388 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
395 * Get the Database object in use
399 public function getDatabase() {