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