Special:Upload should not crash on failing previews
[mediawiki.git] / includes / specials / pagers / DeletedContribsPager.php
blob9ffcce9ef8b80521747ee94f6c8c00f4f3050fd0
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 use MediaWiki\MediaWikiServices;
27 class DeletedContribsPager extends IndexPager {
29 public $mDefaultDirection = IndexPager::DIR_DESCENDING;
30 public $messages;
31 public $target;
32 public $namespace = '';
33 public $mDb;
35 /**
36 * @var string Navigation bar with paging links.
38 protected $mNavigationBar;
40 function __construct( IContextSource $context, $target, $namespace = false ) {
41 parent::__construct( $context );
42 $msgs = [ 'deletionlog', 'undeleteviewlink', 'diff' ];
43 foreach ( $msgs as $msg ) {
44 $this->messages[$msg] = $this->msg( $msg )->text();
46 $this->target = $target;
47 $this->namespace = $namespace;
48 $this->mDb = wfGetDB( DB_REPLICA, 'contributions' );
51 function getDefaultQuery() {
52 $query = parent::getDefaultQuery();
53 $query['target'] = $this->target;
55 return $query;
58 function getQueryInfo() {
59 list( $index, $userCond ) = $this->getUserCond();
60 $conds = array_merge( $userCond, $this->getNamespaceCond() );
61 $user = $this->getUser();
62 // Paranoia: avoid brute force searches (bug 17792)
63 if ( !$user->isAllowed( 'deletedhistory' ) ) {
64 $conds[] = $this->mDb->bitAnd( 'ar_deleted', Revision::DELETED_USER ) . ' = 0';
65 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
66 $conds[] = $this->mDb->bitAnd( 'ar_deleted', Revision::SUPPRESSED_USER ) .
67 ' != ' . Revision::SUPPRESSED_USER;
70 return [
71 'tables' => [ 'archive' ],
72 'fields' => [
73 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp', 'ar_comment',
74 'ar_minor_edit', 'ar_user', 'ar_user_text', 'ar_deleted'
76 'conds' => $conds,
77 'options' => [ 'USE INDEX' => $index ]
81 /**
82 * This method basically executes the exact same code as the parent class, though with
83 * a hook added, to allow extensions to add additional queries.
85 * @param string $offset Index offset, inclusive
86 * @param int $limit Exact query limit
87 * @param bool $descending Query direction, false for ascending, true for descending
88 * @return ResultWrapper
90 function reallyDoQuery( $offset, $limit, $descending ) {
91 $data = [ parent::reallyDoQuery( $offset, $limit, $descending ) ];
93 // This hook will allow extensions to add in additional queries, nearly
94 // identical to ContribsPager::reallyDoQuery.
95 Hooks::run(
96 'DeletedContribsPager::reallyDoQuery',
97 [ &$data, $this, $offset, $limit, $descending ]
100 $result = [];
102 // loop all results and collect them in an array
103 foreach ( $data as $query ) {
104 foreach ( $query as $i => $row ) {
105 // use index column as key, allowing us to easily sort in PHP
106 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
110 // sort results
111 if ( $descending ) {
112 ksort( $result );
113 } else {
114 krsort( $result );
117 // enforce limit
118 $result = array_slice( $result, 0, $limit );
120 // get rid of array keys
121 $result = array_values( $result );
123 return new FakeResultWrapper( $result );
126 function getUserCond() {
127 $condition = [];
129 $condition['ar_user_text'] = $this->target;
130 $index = 'usertext_timestamp';
132 return [ $index, $condition ];
135 function getIndexField() {
136 return 'ar_timestamp';
139 function getStartBody() {
140 return "<ul>\n";
143 function getEndBody() {
144 return "</ul>\n";
147 function getNavigationBar() {
148 if ( isset( $this->mNavigationBar ) ) {
149 return $this->mNavigationBar;
152 $linkTexts = [
153 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
154 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
155 'first' => $this->msg( 'histlast' )->escaped(),
156 'last' => $this->msg( 'histfirst' )->escaped()
159 $pagingLinks = $this->getPagingLinks( $linkTexts );
160 $limitLinks = $this->getLimitLinks();
161 $lang = $this->getLanguage();
162 $limits = $lang->pipeList( $limitLinks );
164 $firstLast = $lang->pipeList( [ $pagingLinks['first'], $pagingLinks['last'] ] );
165 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
166 $prevNext = $this->msg( 'viewprevnext' )
167 ->rawParams(
168 $pagingLinks['prev'],
169 $pagingLinks['next'],
170 $limits
171 )->escaped();
172 $separator = $this->msg( 'word-separator' )->escaped();
173 $this->mNavigationBar = $firstLast . $separator . $prevNext;
175 return $this->mNavigationBar;
178 function getNamespaceCond() {
179 if ( $this->namespace !== '' ) {
180 return [ 'ar_namespace' => (int)$this->namespace ];
181 } else {
182 return [];
187 * Generates each row in the contributions list.
189 * @todo This would probably look a lot nicer in a table.
190 * @param stdClass $row
191 * @return string
193 function formatRow( $row ) {
194 $ret = '';
195 $classes = [];
198 * There may be more than just revision rows. To make sure that we'll only be processing
199 * revisions here, let's _try_ to build a revision out of our row (without displaying
200 * notices though) and then trying to grab data from the built object. If we succeed,
201 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
202 * to extensions to subscribe to the hook to parse the row.
204 MediaWiki\suppressWarnings();
205 try {
206 $rev = Revision::newFromArchiveRow( $row );
207 $validRevision = (bool)$rev->getId();
208 } catch ( Exception $e ) {
209 $validRevision = false;
211 MediaWiki\restoreWarnings();
213 if ( $validRevision ) {
214 $ret = $this->formatRevisionRow( $row );
217 // Let extensions add data
218 Hooks::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes ] );
220 if ( $classes === [] && $ret === '' ) {
221 wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
222 $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
223 } else {
224 $ret = Html::rawElement( 'li', [ 'class' => $classes ], $ret ) . "\n";
227 return $ret;
231 * Generates each row in the contributions list for archive entries.
233 * Contributions which are marked "top" are currently on top of the history.
234 * For these contributions, a [rollback] link is shown for users with sysop
235 * privileges. The rollback link restores the most recent version that was not
236 * written by the target user.
238 * @todo This would probably look a lot nicer in a table.
239 * @param stdClass $row
240 * @return string
242 function formatRevisionRow( $row ) {
243 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
245 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
247 $rev = new Revision( [
248 'title' => $page,
249 'id' => $row->ar_rev_id,
250 'comment' => $row->ar_comment,
251 'user' => $row->ar_user,
252 'user_text' => $row->ar_user_text,
253 'timestamp' => $row->ar_timestamp,
254 'minor_edit' => $row->ar_minor_edit,
255 'deleted' => $row->ar_deleted,
256 ] );
258 $undelete = SpecialPage::getTitleFor( 'Undelete' );
260 $logs = SpecialPage::getTitleFor( 'Log' );
261 $dellog = $linkRenderer->makeKnownLink(
262 $logs,
263 $this->messages['deletionlog'],
266 'type' => 'delete',
267 'page' => $page->getPrefixedText()
271 $reviewlink = $linkRenderer->makeKnownLink(
272 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
273 $this->messages['undeleteviewlink']
276 $user = $this->getUser();
278 if ( $user->isAllowed( 'deletedtext' ) ) {
279 $last = $linkRenderer->makeKnownLink(
280 $undelete,
281 $this->messages['diff'],
284 'target' => $page->getPrefixedText(),
285 'timestamp' => $rev->getTimestamp(),
286 'diff' => 'prev'
289 } else {
290 $last = htmlspecialchars( $this->messages['diff'] );
293 $comment = Linker::revComment( $rev );
294 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
296 if ( !$user->isAllowed( 'undelete' ) || !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
297 $link = htmlspecialchars( $date ); // unusable link
298 } else {
299 $link = $linkRenderer->makeKnownLink(
300 $undelete,
301 $date,
302 [ 'class' => 'mw-changeslist-date' ],
304 'target' => $page->getPrefixedText(),
305 'timestamp' => $rev->getTimestamp()
309 // Style deleted items
310 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
311 $link = '<span class="history-deleted">' . $link . '</span>';
314 $pagelink = $linkRenderer->makeLink(
315 $page,
316 null,
317 [ 'class' => 'mw-changeslist-title' ]
320 if ( $rev->isMinor() ) {
321 $mflag = ChangesList::flag( 'minor' );
322 } else {
323 $mflag = '';
326 // Revision delete link
327 $del = Linker::getRevDeleteLink( $user, $rev, $page );
328 if ( $del ) {
329 $del .= ' ';
332 $tools = Html::rawElement(
333 'span',
334 [ 'class' => 'mw-deletedcontribs-tools' ],
335 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
336 [ $last, $dellog, $reviewlink ] ) )->escaped()
339 $separator = '<span class="mw-changeslist-separator">. .</span>';
340 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
342 # Denote if username is redacted for this edit
343 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
344 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
347 return $ret;
351 * Get the Database object in use
353 * @return IDatabase
355 public function getDatabase() {
356 return $this->mDb;