Revert unauthorized changes made without discussion of outstanding objections.
[lhc/web/wiklou.git] / includes / SpecialContributions.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 * @subpackage SpecialPage
6 */
7
8 /**
9 * Special page "user contributions".
10 * Shows a list of the contributions of a user.
11 *
12 * @return none
13 * @param string $par (optional) user name of the user for which to show the contributions
14 */
15 function wfSpecialContributions( $par = '' ) {
16 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest;
17 $fname = 'wfSpecialContributions';
18
19 if( $par )
20 $target = $par;
21 else
22 $target = $wgRequest->getVal( 'target' );
23
24 if ( '' == $target ) {
25 $wgOut->errorpage( 'notargettitle', 'notargettext' );
26 return;
27 }
28
29 # FIXME: Change from numeric offsets to date offsets
30 list( $limit, $offset ) = wfCheckLimits( 50, '' );
31 $offlimit = $limit + $offset;
32 $querylimit = $offlimit + 1;
33 $hideminor = ($wgRequest->getVal( 'hideminor' ) ? 1 : 0);
34 $sk = $wgUser->getSkin();
35 $dbr =& wfGetDB( DB_SLAVE );
36 $userCond = "";
37 $namespace = $wgRequest->getVal( 'namespace', '' );
38 if( $namespace != '' ) {
39 $namespace = IntVal( $namespace );
40 } else {
41 $namespace = NULL;
42 }
43
44 $nt = Title::newFromURL( $target );
45 if ( !$nt ) {
46 $wgOut->errorpage( 'notargettitle', 'notargettext' );
47 return;
48 }
49 $nt =& Title::makeTitle( NS_USER, $nt->getDBkey() );
50
51 $id = User::idFromName( $nt->getText() );
52
53 if ( 0 == $id ) {
54 $ul = $nt->getText();
55 } else {
56 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
57 $userCond = '=' . $id;
58 }
59 $talk = $nt->getTalkPage();
60 if( $talk ) {
61 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText(Namespace::getTalk(0)) ) . ')';
62 }
63
64
65 if ( $target == 'newbies' ) {
66 # View the contributions of all recently created accounts
67 $max = $dbr->selectField( 'user', 'max(user_id)', false, $fname );
68 $userCond = '>' . ($max - $max / 100);
69 $ul = wfMsg ( 'newbies' );
70 $id = 0;
71 }
72
73 $wgOut->setSubtitle( wfMsg( 'contribsub', $ul ) );
74
75 if ( $hideminor ) {
76 $cmq = 'AND cur_minor_edit=0';
77 $omq = 'AND old_minor_edit=0';
78 $mlink = $sk->makeKnownLink( $wgContLang->specialPage( 'Contributions' ),
79 WfMsg( 'show' ), "target=" . htmlspecialchars( $nt->getPrefixedURL() ) .
80 "&offset={$offset}&limit={$limit}&hideminor=0&namespace={$namespace}" );
81 } else {
82 $cmq = $omq = '';
83 $mlink = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
84 WfMsg( 'hide' ), 'target=' . htmlspecialchars( $nt->getPrefixedURL() ) .
85 "&offset={$offset}&limit={$limit}&hideminor=1&namespace={$namespace}" );
86 }
87
88 if( !is_null($namespace) ) {
89 $cmq .= " AND cur_namespace = {$namespace}";
90 $omq .= " AND old_namespace = {$namespace}";
91 }
92
93 # We may have to force the index, as some options will cause
94 # MySQL to incorrectly pick eg the namespace index.
95 list( $useIndex, $tailOpts ) = $dbr->makeSelectOptions( array(
96 'USE INDEX' => 'usertext_timestamp',
97 'LIMIT' => $querylimit ) );
98
99 extract( $dbr->tableNames( 'old', 'cur' ) );
100 if ( $userCond == '' ) {
101 $sql = "SELECT cur_namespace,cur_title,cur_timestamp,cur_comment,cur_minor_edit,cur_is_new,cur_user_text FROM $cur $useIndex " .
102 "WHERE cur_user_text='" . $dbr->strencode( $nt->getText() ) . "' {$cmq} " .
103 "ORDER BY inverse_timestamp $tailOpts";
104 $res1 = $dbr->query( $sql, $fname );
105
106 $sql = "SELECT old_namespace,old_title,old_timestamp,old_comment,old_minor_edit,old_user_text,old_id FROM $old $useIndex " .
107 "WHERE old_user_text='" . $dbr->strencode( $nt->getText() ) . "' {$omq} " .
108 "ORDER BY inverse_timestamp $tailOpts";
109 $res2 = $dbr->query( $sql, $fname );
110 } else {
111 $sql = "SELECT cur_namespace,cur_title,cur_timestamp,cur_comment,cur_minor_edit,cur_is_new,cur_user_text FROM $cur $useIndex " .
112 "WHERE cur_user {$userCond} {$cmq} ORDER BY inverse_timestamp $tailOpts";
113 $res1 = $dbr->query( $sql, $fname );
114
115 $sql = "SELECT old_namespace,old_title,old_timestamp,old_comment,old_minor_edit,old_user_text,old_id FROM $old $useIndex " .
116 "WHERE old_user {$userCond} {$omq} ORDER BY inverse_timestamp $tailOpts";
117 $res2 = $dbr->query( $sql, $fname );
118 }
119 $nCur = $dbr->numRows( $res1 );
120 $nOld = $dbr->numRows( $res2 );
121
122 $wgOut->addHTML( namespaceForm( $target, $hideminor, $namespace ) );
123
124 $top = wfShowingResults( $offset, $limit );
125 $wgOut->addHTML( "<p>{$top}\n" );
126
127 $sl = wfViewPrevNext( $offset, $limit,
128 $wgContLang->specialpage( 'Contributions' ),
129 "hideminor={$hideminor}&namespace={$namespace}&target=" . wfUrlEncode( $target ),
130 ($nCur + $nOld) <= $offlimit);
131
132 $shm = wfMsg( 'showhideminor', $mlink );
133 $wgOut->addHTML( "<br />{$sl} ($shm)</p>\n");
134
135
136 if ( 0 == $nCur && 0 == $nOld ) {
137 $wgOut->addHTML( "\n<p>" . wfMsg( 'nocontribs' ) . "</p>\n" );
138 return;
139 }
140 if ( 0 != $nCur ) { $obj1 = $dbr->fetchObject( $res1 ); }
141 if ( 0 != $nOld ) { $obj2 = $dbr->fetchObject( $res2 ); }
142
143 $wgOut->addHTML( "<ul>\n" );
144 for( $n = 0; $n < $offlimit; $n++ ) {
145 if ( 0 == $nCur && 0 == $nOld ) { break; }
146
147 if ( ( 0 == $nOld ) ||
148 ( ( 0 != $nCur ) &&
149 ( $obj1->cur_timestamp >= $obj2->old_timestamp ) ) ) {
150 $ns = $obj1->cur_namespace;
151 $t = $obj1->cur_title;
152 $ts = $obj1->cur_timestamp;
153 $comment =$obj1->cur_comment;
154 $me = $obj1->cur_minor_edit;
155 $isnew = $obj1->cur_is_new;
156 $usertext = $obj1->cur_user_text;
157
158 $obj1 = $dbr->fetchObject( $res1 );
159 $topmark = true;
160 $oldid = false;
161 --$nCur;
162 } else {
163 $ns = $obj2->old_namespace;
164 $t = $obj2->old_title;
165 $ts = $obj2->old_timestamp;
166 $comment =$obj2->old_comment;
167 $me = $obj2->old_minor_edit;
168 $usertext = $obj2->old_user_text;
169 $oldid = $obj2->old_id;
170
171 $obj2 = $dbr->fetchObject( $res2 );
172 $topmark = false;
173 $isnew = false;
174 --$nOld;
175 }
176 if( $n >= $offset )
177 ucListEdit( $sk, $ns, $t, $ts, $topmark, $comment, ( $me > 0), $isnew, $usertext, $oldid );
178 }
179 $wgOut->addHTML( "</ul>\n" );
180
181 # Validations
182 global $wgUseValidation;
183 if( $wgUseValidation ) {
184 require_once( 'SpecialValidate.php' );
185 $val = new Validation ;
186 $val = $val->countUserValidations ( $id ) ;
187 $wgOut->addHTML( wfMsg ( 'val_user_validations', $val ) );
188 }
189
190 $wgOut->addHTML( "<br />{$sl} ($shm)</p>\n");
191 }
192
193
194 /**
195 * Generates each row in the contributions list.
196 *
197 * Contributions which are marked "top" are currently on top of the history.
198 * For these contributions, a [rollback] link is shown for users with sysop
199 * privileges. The rollback link restores the most recent version that was not
200 * written by the target user.
201 *
202 * If the contributions page is called with the parameter &bot=1, all rollback
203 * links also get that parameter. It causes the edit itself and the rollback
204 * to be marked as "bot" edits. Bot edits are hidden by default from recent
205 * changes, so this allows sysops to combat a busy vandal without bothering
206 * other users.
207 *
208 * @todo This would probably look a lot nicer in a table.
209 */
210 function ucListEdit( $sk, $ns, $t, $ts, $topmark, $comment, $isminor, $isnew, $target, $oldid ) {
211 $fname = 'ucListEdit';
212 wfProfileIn( $fname );
213
214 global $wgLang, $wgOut, $wgUser, $wgRequest;
215 static $messages;
216 if( !isset( $messages ) ) {
217 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
218 $messages[$msg] = wfMsg( $msg );
219 }
220 }
221
222 $page =& Title::makeTitle( $ns, $t );
223 $link = $sk->makeKnownLinkObj( $page, '' );
224 $difftext = $topmarktext = '';
225 if($topmark) {
226 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
227 if(!$isnew) {
228 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
229 } else {
230 $difftext .= $messages['newarticle'];
231 }
232
233 if( $wgUser->isAllowed('rollback') ) {
234 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
235 # $target = $wgRequest->getText( 'target' );
236 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
237 $messages['rollbacklink'],
238 'action=rollback&from=' . urlencode( $target ) . $extraRollback ) .']';
239 }
240
241 }
242 if ( $oldid ) {
243 $difftext= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$oldid );
244 }
245 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
246
247 if( $comment ) {
248 $comment = '<em>(' . $sk->formatComment( $comment, $page ) . ')</em> ';
249 }
250 $d = $wgLang->timeanddate( $ts, true );
251
252 if ($isminor) {
253 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
254 } else {
255 $mflag = '';
256 }
257
258 $wgOut->addHTML( "<li>{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}</li>\n" );
259 wfProfileOut( $fname );
260 }
261
262 /**
263 *
264 */
265 function ucCountLink( $lim, $d ) {
266 global $wgUser, $wgContLang, $wgRequest;
267
268 $target = $wgRequest->getText( 'target' );
269 $sk = $wgUser->getSkin();
270 $s = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
271 "{$lim}", "target={$target}&days={$d}&limit={$lim}" );
272 return $s;
273 }
274
275 /**
276 *
277 */
278 function ucDaysLink( $lim, $d ) {
279 global $wgUser, $wgContLang, $wgRequest;
280
281 $target = $wgRequest->getText( 'target' );
282 $sk = $wgUser->getSkin();
283 $s = $sk->makeKnownLink( $wgContLang->specialPage( 'Contributions' ),
284 "{$d}", "target={$target}&days={$d}&limit={$lim}" );
285 return $s;
286 }
287
288 /**
289 * Generates a form used to restrict display of contributions
290 * to a specific namespace
291 *
292 * @return none
293 * @param string $target target user to show contributions for
294 * @param string $hideminor whether minor contributions are hidden
295 * @param string $namespace currently selected namespace, NULL for show all
296 */
297 function namespaceForm ( $target, $hideminor, $namespace ) {
298 global $wgContLang, $wgScript;
299
300 $namespaceselect = '<form><select name="namespace">';
301 $namespaceselect .= '<option value="" '.(is_null($namespace) ? ' selected="selected"' : '').'>'.wfMsg( 'all' ).'</option>';
302 $arr = $wgContLang->getNamespaces();
303 foreach( array_keys( $arr ) as $i ) {
304 if( $i < 0 ) {
305 continue;
306 }
307 $namespacename = str_replace ( "_", " ", $arr[$i] );
308 $n = ($i == 0) ? wfMsg ( 'articlenamespace' ) : $namespacename;
309 $sel = ($i === $namespace) ? ' selected="selected"' : '';
310 $namespaceselect .= "<option value='{$i}'{$sel}>{$n}</option>";
311 }
312 $namespaceselect .= '</select>';
313
314 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
315
316 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
317 $out .= '<input type="hidden" name="title" value="'.$wgContLang->specialpage( 'Contributions' ).'" />';
318 $out .= '<input type="hidden" name="target" value="'.htmlspecialchars( $target ).'" />';
319 $out .= '<input type="hidden" name="hideminor" value="'.$hideminor.'" />';
320 $out .= wfMsg ( 'allpagesformtext2', $namespaceselect, $submitbutton );
321 $out .= '</form></div>';
322 return $out;
323 }
324 ?>