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