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