Slowly merge in a few rev_deleted checks
[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 * @returns integer number of affected rows
242 */
243 public static function markPatrolled( $change ) {
244 $rcid = $change instanceof RecentChange
245 ? $change->mAttribs['rc_id']
246 : $change;
247 $dbw = wfGetDB( DB_MASTER );
248 $dbw->update(
249 'recentchanges',
250 array(
251 'rc_patrolled' => 1
252 ),
253 array(
254 'rc_id' => $rcid
255 ),
256 __METHOD__
257 );
258 return $dbw->affectedRows();
259 }
260
261 # Makes an entry in the database corresponding to an edit
262 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
263 $oldId, $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0,
264 $newId = 0)
265 {
266 if ( !$ip ) {
267 $ip = wfGetIP();
268 if ( !$ip ) {
269 $ip = '';
270 }
271 }
272
273 $rc = new RecentChange;
274 $rc->mAttribs = array(
275 'rc_timestamp' => $timestamp,
276 'rc_cur_time' => $timestamp,
277 'rc_namespace' => $title->getNamespace(),
278 'rc_title' => $title->getDBkey(),
279 'rc_type' => RC_EDIT,
280 'rc_minor' => $minor ? 1 : 0,
281 'rc_cur_id' => $title->getArticleID(),
282 'rc_user' => $user->getID(),
283 'rc_user_text' => $user->getName(),
284 'rc_comment' => $comment,
285 'rc_this_oldid' => $newId,
286 'rc_last_oldid' => $oldId,
287 'rc_bot' => $bot ? 1 : 0,
288 'rc_moved_to_ns' => 0,
289 'rc_moved_to_title' => '',
290 'rc_ip' => $ip,
291 'rc_patrolled' => 0,
292 'rc_new' => 0, # obsolete
293 'rc_old_len' => $oldSize,
294 'rc_new_len' => $newSize
295 );
296
297 $rc->mExtra = array(
298 'prefixedDBkey' => $title->getPrefixedDBkey(),
299 'lastTimestamp' => $lastTimestamp,
300 'oldSize' => $oldSize,
301 'newSize' => $newSize,
302 );
303 $rc->save();
304 return( $rc->mAttribs['rc_id'] );
305 }
306
307 /**
308 * Makes an entry in the database corresponding to page creation
309 * Note: the title object must be loaded with the new id using resetArticleID()
310 * @todo Document parameters and return
311 */
312 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
313 $ip='', $size = 0, $newId = 0 )
314 {
315 if ( !$ip ) {
316 $ip = wfGetIP();
317 if ( !$ip ) {
318 $ip = '';
319 }
320 }
321
322 $rc = new RecentChange;
323 $rc->mAttribs = array(
324 'rc_timestamp' => $timestamp,
325 'rc_cur_time' => $timestamp,
326 'rc_namespace' => $title->getNamespace(),
327 'rc_title' => $title->getDBkey(),
328 'rc_type' => RC_NEW,
329 'rc_minor' => $minor ? 1 : 0,
330 'rc_cur_id' => $title->getArticleID(),
331 'rc_user' => $user->getID(),
332 'rc_user_text' => $user->getName(),
333 'rc_comment' => $comment,
334 'rc_this_oldid' => $newId,
335 'rc_last_oldid' => 0,
336 'rc_bot' => $bot ? 1 : 0,
337 'rc_moved_to_ns' => 0,
338 'rc_moved_to_title' => '',
339 'rc_ip' => $ip,
340 'rc_patrolled' => 0,
341 'rc_new' => 1, # obsolete
342 'rc_old_len' => 0,
343 'rc_new_len' => $size
344 );
345
346 $rc->mExtra = array(
347 'prefixedDBkey' => $title->getPrefixedDBkey(),
348 'lastTimestamp' => 0,
349 'oldSize' => 0,
350 'newSize' => $size
351 );
352 $rc->save();
353 return( $rc->mAttribs['rc_id'] );
354 }
355
356 # Makes an entry in the database corresponding to a rename
357 public static function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
358 {
359 global $wgRequest;
360
361 if ( !$ip ) {
362 $ip = wfGetIP();
363 if ( !$ip ) {
364 $ip = '';
365 }
366 }
367
368 $rc = new RecentChange;
369 $rc->mAttribs = array(
370 'rc_timestamp' => $timestamp,
371 'rc_cur_time' => $timestamp,
372 'rc_namespace' => $oldTitle->getNamespace(),
373 'rc_title' => $oldTitle->getDBkey(),
374 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
375 'rc_minor' => 0,
376 'rc_cur_id' => $oldTitle->getArticleID(),
377 'rc_user' => $user->getID(),
378 'rc_user_text' => $user->getName(),
379 'rc_comment' => $comment,
380 'rc_this_oldid' => 0,
381 'rc_last_oldid' => 0,
382 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot' , true ) : 0,
383 'rc_moved_to_ns' => $newTitle->getNamespace(),
384 'rc_moved_to_title' => $newTitle->getDBkey(),
385 'rc_ip' => $ip,
386 'rc_new' => 0, # obsolete
387 'rc_patrolled' => 1,
388 'rc_old_len' => NULL,
389 'rc_new_len' => NULL,
390 );
391
392 $rc->mExtra = array(
393 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
394 'lastTimestamp' => 0,
395 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
396 );
397 $rc->save();
398 }
399
400 public static function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
401 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
402 }
403
404 public static function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
405 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
406 }
407
408 # A log entry is different to an edit in that previous revisions are
409 # not kept
410 public static function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
411 $type, $action, $target, $logComment, $params )
412 {
413 global $wgRequest;
414
415 if ( !$ip ) {
416 $ip = wfGetIP();
417 if ( !$ip ) {
418 $ip = '';
419 }
420 }
421
422 $rc = new RecentChange;
423 $rc->mAttribs = array(
424 'rc_timestamp' => $timestamp,
425 'rc_cur_time' => $timestamp,
426 'rc_namespace' => $title->getNamespace(),
427 'rc_title' => $title->getDBkey(),
428 'rc_type' => RC_LOG,
429 'rc_minor' => 0,
430 'rc_cur_id' => $title->getArticleID(),
431 'rc_user' => $user->getID(),
432 'rc_user_text' => $user->getName(),
433 'rc_comment' => $comment,
434 'rc_this_oldid' => 0,
435 'rc_last_oldid' => 0,
436 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot' , true ) : 0,
437 'rc_moved_to_ns' => 0,
438 'rc_moved_to_title' => '',
439 'rc_ip' => $ip,
440 'rc_patrolled' => 1,
441 'rc_new' => 0, # obsolete
442 'rc_old_len' => NULL,
443 'rc_new_len' => NULL,
444 );
445 $rc->mExtra = array(
446 'prefixedDBkey' => $title->getPrefixedDBkey(),
447 'lastTimestamp' => 0,
448 'logType' => $type,
449 'logAction' => $action,
450 'logComment' => $logComment,
451 'logTarget' => $target,
452 'logParams' => $params
453 );
454 $rc->save();
455 }
456
457 # Initialises the members of this object from a mysql row object
458 function loadFromRow( $row )
459 {
460 $this->mAttribs = get_object_vars( $row );
461 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
462 $this->mExtra = array();
463 }
464
465 # Makes a pseudo-RC entry from a cur row
466 function loadFromCurRow( $row )
467 {
468 $this->mAttribs = array(
469 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
470 'rc_cur_time' => $row->rev_timestamp,
471 'rc_user' => $row->rev_user,
472 'rc_user_text' => $row->rev_user_text,
473 'rc_namespace' => $row->page_namespace,
474 'rc_title' => $row->page_title,
475 'rc_comment' => $row->rev_comment,
476 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
477 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
478 'rc_cur_id' => $row->page_id,
479 'rc_this_oldid' => $row->rev_id,
480 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
481 'rc_bot' => 0,
482 'rc_moved_to_ns' => 0,
483 'rc_moved_to_title' => '',
484 'rc_ip' => '',
485 'rc_id' => $row->rc_id,
486 'rc_patrolled' => $row->rc_patrolled,
487 'rc_new' => $row->page_is_new, # obsolete
488 'rc_old_len' => $row->rc_old_len,
489 'rc_new_len' => $row->rc_new_len,
490 );
491
492 $this->mExtra = array();
493 }
494
495 /**
496 * Get an attribute value
497 *
498 * @param $name Attribute name
499 * @return mixed
500 */
501 public function getAttribute( $name ) {
502 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : NULL;
503 }
504
505 /**
506 * Gets the end part of the diff URL associated with this object
507 * Blank if no diff link should be displayed
508 */
509 function diffLinkTrail( $forceCur )
510 {
511 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
512 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
513 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
514 if ( $forceCur ) {
515 $trail .= '&diff=0' ;
516 } else {
517 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
518 }
519 } else {
520 $trail = '';
521 }
522 return $trail;
523 }
524
525 function cleanupForIRC( $text ) {
526 return str_replace(array("\n", "\r"), array("", ""), $text);
527 }
528
529 function getIRCLine() {
530 global $wgUseRCPatrol;
531
532 // FIXME: Would be good to replace these 2 extract() calls with something more explicit
533 // e.g. list ($rc_type, $rc_id) = array_values ($this->mAttribs); [or something like that]
534 extract($this->mAttribs);
535 extract($this->mExtra);
536
537 $titleObj =& $this->getTitle();
538 if ( $rc_type == RC_LOG ) {
539 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
540 } else {
541 $title = $titleObj->getPrefixedText();
542 }
543 $title = $this->cleanupForIRC( $title );
544
545 $bad = array("\n", "\r");
546 $empty = array("", "");
547 $title = $titleObj->getPrefixedText();
548 $title = str_replace($bad, $empty, $title);
549
550 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
551 if ( $rc_type == RC_LOG ) {
552 $url = '';
553 } elseif ( $rc_new && $wgUseRCPatrol ) {
554 $url = $titleObj->getInternalURL("rcid=$rc_id");
555 } else if ( $rc_new ) {
556 $url = $titleObj->getInternalURL();
557 } else if ( $wgUseRCPatrol ) {
558 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
559 } else {
560 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
561 }
562
563 if ( isset( $oldSize ) && isset( $newSize ) ) {
564 $szdiff = $newSize - $oldSize;
565 if ($szdiff < -500) {
566 $szdiff = "\002$szdiff\002";
567 } elseif ($szdiff >= 0) {
568 $szdiff = '+' . $szdiff ;
569 }
570 $szdiff = '(' . $szdiff . ')' ;
571 } else {
572 $szdiff = '';
573 }
574
575 $user = $this->cleanupForIRC( $rc_user_text );
576
577 if ( $rc_type == RC_LOG ) {
578 $logTargetText = $logTarget->getPrefixedText();
579 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
580 $flag = $logAction;
581 } else {
582 $comment = $this->cleanupForIRC( $rc_comment );
583 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
584 }
585 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
586 # no colour (\003) switches back to the term default
587 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
588 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
589 return $fullString;
590 }
591
592 /**
593 * Returns the change size (HTML).
594 * The lengths can be given optionally.
595 */
596 function getCharacterDifference( $old = 0, $new = 0 ) {
597 global $wgRCChangedSizeThreshold, $wgLang;
598
599 if( $old === 0 ) {
600 $old = $this->mAttribs['rc_old_len'];
601 }
602 if( $new === 0 ) {
603 $new = $this->mAttribs['rc_new_len'];
604 }
605
606 if( $old === NULL || $new === NULL ) {
607 return '';
608 }
609
610 $szdiff = $new - $old;
611 $formatedSize = wfMsgExt( 'rc-change-size', array( 'parsemag', 'escape'),
612 $wgLang->formatNum($szdiff) );
613
614 if( $szdiff < $wgRCChangedSizeThreshold ) {
615 return '<strong class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</strong>';
616 } elseif( $szdiff === 0 ) {
617 return '<span class=\'mw-plusminus-null\'>(' . $formatedSize . ')</span>';
618 } elseif( $szdiff > 0 ) {
619 return '<span class=\'mw-plusminus-pos\'>(+' . $formatedSize . ')</span>';
620 } else {
621 return '<span class=\'mw-plusminus-neg\'>(' . $formatedSize . ')</span>';
622 }
623 }
624 }
625
626