show "earlier" and "latest" links
[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 function get_first_offset_for_paging() {
108 list($index, $usercond) = $this->get_user_cond();
109 $use_index = $this->dbr->useIndexClause($index);
110
111 $sql = "SELECT rev_timestamp FROM page, revision $use_index " .
112 "WHERE page_id = rev_page AND " .
113 "rev_user_text = " . $this->dbr->addQuotes($this->username);
114 $sql .= $this->get_namespace_cond();
115 $sql .= $this->get_minor_cond();
116 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . ($this->limit + 1);
117 $res = $this->dbr->query($sql);
118 $rows = array();
119 while ($obj = $this->dbr->fetchObject($res))
120 $rows[] = $obj;
121 $this->dbr->freeResult($res);
122 return $rows[count($rows) - 1]->rev_timestamp;
123 }
124
125 /* private */ function make_sql() {
126 $userCond = $condition = $index = $minorQuery = $nsQuery
127 = $offsetQuery = $limitQuery = $nsinvert = "";
128
129 $minorQuery = $this->get_minor_cond();
130 $nsQuery = $this->get_namespace_cond();
131
132 extract($this->dbr->tableNames('page', 'revision'));
133 list($index, $userCond) = $this->get_user_cond();
134
135 $limitQuery = "LIMIT {$this->limit}";
136 if ($this->offset)
137 $offsetQuery = "AND rev_timestamp < {$this->offset}";
138
139 $use_index = $this->dbr->useIndexClause($index);
140 $sql = "SELECT
141 page_namespace,page_title,page_is_new,page_latest,
142 rev_id,rev_timestamp,rev_comment,rev_minor_edit,rev_user_text,
143 rev_deleted
144 FROM $page,$revision $use_index
145 WHERE page_id=rev_page AND $userCond $minorQuery $nsQuery $offsetQuery
146 ORDER BY rev_timestamp DESC $limitQuery";
147 return $sql;
148 }
149
150 function find() {
151 $contribs = array();
152 $res = $this->dbr->query($this->make_sql(), "contribs_finder::find");
153 while ($c = $this->dbr->fetchObject($res))
154 $contribs[] = $c;
155 $this->dbr->freeResult($res);
156 return $contribs;
157 }
158 };
159
160 /**
161 * Special page "user contributions".
162 * Shows a list of the contributions of a user.
163 *
164 * @return none
165 * @param string $par (optional) user name of the user for which to show the contributions
166 */
167 function wfSpecialContributions( $par = null ) {
168 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest, $wgTitle;
169 $fname = 'wfSpecialContributions';
170
171 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
172 if (!strlen($target)) {
173 $wgOut->errorpage('notargettitle', 'notargettext');
174 return;
175 }
176
177 $nt = Title::newFromURL( $target );
178 if (!$nt) {
179 $wgOut->errorpage( 'notargettitle', 'notargettext' );
180 return;
181 }
182 $nt =& Title::makeTitle(NS_USER, $nt->getDBkey());
183
184 $namespace = $wgRequest->getIntOrNull('namespace');
185 $invert = $wgRequest->getBool('invert');
186 $hideminor = $wgRequest->getBool('hideminor');
187 $limit = min($wgRequest->getInt('limit', 50), 500);
188 $offset = $wgRequest->getVal('offset');
189 /* Offset must be an integral. */
190 if (!strlen($offset) || !preg_match("/^[0-9]+$/", $offset))
191 $offset = 0;
192
193 $title = Title::makeTitle(NS_SPECIAL, "Contributions");
194 $urlbits = "hideminor=$hideminor&namespace=$namespace&invert=$invert&target=" . wfUrlEncode($target);
195 $myurl = $title->escapeLocalURL($urlbits);
196
197 $finder = new contribs_finder($nt->getText());
198
199 $finder->set_namespace($namespace, $invert);
200 $finder->set_hide_minor($hideminor);
201 $finder->set_limit($limit);
202 $finder->set_offset($offset);
203
204 if ($wgRequest->getText('go') == "prev") {
205 $prevts = $finder->get_previous_offset_for_paging();
206 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
207 $wgOut->redirect($prevurl);
208 return;
209 }
210
211 if ($wgRequest->getText('go') == "first") {
212 $prevts = $finder->get_first_offset_for_paging();
213 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
214 $wgOut->redirect($prevurl);
215 return;
216 }
217
218 $sk = $wgUser->getSkin();
219
220 $id = User::idFromName($nt->getText());
221
222 if ( 0 == $id ) {
223 $ul = $nt->getText();
224 } else {
225 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
226 $userCond = '=' . $id;
227 }
228 $talk = $nt->getTalkPage();
229 if( $talk ) {
230 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) ) . ')';
231 }
232
233 if ($target == 'newbies') {
234 $ul = wfMsg ('newbies');
235 }
236
237 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
238
239 $contribsPage = Title::makeTitle( NS_SPECIAL, 'Contributions' );
240 $mlink = $sk->makeKnownLinkObj( $contribsPage,
241 $hideminor ? wfMsgHtml( 'show' ) : wfMsgHtml( 'hide' ),
242 wfArrayToCGI( array(
243 'target' => $nt->getPrefixedDbKey(),
244 'offset' => $offset,
245 'limit' => $limit,
246 'hideminor' => $hideminor ? 0 : 1,
247 'namespace' => $namespace ) ) );
248
249 $contribs = $finder->find();
250
251 if (count($contribs) == 0) {
252 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
253 return;
254 }
255
256 list($early, $late) = $finder->get_edit_limits();
257 $lastts = count($contribs) ? $contribs[count($contribs) - 1]->rev_timestamp : 0;
258 $atstart = (!count($contribs) || $late == $contribs[0]->rev_timestamp);
259 $atend = (!count($contribs) || $early == $lastts);
260 wfdebug("early=$early late=$late lastts=$lastts\n");
261 $wgOut->addHTML(ucNamespaceForm($target, $hideminor, $namespace, $invert));
262
263 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
264
265 $firsttext = wfMsgHtml("histfirst");
266 $lasttext = wfMsgHtml("histlast");
267
268 $prevtext = wfMsg("prevn", $limit);
269 if ($atstart) {
270 $lastlink = $lasttext;
271 $prevlink = $prevtext;
272 } else {
273 $lastlink = "<a href=\"$myurl&amp;limit=$limit\">$lasttext</a>";
274 $prevlink = "<a href=\"$myurl&amp;offset=$offset&amp;limit=$limit&amp;go=prev\">$prevtext</a>";
275 }
276
277 $nexttext = wfMsg("nextn", $limit);
278 if ($atend) {
279 $firstlink = $firsttext;
280 $nextlink = $nexttext;
281 } else {
282 $firstlink = "<a href=\"$myurl&amp;limit=$limit&;go=first\">$firsttext</a>";
283 $nextlink = "<a href=\"$myurl&amp;offset=$lastts&amp;limit=$limit\">$nexttext</a>";
284 }
285 $firstlast = "($lastlink | $firstlink)";
286
287 $urls = array();
288 foreach (array(20, 50, 100, 250, 500) as $num)
289 $urls[] = "<a href=\"$myurl&offset=$offset&limit={$num}\">".$wgLang->formatNum($num)."</a>";
290 $bits = implode($urls, ' | ');
291
292 $prevnextbits = "$firstlast " . wfMsgHtml("viewprevnext", $prevlink, $nextlink, $bits);
293
294 $shm = wfMsgHtml( "contribs-showhideminor", $mlink );
295 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)</p>\n");
296
297 $wgOut->addHTML( "<ul>\n" );
298
299 foreach ($contribs as $contrib)
300 $wgOut->addHTML(ucListEdit($sk, $contrib));
301
302 $wgOut->addHTML( "</ul>\n" );
303 $wgOut->addHTML( "<br />{$prevnextbits} ($shm)\n");
304 }
305
306
307 /**
308 * Generates each row in the contributions list.
309 *
310 * Contributions which are marked "top" are currently on top of the history.
311 * For these contributions, a [rollback] link is shown for users with sysop
312 * privileges. The rollback link restores the most recent version that was not
313 * written by the target user.
314 *
315 * If the contributions page is called with the parameter &bot=1, all rollback
316 * links also get that parameter. It causes the edit itself and the rollback
317 * to be marked as "bot" edits. Bot edits are hidden by default from recent
318 * changes, so this allows sysops to combat a busy vandal without bothering
319 * other users.
320 *
321 * @todo This would probably look a lot nicer in a table.
322 */
323 function ucListEdit( $sk, $row ) {
324 $fname = 'ucListEdit';
325 wfProfileIn( $fname );
326
327 global $wgLang, $wgOut, $wgUser, $wgRequest;
328 static $messages;
329 if( !isset( $messages ) ) {
330 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
331 $messages[$msg] = wfMsg( $msg );
332 }
333 }
334
335 $page =& Title::makeTitle( $row->page_namespace, $row->page_title );
336 $link = $sk->makeKnownLinkObj( $page, '' );
337 $difftext = $topmarktext = '';
338 if( $row->rev_id == $row->page_latest ) {
339 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
340 if( !$row->page_is_new ) {
341 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
342 } else {
343 $difftext .= $messages['newarticle'];
344 }
345
346 if( $wgUser->isAllowed('rollback') ) {
347 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
348 $extraRollback .= '&token=' . urlencode(
349 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text ) ) );
350 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
351 $messages['rollbacklink'],
352 'action=rollback&from=' . urlencode( $row->rev_user_text ) . $extraRollback ) .']';
353 }
354
355 }
356 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
357 $difftext = '(' . $messages['diff'] . ')';
358 } else {
359 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id );
360 }
361 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
362
363 $comment = $sk->commentBlock( $row->rev_comment, $page );
364 $d = $wgLang->timeanddate( $row->rev_timestamp, true );
365
366 if( $row->rev_minor_edit ) {
367 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
368 } else {
369 $mflag = '';
370 }
371
372 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
373 if( $row->rev_deleted ) {
374 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
375 }
376 $ret = "<li>$ret</li>\n";
377 wfProfileOut( $fname );
378 return $ret;
379 }
380
381 /**
382 * Generates a form used to restrict display of contributions
383 * to a specific namespace
384 *
385 * @return none
386 * @param string $target target user to show contributions for
387 * @param string $hideminor whether minor contributions are hidden
388 * @param string $namespace currently selected namespace, NULL for show all
389 * @param bool $invert inverts the namespace selection on true (default null)
390 */
391 function ucNamespaceForm ( $target, $hideminor, $namespace, $invert ) {
392 global $wgContLang, $wgScript;
393
394 $namespaceselect = "<select name='namespace' id='nsselectbox'>";
395 $namespaceselect .= '<option value="" '.(is_null($namespace) ? ' selected="selected"' : '').'>'.wfMsgHtml( 'contributionsall' ).'</option>';
396 $arr = $wgContLang->getFormattedNamespaces();
397 foreach( $arr as $ns => $name ) {
398 if( $ns < NS_MAIN )
399 continue;
400 $n = $ns === NS_MAIN ? wfMsgHtml( 'blanknamespace' ) : htmlspecialchars( $name );
401 $sel = $namespace == $ns ? ' selected="selected"' : '';
402 $namespaceselect .= "<option value='$ns'$sel>$n</option>";
403 }
404 $namespaceselect .= '</select>';
405
406 $action = htmlspecialchars( $wgScript );
407 $out = "<div class='namespaceselector'><form method='get' action=\"$action\">";
408 $out .= '<input type="hidden" name="title" value="' . htmlspecialchars( $wgContLang->specialpage( 'Contributions' ) ) . '" />';
409 $out .= '<input type="hidden" name="target" value="' . htmlspecialchars( $target ) . '" />';
410 $out .= '<input type="hidden" name="hideminor" value="' . ( $hideminor ? 1 : 0 ) .'" />';
411 $out .= "
412 <div id='nsselect' class='contributions'>
413 <label for='nsselectbox'>" . wfMsgHtml('namespace') . "</label>
414 $namespaceselect
415 <input type='submit' value=\"" . wfMsgHtml( 'allpagessubmit' ) . "\" />
416 <input type='checkbox' name='invert' value='1' id='nsinvert'" . ( $invert ? ' checked="checked"' : '' ) . " />
417 <label for='nsinvert'>" . wfMsgHtml('invert') . "</label>
418 </div>";
419 $out .= '</form></div>';
420 return $out;
421 }
422 ?>