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