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