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