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