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