]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/specials/pagers/DeletedContribsPager.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.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\ResultWrapper;
27 use Wikimedia\Rdbms\FakeResultWrapper;
28
29 class DeletedContribsPager extends IndexPager {
30
31         public $mDefaultDirection = IndexPager::DIR_DESCENDING;
32         public $messages;
33         public $target;
34         public $namespace = '';
35         public $mDb;
36
37         /**
38          * @var string Navigation bar with paging links.
39          */
40         protected $mNavigationBar;
41
42         function __construct( IContextSource $context, $target, $namespace = false ) {
43                 parent::__construct( $context );
44                 $msgs = [ 'deletionlog', 'undeleteviewlink', 'diff' ];
45                 foreach ( $msgs as $msg ) {
46                         $this->messages[$msg] = $this->msg( $msg )->text();
47                 }
48                 $this->target = $target;
49                 $this->namespace = $namespace;
50                 $this->mDb = wfGetDB( DB_REPLICA, 'contributions' );
51         }
52
53         function getDefaultQuery() {
54                 $query = parent::getDefaultQuery();
55                 $query['target'] = $this->target;
56
57                 return $query;
58         }
59
60         function getQueryInfo() {
61                 list( $index, $userCond ) = $this->getUserCond();
62                 $conds = array_merge( $userCond, $this->getNamespaceCond() );
63                 $user = $this->getUser();
64                 // Paranoia: avoid brute force searches (T19792)
65                 if ( !$user->isAllowed( 'deletedhistory' ) ) {
66                         $conds[] = $this->mDb->bitAnd( 'ar_deleted', Revision::DELETED_USER ) . ' = 0';
67                 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
68                         $conds[] = $this->mDb->bitAnd( 'ar_deleted', Revision::SUPPRESSED_USER ) .
69                                 ' != ' . Revision::SUPPRESSED_USER;
70                 }
71
72                 $commentQuery = CommentStore::newKey( 'ar_comment' )->getJoin();
73
74                 return [
75                         'tables' => [ 'archive' ] + $commentQuery['tables'],
76                         'fields' => [
77                                 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp',
78                                 'ar_minor_edit', 'ar_user', 'ar_user_text', 'ar_deleted'
79                         ] + $commentQuery['fields'],
80                         'conds' => $conds,
81                         'options' => [ 'USE INDEX' => [ 'archive' => $index ] ],
82                         'join_conds' => $commentQuery['joins'],
83                 ];
84         }
85
86         /**
87          * This method basically executes the exact same code as the parent class, though with
88          * a hook added, to allow extensions to add additional queries.
89          *
90          * @param string $offset Index offset, inclusive
91          * @param int $limit Exact query limit
92          * @param bool $descending Query direction, false for ascending, true for descending
93          * @return ResultWrapper
94          */
95         function reallyDoQuery( $offset, $limit, $descending ) {
96                 $data = [ parent::reallyDoQuery( $offset, $limit, $descending ) ];
97
98                 // This hook will allow extensions to add in additional queries, nearly
99                 // identical to ContribsPager::reallyDoQuery.
100                 Hooks::run(
101                         'DeletedContribsPager::reallyDoQuery',
102                         [ &$data, $this, $offset, $limit, $descending ]
103                 );
104
105                 $result = [];
106
107                 // loop all results and collect them in an array
108                 foreach ( $data as $query ) {
109                         foreach ( $query as $i => $row ) {
110                                 // use index column as key, allowing us to easily sort in PHP
111                                 $result[$row->{$this->getIndexField()} . "-$i"] = $row;
112                         }
113                 }
114
115                 // sort results
116                 if ( $descending ) {
117                         ksort( $result );
118                 } else {
119                         krsort( $result );
120                 }
121
122                 // enforce limit
123                 $result = array_slice( $result, 0, $limit );
124
125                 // get rid of array keys
126                 $result = array_values( $result );
127
128                 return new FakeResultWrapper( $result );
129         }
130
131         function getUserCond() {
132                 $condition = [];
133
134                 $condition['ar_user_text'] = $this->target;
135                 $index = 'ar_usertext_timestamp';
136
137                 return [ $index, $condition ];
138         }
139
140         function getIndexField() {
141                 return 'ar_timestamp';
142         }
143
144         function getStartBody() {
145                 return "<ul>\n";
146         }
147
148         function getEndBody() {
149                 return "</ul>\n";
150         }
151
152         function getNavigationBar() {
153                 if ( isset( $this->mNavigationBar ) ) {
154                         return $this->mNavigationBar;
155                 }
156
157                 $linkTexts = [
158                         'prev' => $this->msg( 'pager-newer-n' )->numParams( $this->mLimit )->escaped(),
159                         'next' => $this->msg( 'pager-older-n' )->numParams( $this->mLimit )->escaped(),
160                         'first' => $this->msg( 'histlast' )->escaped(),
161                         'last' => $this->msg( 'histfirst' )->escaped()
162                 ];
163
164                 $pagingLinks = $this->getPagingLinks( $linkTexts );
165                 $limitLinks = $this->getLimitLinks();
166                 $lang = $this->getLanguage();
167                 $limits = $lang->pipeList( $limitLinks );
168
169                 $firstLast = $lang->pipeList( [ $pagingLinks['first'], $pagingLinks['last'] ] );
170                 $firstLast = $this->msg( 'parentheses' )->rawParams( $firstLast )->escaped();
171                 $prevNext = $this->msg( 'viewprevnext' )
172                         ->rawParams(
173                                 $pagingLinks['prev'],
174                                 $pagingLinks['next'],
175                                 $limits
176                         )->escaped();
177                 $separator = $this->msg( 'word-separator' )->escaped();
178                 $this->mNavigationBar = $firstLast . $separator . $prevNext;
179
180                 return $this->mNavigationBar;
181         }
182
183         function getNamespaceCond() {
184                 if ( $this->namespace !== '' ) {
185                         return [ 'ar_namespace' => (int)$this->namespace ];
186                 } else {
187                         return [];
188                 }
189         }
190
191         /**
192          * Generates each row in the contributions list.
193          *
194          * @todo This would probably look a lot nicer in a table.
195          * @param stdClass $row
196          * @return string
197          */
198         function formatRow( $row ) {
199                 $ret = '';
200                 $classes = [];
201                 $attribs = [];
202
203                 /*
204                  * There may be more than just revision rows. To make sure that we'll only be processing
205                  * revisions here, let's _try_ to build a revision out of our row (without displaying
206                  * notices though) and then trying to grab data from the built object. If we succeed,
207                  * we're definitely dealing with revision data and we may proceed, if not, we'll leave it
208                  * to extensions to subscribe to the hook to parse the row.
209                  */
210                 MediaWiki\suppressWarnings();
211                 try {
212                         $rev = Revision::newFromArchiveRow( $row );
213                         $validRevision = (bool)$rev->getId();
214                 } catch ( Exception $e ) {
215                         $validRevision = false;
216                 }
217                 MediaWiki\restoreWarnings();
218
219                 if ( $validRevision ) {
220                         $attribs['data-mw-revid'] = $rev->getId();
221                         $ret = $this->formatRevisionRow( $row );
222                 }
223
224                 // Let extensions add data
225                 Hooks::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] );
226                 $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] );
227
228                 if ( $classes === [] && $attribs === [] && $ret === '' ) {
229                         wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" );
230                         $ret = "<!-- Could not format Special:DeletedContribution row. -->\n";
231                 } else {
232                         $attribs['class'] = $classes;
233                         $ret = Html::rawElement( 'li', $attribs, $ret ) . "\n";
234                 }
235
236                 return $ret;
237         }
238
239         /**
240          * Generates each row in the contributions list for archive entries.
241          *
242          * Contributions which are marked "top" are currently on top of the history.
243          * For these contributions, a [rollback] link is shown for users with sysop
244          * privileges. The rollback link restores the most recent version that was not
245          * written by the target user.
246          *
247          * @todo This would probably look a lot nicer in a table.
248          * @param stdClass $row
249          * @return string
250          */
251         function formatRevisionRow( $row ) {
252                 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
253
254                 $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
255
256                 $rev = new Revision( [
257                         'title' => $page,
258                         'id' => $row->ar_rev_id,
259                         'comment' => CommentStore::newKey( 'ar_comment' )->getComment( $row )->text,
260                         'user' => $row->ar_user,
261                         'user_text' => $row->ar_user_text,
262                         'timestamp' => $row->ar_timestamp,
263                         'minor_edit' => $row->ar_minor_edit,
264                         'deleted' => $row->ar_deleted,
265                 ] );
266
267                 $undelete = SpecialPage::getTitleFor( 'Undelete' );
268
269                 $logs = SpecialPage::getTitleFor( 'Log' );
270                 $dellog = $linkRenderer->makeKnownLink(
271                         $logs,
272                         $this->messages['deletionlog'],
273                         [],
274                         [
275                                 'type' => 'delete',
276                                 'page' => $page->getPrefixedText()
277                         ]
278                 );
279
280                 $reviewlink = $linkRenderer->makeKnownLink(
281                         SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
282                         $this->messages['undeleteviewlink']
283                 );
284
285                 $user = $this->getUser();
286
287                 if ( $user->isAllowed( 'deletedtext' ) ) {
288                         $last = $linkRenderer->makeKnownLink(
289                                 $undelete,
290                                 $this->messages['diff'],
291                                 [],
292                                 [
293                                         'target' => $page->getPrefixedText(),
294                                         'timestamp' => $rev->getTimestamp(),
295                                         'diff' => 'prev'
296                                 ]
297                         );
298                 } else {
299                         $last = htmlspecialchars( $this->messages['diff'] );
300                 }
301
302                 $comment = Linker::revComment( $rev );
303                 $date = $this->getLanguage()->userTimeAndDate( $rev->getTimestamp(), $user );
304
305                 if ( !$user->isAllowed( 'undelete' ) || !$rev->userCan( Revision::DELETED_TEXT, $user ) ) {
306                         $link = htmlspecialchars( $date ); // unusable link
307                 } else {
308                         $link = $linkRenderer->makeKnownLink(
309                                 $undelete,
310                                 $date,
311                                 [ 'class' => 'mw-changeslist-date' ],
312                                 [
313                                         'target' => $page->getPrefixedText(),
314                                         'timestamp' => $rev->getTimestamp()
315                                 ]
316                         );
317                 }
318                 // Style deleted items
319                 if ( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
320                         $link = '<span class="history-deleted">' . $link . '</span>';
321                 }
322
323                 $pagelink = $linkRenderer->makeLink(
324                         $page,
325                         null,
326                         [ 'class' => 'mw-changeslist-title' ]
327                 );
328
329                 if ( $rev->isMinor() ) {
330                         $mflag = ChangesList::flag( 'minor' );
331                 } else {
332                         $mflag = '';
333                 }
334
335                 // Revision delete link
336                 $del = Linker::getRevDeleteLink( $user, $rev, $page );
337                 if ( $del ) {
338                         $del .= ' ';
339                 }
340
341                 $tools = Html::rawElement(
342                         'span',
343                         [ 'class' => 'mw-deletedcontribs-tools' ],
344                         $this->msg( 'parentheses' )->rawParams( $this->getLanguage()->pipeList(
345                                 [ $last, $dellog, $reviewlink ] ) )->escaped()
346                 );
347
348                 $separator = '<span class="mw-changeslist-separator">. .</span>';
349                 $ret = "{$del}{$link} {$tools} {$separator} {$mflag} {$pagelink} {$comment}";
350
351                 # Denote if username is redacted for this edit
352                 if ( $rev->isDeleted( Revision::DELETED_USER ) ) {
353                         $ret .= " <strong>" . $this->msg( 'rev-deleted-user-contribs' )->escaped() . "</strong>";
354                 }
355
356                 return $ret;
357         }
358
359         /**
360          * Get the Database object in use
361          *
362          * @return IDatabase
363          */
364         public function getDatabase() {
365                 return $this->mDb;
366         }
367 }