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