begin cleanup on magnus' url upload thingy
[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 }
191
192 # Marks a certain row as patrolled
193 function markPatrolled( $rcid )
194 {
195 $fname = 'RecentChange::markPatrolled';
196
197 $dbw =& wfGetDB( DB_MASTER );
198
199 $dbw->update( 'recentchanges',
200 array( /* SET */
201 'rc_patrolled' => 1
202 ), array( /* WHERE */
203 'rc_id' => $rcid
204 ), $fname
205 );
206 }
207
208 # Makes an entry in the database corresponding to an edit
209 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
210 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
211 $newId = 0)
212 {
213 if ( $bot == 'default' ) {
214 $bot = $user->isAllowed( 'bot' );
215 }
216
217 if ( !$ip ) {
218 $ip = wfGetIP();
219 if ( !$ip ) {
220 $ip = '';
221 }
222 }
223
224 $rc = new RecentChange;
225 $rc->mAttribs = array(
226 'rc_timestamp' => $timestamp,
227 'rc_cur_time' => $timestamp,
228 'rc_namespace' => $title->getNamespace(),
229 'rc_title' => $title->getDBkey(),
230 'rc_type' => RC_EDIT,
231 'rc_minor' => $minor ? 1 : 0,
232 'rc_cur_id' => $title->getArticleID(),
233 'rc_user' => $user->getID(),
234 'rc_user_text' => $user->getName(),
235 'rc_comment' => $comment,
236 'rc_this_oldid' => $newId,
237 'rc_last_oldid' => $oldId,
238 'rc_bot' => $bot ? 1 : 0,
239 'rc_moved_to_ns' => 0,
240 'rc_moved_to_title' => '',
241 'rc_ip' => $ip,
242 'rc_patrolled' => 0,
243 'rc_new' => 0 # obsolete
244 );
245
246 $rc->mExtra = array(
247 'prefixedDBkey' => $title->getPrefixedDBkey(),
248 'lastTimestamp' => $lastTimestamp,
249 'oldSize' => $oldSize,
250 'newSize' => $newSize,
251 );
252 $rc->save();
253 return( $rc->mAttribs['rc_id'] );
254 }
255
256 /**
257 * Makes an entry in the database corresponding to page creation
258 * Note: the title object must be loaded with the new id using resetArticleID()
259 * @todo Document parameters and return
260 * @public
261 * @static
262 */
263 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default",
264 $ip='', $size = 0, $newId = 0 )
265 {
266 if ( !$ip ) {
267 $ip = wfGetIP();
268 if ( !$ip ) {
269 $ip = '';
270 }
271 }
272 if ( $bot == 'default' ) {
273 $bot = $user->isAllowed( 'bot' );
274 }
275
276 $rc = new RecentChange;
277 $rc->mAttribs = array(
278 'rc_timestamp' => $timestamp,
279 'rc_cur_time' => $timestamp,
280 'rc_namespace' => $title->getNamespace(),
281 'rc_title' => $title->getDBkey(),
282 'rc_type' => RC_NEW,
283 'rc_minor' => $minor ? 1 : 0,
284 'rc_cur_id' => $title->getArticleID(),
285 'rc_user' => $user->getID(),
286 'rc_user_text' => $user->getName(),
287 'rc_comment' => $comment,
288 'rc_this_oldid' => $newId,
289 'rc_last_oldid' => 0,
290 'rc_bot' => $bot ? 1 : 0,
291 'rc_moved_to_ns' => 0,
292 'rc_moved_to_title' => '',
293 'rc_ip' => $ip,
294 'rc_patrolled' => 0,
295 'rc_new' => 1 # obsolete
296 );
297
298 $rc->mExtra = array(
299 'prefixedDBkey' => $title->getPrefixedDBkey(),
300 'lastTimestamp' => 0,
301 'oldSize' => 0,
302 'newSize' => $size
303 );
304 $rc->save();
305 return( $rc->mAttribs['rc_id'] );
306 }
307
308 # Makes an entry in the database corresponding to a rename
309 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
310 {
311 if ( !$ip ) {
312 $ip = wfGetIP();
313 if ( !$ip ) {
314 $ip = '';
315 }
316 }
317
318 $rc = new RecentChange;
319 $rc->mAttribs = array(
320 'rc_timestamp' => $timestamp,
321 'rc_cur_time' => $timestamp,
322 'rc_namespace' => $oldTitle->getNamespace(),
323 'rc_title' => $oldTitle->getDBkey(),
324 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
325 'rc_minor' => 0,
326 'rc_cur_id' => $oldTitle->getArticleID(),
327 'rc_user' => $user->getID(),
328 'rc_user_text' => $user->getName(),
329 'rc_comment' => $comment,
330 'rc_this_oldid' => 0,
331 'rc_last_oldid' => 0,
332 'rc_bot' => $user->isAllowed( 'bot' ) ? 1 : 0,
333 'rc_moved_to_ns' => $newTitle->getNamespace(),
334 'rc_moved_to_title' => $newTitle->getDBkey(),
335 'rc_ip' => $ip,
336 'rc_new' => 0, # obsolete
337 'rc_patrolled' => 1
338 );
339
340 $rc->mExtra = array(
341 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
342 'lastTimestamp' => 0,
343 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
344 );
345 $rc->save();
346 }
347
348 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
349 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
350 }
351
352 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
353 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
354 }
355
356 # A log entry is different to an edit in that previous revisions are
357 # not kept
358 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='',
359 $type, $action, $target, $logComment, $params )
360 {
361 if ( !$ip ) {
362 $ip = wfGetIP();
363 if ( !$ip ) {
364 $ip = '';
365 }
366 }
367
368 $rc = new RecentChange;
369 $rc->mAttribs = array(
370 'rc_timestamp' => $timestamp,
371 'rc_cur_time' => $timestamp,
372 'rc_namespace' => $title->getNamespace(),
373 'rc_title' => $title->getDBkey(),
374 'rc_type' => RC_LOG,
375 'rc_minor' => 0,
376 'rc_cur_id' => $title->getArticleID(),
377 'rc_user' => $user->getID(),
378 'rc_user_text' => $user->getName(),
379 'rc_comment' => $comment,
380 'rc_this_oldid' => 0,
381 'rc_last_oldid' => 0,
382 'rc_bot' => $user->isAllowed( 'bot' ) ? 1 : 0,
383 'rc_moved_to_ns' => 0,
384 'rc_moved_to_title' => '',
385 'rc_ip' => $ip,
386 'rc_patrolled' => 1,
387 'rc_new' => 0 # obsolete
388 );
389 $rc->mExtra = array(
390 'prefixedDBkey' => $title->getPrefixedDBkey(),
391 'lastTimestamp' => 0,
392 'logType' => $type,
393 'logAction' => $action,
394 'logComment' => $logComment,
395 'logTarget' => $target,
396 'logParams' => $params
397 );
398 $rc->save();
399 }
400
401 # Initialises the members of this object from a mysql row object
402 function loadFromRow( $row )
403 {
404 $this->mAttribs = get_object_vars( $row );
405 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
406 $this->mExtra = array();
407 }
408
409 # Makes a pseudo-RC entry from a cur row, for watchlists and things
410 function loadFromCurRow( $row )
411 {
412 $this->mAttribs = array(
413 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
414 'rc_cur_time' => $row->rev_timestamp,
415 'rc_user' => $row->rev_user,
416 'rc_user_text' => $row->rev_user_text,
417 'rc_namespace' => $row->page_namespace,
418 'rc_title' => $row->page_title,
419 'rc_comment' => $row->rev_comment,
420 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
421 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
422 'rc_cur_id' => $row->page_id,
423 'rc_this_oldid' => $row->rev_id,
424 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
425 'rc_bot' => 0,
426 'rc_moved_to_ns' => 0,
427 'rc_moved_to_title' => '',
428 'rc_ip' => '',
429 'rc_id' => $row->rc_id,
430 'rc_patrolled' => $row->rc_patrolled,
431 'rc_new' => $row->page_is_new # obsolete
432 );
433
434 $this->mExtra = array();
435 }
436
437
438 /**
439 * Gets the end part of the diff URL associated with this object
440 * Blank if no diff link should be displayed
441 */
442 function diffLinkTrail( $forceCur )
443 {
444 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
445 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
446 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
447 if ( $forceCur ) {
448 $trail .= '&diff=0' ;
449 } else {
450 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
451 }
452 } else {
453 $trail = '';
454 }
455 return $trail;
456 }
457
458 function cleanupForIRC( $text ) {
459 return str_replace(array("\n", "\r"), array("", ""), $text);
460 }
461
462 function getIRCLine() {
463 global $wgUseRCPatrol;
464
465 extract($this->mAttribs);
466 extract($this->mExtra);
467
468 $titleObj =& $this->getTitle();
469 if ( $rc_type == RC_LOG ) {
470 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
471 } else {
472 $title = $titleObj->getPrefixedText();
473 }
474 $title = $this->cleanupForIRC( $title );
475
476 $bad = array("\n", "\r");
477 $empty = array("", "");
478 $title = $titleObj->getPrefixedText();
479 $title = str_replace($bad, $empty, $title);
480
481 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
482 if ( $rc_type == RC_LOG ) {
483 $url = '';
484 } elseif ( $rc_new && $wgUseRCPatrol ) {
485 $url = $titleObj->getInternalURL("rcid=$rc_id");
486 } else if ( $rc_new ) {
487 $url = $titleObj->getInternalURL();
488 } else if ( $wgUseRCPatrol ) {
489 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
490 } else {
491 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
492 }
493
494 if ( isset( $oldSize ) && isset( $newSize ) ) {
495 $szdiff = $newSize - $oldSize;
496 if ($szdiff < -500) {
497 $szdiff = "\002$szdiff\002";
498 } elseif ($szdiff >= 0) {
499 $szdiff = '+' . $szdiff ;
500 }
501 $szdiff = '(' . $szdiff . ')' ;
502 } else {
503 $szdiff = '';
504 }
505
506 $user = $this->cleanupForIRC( $rc_user_text );
507
508 if ( $rc_type == RC_LOG ) {
509 $logTargetText = $logTarget->getPrefixedText();
510 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
511 $flag = $logAction;
512 } else {
513 $comment = $this->cleanupForIRC( $rc_comment );
514 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
515 }
516 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
517 # no colour (\003) switches back to the term default
518 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
519 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
520 return $fullString;
521 }
522
523 }
524 ?>