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