add namespace filtering without UI so i can it on the live situ
[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 LIMIT " . ($this->limit+1);
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 "rev_user_text = " . $this->dbr->addQuotes($this->username) .
105 $nscond;
106 $sql .= " ORDER BY rev_timestamp ASC LIMIT " . ($this->limit + 1);
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_timestamp,rev_comment,rev_minor_edit,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 $limitQuery";
134 return $sql;
135 }
136
137 function find() {
138 $contribs = array();
139 $res = $this->dbr->query($this->make_sql(), "contribs_finder::find");
140 while ($c = $this->dbr->fetchObject($res))
141 $contribs[] = $c;
142 $this->dbr->freeResult($res);
143 return $contribs;
144 }
145 };
146
147 /**
148 * Special page "user contributions".
149 * Shows a list of the contributions of a user.
150 *
151 * @return none
152 * @param string $par (optional) user name of the user for which to show the contributions
153 */
154 function wfSpecialContributions( $par = null ) {
155 global $wgUser, $wgOut, $wgLang, $wgContLang, $wgRequest, $wgTitle;
156 $fname = 'wfSpecialContributions';
157
158 $target = isset($par) ? $par : $wgRequest->getVal( 'target' );
159 if (!strlen($target)) {
160 $wgOut->errorpage('notargettitle', 'notargettext');
161 return;
162 }
163
164 $nt = Title::newFromURL( $target );
165 if (!$nt) {
166 $wgOut->errorpage( 'notargettitle', 'notargettext' );
167 return;
168 }
169 $nt =& Title::makeTitle(NS_USER, $nt->getDBkey());
170
171 $limit = min($wgRequest->getInt('limit', 50), 500);
172 $offset = $wgRequest->getVal('offset');
173 /* Offset must be an integral. */
174 if (!strlen($offset) || !preg_match("/^[0-9]+$/", $offset))
175 $offset = 0;
176
177 $title = Title::makeTitle(NS_SPECIAL, "Contributions");
178 $urlbits = "target=" . wfUrlEncode($target);
179 $myurl = $title->escapeLocalURL($urlbits);
180
181 $finder = new contribs_finder(($target == 'newbies') ? 'newbies' : $nt->getText());
182
183 $finder->set_limit($limit);
184 $finder->set_offset($offset);
185
186 if (($ns = $wgRequest->getVal('namespace', null)) !== null)
187 $finder->set_namespace($ns);
188
189 if ($wgRequest->getText('go') == "prev") {
190 $prevts = $finder->get_previous_offset_for_paging();
191 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
192 $wgOut->redirect($prevurl);
193 return;
194 }
195
196 if ($wgRequest->getText('go') == "first") {
197 $prevts = $finder->get_first_offset_for_paging();
198 $prevurl = $title->getLocalURL($urlbits . "&offset=$prevts&limit=$limit");
199 $wgOut->redirect($prevurl);
200 return;
201 }
202
203 $sk = $wgUser->getSkin();
204
205 $id = User::idFromName($nt->getText());
206
207 if ( 0 == $id ) {
208 $ul = $nt->getText();
209 } else {
210 $ul = $sk->makeLinkObj( $nt, htmlspecialchars( $nt->getText() ) );
211 $userCond = '=' . $id;
212 }
213 $talk = $nt->getTalkPage();
214 if( $talk ) {
215 $ul .= ' (' . $sk->makeLinkObj( $talk, $wgLang->getNsText( NS_TALK ) ) . ')';
216 }
217
218 if ($target == 'newbies') {
219 $ul = wfMsg ('newbies');
220 }
221
222 $wgOut->setSubtitle( wfMsgHtml( 'contribsub', $ul ) );
223
224 $contribsPage = Title::makeTitle( NS_SPECIAL, 'Contributions' );
225 $contribs = $finder->find();
226
227 if (count($contribs) == 0) {
228 $wgOut->addWikiText( wfMsg( "nocontribs" ) );
229 return;
230 }
231
232 list($early, $late) = $finder->get_edit_limits();
233 $lastts = count($contribs) ? $contribs[count($contribs) - 1]->rev_timestamp : 0;
234 $atstart = (!count($contribs) || $late == $contribs[0]->rev_timestamp);
235 $atend = (!count($contribs) || $early == $lastts);
236
237 $lasturl = $wgTitle->escapeLocalURL("action=history&limit={$limit}");
238
239 $firsttext = wfMsgHtml("histfirst");
240 $lasttext = wfMsgHtml("histlast");
241
242 $prevtext = wfMsg("prevn", $limit);
243 if ($atstart) {
244 $lastlink = $lasttext;
245 $prevlink = $prevtext;
246 } else {
247 $lastlink = "<a href=\"$myurl&amp;limit=$limit\">$lasttext</a>";
248 $prevlink = "<a href=\"$myurl&amp;offset=$offset&amp;limit=$limit&amp;go=prev\">$prevtext</a>";
249 }
250
251 $nexttext = wfMsg("nextn", $limit);
252 if ($atend) {
253 $firstlink = $firsttext;
254 $nextlink = $nexttext;
255 } else {
256 $firstlink = "<a href=\"$myurl&amp;limit=$limit&amp;go=first\">$firsttext</a>";
257 $nextlink = "<a href=\"$myurl&amp;offset=$lastts&amp;limit=$limit\">$nexttext</a>";
258 }
259 $firstlast = "($lastlink | $firstlink)";
260
261 $urls = array();
262 foreach (array(20, 50, 100, 250, 500) as $num)
263 $urls[] = "<a href=\"$myurl&amp;offset=$offset&amp;limit={$num}\">".$wgLang->formatNum($num)."</a>";
264 $bits = implode($urls, ' | ');
265
266 $prevnextbits = "$firstlast " . wfMsgHtml("viewprevnext", $prevlink, $nextlink, $bits);
267
268 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n");
269
270 $wgOut->addHTML( "<ul>\n" );
271
272 foreach ($contribs as $contrib)
273 $wgOut->addHTML(ucListEdit($sk, $contrib));
274
275 $wgOut->addHTML( "</ul>\n" );
276 $wgOut->addHTML( "<p>{$prevnextbits}</p>\n");
277 }
278
279
280 /**
281 * Generates each row in the contributions list.
282 *
283 * Contributions which are marked "top" are currently on top of the history.
284 * For these contributions, a [rollback] link is shown for users with sysop
285 * privileges. The rollback link restores the most recent version that was not
286 * written by the target user.
287 *
288 * If the contributions page is called with the parameter &bot=1, all rollback
289 * links also get that parameter. It causes the edit itself and the rollback
290 * to be marked as "bot" edits. Bot edits are hidden by default from recent
291 * changes, so this allows sysops to combat a busy vandal without bothering
292 * other users.
293 *
294 * @todo This would probably look a lot nicer in a table.
295 */
296 function ucListEdit( $sk, $row ) {
297 $fname = 'ucListEdit';
298 wfProfileIn( $fname );
299
300 global $wgLang, $wgOut, $wgUser, $wgRequest;
301 static $messages;
302 if( !isset( $messages ) ) {
303 foreach( explode( ' ', 'uctop diff newarticle rollbacklink diff hist minoreditletter' ) as $msg ) {
304 $messages[$msg] = wfMsg( $msg );
305 }
306 }
307
308 $page =& Title::makeTitle( $row->page_namespace, $row->page_title );
309 $link = $sk->makeKnownLinkObj( $page, '' );
310 $difftext = $topmarktext = '';
311 if( $row->rev_id == $row->page_latest ) {
312 $topmarktext .= '<strong>' . $messages['uctop'] . '</strong>';
313 if( !$row->page_is_new ) {
314 $difftext .= $sk->makeKnownLinkObj( $page, '(' . $messages['diff'] . ')', 'diff=0' );
315 } else {
316 $difftext .= $messages['newarticle'];
317 }
318
319 if( $wgUser->isAllowed('rollback') ) {
320 $extraRollback = $wgRequest->getBool( 'bot' ) ? '&bot=1' : '';
321 $extraRollback .= '&token=' . urlencode(
322 $wgUser->editToken( array( $page->getPrefixedText(), $row->rev_user_text ) ) );
323 $topmarktext .= ' ['. $sk->makeKnownLinkObj( $page,
324 $messages['rollbacklink'],
325 'action=rollback&from=' . urlencode( $row->rev_user_text ) . $extraRollback ) .']';
326 }
327
328 }
329 if( $row->rev_deleted && !$wgUser->isAllowed( 'undelete' ) ) {
330 $difftext = '(' . $messages['diff'] . ')';
331 } else {
332 $difftext = $sk->makeKnownLinkObj( $page, '(' . $messages['diff'].')', 'diff=prev&oldid='.$row->rev_id );
333 }
334 $histlink='('.$sk->makeKnownLinkObj( $page, $messages['hist'], 'action=history' ) . ')';
335
336 $comment = $sk->commentBlock( $row->rev_comment, $page );
337 $d = $wgLang->timeanddate( $row->rev_timestamp, true );
338
339 if( $row->rev_minor_edit ) {
340 $mflag = '<span class="minor">' . $messages['minoreditletter'] . '</span> ';
341 } else {
342 $mflag = '';
343 }
344
345 $ret = "{$d} {$histlink} {$difftext} {$mflag} {$link} {$comment} {$topmarktext}";
346 if( $row->rev_deleted ) {
347 $ret = '<span class="deleted">' . $ret . '</span> ' . htmlspecialchars( wfMsg( 'deletedrev' ) );
348 }
349 $ret = "<li>$ret</li>\n";
350 wfProfileOut( $fname );
351 return $ret;
352 }
353
354 ?>