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