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