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