* Yummie ? : syntax.
[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( NS_TALK ) ) . ')';
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 $minorQuery = "AND rev_minor_edit=0";
77 $mlink = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
78 WfMsg( "show" ), "target=" . htmlspecialchars( $nt->getPrefixedURL() ) .
79 "&offset={$offset}&limit={$limit}&hideminor=0&namespace={$namespace}" );
80 } else {
81 $minorQuery = "";
82 $mlink = $sk->makeKnownLink( $wgContLang->specialPage( "Contributions" ),
83 WfMsg( 'hide' ), 'target=' . htmlspecialchars( $nt->getPrefixedURL() ) .
84 "&offset={$offset}&limit={$limit}&hideminor=1&namespace={$namespace}" );
85 }
86
87 if( !is_null($namespace) ) {
88 $minorQuery .= " AND page_namespace = {$namespace}";
89 }
90
91 extract( $dbr->tableNames( 'page', 'revision' ) );
92 if ( $userCond == "" ) {
93 $condition = "rev_user_text=" . $dbr->addQuotes( $nt->getText() );
94 $index = 'usertext_timestamp';
95 } else {
96 $condition = "rev_user {$userCond}";
97 $index = 'user_timestamp';
98 }
99
100
101 $use_index = $dbr->useIndexClause( $index );
102 $sql = "SELECT
103 page_namespace,page_title,page_is_new,page_latest,
104 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
105 rev_deleted
106 FROM $page,$revision $use_index
107 WHERE page_id=rev_page AND $condition $minorQuery " .
108 "ORDER BY rev_timestamp DESC LIMIT {$querylimit}";
109 $res = $dbr->query( $sql, $fname );
110 $numRows = $dbr->numRows( $res );
111
112 $wgOut->addHTML( namespaceForm( $target, $hideminor, $namespace ) );
113
114 $top = wfShowingResults( $offset, $limit );
115 $wgOut->addHTML( "<p>{$top}\n" );
116
117 $sl = wfViewPrevNext( $offset, $limit,
118 $wgContLang->specialpage( "Contributions" ),
119 "hideminor={$hideminor}&namespace={$namespace}&target=" . wfUrlEncode( $target ),
120 ($numRows) <= $offlimit);
121
122 $shm = wfMsg( "showhideminor", $mlink );
123 $wgOut->addHTML( "<br />{$sl} ($shm)</p>\n");
124
125
126 if ( 0 == $numRows ) {
127 $wgOut->addHTML( "\n<p>" . wfMsg( "nocontribs" ) . "</p>\n" );
128 return;
129 }
130
131 $wgOut->addHTML( "<ul>\n" );
132 while( $obj = $dbr->fetchObject( $res ) ) {
133 $wgOut->addHTML( ucListEdit( $sk, $obj ) );
134 }
135 $wgOut->addHTML( "</ul>\n" );
136
137 $wgOut->addHTML( "<br />{$sl} ($shm)\n");
138 }
139
140
141 /**
142 * Generates each row in the contributions list.
143 *
144 * Contributions which are marked "top" are currently on top of the history.
145 * For these contributions, a [rollback] link is shown for users with sysop
146 * privileges. The rollback link restores the most recent version that was not
147 * written by the target user.
148 *
149 * If the contributions page is called with the parameter &bot=1, all rollback
150 * links also get that parameter. It causes the edit itself and the rollback
151 * to be marked as "bot" edits. Bot edits are hidden by default from recent
152 * changes, so this allows sysops to combat a busy vandal without bothering
153 * other users.
154 *
155 * @todo This would probably look a lot nicer in a table.
156 */
157 function ucListEdit( $sk, $row ) {
158 $fname = 'ucListEdit';
159 wfProfileIn( $fname );
160
161 global $wgLang, $wgOut, $wgUser, $wgRequest;
162 static $messages;
163 if( !isset( $messages ) ) {
164 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
165 $messages[$msg] = wfMsg( $msg );
166 }
167 }
168
169 $page =& Title::makeTitle( $row->page_namespace, $row->page_title );
170 $link = $sk->makeKnownLinkObj( $page, '' );
171 $difftext = $topmarktext = '';
172 if( $row->rev_id == $row->page_latest ) {
173 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
174 if( !$row->page_is_new ) {
175 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
176 } else {
177 $difftext .= $messages['newarticle'];
178 }
179
180 if( $wgUser->isAllowed('rollback') ) {
181 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
182 $extraRollback .= '&token=' . urlencode(
183 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text ) ) );
184 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
185 $messages['rollbacklink'],
186 'action=rollback&from=' . urlencode( $row->rev_user_text ) . $extraRollback ) .']';
187 }
188
189 }
190 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
191 $difftext = '(' . $messages['diff'] . ')';
192 } else {
193 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id );
194 }
195 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
196
197 $comment = $sk->commentBlock( $row->rev_comment, $page );
198 $d = $wgLang->timeanddate( $row->rev_timestamp, true );
199
200 if( $row->rev_minor_edit ) {
201 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
202 } else {
203 $mflag = '';
204 }
205
206 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
207 if( $row->rev_deleted ) {
208 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
209 }
210 $ret = "<li>$ret</li>\n";
211 wfProfileOut( $fname );
212 return $ret;
213 }
214
215 /**
216 * Generates a form used to restrict display of contributions
217 * to a specific namespace
218 *
219 * @return none
220 * @param string $target target user to show contributions for
221 * @param string $hideminor whether minor contributions are hidden
222 * @param string $namespace currently selected namespace, NULL for show all
223 */
224 function namespaceForm ( $target, $hideminor, $namespace ) {
225 global $wgContLang, $wgScript;
226
227 $namespaceselect = '<select name="namespace">';
228 $namespaceselect .= '<option value="" '.(is_null($namespace) ? ' selected="selected"' : '').'>'.wfMsg( 'contributionsall' ).'</option>';
229 $arr = $wgContLang->getNamespaces();
230 foreach( array_keys( $arr ) as $i ) {
231 if( $i < 0 ) {
232 continue;
233 }
234 $namespacename = str_replace ( "_", " ", $arr[$i] );
235 $n = ($i == 0) ? wfMsg ( 'articlenamespace' ) : $namespacename;
236 $sel = ($i === $namespace) ? ' selected="selected"' : '';
237 $namespaceselect .= "<option value='{$i}'{$sel}>{$n}</option>";
238 }
239 $namespaceselect .= '</select>';
240
241 $submitbutton = '<input type="submit" value="' . wfMsg( 'allpagessubmit' ) . '" />';
242
243 $out = "<div class='namespaceselector'><form method='get' action='{$wgScript}'>";
244 $out .= '<input type="hidden" name="title" value="'.$wgContLang->specialpage( 'Contributions' ).'" />';
245 $out .= '<input type="hidden" name="target" value="'.htmlspecialchars( $target ).'" />';
246 $out .= '<input type="hidden" name="hideminor" value="'.$hideminor.'" />';
247 $out .= wfMsg ( 'contributionsformtext', $namespaceselect, $submitbutton );
248 $out .= '</form></div>';
249 return $out;
250 }
251 ?>