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