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