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