Follow up to r56639: Remove some existence check duplication and fix ApiUpload for...
[lhc/web/wiklou.git] / includes / specials / SpecialDeletedContributions.php
1 <?php
2 /**
3 * Implements Special:DeletedContributions to display archived revisions
4 * @ingroup SpecialPage
5 */
6
7 class DeletedContribsPager extends IndexPager {
8 public $mDefaultDirection = true;
9 var $messages, $target;
10 var $namespace = '', $mDb;
11
12 function __construct( $target, $namespace = false ) {
13 parent::__construct();
14 foreach( explode( ' ', 'deletionlog undeleteviewlink diff' ) as $msg ) {
15 $this->messages[$msg] = wfMsgExt( $msg, array( 'escape') );
16 }
17 $this->target = $target;
18 $this->namespace = $namespace;
19 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
20 }
21
22 function getDefaultQuery() {
23 $query = parent::getDefaultQuery();
24 $query['target'] = $this->target;
25 return $query;
26 }
27
28 function getQueryInfo() {
29 global $wgUser;
30 list( $index, $userCond ) = $this->getUserCond();
31 $conds = array_merge( $userCond, $this->getNamespaceCond() );
32 // Paranoia: avoid brute force searches (bug 17792)
33 if( !$wgUser->isAllowed( 'deleterevision' ) ) {
34 $conds[] = $this->mDb->bitAnd('ar_deleted',Revision::DELETED_USER) . ' = 0';
35 } else if( !$wgUser->isAllowed( 'suppressrevision' ) ) {
36 $conds[] = $this->mDb->bitAnd('ar_deleted',Revision::SUPPRESSED_USER) .
37 ' != ' . Revision::SUPPRESSED_USER;
38 }
39 return array(
40 'tables' => array( 'archive' ),
41 'fields' => array(
42 'ar_rev_id', 'ar_namespace', 'ar_title', 'ar_timestamp', 'ar_comment', 'ar_minor_edit',
43 'ar_user', 'ar_user_text', 'ar_deleted'
44 ),
45 'conds' => $conds,
46 'options' => array( 'USE INDEX' => $index )
47 );
48 }
49
50 function getUserCond() {
51 $condition = array();
52
53 $condition['ar_user_text'] = $this->target;
54 $index = 'usertext_timestamp';
55
56 return array( $index, $condition );
57 }
58
59 function getIndexField() {
60 return 'ar_timestamp';
61 }
62
63 function getStartBody() {
64 return "<ul>\n";
65 }
66
67 function getEndBody() {
68 return "</ul>\n";
69 }
70
71 function getNavigationBar() {
72 global $wgLang;
73
74 if ( isset( $this->mNavigationBar ) ) {
75 return $this->mNavigationBar;
76 }
77 $fmtLimit = $wgLang->formatNum( $this->mLimit );
78 $linkTexts = array(
79 'prev' => wfMsgExt( 'pager-newer-n', array( 'escape', 'parsemag' ), $fmtLimit ),
80 'next' => wfMsgExt( 'pager-older-n', array( 'escape', 'parsemag' ), $fmtLimit ),
81 'first' => wfMsgHtml( 'histlast' ),
82 'last' => wfMsgHtml( 'histfirst' )
83 );
84
85 $pagingLinks = $this->getPagingLinks( $linkTexts );
86 $limitLinks = $this->getLimitLinks();
87 $limits = $wgLang->pipeList( $limitLinks );
88
89 $this->mNavigationBar = "(" . $wgLang->pipeList( array( $pagingLinks['first'], $pagingLinks['last'] ) ) . ") " .
90 wfMsgExt( 'viewprevnext', array( 'parsemag', 'escape', 'replaceafter' ), $pagingLinks['prev'], $pagingLinks['next'], $limits );
91 return $this->mNavigationBar;
92 }
93
94 function getNamespaceCond() {
95 if ( $this->namespace !== '' ) {
96 return array( 'ar_namespace' => (int)$this->namespace );
97 } else {
98 return array();
99 }
100 }
101
102 /**
103 * Generates each row in the contributions list.
104 *
105 * Contributions which are marked "top" are currently on top of the history.
106 * For these contributions, a [rollback] link is shown for users with sysop
107 * privileges. The rollback link restores the most recent version that was not
108 * written by the target user.
109 *
110 * @todo This would probably look a lot nicer in a table.
111 */
112 function formatRow( $row ) {
113 global $wgUser, $wgLang;
114 wfProfileIn( __METHOD__ );
115
116 $sk = $this->getSkin();
117
118 $rev = new Revision( array(
119 'id' => $row->ar_rev_id,
120 'comment' => $row->ar_comment,
121 'user' => $row->ar_user,
122 'user_text' => $row->ar_user_text,
123 'timestamp' => $row->ar_timestamp,
124 'minor_edit' => $row->ar_minor_edit,
125 'deleted' => $row->ar_deleted,
126 ) );
127
128 $page = Title::makeTitle( $row->ar_namespace, $row->ar_title );
129
130 $undelete = SpecialPage::getTitleFor( 'Undelete' );
131
132 $logs = SpecialPage::getTitleFor( 'Log' );
133 $dellog = $sk->linkKnown(
134 $logs,
135 $this->messages['deletionlog'],
136 array(),
137 array(
138 'type' => 'delete',
139 'page' => $page->getPrefixedText()
140 )
141 );
142
143 $reviewlink = $sk->linkKnown(
144 SpecialPage::getTitleFor( 'Undelete', $page->getPrefixedDBkey() ),
145 $this->messages['undeleteviewlink']
146 );
147
148 if( $wgUser->isAllowed('undelete') ) {
149 $last = $sk->linkKnown(
150 $undelete,
151 $this->messages['diff'],
152 array(),
153 array(
154 'target' => $page->getPrefixedText(),
155 'timestamp' => $rev->getTimestamp(),
156 'diff' => 'prev'
157 )
158 );
159 } else {
160 $last = $this->messages['diff'];
161 }
162
163 $comment = $sk->revComment( $rev );
164 $date = htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) );
165
166 if( !$wgUser->isAllowed('undelete') || !$rev->userCan(Revision::DELETED_TEXT) ) {
167 $link = $date; // unusable link
168 } else {
169 $link = $sk->linkKnown(
170 $undelete,
171 $date,
172 array(),
173 array(
174 'target' => $page->getPrefixedText(),
175 'timestamp' => $rev->getTimestamp()
176 )
177 );
178 }
179 // Style deleted items
180 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
181 $link = '<span class="history-deleted">' . $link . '</span>';
182 }
183
184 $pagelink = $sk->link( $page );
185
186 if( $rev->isMinor() ) {
187 $mflag = ChangesList::flag( 'minor' );
188 } else {
189 $mflag = '';
190 }
191
192 if( $wgUser->isAllowed( 'deleterevision' ) ) {
193 // If revision was hidden from sysops
194 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
195 $del = Xml::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
196 '(' . $this->message['rev-delundel'] . ')' ) . ' ';
197 // Otherwise, show the link...
198 } else {
199 $query = array(
200 'type' => 'archive',
201 'target' => $page->getPrefixedDbkey(),
202 'ids' => $rev->getTimestamp() );
203 $del = $this->mSkin->revDeleteLink( $query,
204 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ) . ' ';
205 }
206 } else {
207 $del = '';
208 }
209
210 $ret = "{$del}{$link} ({$last}) ({$dellog}) ({$reviewlink}) . . {$mflag} {$pagelink} {$comment}";
211
212 $ret = "<li>$ret</li>\n";
213
214 wfProfileOut( __METHOD__ );
215 return $ret;
216 }
217
218 /**
219 * Get the Database object in use
220 *
221 * @return Database
222 */
223 public function getDatabase() {
224 return $this->mDb;
225 }
226 }
227
228 class DeletedContributionsPage extends SpecialPage {
229 function __construct() {
230 parent::__construct( 'DeletedContributions', 'deletedhistory',
231 /*listed*/ true, /*function*/ false, /*file*/ false );
232 }
233
234 /**
235 * Special page "deleted user contributions".
236 * Shows a list of the deleted contributions of a user.
237 *
238 * @return none
239 * @param $par String: (optional) user name of the user for which to show the contributions
240 */
241 function execute( $par ) {
242 global $wgUser;
243 $this->setHeaders();
244
245 if ( !$this->userCanExecute( $wgUser ) ) {
246 $this->displayRestrictionError();
247 return;
248 }
249
250 global $wgUser, $wgOut, $wgLang, $wgRequest;
251
252 $wgOut->setPageTitle( wfMsgExt( 'deletedcontributions-title', array( 'parsemag' ) ) );
253
254 $options = array();
255
256 if ( isset( $par ) ) {
257 $target = $par;
258 } else {
259 $target = $wgRequest->getVal( 'target' );
260 }
261
262 if ( !strlen( $target ) ) {
263 $wgOut->addHTML( $this->getForm( '' ) );
264 return;
265 }
266
267 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
268 $options['target'] = $target;
269
270 $nt = Title::makeTitleSafe( NS_USER, $target );
271 if ( !$nt ) {
272 $wgOut->addHTML( $this->getForm( '' ) );
273 return;
274 }
275 $id = User::idFromName( $nt->getText() );
276
277 $target = $nt->getText();
278 $wgOut->setSubtitle( $this->getSubTitle( $nt, $id ) );
279
280 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
281 $options['namespace'] = intval( $ns );
282 } else {
283 $options['namespace'] = '';
284 }
285
286 $wgOut->addHTML( $this->getForm( $options ) );
287
288 $pager = new DeletedContribsPager( $target, $options['namespace'] );
289 if ( !$pager->getNumRows() ) {
290 $wgOut->addWikiMsg( 'nocontribs' );
291 return;
292 }
293
294 # Show a message about slave lag, if applicable
295 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
296 $wgOut->showLagWarning( $lag );
297
298 $wgOut->addHTML(
299 '<p>' . $pager->getNavigationBar() . '</p>' .
300 $pager->getBody() .
301 '<p>' . $pager->getNavigationBar() . '</p>' );
302
303 # If there were contributions, and it was a valid user or IP, show
304 # the appropriate "footer" message - WHOIS tools, etc.
305 if( $target != 'newbies' ) {
306 $message = IP::isIPAddress( $target )
307 ? 'sp-contributions-footer-anon'
308 : 'sp-contributions-footer';
309
310
311 $text = wfMsgNoTrans( $message, $target );
312 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
313 $wgOut->wrapWikiMsg( "<div class='mw-contributions-footer'>\n$1\n</div>", array( $message, $target ) );
314 }
315 }
316 }
317
318 /**
319 * Generates the subheading with links
320 * @param $nt @see Title object for the target
321 */
322 function getSubTitle( $nt, $id ) {
323 global $wgSysopUserBans, $wgLang, $wgUser;
324
325 $sk = $wgUser->getSkin();
326
327 if ( 0 == $id ) {
328 $user = htmlspecialchars( $nt->getText() );
329 } else {
330 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
331 }
332 $talk = $nt->getTalkPage();
333 if( $talk ) {
334 # Talk page link
335 $tools[] = $sk->link( $talk, wfMsgHtml( 'talkpagelinktext' ) );
336 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
337 # Block link
338 if( $wgUser->isAllowed( 'block' ) )
339 $tools[] = $sk->linkKnown(
340 SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
341 wfMsgHtml( 'blocklink' )
342 );
343 # Block log link
344 $tools[] = $sk->linkKnown(
345 SpecialPage::getTitleFor( 'Log' ),
346 wfMsgHtml( 'sp-contributions-blocklog' ),
347 array(),
348 array(
349 'type' => 'block',
350 'page' => $nt->getPrefixedText()
351 )
352 );
353 }
354 # Other logs link
355 $tools[] = $sk->linkKnown(
356 SpecialPage::getTitleFor( 'Log' ),
357 wfMsgHtml( 'sp-contributions-logs' ),
358 array(),
359 array( 'user' => $nt->getText() )
360 );
361 # Link to undeleted contributions
362 $tools[] = $sk->linkKnown(
363 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
364 wfMsgHtml( 'sp-deletedcontributions-contribs' )
365 );
366
367 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
368
369 $links = $wgLang->pipeList( $tools );
370 }
371
372 // Old message 'contribsub' had one parameter, but that doesn't work for
373 // languages that want to put the "for" bit right after $user but before
374 // $links. If 'contribsub' is around, use it for reverse compatibility,
375 // otherwise use 'contribsub2'.
376 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
377 return wfMsgHtml( 'contribsub2', $user, $links );
378 } else {
379 return wfMsgHtml( 'contribsub', "$user ($links)" );
380 }
381 }
382
383 /**
384 * Generates the namespace selector form with hidden attributes.
385 * @param $options Array: the options to be included.
386 */
387 function getForm( $options ) {
388 global $wgScript, $wgRequest;
389
390 $options['title'] = SpecialPage::getTitleFor( 'DeletedContributions' )->getPrefixedText();
391 if ( !isset( $options['target'] ) ) {
392 $options['target'] = '';
393 } else {
394 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
395 }
396
397 if ( !isset( $options['namespace'] ) ) {
398 $options['namespace'] = '';
399 }
400
401 if ( !isset( $options['contribs'] ) ) {
402 $options['contribs'] = 'user';
403 }
404
405 if ( $options['contribs'] == 'newbie' ) {
406 $options['target'] = '';
407 }
408
409 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
410
411 foreach ( $options as $name => $value ) {
412 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
413 continue;
414 }
415 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
416 }
417
418 $f .= Xml::openElement( 'fieldset' ) .
419 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
420 Xml::tags( 'label', array( 'for' => 'target' ), wfMsgExt( 'sp-contributions-username', 'parseinline' ) ) . ' ' .
421 Html::input( 'target', $options['target'], 'text', array(
422 'size' => '20',
423 'required' => ''
424 ) + ( $options['target'] ? array() : array( 'autofocus' ) ) ) . ' '.
425 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
426 Xml::namespaceSelector( $options['namespace'], '' ) . ' ' .
427 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
428 Xml::closeElement( 'fieldset' ) .
429 Xml::closeElement( 'form' );
430 return $f;
431 }
432 }