Allow filtering of Special:Contributions by namespace. Originally intended
[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 extract( $dbr->tableNames( 'old', 'cur' ) );
94 if ( $userCond == '' ) {
95 $sql = "SELECT cur_namespace,cur_title,cur_timestamp,cur_comment,cur_minor_edit,cur_is_new,cur_user_text FROM $cur " .
96 "WHERE cur_user_text='" . $dbr->strencode( $nt->getText() ) . "' {$cmq} " .
97 "ORDER BY inverse_timestamp LIMIT {$querylimit}";
98 $res1 = $dbr->query( $sql, $fname );
99
100 $sql = "SELECT old_namespace,old_title,old_timestamp,old_comment,old_minor_edit,old_user_text,old_id FROM $old " .
101 "WHERE old_user_text='" . $dbr->strencode( $nt->getText() ) . "' {$omq} " .
102 "ORDER BY inverse_timestamp LIMIT {$querylimit}";
103 $res2 = $dbr->query( $sql, $fname );
104 } else {
105 $sql = "SELECT cur_namespace,cur_title,cur_timestamp,cur_comment,cur_minor_edit,cur_is_new,cur_user_text FROM $cur " .
106 "WHERE cur_user {$userCond} {$cmq} ORDER BY inverse_timestamp LIMIT {$querylimit}";
107 $res1 = $dbr->query( $sql, $fname );
108
109 $sql = "SELECT old_namespace,old_title,old_timestamp,old_comment,old_minor_edit,old_user_text,old_id FROM $old " .
110 "WHERE old_user {$userCond} {$omq} ORDER BY inverse_timestamp LIMIT {$querylimit}";
111 $res2 = $dbr->query( $sql, $fname );
112 }
113 $nCur = $dbr->numRows( $res1 );
114 $nOld = $dbr->numRows( $res2 );
115
116 $wgOut->addHTML( namespaceForm( $target, $hideminor, $namespace ) );
117
118 $top = wfShowingResults( $offset, $limit );
119 $wgOut->addHTML( "<p>{$top}\n" );
120
121 $sl = wfViewPrevNext( $offset, $limit,
122 $wgContLang->specialpage( 'Contributions' ),
123 "hideminor={$hideminor}&namespace={$namespace}&target=" . wfUrlEncode( $target ),
124 ($nCur + $nOld) <= $offlimit);
125
126 $shm = wfMsg( 'showhideminor', $mlink );
127 $wgOut->addHTML( "<br />{$sl} ($shm)</p>\n");
128
129
130 if ( 0 == $nCur && 0 == $nOld ) {
131 $wgOut->addHTML( "\n<p>" . wfMsg( 'nocontribs' ) . "</p>\n" );
132 return;
133 }
134 if ( 0 != $nCur ) { $obj1 = $dbr->fetchObject( $res1 ); }
135 if ( 0 != $nOld ) { $obj2 = $dbr->fetchObject( $res2 ); }
136
137 $wgOut->addHTML( "<ul>\n" );
138 for( $n = 0; $n < $offlimit; $n++ ) {
139 if ( 0 == $nCur && 0 == $nOld ) { break; }
140
141 if ( ( 0 == $nOld ) ||
142 ( ( 0 != $nCur ) &&
143 ( $obj1->cur_timestamp >= $obj2->old_timestamp ) ) ) {
144 $ns = $obj1->cur_namespace;
145 $t = $obj1->cur_title;
146 $ts = $obj1->cur_timestamp;
147 $comment =$obj1->cur_comment;
148 $me = $obj1->cur_minor_edit;
149 $isnew = $obj1->cur_is_new;
150 $usertext = $obj1->cur_user_text;
151
152 $obj1 = $dbr->fetchObject( $res1 );
153 $topmark = true;
154 $oldid = false;
155 --$nCur;
156 } else {
157 $ns = $obj2->old_namespace;
158 $t = $obj2->old_title;
159 $ts = $obj2->old_timestamp;
160 $comment =$obj2->old_comment;
161 $me = $obj2->old_minor_edit;
162 $usertext = $obj2->old_user_text;
163 $oldid = $obj2->old_id;
164
165 $obj2 = $dbr->fetchObject( $res2 );
166 $topmark = false;
167 $isnew = false;
168 --$nOld;
169 }
170 if( $n >= $offset )
171 ucListEdit( $sk, $ns, $t, $ts, $topmark, $comment, ( $me > 0), $isnew, $usertext, $oldid );
172 }
173 $wgOut->addHTML( "</ul>\n" );
174
175 # Validations
176 global $wgUseValidation;
177 if( $wgUseValidation ) {
178 require_once( 'SpecialValidate.php' );
179 $val = new Validation ;
180 $val = $val->countUserValidations ( $id ) ;
181 $wgOut->addHTML( wfMsg ( 'val_user_validations', $val ) );
182 }
183
184 $wgOut->addHTML( "<br />{$sl} ($shm)</p>\n");
185 }
186
187
188 /**
189 * Generates each row in the contributions list.
190 *
191 * Contributions which are marked "top" are currently on top of the history.
192 * For these contributions, a [rollback] link is shown for users with sysop
193 * privileges. The rollback link restores the most recent version that was not
194 * written by the target user.
195 *
196 * If the contributions page is called with the parameter &bot=1, all rollback
197 * links also get that parameter. It causes the edit itself and the rollback
198 * to be marked as "bot" edits. Bot edits are hidden by default from recent
199 * changes, so this allows sysops to combat a busy vandal without bothering
200 * other users.
201 *
202 * @todo This would probably look a lot nicer in a table.
203 */
204 function ucListEdit( $sk, $ns, $t, $ts, $topmark, $comment, $isminor, $isnew, $target, $oldid ) {
205 global $wgLang, $wgOut, $wgUser, $wgRequest;
206 $page = Title::makeName( $ns, $t );
207 $link = $sk->makeKnownLink( $page, '' );
208 $difftext = $topmarktext = '';
209 if($topmark) {
210 $topmarktext .= '<strong>' . wfMsg('uctop') . '</strong>';
211 if(!$isnew) {
212 $difftext .= $sk->makeKnownLink( $page, '(' . wfMsg('diff') . ')', 'diff=0' );
213 } else {
214 $difftext .= wfMsg('newarticle');
215 }
216
217 if( $wgUser->isAllowed('rollback') ) {
218 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
219 # $target = $wgRequest->getText( 'target' );
220 $topmarktext .= ' ['. $sk->makeKnownLink( $page,
221 wfMsg( 'rollbacklink' ),
222 'action=rollback&from=' . urlencode( $target ) . $extraRollback ) .']';
223 }
224
225 }
226 if ( $oldid ) {
227 $difftext= $sk->makeKnownLink( $page, '('.wfMsg('diff').')', 'diff=prev&oldid='.$oldid );
228 }
229 $histlink='('.$sk->makeKnownLink($page,wfMsg('hist'),'action=history').')';
230
231 if($comment) {
232
233 $comment='<em>('. $sk->formatComment($comment, Title::newFromText($t) ) .')</em> ';
234
235 }
236 $d = $wgLang->timeanddate( $ts, true );
237
238 if ($isminor) {
239 $mflag = '<span class="minor">'.wfMsg( 'minoreditletter' ).'</span> ';
240 } else {
241 $mflag = '';
242 }
243
244 $wgOut->addHTML( "<li>{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}</li>\n" );
245 }
246
247 /**
248 *
249 */
250 function ucCountLink( $lim, $d ) {
251 global $wgUser, $wgContLang, $wgRequest;
252
253 $target = $wgRequest->getText( 'target' );
254 $sk = $wgUser->getSkin();
255 $s = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
256 "{$lim}", "target={$target}&days={$d}&limit={$lim}" );
257 return $s;
258 }
259
260 /**
261 *
262 */
263 function ucDaysLink( $lim, $d ) {
264 global $wgUser, $wgContLang, $wgRequest;
265
266 $target = $wgRequest->getText( 'target' );
267 $sk = $wgUser->getSkin();
268 $s = $sk->makeKnownLink( $wgContLang->specialPage( 'Contributions' ),
269 "{$d}", "target={$target}&days={$d}&limit={$lim}" );
270 return $s;
271 }
272
273 /**
274 * Generates a form used to restrict display of contributions
275 * to a specific namespace
276 *
277 * @return none
278 * @param string $target target user to show contributions for
279 * @param string $hideminor whether minor contributions are hidden
280 * @param string $namespace currently selected namespace, NULL for show all
281 */
282 function namespaceForm ( $target, $hideminor, $namespace ) {
283 global $wgContLang, $wgScript;
284
285 $namespaceselect = '<form><select name="namespace">';
286 $namespaceselect .= '<option value="" '.(is_null($namespace) ? ' selected="selected"' : '').'>'.wfMsg( 'all' ).'</option>';
287 $arr = $wgContLang->getNamespaces();
288 foreach( array_keys( $arr ) as $i ) {
289 if( $i < 0 ) {
290 continue;
291 }
292 $namespacename = str_replace ( "_", " ", $arr[$i] );
293 $n = ($i == 0) ? wfMsg ( 'articlenamespace' ) : $namespacename;
294 $sel = ($i === $namespace) ? ' selected="selected"' : '';
295 $namespaceselect .= "<option value='{$i}'{$sel}>{$n}</option>";
296 }
297 $namespaceselect .= '</select>';
298
299 $frombox = '<input type="text" size="20" name="from" value="'
300 . htmlspecialchars ( $from ) . '"/>';
301 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
302
303 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
304 $out .= '<input type="hidden" name="title" value="'.$wgContLang->specialpage( 'Contributions' ).'" />';
305 $out .= '<input type="hidden" name="target" value="'.htmlspecialchars( $target ).'" />';
306 $out .= '<input type="hidden" name="hideminor" value="'.$hideminor.'" />';
307 $out .= wfMsg ( 'allpagesformtext2', $namespaceselect, $submitbutton );
308 $out .= '</form>';
309 return $out;
310 }
311 ?>