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