* Fix explicit s-maxage=0 on raw pages; should help with proxy issues in
[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 if( ( $id != 0 ) || ( $id == 0 && User::isIP( $nt->getText() ) ) ) {
229 if( $wgUser->isAllowed( 'block' ) ) {
230 $ul .= ' | ' . $sk->makeLinkObj( Title::makeTitle( NS_SPECIAL, 'Blockip/' . $nt->getText() ), wfMsgHtml( 'blocklink' ) );
231 }
232 $ul .= ' | ' . $sk->makeLinkObj( Title::makeTitle( NS_SPECIAL, 'Log' ), LogPage::logName( 'block' ), 'type=block&page=' . urlencode( $nt->getPrefixedText() ) );
233 }
234 $ul .= ')';
235 }
236
237 if ($target == 'newbies') {
238 $ul = wfMsg ('newbies');
239 }
240
241 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
242
243 wfRunHooks('SpecialContributionsBeforeMainOutput', $id );
244
245 $arr = $wgContLang->getFormattedNamespaces();
246 $nsform = "<form method='get' action=\"$wgScript\">\n";
247 $nsform .= wfElement('input', array(
248 'name' => 'title',
249 'type' => 'hidden',
250 'value' => $wgTitle->getPrefixedText()));
251 $nsform .= wfElement('input', array(
252 'name' => 'offset',
253 'type' => 'hidden',
254 'value' => $offset));
255 $nsform .= wfElement('input', array(
256 'name' => 'limit',
257 'type' => 'hidden',
258 'value' => $limit));
259 $nsform .= wfElement('input', array(
260 'name' => 'target',
261 'type' => 'hidden',
262 'value' => $target));
263 $nsform .= '<p>';
264 $nsform .= wfMsgHtml('namespace');
265
266 $nsform .= ' ';
267 $nsform .= HTMLnamespaceselector( $ns, '' );
268
269 $nsform .= wfElement('input', array(
270 'type' => 'submit',
271 'value' => wfMsg('allpagessubmit')));
272 $nsform .= "</p></form>\n";
273
274 $wgOut->addHTML($nsform);
275
276 $contribsPage = Title::makeTitle( NS_SPECIAL, 'Contributions' );
277 $contribs = $finder->find();
278
279 if (count($contribs) == 0) {
280 $wgOut->addWikiText( wfMsg( 'nocontribs' ) );
281 return;
282 }
283
284 list($early, $late) = $finder->get_edit_limits();
285 $lastts = count($contribs) ? $contribs[count($contribs) - 1]->rev_timestamp : 0;
286 $atstart = (!count($contribs) || $late == $contribs[0]->rev_timestamp);
287 $atend = (!count($contribs) || $early == $lastts);
288
289 $firsttext = wfMsgHtml('histfirst');
290 $lasttext = wfMsgHtml('histlast');
291
292 $prevtext = wfMsg('prevn', $limit);
293 if ($atstart) {
294 $lastlink = $lasttext;
295 $prevlink = $prevtext;
296 } else {
297 $lastlink = "<a href=\"$myurl&amp;limit=$limit$xnsurl$boturl\">$lasttext</a>";
298 $prevlink = "<a href=\"$myurl&amp;offset=$offset&amp;limit=$limit$xnsurl$boturl&amp;go=prev\">$prevtext</a>";
299 }
300
301 $nexttext = wfMsg('nextn', $limit);
302 if ($atend) {
303 $firstlink = $firsttext;
304 $nextlink = $nexttext;
305 } else {
306 $firstlink = "<a href=\"$myurl&amp;limit=$limit$xnsurl$boturl&amp;go=first\">$firsttext</a>";
307 $nextlink = "<a href=\"$myurl&amp;offset=$lastts&amp;limit=$limit$xnsurl$boturl\">$nexttext</a>";
308 }
309 if ($target == 'newbies') {
310 $firstlast ="($lastlink)";
311 } else {
312 $firstlast = "($lastlink | $firstlink)";
313 }
314
315 $urls = array();
316 foreach (array(20, 50, 100, 250, 500) as $num)
317 $urls[] = "<a href=\"$myurl&amp;offset=$offset&amp;limit={$num}$xnsurl$boturl\">".$wgLang->formatNum($num)."</a>";
318 $bits = implode($urls, ' | ');
319
320 $prevnextbits = $firstlast .' '. wfMsgHtml('viewprevnext', $prevlink, $nextlink, $bits);
321
322 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n");
323
324 $wgOut->addHTML( "<ul>\n" );
325
326 foreach ($contribs as $contrib)
327 $wgOut->addHTML(ucListEdit($sk, $contrib));
328
329 $wgOut->addHTML( "</ul>\n" );
330 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n");
331 }
332
333
334 /**
335 * Generates each row in the contributions list.
336 *
337 * Contributions which are marked "top" are currently on top of the history.
338 * For these contributions, a [rollback] link is shown for users with sysop
339 * privileges. The rollback link restores the most recent version that was not
340 * written by the target user.
341 *
342 * If the contributions page is called with the parameter &bot=1, all rollback
343 * links also get that parameter. It causes the edit itself and the rollback
344 * to be marked as "bot" edits. Bot edits are hidden by default from recent
345 * changes, so this allows sysops to combat a busy vandal without bothering
346 * other users.
347 *
348 * @todo This would probably look a lot nicer in a table.
349 */
350 function ucListEdit( $sk, $row ) {
351 $fname = 'ucListEdit';
352 wfProfileIn( $fname );
353
354 global $wgLang, $wgOut, $wgUser, $wgRequest;
355 static $messages;
356 if( !isset( $messages ) ) {
357 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
358 $messages[$msg] = wfMsg( $msg );
359 }
360 }
361
362 $page =& Title::makeTitle( $row->page_namespace, $row->page_title );
363 $link = $sk->makeKnownLinkObj( $page, '' );
364 $difftext = $topmarktext = '';
365 if( $row->rev_id == $row->page_latest ) {
366 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
367 if( !$row->page_is_new ) {
368 $difftext .= '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=0' ) . ')';
369 } else {
370 $difftext .= $messages['newarticle'];
371 }
372
373 if( $wgUser->isAllowed('rollback') ) {
374 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
375 $extraRollback .= '&token=' . urlencode(
376 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text ) ) );
377 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
378 $messages['rollbacklink'],
379 'action=rollback&from=' . urlencode( $row->rev_user_text ) . $extraRollback ) .']';
380 }
381
382 }
383 if( $row->rev_deleted && !$wgUser->isAllowed( 'delete' ) ) {
384 $difftext = '(' . $messages['diff'] . ')';
385 } else {
386 $difftext = '(' . $sk->makeKnownLinkObj( $page, $messages['diff'], 'diff=prev&oldid='.$row->rev_id ) . ')';
387 }
388 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
389
390 $comment = $sk->commentBlock( $row->rev_comment, $page, (bool)$row->rev_deleted );
391 $d = $wgLang->timeanddate( wfTimestamp(TS_MW, $row->rev_timestamp), true );
392
393 if( $row->rev_minor_edit ) {
394 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
395 } else {
396 $mflag = '';
397 }
398
399 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
400 if( $row->rev_deleted ) {
401 $ret = "<span class='deleted'>$ret</span>";
402 }
403 $ret = "<li>$ret</li>\n";
404 wfProfileOut( $fname );
405 return $ret;
406 }
407
408 ?>