Merge "Add CollationFa"
[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
27 class DeletedContribsPager extends IndexPager {
28
29 public $mDefaultDirection = IndexPager::DIR_DESCENDING;
30 public $messages;
31 public $target;
32 public $namespace = '';
33 public $mDb;
34
35 /**
36 * @var string Navigation bar with paging links.
37 */
38 protected $mNavigationBar;
39
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();
45 }
46 $this->target = $target;
47 $this->namespace = $namespace;
48 $this->mDb = wfGetDB( DB_REPLICA, 'contributions' );
49 }
50
51 function getDefaultQuery() {
52 $query = parent::getDefaultQuery();
53 $query['target'] = $this->target;
54
55 return $query;
56 }
57
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;
68 }
69
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'
75 ],
76 'conds' => $conds,
77 'options' => [ 'USE INDEX' => $index ]
78 ];
79 }
80
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.
84 *
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
89 */
90 function reallyDoQuery( $offset, $limit, $descending ) {
91 $data = [ parent::reallyDoQuery( $offset, $limit, $descending ) ];
92
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 ]
98 );
99
100 $result = [];
101
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;
107 }
108 }
109
110 // sort results
111 if ( $descending ) {
112 ksort( $result );
113 } else {
114 krsort( $result );
115 }
116
117 // enforce limit
118 $result = array_slice( $result, 0, $limit );
119
120 // get rid of array keys
121 $result = array_values( $result );
122
123 return new FakeResultWrapper( $result );
124 }
125
126 function getUserCond() {
127 $condition = [];
128
129 $condition['ar_user_text'] = $this->target;
130 $index = 'usertext_timestamp';
131
132 return [ $index, $condition ];
133 }
134
135 function getIndexField() {
136 return 'ar_timestamp';
137 }
138
139 function getStartBody() {
140 return "<ul>\n";
141 }
142
143 function getEndBody() {
144 return "</ul>\n";
145 }
146
147 function getNavigationBar() {
148 if ( isset( $this->mNavigationBar ) ) {
149 return $this->mNavigationBar;
150 }
151
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()
157 ];
158
159 $pagingLinks = $this->getPagingLinks( $linkTexts );
160 $limitLinks = $this->getLimitLinks();
161 $lang = $this->getLanguage();
162 $limits = $lang->pipeList( $limitLinks );
163
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;
174
175 return $this->mNavigationBar;
176 }
177
178 function getNamespaceCond() {
179 if ( $this->namespace !== '' ) {
180 return [ 'ar_namespace' => (int)$this->namespace ];
181 } else {
182 return [];
183 }
184 }
185
186 /**
187 * Generates each row in the contributions list.
188 *
189 * @todo This would probably look a lot nicer in a table.
190 * @param stdClass $row
191 * @return string
192 */
193 function formatRow( $row ) {
194 $ret = '';
195 $classes = [];
196
197 /*
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.
203 */
204 MediaWiki\suppressWarnings();
205 try {
206 $rev = Revision::newFromArchiveRow( $row );
207 $validRevision = (bool)$rev->getId();
208 } catch ( Exception $e ) {
209 $validRevision = false;
210 }
211 MediaWiki\restoreWarnings();
212
213 if ( $validRevision ) {
214 $ret = $this->formatRevisionRow( $row );
215 }
216
217 // Let extensions add data
218 Hooks::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes ] );
219
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";
225 }
226
227 return $ret;
228 }
229
230 /**
231 * Generates each row in the contributions list for archive entries.
232 *
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.
237 *
238 * @todo This would probably look a lot nicer in a table.
239 * @param stdClass $row
240 * @return string
241 */
242 function formatRevisionRow( $row ) {
243 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
244
245 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
246
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 ] );
257
258 $undelete = SpecialPage::getTitleFor( 'Undelete' );
259
260 $logs = SpecialPage::getTitleFor( 'Log' );
261 $dellog = $linkRenderer->makeKnownLink(
262 $logs,
263 $this->messages['deletionlog'],
264 [],
265 [
266 'type' => 'delete',
267 'page' => $page->getPrefixedText()
268 ]
269 );
270
271 $reviewlink = $linkRenderer->makeKnownLink(
272 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
273 $this->messages['undeleteviewlink']
274 );
275
276 $user = $this->getUser();
277
278 if ( $user->isAllowed( 'deletedtext' ) ) {
279 $last = $linkRenderer->makeKnownLink(
280 $undelete,
281 $this->messages['diff'],
282 [],
283 [
284 'target' => $page->getPrefixedText(),
285 'timestamp' => $rev->getTimestamp(),
286 'diff' => 'prev'
287 ]
288 );
289 } else {
290 $last = htmlspecialchars( $this->messages['diff'] );
291 }
292
293 $comment = Linker::revComment( $rev );
294 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
295
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' ],
303 [
304 'target' => $page->getPrefixedText(),
305 'timestamp' => $rev->getTimestamp()
306 ]
307 );
308 }
309 // Style deleted items
310 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
311 $link = '<span class="history-deleted">' . $link . '</span>';
312 }
313
314 $pagelink = $linkRenderer->makeLink(
315 $page,
316 null,
317 [ 'class' => 'mw-changeslist-title' ]
318 );
319
320 if ( $rev->isMinor() ) {
321 $mflag = ChangesList::flag( 'minor' );
322 } else {
323 $mflag = '';
324 }
325
326 // Revision delete link
327 $del = Linker::getRevDeleteLink( $user, $rev, $page );
328 if ( $del ) {
329 $del .= ' ';
330 }
331
332 $tools = Html::rawElement(
333 'span',
334 [ 'class' => 'mw-deletedcontribs-tools' ],
335 $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
336 [ $last, $dellog, $reviewlink ] ) )->escaped()
337 );
338
339 $separator = '<span class="mw-changeslist-separator">. .</span>';
340 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
341
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>";
345 }
346
347 return $ret;
348 }
349
350 /**
351 * Get the Database object in use
352 *
353 * @return IDatabase
354 */
355 public function getDatabase() {
356 return $this->mDb;
357 }
358 }