* (bug 12938) Fix template expansion and 404 returns for action=raw with section
[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 roll-
118 * back privileges. The rollback link restores the most recent version that
119 * was not 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( !$page->getUserPermissionsErrors( 'rollback', $wgUser )
143 && !$page->getUserPermissionsErrors( 'edit', $wgUser ) ) {
144 $topmarktext .= ' '.$sk->generateRollback( $rev );
145 }
146
147 }
148 if( $rev->userCan( Revision::DELETED_TEXT ) ) {
149 $difftext = '(' . $sk->makeKnownLinkObj( $page, $this->messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
150 } else {
151 $difftext = '(' . $this->messages['diff'] . ')';
152 }
153 $histlink='('.$sk->makeKnownLinkObj( $page, $this->messages['hist'], 'action=history' ) . ')';
154
155 $comment = $wgContLang->getDirMark() . $sk->revComment( $rev );
156 $d = $wgLang->timeanddate( wfTimestamp( TS_MW, $row->rev_timestamp ), true );
157
158 if( $this->target == 'newbies' ) {
159 $userlink = ' . . ' . $sk->userLink( $row->rev_user, $row->rev_user_text );
160 $userlink .= ' (' . $sk->userTalkLink( $row->rev_user, $row->rev_user_text ) . ') ';
161 } else {
162 $userlink = '';
163 }
164
165 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
166 $d = '<span class="history-deleted">' . $d . '</span>';
167 }
168
169 if( $row->rev_minor_edit ) {
170 $mflag = '<span class="minor">' . $this->messages['minoreditletter'] . '</span> ';
171 } else {
172 $mflag = '';
173 }
174
175 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link}{$userlink}{$comment} {$topmarktext}";
176 if( $rev->isDeleted( Revision::DELETED_TEXT ) ) {
177 $ret .= ' ' . wfMsgHtml( 'deletedrev' );
178 }
179 $ret = "<li>$ret</li>\n";
180 wfProfileOut( __METHOD__ );
181 return $ret;
182 }
183
184 /**
185 * Get the Database object in use
186 *
187 * @return Database
188 */
189 public function getDatabase() {
190 return $this->mDb;
191 }
192
193 }
194
195 /**
196 * Special page "user contributions".
197 * Shows a list of the contributions of a user.
198 *
199 * @return none
200 * @param $par String: (optional) user name of the user for which to show the contributions
201 */
202 function wfSpecialContributions( $par = null ) {
203 global $wgUser, $wgOut, $wgLang, $wgRequest;
204
205 $options = array();
206
207 if ( isset( $par ) && $par == 'newbies' ) {
208 $target = 'newbies';
209 $options['contribs'] = 'newbie';
210 } elseif ( isset( $par ) ) {
211 $target = $par;
212 } else {
213 $target = $wgRequest->getVal( 'target' );
214 }
215
216 // check for radiobox
217 if ( $wgRequest->getVal( 'contribs' ) == 'newbie' ) {
218 $target = 'newbies';
219 $options['contribs'] = 'newbie';
220 }
221
222 if ( !strlen( $target ) ) {
223 $wgOut->addHTML( contributionsForm( '' ) );
224 return;
225 }
226
227 $options['limit'] = $wgRequest->getInt( 'limit', 50 );
228 $options['target'] = $target;
229
230 $nt = Title::makeTitleSafe( NS_USER, $target );
231 if ( !$nt ) {
232 $wgOut->addHTML( contributionsForm( '' ) );
233 return;
234 }
235 $id = User::idFromName( $nt->getText() );
236
237 if ( $target != 'newbies' ) {
238 $target = $nt->getText();
239 $wgOut->setSubtitle( contributionsSub( $nt, $id ) );
240 } else {
241 $wgOut->setSubtitle( wfMsgHtml( 'sp-contributions-newbies-sub') );
242 }
243
244 if ( ( $ns = $wgRequest->getVal( 'namespace', null ) ) !== null && $ns !== '' ) {
245 $options['namespace'] = intval( $ns );
246 } else {
247 $options['namespace'] = '';
248 }
249 if ( $wgUser->isAllowed( 'markbotedit' ) && $wgRequest->getBool( 'bot' ) ) {
250 $options['bot'] = '1';
251 }
252
253 $skip = $wgRequest->getText( 'offset' ) || $wgRequest->getText( 'dir' ) == 'prev';
254 # Offset overrides year/month selection
255 if ( ( $month = $wgRequest->getIntOrNull( 'month' ) ) !== null && $month !== -1 ) {
256 $options['month'] = intval( $month );
257 } else {
258 $options['month'] = '';
259 }
260 if ( ( $year = $wgRequest->getIntOrNull( 'year' ) ) !== null ) {
261 $options['year'] = intval( $year );
262 } else if( $options['month'] ) {
263 $thisMonth = intval( gmdate( 'n' ) );
264 $thisYear = intval( gmdate( 'Y' ) );
265 if( intval( $options['month'] ) > $thisMonth ) {
266 $thisYear--;
267 }
268 $options['year'] = $thisYear;
269 } else {
270 $options['year'] = '';
271 }
272
273 wfRunHooks( 'SpecialContributionsBeforeMainOutput', $id );
274
275 $wgOut->addHTML( contributionsForm( $options ) );
276 # Show original selected options, don't apply them so as to allow paging
277 $_GET['year'] = ''; // hack for Pager
278 $_GET['month'] = ''; // hack for Pager
279 if( $skip ) {
280 $options['year'] = '';
281 $options['month'] = '';
282 }
283
284 $pager = new ContribsPager( $target, $options['namespace'], $options['year'], $options['month'] );
285 if ( !$pager->getNumRows() ) {
286 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
287 return;
288 }
289
290 # Show a message about slave lag, if applicable
291 if( ( $lag = $pager->getDatabase()->getLag() ) > 0 )
292 $wgOut->showLagWarning( $lag );
293
294 $wgOut->addHTML(
295 '<p>' . $pager->getNavigationBar() . '</p>' .
296 $pager->getBody() .
297 '<p>' . $pager->getNavigationBar() . '</p>' );
298
299 # If there were contributions, and it was a valid user or IP, show
300 # the appropriate "footer" message - WHOIS tools, etc.
301 if( $target != 'newbies' ) {
302 $message = IP::isIPAddress( $target )
303 ? 'sp-contributions-footer-anon'
304 : 'sp-contributions-footer';
305
306
307 $text = wfMsg( $message, $target );
308 if( !wfEmptyMsg( $message, $text ) && $text != '-' ) {
309 $wgOut->addHtml( '<div class="mw-contributions-footer">' );
310 $wgOut->addWikiText( $text );
311 $wgOut->addHtml( '</div>' );
312 }
313 }
314 }
315
316 /**
317 * Generates the subheading with links
318 * @param Title $nt Title object for the target
319 * @param integer $id User ID for the target
320 * @return String: appropriately-escaped HTML to be output literally
321 */
322 function contributionsSub( $nt, $id ) {
323 global $wgSysopUserBans, $wgLang, $wgUser;
324
325 $sk = $wgUser->getSkin();
326
327 if ( 0 == $id ) {
328 $user = $nt->getText();
329 } else {
330 $user = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
331 }
332 $talk = $nt->getTalkPage();
333 if( $talk ) {
334 # Talk page link
335 $tools[] = $sk->makeLinkObj( $talk, wfMsgHtml( 'talkpagelinktext' ) );
336 if( ( $id != 0 && $wgSysopUserBans ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
337 # Block link
338 if( $wgUser->isAllowed( 'block' ) )
339 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Blockip', $nt->getDBkey() ), wfMsgHtml( 'blocklink' ) );
340 # Block log link
341 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'sp-contributions-blocklog' ), 'type=block&page=' . $nt->getPrefixedUrl() );
342 }
343 # Other logs link
344 $tools[] = $sk->makeKnownLinkObj( SpecialPage::getTitleFor( 'Log' ), wfMsgHtml( 'log' ), 'user=' . $nt->getPartialUrl() );
345
346 wfRunHooks( 'ContributionsToolLinks', array( $id, $nt, &$tools ) );
347
348 $links = implode( ' | ', $tools );
349 }
350
351 // Old message 'contribsub' had one parameter, but that doesn't work for
352 // languages that want to put the "for" bit right after $user but before
353 // $links. If 'contribsub' is around, use it for reverse compatibility,
354 // otherwise use 'contribsub2'.
355 if( wfEmptyMsg( 'contribsub', wfMsg( 'contribsub' ) ) ) {
356 return wfMsgHtml( 'contribsub2', $user, $links );
357 } else {
358 return wfMsgHtml( 'contribsub', "$user ($links)" );
359 }
360 }
361
362 /**
363 * Generates the namespace selector form with hidden attributes.
364 * @param $options Array: the options to be included.
365 */
366 function contributionsForm( $options ) {
367 global $wgScript, $wgTitle, $wgRequest;
368
369 $options['title'] = $wgTitle->getPrefixedText();
370 if ( !isset( $options['target'] ) ) {
371 $options['target'] = '';
372 } else {
373 $options['target'] = str_replace( '_' , ' ' , $options['target'] );
374 }
375
376 if ( !isset( $options['namespace'] ) ) {
377 $options['namespace'] = '';
378 }
379
380 if ( !isset( $options['contribs'] ) ) {
381 $options['contribs'] = 'user';
382 }
383
384 if ( !isset( $options['year'] ) ) {
385 $options['year'] = '';
386 }
387
388 if ( !isset( $options['month'] ) ) {
389 $options['month'] = '';
390 }
391
392 if ( $options['contribs'] == 'newbie' ) {
393 $options['target'] = '';
394 }
395
396 $f = Xml::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript ) );
397
398 foreach ( $options as $name => $value ) {
399 if ( in_array( $name, array( 'namespace', 'target', 'contribs', 'year', 'month' ) ) ) {
400 continue;
401 }
402 $f .= "\t" . Xml::hidden( $name, $value ) . "\n";
403 }
404
405 $f .= '<fieldset>' .
406 Xml::element( 'legend', array(), wfMsg( 'sp-contributions-search' ) ) .
407 Xml::radioLabel( wfMsgExt( 'sp-contributions-newbies', array( 'parseinline' ) ), 'contribs' , 'newbie' , 'newbie', $options['contribs'] == 'newbie' ? true : false ) . '<br />' .
408 Xml::radioLabel( wfMsgExt( 'sp-contributions-username', array( 'parseinline' ) ), 'contribs' , 'user', 'user', $options['contribs'] == 'user' ? true : false ) . ' ' .
409 Xml::input( 'target', 20, $options['target']) . ' '.
410 '<span style="white-space: nowrap">' .
411 Xml::label( wfMsg( 'namespace' ), 'namespace' ) . ' ' .
412 Xml::namespaceSelector( $options['namespace'], '' ) .
413 '</span>' .
414 Xml::openElement( 'p' ) .
415 '<span style="white-space: nowrap">' .
416 Xml::label( wfMsg( 'year' ), 'year' ) . ' '.
417 Xml::input( 'year', 4, $options['year'], array('id' => 'year', 'maxlength' => 4) ) .
418 '</span>' .
419 ' '.
420 '<span style="white-space: nowrap">' .
421 Xml::label( wfMsg( 'month' ), 'month' ) . ' '.
422 Xml::monthSelector( $options['month'], -1 ) . ' '.
423 '</span>' .
424 Xml::submitButton( wfMsg( 'sp-contributions-submit' ) ) .
425 Xml::closeElement( 'p' );
426
427 $explain = wfMsgExt( 'sp-contributions-explain', 'parseinline' );
428 if( !wfEmptyMsg( 'sp-contributions-explain', $explain ) )
429 $f .= "<p>{$explain}</p>";
430
431 $f .= '</fieldset>' .
432 Xml::closeElement( 'form' );
433 return $f;
434 }