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