Use NULL instead of 0 to indicate "no page" for cases where rc_cur_id is a FK.
[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 *
28 * mExtra:
29 * prefixedDBkey prefixed db key, used by external app via msg queue
30 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
31 * lang the interwiki prefix, automatically set in save()
32 * oldSize text size before the change
33 * newSize text size after the change
34 *
35 * temporary: not stored in the database
36 * notificationtimestamp
37 * numberofWatchingusers
38 *
39 * @todo document functions and variables
40 * @package MediaWiki
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 /* static */ function newFromRow( $row )
51 {
52 $rc = new RecentChange;
53 $rc->loadFromRow( $row );
54 return $rc;
55 }
56
57 /* 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 # Accessors
67
68 function setAttribs( $attribs )
69 {
70 $this->mAttribs = $attribs;
71 }
72
73 function setExtra( $extra )
74 {
75 $this->mExtra = $extra;
76 }
77
78 function &getTitle()
79 {
80 if ( $this->mTitle === false ) {
81 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
82 }
83 return $this->mTitle;
84 }
85
86 function getMovedToTitle()
87 {
88 if ( $this->mMovedToTitle === false ) {
89 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'],
90 $this->mAttribs['rc_moved_to_title'] );
91 }
92 return $this->mMovedToTitle;
93 }
94
95 # Writes the data in this object to the database
96 function save()
97 {
98 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix, $wgUseRCPatrol;
99 $fname = 'RecentChange::save';
100
101 $dbw =& wfGetDB( DB_MASTER );
102 if ( !is_array($this->mExtra) ) {
103 $this->mExtra = array();
104 }
105 $this->mExtra['lang'] = $wgLocalInterwiki;
106
107 if ( !$wgPutIPinRC ) {
108 $this->mAttribs['rc_ip'] = '';
109 }
110
111 # Fixup database timestamps
112 $this->mAttribs['rc_timestamp'] = $dbw->timestamp($this->mAttribs['rc_timestamp']);
113 $this->mAttribs['rc_cur_time'] = $dbw->timestamp($this->mAttribs['rc_cur_time']);
114 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
115
116 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
117 if ( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id']==0 ) {
118 unset ( $this->mAttribs['rc_cur_id'] );
119 }
120
121 # Insert new row
122 $dbw->insert( 'recentchanges', $this->mAttribs, $fname );
123
124 # Set the ID
125 $this->mAttribs['rc_id'] = $dbw->insertId();
126
127 # Update old rows, if necessary
128 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
129 $lastTime = $this->mExtra['lastTimestamp'];
130 #$now = $this->mAttribs['rc_timestamp'];
131 #$curId = $this->mAttribs['rc_cur_id'];
132
133 # Don't bother looking for entries that have probably
134 # been purged, it just locks up the indexes needlessly.
135 global $wgRCMaxAge;
136 $age = time() - wfTimestamp( TS_UNIX, $lastTime );
137 if( $age < $wgRCMaxAge ) {
138 # live hack, will commit once tested - kate
139 # Update rc_this_oldid for the entries which were current
140 #
141 #$oldid = $this->mAttribs['rc_last_oldid'];
142 #$ns = $this->mAttribs['rc_namespace'];
143 #$title = $this->mAttribs['rc_title'];
144 #
145 #$dbw->update( 'recentchanges',
146 # array( /* SET */
147 # 'rc_this_oldid' => $oldid
148 # ), array( /* WHERE */
149 # 'rc_namespace' => $ns,
150 # 'rc_title' => $title,
151 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
152 # ), $fname
153 #);
154 }
155
156 # Update rc_cur_time
157 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
158 # array( 'rc_cur_id' => $curId ), $fname );
159 }
160
161 # Notify external application via UDP
162 if ( $wgRC2UDPAddress ) {
163 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
164 if ( $conn ) {
165 $line = $wgRC2UDPPrefix . $this->getIRCLine();
166 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
167 socket_close( $conn );
168 }
169 }
170
171 // E-mail notifications
172 global $wgUseEnotif;
173 if( $wgUseEnotif ) {
174 # this would be better as an extension hook
175 include_once( "UserMailer.php" );
176 $enotif = new EmailNotification();
177 $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
178 $enotif->notifyOnPageChange( $title,
179 $this->mAttribs['rc_timestamp'],
180 $this->mAttribs['rc_comment'],
181 $this->mAttribs['rc_minor'],
182 $this->mAttribs['rc_last_oldid'] );
183 }
184
185 }
186
187 # Marks a certain row as patrolled
188 function markPatrolled( $rcid )
189 {
190 $fname = 'RecentChange::markPatrolled';
191
192 $dbw =& wfGetDB( DB_MASTER );
193
194 $dbw->update( 'recentchanges',
195 array( /* SET */
196 'rc_patrolled' => 1
197 ), array( /* WHERE */
198 'rc_id' => $rcid
199 ), $fname
200 );
201 }
202
203 # Makes an entry in the database corresponding to an edit
204 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
205 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
206 $newId = 0)
207 {
208 if ( $bot == 'default' ) {
209 $bot = $user->isAllowed( 'bot' );
210 }
211
212 if ( !$ip ) {
213 $ip = wfGetIP();
214 if ( !$ip ) {
215 $ip = '';
216 }
217 }
218
219 $rc = new RecentChange;
220 $rc->mAttribs = array(
221 'rc_timestamp' => $timestamp,
222 'rc_cur_time' => $timestamp,
223 'rc_namespace' => $title->getNamespace(),
224 'rc_title' => $title->getDBkey(),
225 'rc_type' => RC_EDIT,
226 'rc_minor' => $minor ? 1 : 0,
227 'rc_cur_id' => $title->getArticleID(),
228 'rc_user' => $user->getID(),
229 'rc_user_text' => $user->getName(),
230 'rc_comment' => $comment,
231 'rc_this_oldid' => $newId,
232 'rc_last_oldid' => $oldId,
233 'rc_bot' => $bot ? 1 : 0,
234 'rc_moved_to_ns' => 0,
235 'rc_moved_to_title' => '',
236 'rc_ip' => $ip,
237 'rc_patrolled' => 0,
238 'rc_new' => 0 # obsolete
239 );
240
241 $rc->mExtra = array(
242 'prefixedDBkey' => $title->getPrefixedDBkey(),
243 'lastTimestamp' => $lastTimestamp,
244 'oldSize' => $oldSize,
245 'newSize' => $newSize,
246 );
247 $rc->save();
248 return( $rc->mAttribs['rc_id'] );
249 }
250
251 /**
252 * Makes an entry in the database corresponding to page creation
253 * Note: the title object must be loaded with the new id using resetArticleID()
254 * @todo Document parameters and return
255 * @public
256 * @static
257 */
258 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default",
259 $ip='', $size = 0, $newId = 0 )
260 {
261 if ( !$ip ) {
262 $ip = wfGetIP();
263 if ( !$ip ) {
264 $ip = '';
265 }
266 }
267 if ( $bot == 'default' ) {
268 $bot = $user->isAllowed( 'bot' );
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_NEW,
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' => 0,
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' => 1 # obsolete
291 );
292
293 $rc->mExtra = array(
294 'prefixedDBkey' => $title->getPrefixedDBkey(),
295 'lastTimestamp' => 0,
296 'oldSize' => 0,
297 'newSize' => $size
298 );
299 $rc->save();
300 return( $rc->mAttribs['rc_id'] );
301 }
302
303 # Makes an entry in the database corresponding to a rename
304 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
305 {
306 if ( !$ip ) {
307 $ip = wfGetIP();
308 if ( !$ip ) {
309 $ip = '';
310 }
311 }
312
313 $rc = new RecentChange;
314 $rc->mAttribs = array(
315 'rc_timestamp' => $timestamp,
316 'rc_cur_time' => $timestamp,
317 'rc_namespace' => $oldTitle->getNamespace(),
318 'rc_title' => $oldTitle->getDBkey(),
319 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
320 'rc_minor' => 0,
321 'rc_cur_id' => $oldTitle->getArticleID(),
322 'rc_user' => $user->getID(),
323 'rc_user_text' => $user->getName(),
324 'rc_comment' => $comment,
325 'rc_this_oldid' => 0,
326 'rc_last_oldid' => 0,
327 'rc_bot' => $user->isAllowed( 'bot' ) ? 1 : 0,
328 'rc_moved_to_ns' => $newTitle->getNamespace(),
329 'rc_moved_to_title' => $newTitle->getDBkey(),
330 'rc_ip' => $ip,
331 'rc_new' => 0, # obsolete
332 'rc_patrolled' => 1
333 );
334
335 $rc->mExtra = array(
336 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
337 'lastTimestamp' => 0,
338 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
339 );
340 $rc->save();
341 }
342
343 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
344 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
345 }
346
347 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
348 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
349 }
350
351 # A log entry is different to an edit in that previous revisions are
352 # not kept
353 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
354 $type, $action, $target, $logComment, $params )
355 {
356 if ( !$ip ) {
357 $ip = wfGetIP();
358 if ( !$ip ) {
359 $ip = '';
360 }
361 }
362
363 $rc = new RecentChange;
364 $rc->mAttribs = array(
365 'rc_timestamp' => $timestamp,
366 'rc_cur_time' => $timestamp,
367 'rc_namespace' => $title->getNamespace(),
368 'rc_title' => $title->getDBkey(),
369 'rc_type' => RC_LOG,
370 'rc_minor' => 0,
371 'rc_cur_id' => $title->getArticleID(),
372 'rc_user' => $user->getID(),
373 'rc_user_text' => $user->getName(),
374 'rc_comment' => $comment,
375 'rc_this_oldid' => 0,
376 'rc_last_oldid' => 0,
377 'rc_bot' => $user->isAllowed( 'bot' ) ? 1 : 0,
378 'rc_moved_to_ns' => 0,
379 'rc_moved_to_title' => '',
380 'rc_ip' => $ip,
381 'rc_patrolled' => 1,
382 'rc_new' => 0 # obsolete
383 );
384 $rc->mExtra = array(
385 'prefixedDBkey' => $title->getPrefixedDBkey(),
386 'lastTimestamp' => 0,
387 'logType' => $type,
388 'logAction' => $action,
389 'logComment' => $logComment,
390 'logTarget' => $target,
391 'logParams' => $params
392 );
393 $rc->save();
394 }
395
396 # Initialises the members of this object from a mysql row object
397 function loadFromRow( $row )
398 {
399 $this->mAttribs = get_object_vars( $row );
400 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
401 $this->mExtra = array();
402 }
403
404 # Makes a pseudo-RC entry from a cur row, for watchlists and things
405 function loadFromCurRow( $row )
406 {
407 $this->mAttribs = array(
408 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
409 'rc_cur_time' => $row->rev_timestamp,
410 'rc_user' => $row->rev_user,
411 'rc_user_text' => $row->rev_user_text,
412 'rc_namespace' => $row->page_namespace,
413 'rc_title' => $row->page_title,
414 'rc_comment' => $row->rev_comment,
415 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
416 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
417 'rc_cur_id' => $row->page_id,
418 'rc_this_oldid' => $row->rev_id,
419 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
420 'rc_bot' => 0,
421 'rc_moved_to_ns' => 0,
422 'rc_moved_to_title' => '',
423 'rc_ip' => '',
424 'rc_id' => $row->rc_id,
425 'rc_patrolled' => $row->rc_patrolled,
426 'rc_new' => $row->page_is_new # obsolete
427 );
428
429 $this->mExtra = array();
430 }
431
432
433 /**
434 * Gets the end part of the diff URL associated with this object
435 * Blank if no diff link should be displayed
436 */
437 function diffLinkTrail( $forceCur )
438 {
439 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
440 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
441 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
442 if ( $forceCur ) {
443 $trail .= '&diff=0' ;
444 } else {
445 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
446 }
447 } else {
448 $trail = '';
449 }
450 return $trail;
451 }
452
453 function cleanupForIRC( $text ) {
454 return str_replace(array("\n", "\r"), array("", ""), $text);
455 }
456
457 function getIRCLine() {
458 global $wgUseRCPatrol;
459
460 extract($this->mAttribs);
461 extract($this->mExtra);
462
463 $titleObj =& $this->getTitle();
464 if ( $rc_type == RC_LOG ) {
465 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
466 } else {
467 $title = $titleObj->getPrefixedText();
468 }
469 $title = $this->cleanupForIRC( $title );
470
471 $bad = array("\n", "\r");
472 $empty = array("", "");
473 $title = $titleObj->getPrefixedText();
474 $title = str_replace($bad, $empty, $title);
475
476 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
477 if ( $rc_type == RC_LOG ) {
478 $url = '';
479 } elseif ( $rc_new && $wgUseRCPatrol ) {
480 $url = $titleObj->getInternalURL("rcid=$rc_id");
481 } else if ( $rc_new ) {
482 $url = $titleObj->getInternalURL();
483 } else if ( $wgUseRCPatrol ) {
484 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
485 } else {
486 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
487 }
488
489 if ( isset( $oldSize ) && isset( $newSize ) ) {
490 $szdiff = $newSize - $oldSize;
491 if ($szdiff < -500) {
492 $szdiff = "\002$szdiff\002";
493 } elseif ($szdiff >= 0) {
494 $szdiff = '+' . $szdiff ;
495 }
496 $szdiff = '(' . $szdiff . ')' ;
497 } else {
498 $szdiff = '';
499 }
500
501 $user = $this->cleanupForIRC( $rc_user_text );
502
503 if ( $rc_type == RC_LOG ) {
504 $logTargetText = $logTarget->getPrefixedText();
505 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
506 $flag = $logAction;
507 } else {
508 $comment = $this->cleanupForIRC( $rc_comment );
509 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
510 }
511 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
512 # no colour (\003) switches back to the term default
513 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
514 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
515 return $fullString;
516 }
517
518 }
519 ?>