Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / specials / pagers / DeletedContribsPager.php
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.
7 *
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.
12 *
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
17 *
18 * @file
19 * @ingroup Pager
20 */
21
22 /**
23 * @ingroup Pager
24 */
25 use MediaWiki\MediaWikiServices;
26 use MediaWiki\Storage\RevisionRecord;
27 use Wikimedia\Rdbms\IDatabase;
28 use Wikimedia\Rdbms\IResultWrapper;
29 use Wikimedia\Rdbms\FakeResultWrapper;
30
31 class DeletedContribsPager extends IndexPager {
32
33 /**
34 * @var bool Default direction for pager
35 */
36 public $mDefaultDirection = IndexPager::DIR_DESCENDING;
37
38 /**
39 * @var string[] Local cache for escaped messages
40 */
41 public $messages;
42
43 /**
44 * @var string User name, or a string describing an IP address range
45 */
46 public $target;
47
48 /**
49 * @var string|int A single namespace number, or an empty string for all namespaces
50 */
51 public $namespace = '';
52
53 /**
54 * @var IDatabase
55 */
56 public $mDb;
57
58 /**
59 * @var string Navigation bar with paging links.
60 */
61 protected $mNavigationBar;
62
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();
68 }
69 $this->target = $target;
70 $this->namespace = $namespace;
71 $this->mDb = wfGetDB( DB_REPLICA, 'contributions' );
72 }
73
74 function getDefaultQuery() {
75 $query = parent::getDefaultQuery();
76 $query['target'] = $this->target;
77
78 return $query;
79 }
80
81 function getQueryInfo() {
82 $userCond = [
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
86 )['conds']
87 ];
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;
96 }
97
98 $commentQuery = CommentStore::getStore()->getJoin( 'ar_comment' );
99 $actorQuery = ActorMigration::newMigration()->getJoin( 'ar_user' );
100
101 return [
102 'tables' => [ 'archive' ] + $commentQuery['tables'] + $actorQuery['tables'],
103 'fields' => [
104 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp',
105 'ar_minor_edit', 'ar_deleted'
106 ] + $commentQuery['fields'] + $actorQuery['fields'],
107 'conds' => $conds,
108 'options' => [],
109 'join_conds' => $commentQuery['joins'] + $actorQuery['joins'],
110 ];
111 }
112
113 /**
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.
116 *
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
121 */
122 function reallyDoQuery( $offset, $limit, $order ) {
123 $data = [ parent::reallyDoQuery( $offset, $limit, $order ) ];
124
125 // This hook will allow extensions to add in additional queries, nearly
126 // identical to ContribsPager::reallyDoQuery.
127 Hooks::run(
128 'DeletedContribsPager::reallyDoQuery',
129 [ &$data, $this, $offset, $limit, $order ]
130 );
131
132 $result = [];
133
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;
139 }
140 }
141
142 // sort results
143 if ( $order === self::QUERY_ASCENDING ) {
144 ksort( $result );
145 } else {
146 krsort( $result );
147 }
148
149 // enforce limit
150 $result = array_slice( $result, 0, $limit );
151
152 // get rid of array keys
153 $result = array_values( $result );
154
155 return new FakeResultWrapper( $result );
156 }
157
158 function getIndexField() {
159 return 'ar_timestamp';
160 }
161
162 /**
163 * @return string
164 */
165 public function getTarget() {
166 return $this->target;
167 }
168
169 /**
170 * @return int|string
171 */
172 public function getNamespace() {
173 return $this->namespace;
174 }
175
176 protected function getStartBody() {
177 return "<ul>\n";
178 }
179
180 protected function getEndBody() {
181 return "</ul>\n";
182 }
183
184 function getNavigationBar() {
185 if ( isset( $this->mNavigationBar ) ) {
186 return $this->mNavigationBar;
187 }
188
189 $linkTexts = [
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()
194 ];
195
196 $pagingLinks = $this->getPagingLinks( $linkTexts );
197 $limitLinks = $this->getLimitLinks();
198 $lang = $this->getLanguage();
199 $limits = $lang->pipeList( $limitLinks );
200
201 $firstLast = $lang->pipeList( [ $pagingLinks['first'], $pagingLinks['last'] ] );
202 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
203 $prevNext = $this->msg( 'viewprevnext' )
204 ->rawParams(
205 $pagingLinks['prev'],
206 $pagingLinks['next'],
207 $limits
208 )->escaped();
209 $separator = $this->msg( 'word-separator' )->escaped();
210 $this->mNavigationBar = $firstLast . $separator . $prevNext;
211
212 return $this->mNavigationBar;
213 }
214
215 function getNamespaceCond() {
216 if ( $this->namespace !== '' ) {
217 return [ 'ar_namespace' => (int)$this->namespace ];
218 } else {
219 return [];
220 }
221 }
222
223 /**
224 * Generates each row in the contributions list.
225 *
226 * @todo This would probably look a lot nicer in a table.
227 * @param stdClass $row
228 * @return string
229 */
230 function formatRow( $row ) {
231 $ret = '';
232 $classes = [];
233 $attribs = [];
234
235 /*
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.
241 */
242 Wikimedia\suppressWarnings();
243 try {
244 $rev = Revision::newFromArchiveRow( $row );
245 $validRevision = (bool)$rev->getId();
246 } catch ( Exception $e ) {
247 $validRevision = false;
248 }
249 Wikimedia\restoreWarnings();
250
251 if ( $validRevision ) {
252 $attribs['data-mw-revid'] = $rev->getId();
253 $ret = $this->formatRevisionRow( $row );
254 }
255
256 // Let extensions add data
257 Hooks::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] );
258 $attribs = array_filter( $attribs,
259 [ Sanitizer::class, 'isReservedDataAttribute' ],
260 ARRAY_FILTER_USE_KEY
261 );
262
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";
266 } else {
267 $attribs['class'] = $classes;
268 $ret = Html::rawElement( 'li', $attribs, $ret ) . "\n";
269 }
270
271 return $ret;
272 }
273
274 /**
275 * Generates each row in the contributions list for archive entries.
276 *
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.
281 *
282 * @todo This would probably look a lot nicer in a table.
283 * @param stdClass $row
284 * @return string
285 */
286 function formatRevisionRow( $row ) {
287 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
288
289 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
290
291 $rev = new Revision( [
292 'title' => $page,
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,
301 ] );
302
303 $undelete = SpecialPage::getTitleFor( 'Undelete' );
304
305 $logs = SpecialPage::getTitleFor( 'Log' );
306 $dellog = $linkRenderer->makeKnownLink(
307 $logs,
308 $this->messages['deletionlog'],
309 [],
310 [
311 'type' => 'delete',
312 'page' => $page->getPrefixedText()
313 ]
314 );
315
316 $reviewlink = $linkRenderer->makeKnownLink(
317 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
318 $this->messages['undeleteviewlink']
319 );
320
321 $user = $this->getUser();
322
323 if ( $user->isAllowed( 'deletedtext' ) ) {
324 $last = $linkRenderer->makeKnownLink(
325 $undelete,
326 $this->messages['diff'],
327 [],
328 [
329 'target' => $page->getPrefixedText(),
330 'timestamp' => $rev->getTimestamp(),
331 'diff' => 'prev'
332 ]
333 );
334 } else {
335 $last = htmlspecialchars( $this->messages['diff'] );
336 }
337
338 $comment = Linker::revComment( $rev );
339 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
340
341 if ( !$user->isAllowed( 'undelete' ) || !$rev->userCan( RevisionRecord::DELETED_TEXT, $user ) ) {
342 $link = htmlspecialchars( $date ); // unusable link
343 } else {
344 $link = $linkRenderer->makeKnownLink(
345 $undelete,
346 $date,
347 [ 'class' => 'mw-changeslist-date' ],
348 [
349 'target' => $page->getPrefixedText(),
350 'timestamp' => $rev->getTimestamp()
351 ]
352 );
353 }
354 // Style deleted items
355 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
356 $link = '<span class="history-deleted">' . $link . '</span>';
357 }
358
359 $pagelink = $linkRenderer->makeLink(
360 $page,
361 null,
362 [ 'class' => 'mw-changeslist-title' ]
363 );
364
365 if ( $rev->isMinor() ) {
366 $mflag = ChangesList::flag( 'minor' );
367 } else {
368 $mflag = '';
369 }
370
371 // Revision delete link
372 $del = Linker::getRevDeleteLink( $user, $rev, $page );
373 if ( $del ) {
374 $del .= ' ';
375 }
376
377 $tools = Html::rawElement(
378 'span',
379 [ 'class' => 'mw-deletedcontribs-tools' ],
380 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
381 [ $last, $dellog, $reviewlink ] ) )->escaped()
382 );
383
384 $separator = '<span class="mw-changeslist-separator">. .</span>';
385 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
386
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>";
390 }
391
392 return $ret;
393 }
394
395 /**
396 * Get the Database object in use
397 *
398 * @return IDatabase
399 */
400 public function getDatabase() {
401 return $this->mDb;
402 }
403 }