9272ede23991fedc4d321aede2aaa978dfcc456e
[lhc/web/wiklou.git] / includes / SpecialContributions.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage SpecialPage
5 */
6
7 /**
8 * Special page "user contributions".
9 * Shows a list of the contributions of a user.
10 *
11 * @return none
12 * @param string $par (optional) user name of the user for which to show the contributions
13 */
14 function wfSpecialContributions( $par = null ) {
15 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest;
16 $fname = 'wfSpecialContributions';
17
18 // GET values
19 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
20 $namespace = $wgRequest->getIntOrNull( 'namespace' );
21 $invert = $wgRequest->getBool( 'invert' );
22 $hideminor = ($wgRequest->getBool( 'hideminor' ) ? true : false);
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 $querylimit = $limit + 1;
32 $sk = $wgUser->getSkin();
33 $dbr =& wfGetDB( DB_SLAVE );
34 $userCond = "";
35
36 $nt = Title::newFromURL( $target );
37 if ( !$nt ) {
38 $wgOut->errorpage( 'notargettitle', 'notargettext' );
39 return;
40 }
41 $nt =& Title::makeTitle( NS_USER, $nt->getDBkey() );
42
43 $id = User::idFromName( $nt->getText() );
44
45 if ( 0 == $id ) {
46 $ul = $nt->getText();
47 } else {
48 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
49 $userCond = '=' . $id;
50 }
51 $talk = $nt->getTalkPage();
52 if( $talk ) {
53 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) ) . ')';
54 }
55
56
57 if ( $target == 'newbies' ) {
58 # View the contributions of all recently created accounts
59 $max = $dbr->selectField( 'user', 'max(user_id)', false, $fname );
60 $userCond = '>' . ($max - $max / 100);
61 $ul = wfMsg ( 'newbies' );
62 $id = 0;
63 }
64
65 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
66
67 $contribsPage = Title::makeTitle( NS_SPECIAL, 'Contributions' );
68 $mlink = $sk->makeKnownLinkObj( $contribsPage,
69 $hideminor ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' ),
70 wfArrayToCGI( array(
71 'target' => $nt->getPrefixedDbKey(),
72 'offset' => $offset,
73 'limit' => $limit,
74 'hideminor' => $hideminor ? 0 : 1,
75 'namespace' => $namespace ) ) );
76
77 $minorQuery = $hideminor ? "AND rev_minor_edit=0" : "";
78 if( !is_null($namespace) ) {
79 $minorQuery .= ' AND page_namespace ' . ($invert ? '!' : '') . "= {$namespace}";
80 }
81
82 extract( $dbr->tableNames( 'page', 'revision' ) );
83 if ( $userCond == "" ) {
84 $condition = "rev_user_text=" . $dbr->addQuotes( $nt->getText() );
85 $index = 'usertext_timestamp';
86 } else {
87 $condition = "rev_user {$userCond}";
88 $index = 'user_timestamp';
89 }
90
91
92 $use_index = $dbr->useIndexClause( $index );
93 $sql = "SELECT
94 page_namespace,page_title,page_is_new,page_latest,
95 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
96 rev_deleted
97 FROM $page,$revision $use_index
98 WHERE page_id=rev_page AND $condition $minorQuery " .
99 "ORDER BY rev_timestamp DESC " . $dbr->limitResult( $querylimit, $offset );
100 $res = $dbr->query( $sql, $fname );
101 $numRows = $dbr->numRows( $res );
102
103 $wgOut->addHTML( ucNamespaceForm( $target, $hideminor, $namespace, $invert ) );
104
105 $top = wfShowingResults( $offset, $limit );
106 $wgOut->addHTML( "<p>{$top}\n" );
107
108 $sl = wfViewPrevNext( $offset, $limit,
109 $wgContLang->specialpage( "Contributions" ),
110 "hideminor=$hideminor&namespace=$namespace&invert=$invert&target=" . wfUrlEncode( $target ),
111 ($numRows) <= $limit);
112
113 $shm = wfMsgHtml( "showhideminor", $mlink );
114 $wgOut->addHTML( "<br />{$sl} ($shm)</p>\n");
115
116
117 if ( 0 == $numRows ) {
118 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
119 return;
120 }
121
122 $wgOut->addHTML( "<ul>\n" );
123 $n = 0;
124 while( $obj = $dbr->fetchObject( $res ) ) {
125 if( ++$n > $limit ) {
126 // Extra row for determining 'next'ability, don't display
127 break;
128 }
129 $wgOut->addHTML( ucListEdit( $sk, $obj ) );
130 }
131 $dbr->freeResult( $res );
132 $wgOut->addHTML( "</ul>\n" );
133
134 $wgOut->addHTML( "<br />{$sl} ($shm)\n");
135 }
136
137
138 /**
139 * Generates each row in the contributions list.
140 *
141 * Contributions which are marked "top" are currently on top of the history.
142 * For these contributions, a [rollback] link is shown for users with sysop
143 * privileges. The rollback link restores the most recent version that was not
144 * written by the target user.
145 *
146 * If the contributions page is called with the parameter &bot=1, all rollback
147 * links also get that parameter. It causes the edit itself and the rollback
148 * to be marked as "bot" edits. Bot edits are hidden by default from recent
149 * changes, so this allows sysops to combat a busy vandal without bothering
150 * other users.
151 *
152 * @todo This would probably look a lot nicer in a table.
153 */
154 function ucListEdit( $sk, $row ) {
155 $fname = 'ucListEdit';
156 wfProfileIn( $fname );
157
158 global $wgLang, $wgOut, $wgUser, $wgRequest;
159 static $messages;
160 if( !isset( $messages ) ) {
161 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
162 $messages[$msg] = wfMsg( $msg );
163 }
164 }
165
166 $page =& Title::makeTitle( $row->page_namespace, $row->page_title );
167 $link = $sk->makeKnownLinkObj( $page, '' );
168 $difftext = $topmarktext = '';
169 if( $row->rev_id == $row->page_latest ) {
170 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
171 if( !$row->page_is_new ) {
172 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
173 } else {
174 $difftext .= $messages['newarticle'];
175 }
176
177 if( $wgUser->isAllowed('rollback') ) {
178 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
179 $extraRollback .= '&token=' . urlencode(
180 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text ) ) );
181 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
182 $messages['rollbacklink'],
183 'action=rollback&from=' . urlencode( $row->rev_user_text ) . $extraRollback ) .']';
184 }
185
186 }
187 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
188 $difftext = '(' . $messages['diff'] . ')';
189 } else {
190 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id );
191 }
192 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
193
194 $comment = $sk->commentBlock( $row->rev_comment, $page );
195 $d = $wgLang->timeanddate( $row->rev_timestamp, true );
196
197 if( $row->rev_minor_edit ) {
198 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
199 } else {
200 $mflag = '';
201 }
202
203 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
204 if( $row->rev_deleted ) {
205 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
206 }
207 $ret = "<li>$ret</li>\n";
208 wfProfileOut( $fname );
209 return $ret;
210 }
211
212 /**
213 * Generates a form used to restrict display of contributions
214 * to a specific namespace
215 *
216 * @return none
217 * @param string $target target user to show contributions for
218 * @param string $hideminor whether minor contributions are hidden
219 * @param string $namespace currently selected namespace, NULL for show all
220 * @param bool $invert inverts the namespace selection on true (default null)
221 */
222 function ucNamespaceForm ( $target, $hideminor, $namespace, $invert ) {
223 global $wgContLang, $wgScript;
224
225 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
226 $namespaceselect .= '<option value="" '.(is_null($namespace) ? ' selected="selected"' : '').'>'.wfMsgHtml( 'contributionsall' ).'</option>';
227 $arr = $wgContLang->getFormattedNamespaces();
228 foreach( $arr as $ns => $name ) {
229 if( $ns < NS_MAIN )
230 continue;
231 $n = $ns === NS_MAIN ? wfMsgHtml( 'blanknamespace' ) : htmlspecialchars( $name );
232 $sel = $namespace == $ns ? ' selected="selected"' : '';
233 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
234 }
235 $namespaceselect .= '</select>';
236
237 $action = htmlspecialchars( $wgScript );
238 $out = "<div class='namespaceselector'><form method='get' action=\"$action\">";
239 $out .= '<input type="hidden" name="title" value="' . htmlspecialchars( $wgContLang->specialpage( 'Contributions' ) ) . '" />';
240 $out .= '<input type="hidden" name="target" value="' . htmlspecialchars( $target ) . '" />';
241 $out .= '<input type="hidden" name="hideminor" value="' . ( $hideminor ? 1 : 0 ) .'" />';
242 $out .= "
243 <div id='nsselect' class='contributions'>
244 <label for='nsselectbox'>" . wfMsgHtml('namespace') . "</label>
245 $namespaceselect
246 <input type='submit' value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />
247 <input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . " />
248 <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>
249 </div>";
250 $out .= '</form></div>';
251 return $out;
252 }
253 ?>