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