Merge "objectcache: move MemcachedClient class to /utils subdir"
[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 ( !MediaWikiServices::getInstance()
94 ->getPermissionManager()
95 ->userHasAnyRight( $user, 'suppressrevision', 'viewsuppressed' )
96 ) {
97 $conds[] = $this->mDb->bitAnd( 'ar_deleted', RevisionRecord::SUPPRESSED_USER ) .
98 ' != ' . RevisionRecord::SUPPRESSED_USER;
99 }
100
101 $commentQuery = CommentStore::getStore()->getJoin( 'ar_comment' );
102 $actorQuery = ActorMigration::newMigration()->getJoin( 'ar_user' );
103
104 return [
105 'tables' => [ 'archive' ] + $commentQuery['tables'] + $actorQuery['tables'],
106 'fields' => [
107 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp',
108 'ar_minor_edit', 'ar_deleted'
109 ] + $commentQuery['fields'] + $actorQuery['fields'],
110 'conds' => $conds,
111 'options' => [],
112 'join_conds' => $commentQuery['joins'] + $actorQuery['joins'],
113 ];
114 }
115
116 /**
117 * This method basically executes the exact same code as the parent class, though with
118 * a hook added, to allow extensions to add additional queries.
119 *
120 * @param string $offset Index offset, inclusive
121 * @param int $limit Exact query limit
122 * @param bool $order IndexPager::QUERY_ASCENDING or IndexPager::QUERY_DESCENDING
123 * @return IResultWrapper
124 */
125 function reallyDoQuery( $offset, $limit, $order ) {
126 $data = [ parent::reallyDoQuery( $offset, $limit, $order ) ];
127
128 // This hook will allow extensions to add in additional queries, nearly
129 // identical to ContribsPager::reallyDoQuery.
130 Hooks::run(
131 'DeletedContribsPager::reallyDoQuery',
132 [ &$data, $this, $offset, $limit, $order ]
133 );
134
135 $result = [];
136
137 // loop all results and collect them in an array
138 foreach ( $data as $query ) {
139 foreach ( $query as $i => $row ) {
140 // use index column as key, allowing us to easily sort in PHP
141 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
142 }
143 }
144
145 // sort results
146 if ( $order === self::QUERY_ASCENDING ) {
147 ksort( $result );
148 } else {
149 krsort( $result );
150 }
151
152 // enforce limit
153 $result = array_slice( $result, 0, $limit );
154
155 // get rid of array keys
156 $result = array_values( $result );
157
158 return new FakeResultWrapper( $result );
159 }
160
161 function getIndexField() {
162 return 'ar_timestamp';
163 }
164
165 /**
166 * @return string
167 */
168 public function getTarget() {
169 return $this->target;
170 }
171
172 /**
173 * @return int|string
174 */
175 public function getNamespace() {
176 return $this->namespace;
177 }
178
179 protected function getStartBody() {
180 return "<ul>\n";
181 }
182
183 protected function getEndBody() {
184 return "</ul>\n";
185 }
186
187 function getNavigationBar() {
188 if ( isset( $this->mNavigationBar ) ) {
189 return $this->mNavigationBar;
190 }
191
192 $linkTexts = [
193 'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
194 'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
195 'first' => $this->msg( 'histlast' )->escaped(),
196 'last' => $this->msg( 'histfirst' )->escaped()
197 ];
198
199 $pagingLinks = $this->getPagingLinks( $linkTexts );
200 $limitLinks = $this->getLimitLinks();
201 $lang = $this->getLanguage();
202 $limits = $lang->pipeList( $limitLinks );
203
204 $firstLast = $lang->pipeList( [ $pagingLinks['first'], $pagingLinks['last'] ] );
205 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
206 $prevNext = $this->msg( 'viewprevnext' )
207 ->rawParams(
208 $pagingLinks['prev'],
209 $pagingLinks['next'],
210 $limits
211 )->escaped();
212 $separator = $this->msg( 'word-separator' )->escaped();
213 $this->mNavigationBar = $firstLast . $separator . $prevNext;
214
215 return $this->mNavigationBar;
216 }
217
218 function getNamespaceCond() {
219 if ( $this->namespace !== '' ) {
220 return [ 'ar_namespace' => (int)$this->namespace ];
221 } else {
222 return [];
223 }
224 }
225
226 /**
227 * Generates each row in the contributions list.
228 *
229 * @todo This would probably look a lot nicer in a table.
230 * @param stdClass $row
231 * @return string
232 */
233 function formatRow( $row ) {
234 $ret = '';
235 $classes = [];
236 $attribs = [];
237
238 /*
239 * There may be more than just revision rows. To make sure that we'll only be processing
240 * revisions here, let's _try_ to build a revision out of our row (without displaying
241 * notices though) and then trying to grab data from the built object. If we succeed,
242 * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
243 * to extensions to subscribe to the hook to parse the row.
244 */
245 Wikimedia\suppressWarnings();
246 try {
247 $rev = Revision::newFromArchiveRow( $row );
248 $validRevision = (bool)$rev->getId();
249 } catch ( Exception $e ) {
250 $validRevision = false;
251 }
252 Wikimedia\restoreWarnings();
253
254 if ( $validRevision ) {
255 $attribs['data-mw-revid'] = $rev->getId();
256 $ret = $this->formatRevisionRow( $row );
257 }
258
259 // Let extensions add data
260 Hooks::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] );
261 $attribs = array_filter( $attribs,
262 [ Sanitizer::class, 'isReservedDataAttribute' ],
263 ARRAY_FILTER_USE_KEY
264 );
265
266 if ( $classes === [] && $attribs === [] && $ret === '' ) {
267 wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
268 $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
269 } else {
270 $attribs['class'] = $classes;
271 $ret = Html::rawElement( 'li', $attribs, $ret ) . "\n";
272 }
273
274 return $ret;
275 }
276
277 /**
278 * Generates each row in the contributions list for archive entries.
279 *
280 * Contributions which are marked "top" are currently on top of the history.
281 * For these contributions, a [rollback] link is shown for users with sysop
282 * privileges. The rollback link restores the most recent version that was not
283 * written by the target user.
284 *
285 * @todo This would probably look a lot nicer in a table.
286 * @param stdClass $row
287 * @return string
288 */
289 function formatRevisionRow( $row ) {
290 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
291
292 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
293
294 $rev = new Revision( [
295 'title' => $page,
296 'id' => $row->ar_rev_id,
297 'comment' => CommentStore::getStore()->getComment( 'ar_comment', $row )->text,
298 'user' => $row->ar_user,
299 'user_text' => $row->ar_user_text,
300 'actor' => $row->ar_actor ?? null,
301 'timestamp' => $row->ar_timestamp,
302 'minor_edit' => $row->ar_minor_edit,
303 'deleted' => $row->ar_deleted,
304 ] );
305
306 $undelete = SpecialPage::getTitleFor( 'Undelete' );
307
308 $logs = SpecialPage::getTitleFor( 'Log' );
309 $dellog = $linkRenderer->makeKnownLink(
310 $logs,
311 $this->messages['deletionlog'],
312 [],
313 [
314 'type' => 'delete',
315 'page' => $page->getPrefixedText()
316 ]
317 );
318
319 $reviewlink = $linkRenderer->makeKnownLink(
320 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
321 $this->messages['undeleteviewlink']
322 );
323
324 $user = $this->getUser();
325
326 if ( $user->isAllowed( 'deletedtext' ) ) {
327 $last = $linkRenderer->makeKnownLink(
328 $undelete,
329 $this->messages['diff'],
330 [],
331 [
332 'target' => $page->getPrefixedText(),
333 'timestamp' => $rev->getTimestamp(),
334 'diff' => 'prev'
335 ]
336 );
337 } else {
338 $last = htmlspecialchars( $this->messages['diff'] );
339 }
340
341 $comment = Linker::revComment( $rev );
342 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
343
344 if ( !$user->isAllowed( 'undelete' ) || !$rev->userCan( RevisionRecord::DELETED_TEXT, $user ) ) {
345 $link = htmlspecialchars( $date ); // unusable link
346 } else {
347 $link = $linkRenderer->makeKnownLink(
348 $undelete,
349 $date,
350 [ 'class' => 'mw-changeslist-date' ],
351 [
352 'target' => $page->getPrefixedText(),
353 'timestamp' => $rev->getTimestamp()
354 ]
355 );
356 }
357 // Style deleted items
358 if ( $rev->isDeleted( RevisionRecord::DELETED_TEXT ) ) {
359 $link = '<span class="history-deleted">' . $link . '</span>';
360 }
361
362 $pagelink = $linkRenderer->makeLink(
363 $page,
364 null,
365 [ 'class' => 'mw-changeslist-title' ]
366 );
367
368 if ( $rev->isMinor() ) {
369 $mflag = ChangesList::flag( 'minor' );
370 } else {
371 $mflag = '';
372 }
373
374 // Revision delete link
375 $del = Linker::getRevDeleteLink( $user, $rev, $page );
376 if ( $del ) {
377 $del .= ' ';
378 }
379
380 $tools = Html::rawElement(
381 'span',
382 [ 'class' => 'mw-deletedcontribs-tools' ],
383 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
384 [ $last, $dellog, $reviewlink ] ) )->escaped()
385 );
386
387 $separator = '<span class="mw-changeslist-separator">. .</span>';
388 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
389
390 # Denote if username is redacted for this edit
391 if ( $rev->isDeleted( RevisionRecord::DELETED_USER ) ) {
392 $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
393 }
394
395 return $ret;
396 }
397
398 /**
399 * Get the Database object in use
400 *
401 * @return IDatabase
402 */
403 public function getDatabase() {
404 return $this->mDb;
405 }
406 }