clean up bizarre code - use timestamp for links again (as it is on deployment)
[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 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( '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 $last = $sk->linkKnown(
149 $undelete,
150 $this->messages['diff'],
151 array(),
152 array(
153 'target' => $page->getPrefixedText(),
154 'timestamp' => $rev->getTimestamp(),
155 'diff' => 'prev'
156 )
157 );
158
159 $comment = $sk->revComment( $rev );
160 $date = htmlspecialchars( $wgLang->timeanddate( $rev->getTimestamp(), true ) );
161
162 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
163 $link = '<span class="history-deleted">' . $date . '</span>';
164 } else {
165 $link = $sk->linkKnown(
166 $undelete,
167 $date,
168 array(),
169 array(
170 'target' => $page->getPrefixedText(),
171 'timestamp' => $rev->getTimestamp()
172 )
173 );
174 }
175
176 $pagelink = $sk->link( $page );
177
178 if( $rev->isMinor() ) {
179 $mflag = '<abbr class="minor">' . $this->messages['minoreditletter'] . '</abbr> ';
180 } else {
181 $mflag = '';
182 }
183
184 if( $wgUser->isAllowed( 'deleterevision' ) ) {
185 // If revision was hidden from sysops
186 if( !$rev->userCan( Revision::DELETED_RESTRICTED ) ) {
187 $del = Xml::tags( 'span', array( 'class'=>'mw-revdelundel-link' ),
188 '(' . $this->message['rev-delundel'] . ')' ) . ' ';
189 // Otherwise, show the link...
190 } else {
191 $query = array(
192 'type' => 'archive',
193 'target' => $page->getPrefixedDbkey(),
194 'ids' => $rev->getTimestamp() );
195 $del = $this->mSkin->revDeleteLink( $query,
196 $rev->isDeleted( Revision::DELETED_RESTRICTED ) ) . ' ';
197 }
198 } else {
199 $del = '';
200 }
201
202 $ret = "{$del}{$link} ({$last}) ({$dellog}) ({$reviewlink}) . . {$mflag} {$pagelink} {$comment}";
203
204 $ret = "<li>$ret</li>\n";
205
206 wfProfileOut( __METHOD__ );
207 return $ret;
208 }
209
210 /**
211 * Get the Database object in use
212 *
213 * @return Database
214 */
215 public function getDatabase() {
216 return $this->mDb;
217 }
218 }
219
220 class DeletedContributionsPage extends SpecialPage {
221 function __construct() {
222 parent::__construct( 'DeletedContributions', 'deletedhistory',
223 /*listed*/ true, /*function*/ false, /*file*/ false );
224 }
225
226 /**
227 * Special page "deleted user contributions".
228 * Shows a list of the deleted contributions of a user.
229 *
230 * @return none
231 * @param $par String: (optional) user name of the user for which to show the contributions
232 */
233 function execute( $par ) {
234 global $wgUser;
235 $this->setHeaders();
236
237 if ( !$this->userCanExecute( $wgUser ) ) {
238 $this->displayRestrictionError();
239 return;
240 }
241
242 global $wgUser, $wgOut, $wgLang, $wgRequest;
243
244 $wgOut->setPageTitle( wfMsgExt( 'deletedcontributions-title', array( 'parsemag' ) ) );
245
246 $options = array();
247
248 if ( isset( $par ) ) {
249 $target = $par;
250 } else {
251 $target = $wgRequest->getVal( 'target' );
252 }
253
254 if ( !strlen( $target ) ) {
255 $wgOut->addHTML( $this->getForm( '' ) );
256 return;
257 }
258
259 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
260 $options['target'] = $target;
261
262 $nt = Title::makeTitleSafe( NS_USER, $target );
263 if ( !$nt ) {
264 $wgOut->addHTML( $this->getForm( '' ) );
265 return;
266 }
267 $id = User::idFromName( $nt->getText() );
268
269 $target = $nt->getText();
270 $wgOut->setSubtitle( $this->getSubTitle( $nt, $id ) );
271
272 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
273 $options['namespace'] = intval( $ns );
274 } else {
275 $options['namespace'] = '';
276 }
277
278 $wgOut->addHTML( $this->getForm( $options ) );
279
280 $pager = new DeletedContribsPager( $target, $options['namespace'] );
281 if ( !$pager->getNumRows() ) {
282 $wgOut->addWikiMsg( 'nocontribs' );
283 return;
284 }
285
286 # Show a message about slave lag, if applicable
287 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
288 $wgOut->showLagWarning( $lag );
289
290 $wgOut->addHTML(
291 '<p>' . $pager->getNavigationBar() . '</p>' .
292 $pager->getBody() .
293 '<p>' . $pager->getNavigationBar() . '</p>' );
294
295 # If there were contributions, and it was a valid user or IP, show
296 # the appropriate "footer" message - WHOIS tools, etc.
297 if( $target != 'newbies' ) {
298 $message = IP::isIPAddress( $target )
299 ? 'sp-contributions-footer-anon'
300 : 'sp-contributions-footer';
301
302
303 $text = wfMsgNoTrans( $message, $target );
304 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
305 $wgOut->wrapWikiMsg( "<div class='mw-contributions-footer'>\n$1\n</div>", array( $message, $target ) );
306 }
307 }
308 }
309
310 /**
311 * Generates the subheading with links
312 * @param $nt @see Title object for the target
313 */
314 function getSubTitle( $nt, $id ) {
315 global $wgSysopUserBans, $wgLang, $wgUser;
316
317 $sk = $wgUser->getSkin();
318
319 if ( 0 == $id ) {
320 $user = htmlspecialchars( $nt->getText() );
321 } else {
322 $user = $sk->link( $nt, htmlspecialchars( $nt->getText() ) );
323 }
324 $talk = $nt->getTalkPage();
325 if( $talk ) {
326 # Talk page link
327 $tools[] = $sk->link( $talk, wfMsgHtml( 'talkpagelinktext' ) );
328 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
329 # Block link
330 if( $wgUser->isAllowed( 'block' ) )
331 $tools[] = $sk->linkKnown(
332 SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ),
333 wfMsgHtml( 'blocklink' )
334 );
335 # Block log link
336 $tools[] = $sk->linkKnown(
337 SpecialPage::getTitleFor( 'Log' ),
338 wfMsgHtml( 'sp-contributions-blocklog' ),
339 array(),
340 array(
341 'type' => 'block',
342 'page' => $nt->getPrefixedText()
343 )
344 );
345 }
346 # Other logs link
347 $tools[] = $sk->linkKnown(
348 SpecialPage::getTitleFor( 'Log' ),
349 wfMsgHtml( 'sp-contributions-logs' ),
350 array(),
351 array( 'user' => $nt->getText() )
352 );
353 # Link to undeleted contributions
354 $tools[] = $sk->linkKnown(
355 SpecialPage::getTitleFor( 'Contributions', $nt->getDBkey() ),
356 wfMsgHtml( 'sp-deletedcontributions-contribs' )
357 );
358
359 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
360
361 $links = $wgLang->pipeList( $tools );
362 }
363
364 // Old message 'contribsub' had one parameter, but that doesn't work for
365 // languages that want to put the "for" bit right after $user but before
366 // $links. If 'contribsub' is around, use it for reverse compatibility,
367 // otherwise use 'contribsub2'.
368 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
369 return wfMsgHtml( 'contribsub2', $user, $links );
370 } else {
371 return wfMsgHtml( 'contribsub', "$user ($links)" );
372 }
373 }
374
375 /**
376 * Generates the namespace selector form with hidden attributes.
377 * @param $options Array: the options to be included.
378 */
379 function getForm( $options ) {
380 global $wgScript, $wgRequest;
381
382 $options['title'] = SpecialPage::getTitleFor( 'DeletedContributions' )->getPrefixedText();
383 if ( !isset( $options['target'] ) ) {
384 $options['target'] = '';
385 } else {
386 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
387 }
388
389 if ( !isset( $options['namespace'] ) ) {
390 $options['namespace'] = '';
391 }
392
393 if ( !isset( $options['contribs'] ) ) {
394 $options['contribs'] = 'user';
395 }
396
397 if ( $options['contribs'] == 'newbie' ) {
398 $options['target'] = '';
399 }
400
401 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
402
403 foreach ( $options as $name => $value ) {
404 if ( in_array( $name, array( 'namespace', 'target', 'contribs' ) ) ) {
405 continue;
406 }
407 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
408 }
409
410 $f .= Xml::openElement( 'fieldset' ) .
411 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
412 Xml::tags( 'label', array( 'for' => 'target' ), wfMsgExt( 'sp-contributions-username', 'parseinline' ) ) . ' ' .
413 Xml::input( 'target', 20, $options['target']) . ' '.
414 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
415 Xml::namespaceSelector( $options['namespace'], '' ) . ' ' .
416 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
417 Xml::closeElement( 'fieldset' ) .
418 Xml::closeElement( 'form' );
419 return $f;
420 }
421 }