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