AuthManager: Break AuthPlugin::addUser more explicitly
[mediawiki.git] / includes / specials / pagers / DeletedContribsPager.php
blobf2421f871a7c477a6e8c821145cfc602937e46bb
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.
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
18 * @file
19 * @ingroup Pager
22 /**
23 * @ingroup Pager
25 class DeletedContribsPager extends IndexPager {
27 public $mDefaultDirection = IndexPager::DIR_DESCENDING;
28 public $messages;
29 public $target;
30 public $namespace = '';
31 public $mDb;
33 /**
34 * @var string Navigation bar with paging links.
36 protected $mNavigationBar;
38 function __construct( IContextSource $context, $target, $namespace = false ) {
39 parent::__construct( $context );
40 $msgs = [ 'deletionlog', 'undeleteviewlink', 'diff' ];
41 foreach ( $msgs as $msg ) {
42 $this->messages[$msg] = $this->msg( $msg )->escaped();
44 $this->target = $target;
45 $this->namespace = $namespace;
46 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
49 function getDefaultQuery() {
50 $query = parent::getDefaultQuery();
51 $query['target'] = $this->target;
53 return $query;
56 function getQueryInfo() {
57 list( $index, $userCond ) = $this->getUserCond();
58 $conds = array_merge( $userCond, $this->getNamespaceCond() );
59 $user = $this->getUser();
60 // Paranoia: avoid brute force searches (bug 17792)
61 if ( !$user->isAllowed( 'deletedhistory' ) ) {
62 $conds[] = $this->mDb->bitAnd( 'ar_deleted', Revision::DELETED_USER ) . ' = 0';
63 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
64 $conds[] = $this->mDb->bitAnd( 'ar_deleted', Revision::SUPPRESSED_USER ) .
65 ' != ' . Revision::SUPPRESSED_USER;
68 return [
69 'tables' => [ 'archive' ],
70 'fields' => [
71 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp', 'ar_comment',
72 'ar_minor_edit', 'ar_user', 'ar_user_text', 'ar_deleted'
74 'conds' => $conds,
75 'options' => [ 'USE INDEX' => $index ]
79 /**
80 * This method basically executes the exact same code as the parent class, though with
81 * a hook added, to allow extensions to add additional queries.
83 * @param string $offset Index offset, inclusive
84 * @param int $limit Exact query limit
85 * @param bool $descending Query direction, false for ascending, true for descending
86 * @return ResultWrapper
88 function reallyDoQuery( $offset, $limit, $descending ) {
89 $data = [ parent::reallyDoQuery( $offset, $limit, $descending ) ];
91 // This hook will allow extensions to add in additional queries, nearly
92 // identical to ContribsPager::reallyDoQuery.
93 Hooks::run(
94 'DeletedContribsPager::reallyDoQuery',
95 [ &$data, $this, $offset, $limit, $descending ]
98 $result = [];
100 // loop all results and collect them in an array
101 foreach ( $data as $query ) {
102 foreach ( $query as $i => $row ) {
103 // use index column as key, allowing us to easily sort in PHP
104 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
108 // sort results
109 if ( $descending ) {
110 ksort( $result );
111 } else {
112 krsort( $result );
115 // enforce limit
116 $result = array_slice( $result, 0, $limit );
118 // get rid of array keys
119 $result = array_values( $result );
121 return new FakeResultWrapper( $result );
124 function getUserCond() {
125 $condition = [];
127 $condition['ar_user_text'] = $this->target;
128 $index = 'usertext_timestamp';
130 return [ $index, $condition ];
133 function getIndexField() {
134 return 'ar_timestamp';
137 function getStartBody() {
138 return "<ul>\n";
141 function getEndBody() {
142 return "</ul>\n";
145 function getNavigationBar() {
146 if ( isset( $this->mNavigationBar ) ) {
147 return $this->mNavigationBar;
150 $linkTexts = [
151 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
152 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
153 'first' => $this->msg( 'histlast' )->escaped(),
154 'last' => $this->msg( 'histfirst' )->escaped()
157 $pagingLinks = $this->getPagingLinks( $linkTexts );
158 $limitLinks = $this->getLimitLinks();
159 $lang = $this->getLanguage();
160 $limits = $lang->pipeList( $limitLinks );
162 $firstLast = $lang->pipeList( [ $pagingLinks['first'], $pagingLinks['last'] ] );
163 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
164 $prevNext = $this->msg( 'viewprevnext' )
165 ->rawParams(
166 $pagingLinks['prev'],
167 $pagingLinks['next'],
168 $limits
169 )->escaped();
170 $separator = $this->msg( 'word-separator' )->escaped();
171 $this->mNavigationBar = $firstLast . $separator . $prevNext;
173 return $this->mNavigationBar;
176 function getNamespaceCond() {
177 if ( $this->namespace !== '' ) {
178 return [ 'ar_namespace' => (int)$this->namespace ];
179 } else {
180 return [];
185 * Generates each row in the contributions list.
187 * @todo This would probably look a lot nicer in a table.
188 * @param stdClass $row
189 * @return string
191 function formatRow( $row ) {
192 $ret = '';
193 $classes = [];
196 * There may be more than just revision rows. To make sure that we'll only be processing
197 * revisions here, let's _try_ to build a revision out of our row (without displaying
198 * notices though) and then trying to grab data from the built object. If we succeed,
199 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
200 * to extensions to subscribe to the hook to parse the row.
202 MediaWiki\suppressWarnings();
203 try {
204 $rev = Revision::newFromArchiveRow( $row );
205 $validRevision = (bool)$rev->getId();
206 } catch ( Exception $e ) {
207 $validRevision = false;
209 MediaWiki\restoreWarnings();
211 if ( $validRevision ) {
212 $ret = $this->formatRevisionRow( $row );
215 // Let extensions add data
216 Hooks::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes ] );
218 if ( $classes === [] && $ret === '' ) {
219 wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
220 $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
221 } else {
222 $ret = Html::rawElement( 'li', [ 'class' => $classes ], $ret ) . "\n";
225 return $ret;
229 * Generates each row in the contributions list for archive entries.
231 * Contributions which are marked "top" are currently on top of the history.
232 * For these contributions, a [rollback] link is shown for users with sysop
233 * privileges. The rollback link restores the most recent version that was not
234 * written by the target user.
236 * @todo This would probably look a lot nicer in a table.
237 * @param stdClass $row
238 * @return string
240 function formatRevisionRow( $row ) {
241 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
243 $rev = new Revision( [
244 'title' => $page,
245 'id' => $row->ar_rev_id,
246 'comment' => $row->ar_comment,
247 'user' => $row->ar_user,
248 'user_text' => $row->ar_user_text,
249 'timestamp' => $row->ar_timestamp,
250 'minor_edit' => $row->ar_minor_edit,
251 'deleted' => $row->ar_deleted,
252 ] );
254 $undelete = SpecialPage::getTitleFor( 'Undelete' );
256 $logs = SpecialPage::getTitleFor( 'Log' );
257 $dellog = Linker::linkKnown(
258 $logs,
259 $this->messages['deletionlog'],
262 'type' => 'delete',
263 'page' => $page->getPrefixedText()
267 $reviewlink = Linker::linkKnown(
268 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
269 $this->messages['undeleteviewlink']
272 $user = $this->getUser();
274 if ( $user->isAllowed( 'deletedtext' ) ) {
275 $last = Linker::linkKnown(
276 $undelete,
277 $this->messages['diff'],
280 'target' => $page->getPrefixedText(),
281 'timestamp' => $rev->getTimestamp(),
282 'diff' => 'prev'
285 } else {
286 $last = $this->messages['diff'];
289 $comment = Linker::revComment( $rev );
290 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
291 $date = htmlspecialchars( $date );
293 if ( !$user->isAllowed( 'undelete' ) || !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
294 $link = $date; // unusable link
295 } else {
296 $link = Linker::linkKnown(
297 $undelete,
298 $date,
299 [ 'class' => 'mw-changeslist-date' ],
301 'target' => $page->getPrefixedText(),
302 'timestamp' => $rev->getTimestamp()
306 // Style deleted items
307 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
308 $link = '<span class="history-deleted">' . $link . '</span>';
311 $pagelink = Linker::link(
312 $page,
313 null,
314 [ 'class' => 'mw-changeslist-title' ]
317 if ( $rev->isMinor() ) {
318 $mflag = ChangesList::flag( 'minor' );
319 } else {
320 $mflag = '';
323 // Revision delete link
324 $del = Linker::getRevDeleteLink( $user, $rev, $page );
325 if ( $del ) {
326 $del .= ' ';
329 $tools = Html::rawElement(
330 'span',
331 [ 'class' => 'mw-deletedcontribs-tools' ],
332 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
333 [ $last, $dellog, $reviewlink ] ) )->escaped()
336 $separator = '<span class="mw-changeslist-separator">. .</span>';
337 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
339 # Denote if username is redacted for this edit
340 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
341 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
344 return $ret;
348 * Get the Database object in use
350 * @return IDatabase
352 public function getDatabase() {
353 return $this->mDb;