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