honour namespace/minor/etc in showing paging buttons
[lhc/web/wiklou.git] / includes / SpecialContributions.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage SpecialPage
5 */
6
7 class contribs_finder {
8 var $username, $offset, $limit, $namespace, $invert;
9 var $dbr;
10
11 function contribs_finder($username) {
12 $this->username = $username;
13 $this->dbr =& wfGetDB(DB_SLAVE);
14 }
15
16 function set_limit($limit) {
17 $this->limit = $limit;
18 }
19
20 function set_offset($offset) {
21 $this->offset = $offset;
22 }
23
24 function set_namespace($namespace, $invert = false) {
25 $this->namespace = $namespace;
26 $this->invert = $invert;
27 }
28
29 function set_hide_minor($hm) {
30 $this->hide_minor = $hm;
31 }
32
33 function get_edit_limits() {
34 list($index, $usercond) = $this->get_user_cond();
35
36 $use_index = $this->dbr->useIndexClause($index);
37 $sql = "SELECT MIN(rev_timestamp) as earliest, MAX(rev_timestamp) as latest " .
38 "FROM page, revision $use_index WHERE page_id = rev_page " .
39 "AND ";
40
41 $sql .= $usercond;
42 $sql .= $this->get_namespace_cond();
43 $sql .= $this->get_minor_cond();
44 $res = $this->dbr->query($sql, "contribs_finder::get_edit_limits");
45 $rows = array();
46 while ($o = $this->dbr->fetchObject($res))
47 $rows[] = $o;
48 $row = $rows[count($rows) - 1];
49 return array($row->earliest, $row->latest);
50 }
51
52 function get_user_cond() {
53 $condition = "";
54
55 if ($this->username == 'newbies') {
56 $max = $this->dbr->selectField('user', 'max(user_id)', false, "make_sql");
57 $condition = '>' . ($max - $max / 100);
58 }
59
60 if ($condition == "") {
61 $condition = " rev_user_text=" . $this->dbr->addQuotes($this->username);
62 $index = 'usertext_timestamp';
63 } else {
64 $condition = " rev_user {$condition}";
65 $index = 'user_timestamp';
66 }
67
68 return array($index, $condition);
69 }
70
71 function get_minor_cond() {
72 if ($this->hide_minor)
73 return ' AND rev_minor_edit=0';
74 return '';
75 }
76
77 function get_namespace_cond() {
78 $nsQuery = $nsinvert = "";
79
80 if ($this->invert)
81 $nsinvert = "!";
82
83 if (!is_null($this->namespace))
84 $nsQuery .= "AND page_namespace {$nsinvert}= {$this->namespace}";
85
86 return $nsQuery;
87 }
88
89 function get_previous_offset_for_paging() {
90 list($index, $usercond) = $this->get_user_cond();
91 $use_index = $this->dbr->useIndexClause($index);
92
93 $sql = "SELECT rev_timestamp FROM page, revision $use_index " .
94 "WHERE page_id = rev_page AND rev_timestamp > " . $this->offset . " AND " .
95 "rev_user_text = " . $this->dbr->addQuotes($this->username);
96 $sql .= $this->get_namespace_cond();
97 $sql .= $this->get_minor_cond();
98 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . $this->limit;
99 $res = $this->dbr->query($sql);
100 $rows = array();
101 while ($obj = $this->dbr->fetchObject($res))
102 $rows[] = $obj;
103 $this->dbr->freeResult($res);
104 return $rows[count($rows) - 1]->rev_timestamp;
105 }
106
107 /* private */ function make_sql() {
108 $userCond = $condition = $index = $minorQuery = $nsQuery
109 = $offsetQuery = $limitQuery = $nsinvert = "";
110
111 $minorQuery = $this->get_minor_cond();
112 $nsQuery = $this->get_namespace_cond();
113
114 extract($this->dbr->tableNames('page', 'revision'));
115 list($index, $userCond) = $this->get_user_cond();
116
117 $limitQuery = "LIMIT {$this->limit}";
118 if ($this->offset)
119 $offsetQuery = "AND rev_timestamp < {$this->offset}";
120
121 $use_index = $this->dbr->useIndexClause($index);
122 $sql = "SELECT
123 page_namespace,page_title,page_is_new,page_latest,
124 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
125 rev_deleted
126 FROM $page,$revision $use_index
127 WHERE page_id=rev_page AND $userCond $minorQuery $nsQuery $offsetQuery
128 ORDER BY rev_timestamp DESC $limitQuery";
129 return $sql;
130 }
131
132 function find() {
133 $contribs = array();
134 $res = $this->dbr->query($this->make_sql(), "contribs_finder::find");
135 while ($c = $this->dbr->fetchObject($res))
136 $contribs[] = $c;
137 $this->dbr->freeResult($res);
138 return $contribs;
139 }
140 };
141
142 /**
143 * Special page "user contributions".
144 * Shows a list of the contributions of a user.
145 *
146 * @return none
147 * @param string $par (optional) user name of the user for which to show the contributions
148 */
149 function wfSpecialContributions( $par = null ) {
150 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest, $wgTitle;
151 $fname = 'wfSpecialContributions';
152
153 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
154 if (!strlen($target)) {
155 $wgOut->errorpage('notargettitle', 'notargettext');
156 return;
157 }
158
159 $nt = Title::newFromURL( $target );
160 if (!$nt) {
161 $wgOut->errorpage( 'notargettitle', 'notargettext' );
162 return;
163 }
164 $nt =& Title::makeTitle(NS_USER, $nt->getDBkey());
165
166 $namespace = $wgRequest->getIntOrNull('namespace');
167 $invert = $wgRequest->getBool('invert');
168 $hideminor = $wgRequest->getBool('hideminor');
169 $limit = min($wgRequest->getInt('limit', 50), 500);
170 $offset = $wgRequest->getVal('offset');
171 /* Offset must be an integral. */
172 if (!strlen($offset) || !preg_match("/^[0-9]+$/", $offset))
173 $offset = 0;
174
175 $title = Title::makeTitle(NS_SPECIAL, "Contributions");
176 $urlbits = "hideminor=$hideminor&namespace=$namespace&invert=$invert&target=" . wfUrlEncode($target);
177 $myurl = $title->escapeLocalURL($urlbits);
178
179 $finder = new contribs_finder($nt->getText());
180
181 $finder->set_namespace($namespace, $invert);
182 $finder->set_hide_minor($hideminor);
183 $finder->set_limit($limit);
184 $finder->set_offset($offset);
185
186 if ($wgRequest->getText('go') == "prev") {
187 $prevts = $finder->get_previous_offset_for_paging();
188 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
189 $wgOut->redirect($prevurl);
190 return;
191 }
192
193 $sk = $wgUser->getSkin();
194
195 $id = User::idFromName($nt->getText());
196
197 if ( 0 == $id ) {
198 $ul = $nt->getText();
199 } else {
200 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
201 $userCond = '=' . $id;
202 }
203 $talk = $nt->getTalkPage();
204 if( $talk ) {
205 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) ) . ')';
206 }
207
208 if ($target == 'newbies') {
209 $ul = wfMsg ('newbies');
210 }
211
212 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
213
214 $contribsPage = Title::makeTitle( NS_SPECIAL, 'Contributions' );
215 $mlink = $sk->makeKnownLinkObj( $contribsPage,
216 $hideminor ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' ),
217 wfArrayToCGI( array(
218 'target' => $nt->getPrefixedDbKey(),
219 'offset' => $offset,
220 'limit' => $limit,
221 'hideminor' => $hideminor ? 0 : 1,
222 'namespace' => $namespace ) ) );
223
224 $contribs = $finder->find();
225
226 if (count($contribs) == 0) {
227 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
228 return;
229 }
230
231 list($early, $late) = $finder->get_edit_limits();
232 $lastts = count($contribs) ? $contribs[count($contribs) - 1]->rev_timestamp : 0;
233 $atstart = (!count($contribs) || $late == $contribs[0]->rev_timestamp);
234 $atend = (!count($contribs) || $early == $lastts);
235 wfdebug("early=$early late=$late lastts=$lastts\n");
236 $wgOut->addHTML(ucNamespaceForm($target, $hideminor, $namespace, $invert));
237
238 $prevtext = wfMsg("prevn", $limit);
239 if ($atstart)
240 $prevlink = $prevtext;
241 else
242 $prevlink = "<a href=\"$myurl&amp;offset=$offset&amp;limit=$limit&amp;go=prev\">$prevtext</a>";
243
244 $nexttext = wfMsg("nextn", $limit);
245 if ($atend)
246 $nextlink = $nexttext;
247 else
248 $nextlink = "<a href=\"$myurl&amp;offset=$lastts&amp;limit=$limit\">$nexttext</a>";
249
250 $urls = array();
251 foreach (array(20, 50, 100, 250, 500) as $num)
252 $urls[] = "<a href=\"$myurl&offset=$offset&limit={$num}\">".$wgLang->formatNum($num)."</a>";
253 $bits = implode($urls, ' | ');
254
255 $prevnextbits = wfMsgHtml("viewprevnext", $prevlink, $nextlink, $bits);
256
257 $shm = wfMsgHtml( "showhideminor", $mlink );
258 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)</p>\n");
259
260 $wgOut->addHTML( "<ul>\n" );
261
262 foreach ($contribs as $contrib)
263 $wgOut->addHTML(ucListEdit($sk, $contrib));
264
265 $wgOut->addHTML( "</ul>\n" );
266 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)\n");
267 }
268
269
270 /**
271 * Generates each row in the contributions list.
272 *
273 * Contributions which are marked "top" are currently on top of the history.
274 * For these contributions, a [rollback] link is shown for users with sysop
275 * privileges. The rollback link restores the most recent version that was not
276 * written by the target user.
277 *
278 * If the contributions page is called with the parameter &bot=1, all rollback
279 * links also get that parameter. It causes the edit itself and the rollback
280 * to be marked as "bot" edits. Bot edits are hidden by default from recent
281 * changes, so this allows sysops to combat a busy vandal without bothering
282 * other users.
283 *
284 * @todo This would probably look a lot nicer in a table.
285 */
286 function ucListEdit( $sk, $row ) {
287 $fname = 'ucListEdit';
288 wfProfileIn( $fname );
289
290 global $wgLang, $wgOut, $wgUser, $wgRequest;
291 static $messages;
292 if( !isset( $messages ) ) {
293 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
294 $messages[$msg] = wfMsg( $msg );
295 }
296 }
297
298 $page =& Title::makeTitle( $row->page_namespace, $row->page_title );
299 $link = $sk->makeKnownLinkObj( $page, '' );
300 $difftext = $topmarktext = '';
301 if( $row->rev_id == $row->page_latest ) {
302 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
303 if( !$row->page_is_new ) {
304 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
305 } else {
306 $difftext .= $messages['newarticle'];
307 }
308
309 if( $wgUser->isAllowed('rollback') ) {
310 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
311 $extraRollback .= '&token=' . urlencode(
312 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text ) ) );
313 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
314 $messages['rollbacklink'],
315 'action=rollback&from=' . urlencode( $row->rev_user_text ) . $extraRollback ) .']';
316 }
317
318 }
319 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
320 $difftext = '(' . $messages['diff'] . ')';
321 } else {
322 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id );
323 }
324 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
325
326 $comment = $sk->commentBlock( $row->rev_comment, $page );
327 $d = $wgLang->timeanddate( $row->rev_timestamp, true );
328
329 if( $row->rev_minor_edit ) {
330 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
331 } else {
332 $mflag = '';
333 }
334
335 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
336 if( $row->rev_deleted ) {
337 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
338 }
339 $ret = "<li>$ret</li>\n";
340 wfProfileOut( $fname );
341 return $ret;
342 }
343
344 /**
345 * Generates a form used to restrict display of contributions
346 * to a specific namespace
347 *
348 * @return none
349 * @param string $target target user to show contributions for
350 * @param string $hideminor whether minor contributions are hidden
351 * @param string $namespace currently selected namespace, NULL for show all
352 * @param bool $invert inverts the namespace selection on true (default null)
353 */
354 function ucNamespaceForm ( $target, $hideminor, $namespace, $invert ) {
355 global $wgContLang, $wgScript;
356
357 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
358 $namespaceselect .= '<option value="" '.(is_null($namespace) ? ' selected="selected"' : '').'>'.wfMsgHtml( 'contributionsall' ).'</option>';
359 $arr = $wgContLang->getFormattedNamespaces();
360 foreach( $arr as $ns => $name ) {
361 if( $ns < NS_MAIN )
362 continue;
363 $n = $ns === NS_MAIN ? wfMsgHtml( 'blanknamespace' ) : htmlspecialchars( $name );
364 $sel = $namespace == $ns ? ' selected="selected"' : '';
365 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
366 }
367 $namespaceselect .= '</select>';
368
369 $action = htmlspecialchars( $wgScript );
370 $out = "<div class='namespaceselector'><form method='get' action=\"$action\">";
371 $out .= '<input type="hidden" name="title" value="' . htmlspecialchars( $wgContLang->specialpage( 'Contributions' ) ) . '" />';
372 $out .= '<input type="hidden" name="target" value="' . htmlspecialchars( $target ) . '" />';
373 $out .= '<input type="hidden" name="hideminor" value="' . ( $hideminor ? 1 : 0 ) .'" />';
374 $out .= "
375 <div id='nsselect' class='contributions'>
376 <label for='nsselectbox'>" . wfMsgHtml('namespace') . "</label>
377 $namespaceselect
378 <input type='submit' value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />
379 <input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . " />
380 <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>
381 </div>";
382 $out .= '</form></div>';
383 return $out;
384 }
385 ?>