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