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