* (bug 11795) Be more paranoid about confirming accept-encoding header is present
[lhc/web/wiklou.git] / includes / RecentChange.php
1 <?php
2 /**
3 *
4 */
5
6 /**
7 * Utility class for creating new RC entries
8 * mAttribs:
9 * rc_id id of the row in the recentchanges table
10 * rc_timestamp time the entry was made
11 * rc_cur_time timestamp on the cur row
12 * rc_namespace namespace #
13 * rc_title non-prefixed db key
14 * rc_type is new entry, used to determine whether updating is necessary
15 * rc_minor is minor
16 * rc_cur_id page_id of associated page entry
17 * rc_user user id who made the entry
18 * rc_user_text user name who made the entry
19 * rc_comment edit summary
20 * rc_this_oldid rev_id associated with this entry (or zero)
21 * rc_last_oldid rev_id associated with the entry before this one (or zero)
22 * rc_bot is bot, hidden
23 * rc_ip IP address of the user in dotted quad notation
24 * rc_new obsolete, use rc_type==RC_NEW
25 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
26 * rc_old_len integer byte length of the text before the edit
27 * rc_new_len the same after the edit
28 *
29 * mExtra:
30 * prefixedDBkey prefixed db key, used by external app via msg queue
31 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
32 * lang the interwiki prefix, automatically set in save()
33 * oldSize text size before the change
34 * newSize text size after the change
35 *
36 * temporary: not stored in the database
37 * notificationtimestamp
38 * numberofWatchingusers
39 *
40 * @todo document functions and variables
41 */
42 class RecentChange
43 {
44 var $mAttribs = array(), $mExtra = array();
45 var $mTitle = false, $mMovedToTitle = false;
46 var $numberofWatchingusers = 0 ; # Dummy to prevent error message in SpecialRecentchangeslinked
47
48 # Factory methods
49
50 public static function newFromRow( $row )
51 {
52 $rc = new RecentChange;
53 $rc->loadFromRow( $row );
54 return $rc;
55 }
56
57 public static function newFromCurRow( $row, $rc_this_oldid = 0 )
58 {
59 $rc = new RecentChange;
60 $rc->loadFromCurRow( $row, $rc_this_oldid );
61 $rc->notificationtimestamp = false;
62 $rc->numberofWatchingusers = false;
63 return $rc;
64 }
65
66 /**
67 * Obtain the recent change with a given rc_id value
68 *
69 * @param $rcid rc_id value to retrieve
70 * @return RecentChange
71 */
72 public static function newFromId( $rcid ) {
73 $dbr = wfGetDB( DB_SLAVE );
74 $res = $dbr->select( 'recentchanges', '*', array( 'rc_id' => $rcid ), __METHOD__ );
75 if( $res && $dbr->numRows( $res ) > 0 ) {
76 $row = $dbr->fetchObject( $res );
77 $dbr->freeResult( $res );
78 return self::newFromRow( $row );
79 } else {
80 return NULL;
81 }
82 }
83
84 /**
85 * Find the first recent change matching some specific conditions
86 *
87 * @param array $conds Array of conditions
88 * @param mixed $fname Override the method name in profiling/logs
89 * @return RecentChange
90 */
91 public static function newFromConds( $conds, $fname = false ) {
92 if( $fname === false )
93 $fname = __METHOD__;
94 $dbr = wfGetDB( DB_SLAVE );
95 $res = $dbr->select(
96 'recentchanges',
97 '*',
98 $conds,
99 $fname
100 );
101 if( $res instanceof ResultWrapper && $res->numRows() > 0 ) {
102 $row = $res->fetchObject();
103 $res->free();
104 return self::newFromRow( $row );
105 }
106 return null;
107 }
108
109 # Accessors
110
111 function setAttribs( $attribs )
112 {
113 $this->mAttribs = $attribs;
114 }
115
116 function setExtra( $extra )
117 {
118 $this->mExtra = $extra;
119 }
120
121 function &getTitle()
122 {
123 if ( $this->mTitle === false ) {
124 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
125 }
126 return $this->mTitle;
127 }
128
129 function getMovedToTitle()
130 {
131 if ( $this->mMovedToTitle === false ) {
132 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'],
133 $this->mAttribs['rc_moved_to_title'] );
134 }
135 return $this->mMovedToTitle;
136 }
137
138 # Writes the data in this object to the database
139 function save()
140 {
141 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix;
142 $fname = 'RecentChange::save';
143
144 $dbw = wfGetDB( DB_MASTER );
145 if ( !is_array($this->mExtra) ) {
146 $this->mExtra = array();
147 }
148 $this->mExtra['lang'] = $wgLocalInterwiki;
149
150 if ( !$wgPutIPinRC ) {
151 $this->mAttribs['rc_ip'] = '';
152 }
153
154 ## If our database is strict about IP addresses, use NULL instead of an empty string
155 if ( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) {
156 unset( $this->mAttribs['rc_ip'] );
157 }
158
159 # Fixup database timestamps
160 $this->mAttribs['rc_timestamp'] = $dbw->timestamp($this->mAttribs['rc_timestamp']);
161 $this->mAttribs['rc_cur_time'] = $dbw->timestamp($this->mAttribs['rc_cur_time']);
162 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
163
164 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
165 if ( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id']==0 ) {
166 unset ( $this->mAttribs['rc_cur_id'] );
167 }
168
169 # Insert new row
170 $dbw->insert( 'recentchanges', $this->mAttribs, $fname );
171
172 # Set the ID
173 $this->mAttribs['rc_id'] = $dbw->insertId();
174
175 # Update old rows, if necessary
176 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
177 $lastTime = $this->mExtra['lastTimestamp'];
178 #$now = $this->mAttribs['rc_timestamp'];
179 #$curId = $this->mAttribs['rc_cur_id'];
180
181 # Don't bother looking for entries that have probably
182 # been purged, it just locks up the indexes needlessly.
183 global $wgRCMaxAge;
184 $age = time() - wfTimestamp( TS_UNIX, $lastTime );
185 if( $age < $wgRCMaxAge ) {
186 # live hack, will commit once tested - kate
187 # Update rc_this_oldid for the entries which were current
188 #
189 #$oldid = $this->mAttribs['rc_last_oldid'];
190 #$ns = $this->mAttribs['rc_namespace'];
191 #$title = $this->mAttribs['rc_title'];
192 #
193 #$dbw->update( 'recentchanges',
194 # array( /* SET */
195 # 'rc_this_oldid' => $oldid
196 # ), array( /* WHERE */
197 # 'rc_namespace' => $ns,
198 # 'rc_title' => $title,
199 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
200 # ), $fname
201 #);
202 }
203
204 # Update rc_cur_time
205 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
206 # array( 'rc_cur_id' => $curId ), $fname );
207 }
208
209 # Notify external application via UDP
210 if ( $wgRC2UDPAddress ) {
211 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
212 if ( $conn ) {
213 $line = $wgRC2UDPPrefix . $this->getIRCLine();
214 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
215 socket_close( $conn );
216 }
217 }
218
219 # E-mail notifications
220 global $wgUseEnotif;
221 if( $wgUseEnotif ) {
222 # this would be better as an extension hook
223 global $wgUser;
224 $enotif = new EmailNotification;
225 $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
226 $enotif->notifyOnPageChange( $wgUser, $title,
227 $this->mAttribs['rc_timestamp'],
228 $this->mAttribs['rc_comment'],
229 $this->mAttribs['rc_minor'],
230 $this->mAttribs['rc_last_oldid'] );
231 }
232
233 # Notify extensions
234 wfRunHooks( 'RecentChange_save', array( &$this ) );
235 }
236
237 /**
238 * Mark a given change as patrolled
239 *
240 * @param mixed $change RecentChange or corresponding rc_id
241 */
242 public static function markPatrolled( $change ) {
243 $rcid = $change instanceof RecentChange
244 ? $change->mAttribs['rc_id']
245 : $change;
246 $dbw = wfGetDB( DB_MASTER );
247 $dbw->update(
248 'recentchanges',
249 array(
250 'rc_patrolled' => 1
251 ),
252 array(
253 'rc_id' => $rcid
254 ),
255 __METHOD__
256 );
257 }
258
259 # Makes an entry in the database corresponding to an edit
260 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
261 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
262 $newId = 0)
263 {
264
265 if ( $bot === 'default' ) {
266 $bot = $user->isAllowed( 'bot' );
267 }
268
269 if ( !$ip ) {
270 $ip = wfGetIP();
271 if ( !$ip ) {
272 $ip = '';
273 }
274 }
275
276 $rc = new RecentChange;
277 $rc->mAttribs = array(
278 'rc_timestamp' => $timestamp,
279 'rc_cur_time' => $timestamp,
280 'rc_namespace' => $title->getNamespace(),
281 'rc_title' => $title->getDBkey(),
282 'rc_type' => RC_EDIT,
283 'rc_minor' => $minor ? 1 : 0,
284 'rc_cur_id' => $title->getArticleID(),
285 'rc_user' => $user->getID(),
286 'rc_user_text' => $user->getName(),
287 'rc_comment' => $comment,
288 'rc_this_oldid' => $newId,
289 'rc_last_oldid' => $oldId,
290 'rc_bot' => $bot ? 1 : 0,
291 'rc_moved_to_ns' => 0,
292 'rc_moved_to_title' => '',
293 'rc_ip' => $ip,
294 'rc_patrolled' => 0,
295 'rc_new' => 0, # obsolete
296 'rc_old_len' => $oldSize,
297 'rc_new_len' => $newSize
298 );
299
300 $rc->mExtra = array(
301 'prefixedDBkey' => $title->getPrefixedDBkey(),
302 'lastTimestamp' => $lastTimestamp,
303 'oldSize' => $oldSize,
304 'newSize' => $newSize,
305 );
306 $rc->save();
307 return( $rc->mAttribs['rc_id'] );
308 }
309
310 /**
311 * Makes an entry in the database corresponding to page creation
312 * Note: the title object must be loaded with the new id using resetArticleID()
313 * @todo Document parameters and return
314 */
315 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = 'default',
316 $ip='', $size = 0, $newId = 0 )
317 {
318 if ( !$ip ) {
319 $ip = wfGetIP();
320 if ( !$ip ) {
321 $ip = '';
322 }
323 }
324 if ( $bot === 'default' ) {
325 $bot = $user->isAllowed( 'bot' );
326 }
327
328 $rc = new RecentChange;
329 $rc->mAttribs = array(
330 'rc_timestamp' => $timestamp,
331 'rc_cur_time' => $timestamp,
332 'rc_namespace' => $title->getNamespace(),
333 'rc_title' => $title->getDBkey(),
334 'rc_type' => RC_NEW,
335 'rc_minor' => $minor ? 1 : 0,
336 'rc_cur_id' => $title->getArticleID(),
337 'rc_user' => $user->getID(),
338 'rc_user_text' => $user->getName(),
339 'rc_comment' => $comment,
340 'rc_this_oldid' => $newId,
341 'rc_last_oldid' => 0,
342 'rc_bot' => $bot ? 1 : 0,
343 'rc_moved_to_ns' => 0,
344 'rc_moved_to_title' => '',
345 'rc_ip' => $ip,
346 'rc_patrolled' => 0,
347 'rc_new' => 1, # obsolete
348 'rc_old_len' => 0,
349 'rc_new_len' => $size
350 );
351
352 $rc->mExtra = array(
353 'prefixedDBkey' => $title->getPrefixedDBkey(),
354 'lastTimestamp' => 0,
355 'oldSize' => 0,
356 'newSize' => $size
357 );
358 $rc->save();
359 return( $rc->mAttribs['rc_id'] );
360 }
361
362 # Makes an entry in the database corresponding to a rename
363 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
364 {
365 if ( !$ip ) {
366 $ip = wfGetIP();
367 if ( !$ip ) {
368 $ip = '';
369 }
370 }
371
372 $rc = new RecentChange;
373 $rc->mAttribs = array(
374 'rc_timestamp' => $timestamp,
375 'rc_cur_time' => $timestamp,
376 'rc_namespace' => $oldTitle->getNamespace(),
377 'rc_title' => $oldTitle->getDBkey(),
378 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
379 'rc_minor' => 0,
380 'rc_cur_id' => $oldTitle->getArticleID(),
381 'rc_user' => $user->getID(),
382 'rc_user_text' => $user->getName(),
383 'rc_comment' => $comment,
384 'rc_this_oldid' => 0,
385 'rc_last_oldid' => 0,
386 'rc_bot' => $user->isAllowed( 'bot' ) ? 1 : 0,
387 'rc_moved_to_ns' => $newTitle->getNamespace(),
388 'rc_moved_to_title' => $newTitle->getDBkey(),
389 'rc_ip' => $ip,
390 'rc_new' => 0, # obsolete
391 'rc_patrolled' => 1,
392 'rc_old_len' => NULL,
393 'rc_new_len' => NULL,
394 );
395
396 $rc->mExtra = array(
397 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
398 'lastTimestamp' => 0,
399 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
400 );
401 $rc->save();
402 }
403
404 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
405 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
406 }
407
408 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
409 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
410 }
411
412 # A log entry is different to an edit in that previous revisions are
413 # not kept
414 public static function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
415 $type, $action, $target, $logComment, $params )
416 {
417 if ( !$ip ) {
418 $ip = wfGetIP();
419 if ( !$ip ) {
420 $ip = '';
421 }
422 }
423
424 $rc = new RecentChange;
425 $rc->mAttribs = array(
426 'rc_timestamp' => $timestamp,
427 'rc_cur_time' => $timestamp,
428 'rc_namespace' => $title->getNamespace(),
429 'rc_title' => $title->getDBkey(),
430 'rc_type' => RC_LOG,
431 'rc_minor' => 0,
432 'rc_cur_id' => $title->getArticleID(),
433 'rc_user' => $user->getID(),
434 'rc_user_text' => $user->getName(),
435 'rc_comment' => $comment,
436 'rc_this_oldid' => 0,
437 'rc_last_oldid' => 0,
438 'rc_bot' => $user->isAllowed( 'bot' ) ? 1 : 0,
439 'rc_moved_to_ns' => 0,
440 'rc_moved_to_title' => '',
441 'rc_ip' => $ip,
442 'rc_patrolled' => 1,
443 'rc_new' => 0, # obsolete
444 'rc_old_len' => NULL,
445 'rc_new_len' => NULL,
446 );
447 $rc->mExtra = array(
448 'prefixedDBkey' => $title->getPrefixedDBkey(),
449 'lastTimestamp' => 0,
450 'logType' => $type,
451 'logAction' => $action,
452 'logComment' => $logComment,
453 'logTarget' => $target,
454 'logParams' => $params
455 );
456 $rc->save();
457 }
458
459 # Initialises the members of this object from a mysql row object
460 function loadFromRow( $row )
461 {
462 $this->mAttribs = get_object_vars( $row );
463 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
464 $this->mExtra = array();
465 }
466
467 # Makes a pseudo-RC entry from a cur row
468 function loadFromCurRow( $row )
469 {
470 $this->mAttribs = array(
471 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
472 'rc_cur_time' => $row->rev_timestamp,
473 'rc_user' => $row->rev_user,
474 'rc_user_text' => $row->rev_user_text,
475 'rc_namespace' => $row->page_namespace,
476 'rc_title' => $row->page_title,
477 'rc_comment' => $row->rev_comment,
478 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
479 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
480 'rc_cur_id' => $row->page_id,
481 'rc_this_oldid' => $row->rev_id,
482 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
483 'rc_bot' => 0,
484 'rc_moved_to_ns' => 0,
485 'rc_moved_to_title' => '',
486 'rc_ip' => '',
487 'rc_id' => $row->rc_id,
488 'rc_patrolled' => $row->rc_patrolled,
489 'rc_new' => $row->page_is_new, # obsolete
490 'rc_old_len' => $row->rc_old_len,
491 'rc_new_len' => $row->rc_new_len,
492 );
493
494 $this->mExtra = array();
495 }
496
497 /**
498 * Get an attribute value
499 *
500 * @param $name Attribute name
501 * @return mixed
502 */
503 public function getAttribute( $name ) {
504 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : NULL;
505 }
506
507 /**
508 * Gets the end part of the diff URL associated with this object
509 * Blank if no diff link should be displayed
510 */
511 function diffLinkTrail( $forceCur )
512 {
513 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
514 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
515 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
516 if ( $forceCur ) {
517 $trail .= '&diff=0' ;
518 } else {
519 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
520 }
521 } else {
522 $trail = '';
523 }
524 return $trail;
525 }
526
527 function cleanupForIRC( $text ) {
528 return str_replace(array("\n", "\r"), array("", ""), $text);
529 }
530
531 function getIRCLine() {
532 global $wgUseRCPatrol;
533
534 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
535 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
536 extract($this->mAttribs);
537 extract($this->mExtra);
538
539 $titleObj =& $this->getTitle();
540 if ( $rc_type == RC_LOG ) {
541 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
542 } else {
543 $title = $titleObj->getPrefixedText();
544 }
545 $title = $this->cleanupForIRC( $title );
546
547 $bad = array("\n", "\r");
548 $empty = array("", "");
549 $title = $titleObj->getPrefixedText();
550 $title = str_replace($bad, $empty, $title);
551
552 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
553 if ( $rc_type == RC_LOG ) {
554 $url = '';
555 } elseif ( $rc_new && $wgUseRCPatrol ) {
556 $url = $titleObj->getInternalURL("rcid=$rc_id");
557 } else if ( $rc_new ) {
558 $url = $titleObj->getInternalURL();
559 } else if ( $wgUseRCPatrol ) {
560 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
561 } else {
562 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
563 }
564
565 if ( isset( $oldSize ) && isset( $newSize ) ) {
566 $szdiff = $newSize - $oldSize;
567 if ($szdiff < -500) {
568 $szdiff = "\002$szdiff\002";
569 } elseif ($szdiff >= 0) {
570 $szdiff = '+' . $szdiff ;
571 }
572 $szdiff = '(' . $szdiff . ')' ;
573 } else {
574 $szdiff = '';
575 }
576
577 $user = $this->cleanupForIRC( $rc_user_text );
578
579 if ( $rc_type == RC_LOG ) {
580 $logTargetText = $logTarget->getPrefixedText();
581 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
582 $flag = $logAction;
583 } else {
584 $comment = $this->cleanupForIRC( $rc_comment );
585 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
586 }
587 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
588 # no colour (\003) switches back to the term default
589 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
590 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
591 return $fullString;
592 }
593
594 /**
595 * Returns the change size (HTML).
596 * The lengths can be given optionally.
597 */
598 function getCharacterDifference( $old = 0, $new = 0 ) {
599 global $wgRCChangedSizeThreshold, $wgLang;
600
601 if( $old === 0 ) {
602 $old = $this->mAttribs['rc_old_len'];
603 }
604 if( $new === 0 ) {
605 $new = $this->mAttribs['rc_new_len'];
606 }
607
608 if( $old === NULL || $new === NULL ) {
609 return '';
610 }
611
612 $szdiff = $new - $old;
613 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
614 $wgLang->formatNum($szdiff) );
615
616 if( $szdiff < $wgRCChangedSizeThreshold ) {
617 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
618 } elseif( $szdiff === 0 ) {
619 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
620 } elseif( $szdiff > 0 ) {
621 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
622 } else {
623 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';
624 }
625 }
626 }
627
628