Refactor contribs to use the newer class extension method (about to do bug 6955)
[lhc/web/wiklou.git] / includes / specials / SpecialContributions.php
1 <?php
2 /**
3 * Special:Contributions, show user contributions in a paged list
4 * @file
5 * @ingroup SpecialPage
6 */
7
8 class SpecialContributions extends SpecialPage {
9
10 public function __construct() {
11 parent::__construct( 'Contributions' );
12 }
13
14 function execute( $par ) {
15 global $wgUser, $wgOut, $wgLang, $wgRequest;
16
17 $this->setHeaders();
18 $this->outputHeader();
19
20 $options = array();
21
22 if ( isset( $par ) && $par == 'newbies' ) {
23 $target = 'newbies';
24 $options['contribs'] = 'newbie';
25 } elseif ( isset( $par ) ) {
26 $target = $par;
27 } else {
28 $target = $wgRequest->getVal( 'target' );
29 }
30
31 // check for radiobox
32 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
33 $target = 'newbies';
34 $options['contribs'] = 'newbie';
35 }
36
37 if ( !strlen( $target ) ) {
38 $wgOut->addHTML( $this->getForm( '' ) );
39 return;
40 }
41
42 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
43 $options['target'] = $target;
44
45 $nt = Title::makeTitleSafe( NS_USER, $target );
46 if ( !$nt ) {
47 $wgOut->addHTML( $this->getForm( '' ) );
48 return;
49 }
50 $id = User::idFromName( $nt->getText() );
51
52 if ( $target != 'newbies' ) {
53 $target = $nt->getText();
54 $wgOut->setSubtitle( $this->contributionsSub( $nt, $id ) );
55 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'contributions-title', $target ) ) );
56 } else {
57 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
58 $wgOut->setHTMLTitle( wfMsg( 'pagetitle', wfMsg( 'sp-contributions-newbies-title' ) ) );
59 }
60
61 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
62 $options['namespace'] = intval( $ns );
63 } else {
64 $options['namespace'] = '';
65 }
66
67 // Allows reverts to have the bot flag in recent changes. It is just here to
68 // be passed in the form at the top of the page
69 if ( $wgUser->isAllowed( 'markbotedits' ) && $wgRequest->getBool( 'bot' ) ) {
70 $options['bot'] = '1';
71 }
72
73 $skip = $wgRequest->getText( 'offset' ) || $wgRequest->getText( 'dir' ) == 'prev';
74 # Offset overrides year/month selection
75 if ( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
76 $options['month'] = intval( $month );
77 } else {
78 $options['month'] = '';
79 }
80 if ( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
81 $options['year'] = intval( $year );
82 } else if( $options['month'] ) {
83 $thisMonth = intval( gmdate( 'n' ) );
84 $thisYear = intval( gmdate( 'Y' ) );
85 if( intval( $options['month'] ) > $thisMonth ) {
86 $thisYear--;
87 }
88 $options['year'] = $thisYear;
89 } else {
90 $options['year'] = '';
91 }
92
93 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
94
95 if( $skip ) {
96 $options['year'] = '';
97 $options['month'] = '';
98 }
99
100 $wgOut->addHTML( $this->getForm( $options ) );
101
102 $pager = new ContribsPager( $target, $options['namespace'], $options['year'], $options['month'] );
103 if ( !$pager->getNumRows() ) {
104 $wgOut->addWikiMsg( 'nocontribs' );
105 return;
106 }
107
108 # Show a message about slave lag, if applicable
109 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
110 $wgOut->showLagWarning( $lag );
111
112 $wgOut->addHTML(
113 '<p>' . $pager->getNavigationBar() . '</p>' .
114 $pager->getBody() .
115 '<p>' . $pager->getNavigationBar() . '</p>' );
116
117 # If there were contributions, and it was a valid user or IP, show
118 # the appropriate "footer" message - WHOIS tools, etc.
119 if( $target != 'newbies' ) {
120 $message = IP::isIPAddress( $target )
121 ? 'sp-contributions-footer-anon'
122 : 'sp-contributions-footer';
123
124 $text = wfMsgNoTrans( $message, $target );
125 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
126 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
127 $wgOut->addWikiText( $text );
128 $wgOut->addHtml( '</div>' );
129 }
130 }
131 }
132
133 /**
134 * Generates the subheading with links
135 * @param Title $nt Title object for the target
136 * @param integer $id User ID for the target
137 * @return String: appropriately-escaped HTML to be output literally
138 */
139 protected function contributionsSub( $nt, $id ) {
140 global $wgSysopUserBans, $wgLang, $wgUser;
141
142 $sk = $wgUser->getSkin();
143
144 if ( 0 == $id ) {
145 $user = $nt->getText();
146 } else {
147 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
148 }
149 $talk = $nt->getTalkPage();
150 if( $talk ) {
151 # Talk page link
152 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
153 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && IP::isIPAddress( $nt->getText() ) ) ) {
154 # Block link
155 if( $wgUser->isAllowed( 'block' ) )
156 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip',
157 $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
158 # Block log link
159 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ),
160 wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
161 }
162 # Other logs link
163 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'log' ),
164 'user=' . $nt->getPartialUrl() );
165
166 # Add link to deleted user contributions for priviledged users
167 if( $wgUser->isAllowed( 'deletedhistory' ) ) {
168 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'DeletedContributions',
169 $nt->getDBkey() ), wfMsgHtml( 'deletedcontributions' ) );
170 }
171
172 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
173
174 $links = implode( ' | ', $tools );
175 }
176
177 // Old message 'contribsub' had one parameter, but that doesn't work for
178 // languages that want to put the "for" bit right after $user but before
179 // $links. If 'contribsub' is around, use it for reverse compatibility,
180 // otherwise use 'contribsub2'.
181 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
182 return wfMsgHtml( 'contribsub2', $user, $links );
183 } else {
184 return wfMsgHtml( 'contribsub', "$user ($links)" );
185 }
186 }
187
188 /**
189 * Generates the namespace selector form with hidden attributes.
190 * @param $options Array: the options to be included.
191 */
192 protected function getForm( $options ) {
193 global $wgScript, $wgTitle, $wgRequest;
194
195 $options['title'] = $wgTitle->getPrefixedText();
196 if ( !isset( $options['target'] ) ) {
197 $options['target'] = '';
198 } else {
199 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
200 }
201
202 if ( !isset( $options['namespace'] ) ) {
203 $options['namespace'] = '';
204 }
205
206 if ( !isset( $options['contribs'] ) ) {
207 $options['contribs'] = 'user';
208 }
209
210 if ( !isset( $options['year'] ) ) {
211 $options['year'] = '';
212 }
213
214 if ( !isset( $options['month'] ) ) {
215 $options['month'] = '';
216 }
217
218 if ( $options['contribs'] == 'newbie' ) {
219 $options['target'] = '';
220 }
221
222 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
223
224 foreach ( $options as $name => $value ) {
225 if ( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
226 continue;
227 }
228 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
229 }
230
231 $f .= '<fieldset>' .
232 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
233 Xml::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ),
234 'contribs', 'newbie' , 'newbie', $options['contribs'] == 'newbie' ? true : false ) . '<br />' .
235 Xml::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ),
236 'contribs' , 'user', 'user', $options['contribs'] == 'user' ? true : false ) . ' ' .
237 Xml::input( 'target', 20, $options['target']) . ' '.
238 '<span style="white-space: nowrap">' .
239 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
240 Xml::namespaceSelector( $options['namespace'], '' ) .
241 '</span>' .
242 Xml::openElement( 'p' ) .
243 '<span style="white-space: nowrap">' .
244 Xml::label( wfMsg( 'year' ), 'year' ) . ' '.
245 Xml::input( 'year', 4, $options['year'], array('id' => 'year', 'maxlength' => 4) ) .
246 '</span>' .
247 ' '.
248 '<span style="white-space: nowrap">' .
249 Xml::label( wfMsg( 'month' ), 'month' ) . ' '.
250 Xml::monthSelector( $options['month'], -1 ) . ' '.
251 '</span>' .
252 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
253 Xml::closeElement( 'p' );
254
255 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
256 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
257 $f .= "<p>{$explain}</p>";
258
259 $f .= '</fieldset>' .
260 Xml::closeElement( 'form' );
261 return $f;
262 }
263 }
264
265 /**
266 * Pager for Special:Contributions
267 * @ingroup SpecialPage Pager
268 */
269 class ContribsPager extends ReverseChronologicalPager {
270 public $mDefaultDirection = true;
271 var $messages, $target;
272 var $namespace = '', $mDb;
273
274 function __construct( $target, $namespace = false, $year = false, $month = false ) {
275 parent::__construct();
276 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist newpageletter minoreditletter' ) as $msg ) {
277 $this->messages[$msg] = wfMsgExt( $msg, array( 'escape') );
278 }
279 $this->target = $target;
280 $this->namespace = $namespace;
281
282 $this->getDateCond( $year, $month );
283
284 $this->mDb = wfGetDB( DB_SLAVE, 'contributions' );
285 }
286
287 function getDefaultQuery() {
288 $query = parent::getDefaultQuery();
289 $query['target'] = $this->target;
290 return $query;
291 }
292
293 function getQueryInfo() {
294 list( $tables, $index, $userCond, $join_cond ) = $this->getUserCond();
295 $conds = array_merge( array('page_id=rev_page'), $userCond, $this->getNamespaceCond() );
296 $queryInfo = array(
297 'tables' => $tables,
298 'fields' => array(
299 'page_namespace', 'page_title', 'page_is_new', 'page_latest', 'rev_id', 'rev_page',
300 'rev_text_id', 'rev_timestamp', 'rev_comment', 'rev_minor_edit', 'rev_user',
301 'rev_user_text', 'rev_parent_id', 'rev_deleted'
302 ),
303 'conds' => $conds,
304 'options' => array( 'USE INDEX' => array('revision' => $index) ),
305 'join_conds' => $join_cond
306 );
307 wfRunHooks( 'ContribsPager::getQueryInfo', array( &$this, &$queryInfo ) );
308 return $queryInfo;
309 }
310
311 function getUserCond() {
312 $condition = array();
313 $join_conds = array();
314 if ( $this->target == 'newbies' ) {
315 $tables = array( 'user_groups', 'page', 'revision' );
316 $max = $this->mDb->selectField( 'user', 'max(user_id)', false, __METHOD__ );
317 $condition[] = 'rev_user >' . (int)($max - $max / 100);
318 $condition[] = 'ug_group IS NULL';
319 $index = 'user_timestamp';
320 # FIXME: other groups may have 'bot' rights
321 $join_conds['user_groups'] = array( 'LEFT JOIN', "ug_user = rev_user AND ug_group = 'bot'" );
322 } else {
323 $tables = array( 'page', 'revision' );
324 $condition['rev_user_text'] = $this->target;
325 $index = 'usertext_timestamp';
326 }
327 return array( $tables, $index, $condition, $join_conds );
328 }
329
330 function getNamespaceCond() {
331 if ( $this->namespace !== '' ) {
332 return array( 'page_namespace' => (int)$this->namespace );
333 } else {
334 return array();
335 }
336 }
337
338 function getIndexField() {
339 return 'rev_timestamp';
340 }
341
342 function getStartBody() {
343 return "<ul>\n";
344 }
345
346 function getEndBody() {
347 return "</ul>\n";
348 }
349
350 /**
351 * Generates each row in the contributions list.
352 *
353 * Contributions which are marked "top" are currently on top of the history.
354 * For these contributions, a [rollback] link is shown for users with roll-
355 * back privileges. The rollback link restores the most recent version that
356 * was not written by the target user.
357 *
358 * @todo This would probably look a lot nicer in a table.
359 */
360 function formatRow( $row ) {
361 wfProfileIn( __METHOD__ );
362
363 global $wgLang, $wgUser, $wgContLang;
364
365 $sk = $this->getSkin();
366 $rev = new Revision( $row );
367
368 $page = Title::makeTitle( $row->page_namespace, $row->page_title );
369 $link = $sk->makeKnownLinkObj( $page );
370 $difftext = $topmarktext = '';
371 if( $row->rev_id == $row->page_latest ) {
372 $topmarktext .= '<strong>' . $this->messages['uctop'] . '</strong>';
373 if( !$row->page_is_new ) {
374 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=0' ) . ')';
375 } else {
376 $difftext .= $this->messages['newarticle'];
377 }
378
379 if( !$page->getUserPermissionsErrors( 'rollback', $wgUser )
380 && !$page->getUserPermissionsErrors( 'edit', $wgUser ) ) {
381 $topmarktext .= ' '.$sk->generateRollback( $rev );
382 }
383
384 }
385 # Is there a visible previous revision?
386 if( $rev->userCan(Revision::DELETED_TEXT) ) {
387 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
388 } else {
389 $difftext = '(' . $this->messages['diff'] . ')';
390 }
391 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages['hist'], 'action=history' ) . ')';
392
393 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev, false, true );
394 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
395
396 if( $this->target == 'newbies' ) {
397 $userlink = ' . . ' . $sk->userLink( $row->rev_user, $row->rev_user_text );
398 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user, $row->rev_user_text ) . ') ';
399 } else {
400 $userlink = '';
401 }
402
403 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
404 $d = '<span class="history-deleted">' . $d . '</span>';
405 }
406
407 if( $rev->getParentId() === 0 ) {
408 $nflag = '<span class="newpage">' . $this->messages['newpageletter'] . '</span>';
409 } else {
410 $nflag = '';
411 }
412
413 if( $row->rev_minor_edit ) {
414 $mflag = '<span class="minor">' . $this->messages['minoreditletter'] . '</span> ';
415 } else {
416 $mflag = '';
417 }
418
419 $ret = "{$d} {$histlink} {$difftext} {$nflag}{$mflag} {$link}{$userlink} {$comment} {$topmarktext}";
420 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
421 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
422 }
423 // Let extensions add data
424 wfRunHooks( 'ContributionsLineEnding', array( &$this, &$ret, $row ) );
425
426 $ret = "<li>$ret</li>\n";
427 wfProfileOut( __METHOD__ );
428 return $ret;
429 }
430
431 /**
432 * Get the Database object in use
433 *
434 * @return Database
435 */
436 public function getDatabase() {
437 return $this->mDb;
438 }
439
440 }