Code review!
[lhc/web/wiklou.git] / includes / RecentChange.php
1 <?php
2 /**
3 *
4 * @package MediaWiki
5 */
6
7 /**
8 * Various globals
9 */
10 define( 'RC_EDIT', 0);
11 define( 'RC_NEW', 1);
12 define( 'RC_MOVE', 2);
13 define( 'RC_LOG', 3);
14 define( 'RC_MOVE_OVER_REDIRECT', 4);
15
16
17 /**
18 * Utility class for creating new RC entries
19 * mAttribs:
20 * rc_id id of the row in the recentchanges table
21 * rc_timestamp time the entry was made
22 * rc_cur_time timestamp on the cur row
23 * rc_namespace namespace #
24 * rc_title non-prefixed db key
25 * rc_type is new entry, used to determine whether updating is necessary
26 * rc_minor is minor
27 * rc_cur_id page_id of associated page entry
28 * rc_user user id who made the entry
29 * rc_user_text user name who made the entry
30 * rc_comment edit summary
31 * rc_this_oldid rev_id associated with this entry (or zero)
32 * rc_last_oldid rev_id associated with the entry before this one (or zero)
33 * rc_bot is bot, hidden
34 * rc_ip IP address of the user in dotted quad notation
35 * rc_new obsolete, use rc_type==RC_NEW
36 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
37 *
38 * mExtra:
39 * prefixedDBkey prefixed db key, used by external app via msg queue
40 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
41 * lang the interwiki prefix, automatically set in save()
42 * oldSize text size before the change
43 * newSize text size after the change
44 *
45 * temporary: not stored in the database
46 * notificationtimestamp
47 * numberofWatchingusers
48 *
49 * @todo document functions and variables
50 * @package MediaWiki
51 */
52 class RecentChange
53 {
54 var $mAttribs = array(), $mExtra = array();
55 var $mTitle = false, $mMovedToTitle = false;
56
57 # Factory methods
58
59 /* static */ function newFromRow( $row )
60 {
61 $rc = new RecentChange;
62 $rc->loadFromRow( $row );
63 return $rc;
64 }
65
66 /* static */ function newFromCurRow( $row, $rc_this_oldid = 0 )
67 {
68 $rc = new RecentChange;
69 $rc->loadFromCurRow( $row, $rc_this_oldid );
70 $rc->notificationtimestamp = false;
71 $rc->numberofWatchingusers = false;
72 return $rc;
73 }
74
75 # Accessors
76
77 function setAttribs( $attribs )
78 {
79 $this->mAttribs = $attribs;
80 }
81
82 function setExtra( $extra )
83 {
84 $this->mExtra = $extra;
85 }
86
87 function &getTitle()
88 {
89 if ( $this->mTitle === false ) {
90 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
91 }
92 return $this->mTitle;
93 }
94
95 function getMovedToTitle()
96 {
97 if ( $this->mMovedToTitle === false ) {
98 $this->mMovedToTitle = Title::makeTitle( $this->mAttribs['rc_moved_to_ns'],
99 $this->mAttribs['rc_moved_to_title'] );
100 }
101 return $this->mMovedToTitle;
102 }
103
104 # Writes the data in this object to the database
105 function save()
106 {
107 global $wgLocalInterwiki, $wgPutIPinRC, $wgRC2UDPAddress, $wgRC2UDPPort, $wgRC2UDPPrefix, $wgUseRCPatrol;
108 $fname = 'RecentChange::save';
109
110 $dbw =& wfGetDB( DB_MASTER );
111 if ( !is_array($this->mExtra) ) {
112 $this->mExtra = array();
113 }
114 $this->mExtra['lang'] = $wgLocalInterwiki;
115
116 if ( !$wgPutIPinRC ) {
117 $this->mAttribs['rc_ip'] = '';
118 }
119
120 # Fixup database timestamps
121 $this->mAttribs['rc_timestamp'] = $dbw->timestamp($this->mAttribs['rc_timestamp']);
122 $this->mAttribs['rc_cur_time'] = $dbw->timestamp($this->mAttribs['rc_cur_time']);
123 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'rc_rc_id_seq' );
124
125 # Insert new row
126 $dbw->insert( 'recentchanges', $this->mAttribs, $fname );
127
128 if ( $wgUseRCPatrol ) {
129 # Retrieve the id assigned by the db, but only if we'll use it later
130 $this->mAttribs['rc_id'] = $dbw->insertId();
131 }
132
133 # Update old rows, if necessary
134 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
135 $lastTime = $this->mExtra['lastTimestamp'];
136 #$now = $this->mAttribs['rc_timestamp'];
137 #$curId = $this->mAttribs['rc_cur_id'];
138
139 # Don't bother looking for entries that have probably
140 # been purged, it just locks up the indexes needlessly.
141 global $wgRCMaxAge;
142 $age = time() - wfTimestamp( TS_UNIX, $lastTime );
143 if( $age < $wgRCMaxAge ) {
144 # live hack, will commit once tested - kate
145 # Update rc_this_oldid for the entries which were current
146 #
147 #$oldid = $this->mAttribs['rc_last_oldid'];
148 #$ns = $this->mAttribs['rc_namespace'];
149 #$title = $this->mAttribs['rc_title'];
150 #
151 #$dbw->update( 'recentchanges',
152 # array( /* SET */
153 # 'rc_this_oldid' => $oldid
154 # ), array( /* WHERE */
155 # 'rc_namespace' => $ns,
156 # 'rc_title' => $title,
157 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
158 # ), $fname
159 #);
160 }
161
162 # Update rc_cur_time
163 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
164 # array( 'rc_cur_id' => $curId ), $fname );
165 }
166
167 # Notify external application via UDP
168 if ( $wgRC2UDPAddress ) {
169 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
170 if ( $conn ) {
171 $line = $wgRC2UDPPrefix . $this->getIRCLine();
172 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
173 socket_close( $conn );
174 }
175 }
176
177 // E-mail notifications
178 global $wgUseEnotif;
179 if( $wgUseEnotif ) {
180 # this would be better as an extension hook
181 include_once( "UserMailer.php" );
182 $enotif = new EmailNotification();
183 $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
184 $enotif->notifyOnPageChange( $title,
185 $this->mAttribs['rc_timestamp'],
186 $this->mAttribs['rc_comment'],
187 $this->mAttribs['rc_minor'],
188 $this->mAttribs['rc_last_oldid'] );
189 }
190
191 }
192
193 # Marks a certain row as patrolled
194 function markPatrolled( $rcid )
195 {
196 $fname = 'RecentChange::markPatrolled';
197
198 $dbw =& wfGetDB( DB_MASTER );
199
200 $dbw->update( 'recentchanges',
201 array( /* SET */
202 'rc_patrolled' => 1
203 ), array( /* WHERE */
204 'rc_id' => $rcid
205 ), $fname
206 );
207 }
208
209 # Makes an entry in the database corresponding to an edit
210 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
211 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
212 $newId = 0)
213 {
214 if ( $bot == 'default ' ) {
215 $bot = $user->isBot();
216 }
217
218 if ( !$ip ) {
219 $ip = wfGetIP();
220 if ( !$ip ) {
221 $ip = '';
222 }
223 }
224
225 $rc = new RecentChange;
226 $rc->mAttribs = array(
227 'rc_timestamp' => $timestamp,
228 'rc_cur_time' => $timestamp,
229 'rc_namespace' => $title->getNamespace(),
230 'rc_title' => $title->getDBkey(),
231 'rc_type' => RC_EDIT,
232 'rc_minor' => $minor ? 1 : 0,
233 'rc_cur_id' => $title->getArticleID(),
234 'rc_user' => $user->getID(),
235 'rc_user_text' => $user->getName(),
236 'rc_comment' => $comment,
237 'rc_this_oldid' => $newId,
238 'rc_last_oldid' => $oldId,
239 'rc_bot' => $bot ? 1 : 0,
240 'rc_moved_to_ns' => 0,
241 'rc_moved_to_title' => '',
242 'rc_ip' => $ip,
243 'rc_patrolled' => 0,
244 'rc_new' => 0 # obsolete
245 );
246
247 $rc->mExtra = array(
248 'prefixedDBkey' => $title->getPrefixedDBkey(),
249 'lastTimestamp' => $lastTimestamp,
250 'oldSize' => $oldSize,
251 'newSize' => $newSize,
252 );
253 $rc->save();
254 }
255
256 # Makes an entry in the database corresponding to page creation
257 # Note: the title object must be loaded with the new id using resetArticleID()
258 /*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->isBot();
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 }
301
302 # Makes an entry in the database corresponding to a rename
303 /*static*/ function notifyMove( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='', $overRedir = false )
304 {
305 if ( !$ip ) {
306 $ip = wfGetIP();
307 if ( !$ip ) {
308 $ip = '';
309 }
310 }
311
312 $rc = new RecentChange;
313 $rc->mAttribs = array(
314 'rc_timestamp' => $timestamp,
315 'rc_cur_time' => $timestamp,
316 'rc_namespace' => $oldTitle->getNamespace(),
317 'rc_title' => $oldTitle->getDBkey(),
318 'rc_type' => $overRedir ? RC_MOVE_OVER_REDIRECT : RC_MOVE,
319 'rc_minor' => 0,
320 'rc_cur_id' => $oldTitle->getArticleID(),
321 'rc_user' => $user->getID(),
322 'rc_user_text' => $user->getName(),
323 'rc_comment' => $comment,
324 'rc_this_oldid' => 0,
325 'rc_last_oldid' => 0,
326 'rc_bot' => $user->isBot() ? 1 : 0,
327 'rc_moved_to_ns' => $newTitle->getNamespace(),
328 'rc_moved_to_title' => $newTitle->getDBkey(),
329 'rc_ip' => $ip,
330 'rc_new' => 0, # obsolete
331 'rc_patrolled' => 1
332 );
333
334 $rc->mExtra = array(
335 'prefixedDBkey' => $oldTitle->getPrefixedDBkey(),
336 'lastTimestamp' => 0,
337 'prefixedMoveTo' => $newTitle->getPrefixedDBkey()
338 );
339 $rc->save();
340 }
341
342 /* static */ function notifyMoveToNew( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
343 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, false );
344 }
345
346 /* static */ function notifyMoveOverRedirect( $timestamp, &$oldTitle, &$newTitle, &$user, $comment, $ip='' ) {
347 RecentChange::notifyMove( $timestamp, $oldTitle, $newTitle, $user, $comment, $ip, true );
348 }
349
350 # A log entry is different to an edit in that previous revisions are
351 # not kept
352 /*static*/ function notifyLog( $timestamp, &$title, &$user, $comment, $ip='' )
353 {
354 if ( !$ip ) {
355 $ip = wfGetIP();
356 if ( !$ip ) {
357 $ip = '';
358 }
359 }
360
361 $rc = new RecentChange;
362 $rc->mAttribs = array(
363 'rc_timestamp' => $timestamp,
364 'rc_cur_time' => $timestamp,
365 'rc_namespace' => $title->getNamespace(),
366 'rc_title' => $title->getDBkey(),
367 'rc_type' => RC_LOG,
368 'rc_minor' => 0,
369 'rc_cur_id' => $title->getArticleID(),
370 'rc_user' => $user->getID(),
371 'rc_user_text' => $user->getName(),
372 'rc_comment' => $comment,
373 'rc_this_oldid' => 0,
374 'rc_last_oldid' => 0,
375 'rc_bot' => $user->isBot() ? 1 : 0,
376 'rc_moved_to_ns' => 0,
377 'rc_moved_to_title' => '',
378 'rc_ip' => $ip,
379 'rc_patrolled' => 1,
380 'rc_new' => 0 # obsolete
381 );
382 $rc->mExtra = array(
383 'prefixedDBkey' => $title->getPrefixedDBkey(),
384 'lastTimestamp' => 0
385 );
386 $rc->save();
387 }
388
389 # Initialises the members of this object from a mysql row object
390 function loadFromRow( $row )
391 {
392 $this->mAttribs = get_object_vars( $row );
393 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
394 $this->mExtra = array();
395 }
396
397 # Makes a pseudo-RC entry from a cur row, for watchlists and things
398 function loadFromCurRow( $row )
399 {
400 $this->mAttribs = array(
401 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
402 'rc_cur_time' => $row->rev_timestamp,
403 'rc_user' => $row->rev_user,
404 'rc_user_text' => $row->rev_user_text,
405 'rc_namespace' => $row->page_namespace,
406 'rc_title' => $row->page_title,
407 'rc_comment' => $row->rev_comment,
408 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
409 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
410 'rc_cur_id' => $row->page_id,
411 'rc_this_oldid' => $row->rev_id,
412 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
413 'rc_bot' => 0,
414 'rc_moved_to_ns' => 0,
415 'rc_moved_to_title' => '',
416 'rc_ip' => '',
417 'rc_patrolled' => '1', # we can't support patrolling on the Watchlist
418 # currently because it uses cur, not recentchanges
419 'rc_new' => $row->page_is_new # obsolete
420 );
421
422 $this->mExtra = array();
423 }
424
425
426 /**
427 * Gets the end part of the diff URL associated with this object
428 * Blank if no diff link should be displayed
429 */
430 function diffLinkTrail( $forceCur )
431 {
432 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
433 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
434 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
435 if ( $forceCur ) {
436 $trail .= '&diff=0' ;
437 } else {
438 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
439 }
440 } else {
441 $trail = '';
442 }
443 return $trail;
444 }
445
446 function getIRCLine() {
447 global $wgUseRCPatrol;
448
449 extract($this->mAttribs);
450 extract($this->mExtra);
451
452 $titleObj =& $this->getTitle();
453
454 $bad = array("\n", "\r");
455 $empty = array("", "");
456 $title = $titleObj->getPrefixedText();
457 $title = str_replace($bad, $empty, $title);
458
459 if ( $rc_new && $wgUseRCPatrol ) {
460 $url = $titleObj->getFullURL("rcid=$rc_id");
461 } else if ( $rc_new ) {
462 $url = $titleObj->getFullURL();
463 } else if ( $wgUseRCPatrol ) {
464 $url = $titleObj->getFullURL("diff=0&oldid=$rc_last_oldid&rcid=$rc_id");
465 } else {
466 $url = $titleObj->getFullURL("diff=0&oldid=$rc_last_oldid");
467 }
468
469 if ( isset( $oldSize ) && isset( $newSize ) ) {
470 $szdiff = $newSize - $oldSize;
471 if ($szdiff < -500) {
472 $szdiff = "\002$szdiff\002";
473 } elseif ($szdiff >= 0) {
474 $szdiff = '+' . $szdiff ;
475 }
476 $szdiff = '(' . $szdiff . ')' ;
477 } else {
478 $szdiff = '';
479 }
480
481 $comment = str_replace($bad, $empty, $rc_comment);
482 $user = str_replace($bad, $empty, $rc_user_text);
483 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
484 # see http://www.irssi.org/?page=docs&doc=formats for some colour codes. prefix is \003,
485 # no colour (\003) switches back to the term default
486 $comment = preg_replace("/\/\* (.*) \*\/(.*)/", "\00315\$1\003 - \00310\$2\003", $comment);
487 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
488 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
489
490 return $fullString;
491 }
492 }
493 ?>