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