(bug 5424) "autopatrol" doesn't mark new articles as patrolled
[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 # Set the ID
120 $this->mAttribs['rc_id'] = $dbw->insertId();
121
122 # Update old rows, if necessary
123 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
124 $lastTime = $this->mExtra['lastTimestamp'];
125 #$now = $this->mAttribs['rc_timestamp'];
126 #$curId = $this->mAttribs['rc_cur_id'];
127
128 # Don't bother looking for entries that have probably
129 # been purged, it just locks up the indexes needlessly.
130 global $wgRCMaxAge;
131 $age = time() - wfTimestamp( TS_UNIX, $lastTime );
132 if( $age < $wgRCMaxAge ) {
133 # live hack, will commit once tested - kate
134 # Update rc_this_oldid for the entries which were current
135 #
136 #$oldid = $this->mAttribs['rc_last_oldid'];
137 #$ns = $this->mAttribs['rc_namespace'];
138 #$title = $this->mAttribs['rc_title'];
139 #
140 #$dbw->update( 'recentchanges',
141 # array( /* SET */
142 # 'rc_this_oldid' => $oldid
143 # ), array( /* WHERE */
144 # 'rc_namespace' => $ns,
145 # 'rc_title' => $title,
146 # 'rc_timestamp' => $dbw->timestamp( $lastTime )
147 # ), $fname
148 #);
149 }
150
151 # Update rc_cur_time
152 #$dbw->update( 'recentchanges', array( 'rc_cur_time' => $now ),
153 # array( 'rc_cur_id' => $curId ), $fname );
154 }
155
156 # Notify external application via UDP
157 if ( $wgRC2UDPAddress ) {
158 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
159 if ( $conn ) {
160 $line = $wgRC2UDPPrefix . $this->getIRCLine();
161 socket_sendto( $conn, $line, strlen($line), 0, $wgRC2UDPAddress, $wgRC2UDPPort );
162 socket_close( $conn );
163 }
164 }
165
166 // E-mail notifications
167 global $wgUseEnotif;
168 if( $wgUseEnotif ) {
169 # this would be better as an extension hook
170 include_once( "UserMailer.php" );
171 $enotif = new EmailNotification();
172 $title = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
173 $enotif->notifyOnPageChange( $title,
174 $this->mAttribs['rc_timestamp'],
175 $this->mAttribs['rc_comment'],
176 $this->mAttribs['rc_minor'],
177 $this->mAttribs['rc_last_oldid'] );
178 }
179
180 }
181
182 # Marks a certain row as patrolled
183 function markPatrolled( $rcid )
184 {
185 $fname = 'RecentChange::markPatrolled';
186
187 $dbw =& wfGetDB( DB_MASTER );
188
189 $dbw->update( 'recentchanges',
190 array( /* SET */
191 'rc_patrolled' => 1
192 ), array( /* WHERE */
193 'rc_id' => $rcid
194 ), $fname
195 );
196 }
197
198 # Makes an entry in the database corresponding to an edit
199 /*static*/ function notifyEdit( $timestamp, &$title, $minor, &$user, $comment,
200 $oldId, $lastTimestamp, $bot = "default", $ip = '', $oldSize = 0, $newSize = 0,
201 $newId = 0)
202 {
203 if ( $bot == 'default ' ) {
204 $bot = $user->isBot();
205 }
206
207 if ( !$ip ) {
208 $ip = wfGetIP();
209 if ( !$ip ) {
210 $ip = '';
211 }
212 }
213
214 $rc = new RecentChange;
215 $rc->mAttribs = array(
216 'rc_timestamp' => $timestamp,
217 'rc_cur_time' => $timestamp,
218 'rc_namespace' => $title->getNamespace(),
219 'rc_title' => $title->getDBkey(),
220 'rc_type' => RC_EDIT,
221 'rc_minor' => $minor ? 1 : 0,
222 'rc_cur_id' => $title->getArticleID(),
223 'rc_user' => $user->getID(),
224 'rc_user_text' => $user->getName(),
225 'rc_comment' => $comment,
226 'rc_this_oldid' => $newId,
227 'rc_last_oldid' => $oldId,
228 'rc_bot' => $bot ? 1 : 0,
229 'rc_moved_to_ns' => 0,
230 'rc_moved_to_title' => '',
231 'rc_ip' => $ip,
232 'rc_patrolled' => 0,
233 'rc_new' => 0 # obsolete
234 );
235
236 $rc->mExtra = array(
237 'prefixedDBkey' => $title->getPrefixedDBkey(),
238 'lastTimestamp' => $lastTimestamp,
239 'oldSize' => $oldSize,
240 'newSize' => $newSize,
241 );
242 $rc->save();
243 return( $rc->mAttribs['rc_id'] );
244 }
245
246 # Makes an entry in the database corresponding to page creation
247 # Note: the title object must be loaded with the new id using resetArticleID()
248 /*static*/ function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot = "default",
249 $ip='', $size = 0, $newId = 0 )
250 {
251 if ( !$ip ) {
252 $ip = wfGetIP();
253 if ( !$ip ) {
254 $ip = '';
255 }
256 }
257 if ( $bot == 'default' ) {
258 $bot = $user->isBot();
259 }
260
261 $rc = new RecentChange;
262 $rc->mAttribs = array(
263 'rc_timestamp' => $timestamp,
264 'rc_cur_time' => $timestamp,
265 'rc_namespace' => $title->getNamespace(),
266 'rc_title' => $title->getDBkey(),
267 'rc_type' => RC_NEW,
268 'rc_minor' => $minor ? 1 : 0,
269 'rc_cur_id' => $title->getArticleID(),
270 'rc_user' => $user->getID(),
271 'rc_user_text' => $user->getName(),
272 'rc_comment' => $comment,
273 'rc_this_oldid' => $newId,
274 'rc_last_oldid' => 0,
275 'rc_bot' => $bot ? 1 : 0,
276 'rc_moved_to_ns' => 0,
277 'rc_moved_to_title' => '',
278 'rc_ip' => $ip,
279 'rc_patrolled' => 0,
280 'rc_new' => 1 # obsolete
281 );
282
283 $rc->mExtra = array(
284 'prefixedDBkey' => $title->getPrefixedDBkey(),
285 'lastTimestamp' => 0,
286 'oldSize' => 0,
287 'newSize' => $size
288 );
289 $rc->save();
290 return( $rc->mAttribs['rc_id'] );
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 $type, $action, $target, $logComment, $params )
345 {
346 if ( !$ip ) {
347 $ip = wfGetIP();
348 if ( !$ip ) {
349 $ip = '';
350 }
351 }
352
353 $rc = new RecentChange;
354 $rc->mAttribs = array(
355 'rc_timestamp' => $timestamp,
356 'rc_cur_time' => $timestamp,
357 'rc_namespace' => $title->getNamespace(),
358 'rc_title' => $title->getDBkey(),
359 'rc_type' => RC_LOG,
360 'rc_minor' => 0,
361 'rc_cur_id' => $title->getArticleID(),
362 'rc_user' => $user->getID(),
363 'rc_user_text' => $user->getName(),
364 'rc_comment' => $comment,
365 'rc_this_oldid' => 0,
366 'rc_last_oldid' => 0,
367 'rc_bot' => $user->isBot() ? 1 : 0,
368 'rc_moved_to_ns' => 0,
369 'rc_moved_to_title' => '',
370 'rc_ip' => $ip,
371 'rc_patrolled' => 1,
372 'rc_new' => 0 # obsolete
373 );
374 $rc->mExtra = array(
375 'prefixedDBkey' => $title->getPrefixedDBkey(),
376 'lastTimestamp' => 0,
377 'logType' => $type,
378 'logAction' => $action,
379 'logComment' => $logComment,
380 'logTarget' => $target,
381 'logParams' => $params
382 );
383 $rc->save();
384 }
385
386 # Initialises the members of this object from a mysql row object
387 function loadFromRow( $row )
388 {
389 $this->mAttribs = get_object_vars( $row );
390 $this->mAttribs["rc_timestamp"] = wfTimestamp(TS_MW, $this->mAttribs["rc_timestamp"]);
391 $this->mExtra = array();
392 }
393
394 # Makes a pseudo-RC entry from a cur row, for watchlists and things
395 function loadFromCurRow( $row )
396 {
397 $this->mAttribs = array(
398 'rc_timestamp' => wfTimestamp(TS_MW, $row->rev_timestamp),
399 'rc_cur_time' => $row->rev_timestamp,
400 'rc_user' => $row->rev_user,
401 'rc_user_text' => $row->rev_user_text,
402 'rc_namespace' => $row->page_namespace,
403 'rc_title' => $row->page_title,
404 'rc_comment' => $row->rev_comment,
405 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
406 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
407 'rc_cur_id' => $row->page_id,
408 'rc_this_oldid' => $row->rev_id,
409 'rc_last_oldid' => isset($row->rc_last_oldid) ? $row->rc_last_oldid : 0,
410 'rc_bot' => 0,
411 'rc_moved_to_ns' => 0,
412 'rc_moved_to_title' => '',
413 'rc_ip' => '',
414 'rc_patrolled' => '1', # we can't support patrolling on the Watchlist
415 # currently because it uses cur, not recentchanges
416 'rc_new' => $row->page_is_new # obsolete
417 );
418
419 $this->mExtra = array();
420 }
421
422
423 /**
424 * Gets the end part of the diff URL associated with this object
425 * Blank if no diff link should be displayed
426 */
427 function diffLinkTrail( $forceCur )
428 {
429 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
430 $trail = "curid=" . (int)($this->mAttribs['rc_cur_id']) .
431 "&oldid=" . (int)($this->mAttribs['rc_last_oldid']);
432 if ( $forceCur ) {
433 $trail .= '&diff=0' ;
434 } else {
435 $trail .= '&diff=' . (int)($this->mAttribs['rc_this_oldid']);
436 }
437 } else {
438 $trail = '';
439 }
440 return $trail;
441 }
442
443 function cleanupForIRC( $text ) {
444 return str_replace(array("\n", "\r"), array("", ""), $text);
445 }
446
447 function getIRCLine() {
448 global $wgUseRCPatrol;
449
450 extract($this->mAttribs);
451 extract($this->mExtra);
452
453 $titleObj =& $this->getTitle();
454 if ( $rc_type == RC_LOG ) {
455 $title = Namespace::getCanonicalName( $titleObj->getNamespace() ) . $titleObj->getText();
456 } else {
457 $title = $titleObj->getPrefixedText();
458 }
459 $title = $this->cleanupForIRC( $title );
460
461 $bad = array("\n", "\r");
462 $empty = array("", "");
463 $title = $titleObj->getPrefixedText();
464 $title = str_replace($bad, $empty, $title);
465
466 // FIXME: *HACK* these should be getFullURL(), hacked for SSL madness --brion 2005-12-26
467 if ( $rc_type == RC_LOG ) {
468 $url = '';
469 } elseif ( $rc_new && $wgUseRCPatrol ) {
470 $url = $titleObj->getInternalURL("rcid=$rc_id");
471 } else if ( $rc_new ) {
472 $url = $titleObj->getInternalURL();
473 } else if ( $wgUseRCPatrol ) {
474 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid&rcid=$rc_id");
475 } else {
476 $url = $titleObj->getInternalURL("diff=$rc_this_oldid&oldid=$rc_last_oldid");
477 }
478
479 if ( isset( $oldSize ) && isset( $newSize ) ) {
480 $szdiff = $newSize - $oldSize;
481 if ($szdiff < -500) {
482 $szdiff = "\002$szdiff\002";
483 } elseif ($szdiff >= 0) {
484 $szdiff = '+' . $szdiff ;
485 }
486 $szdiff = '(' . $szdiff . ')' ;
487 } else {
488 $szdiff = '';
489 }
490
491 $user = $this->cleanupForIRC( $rc_user_text );
492
493 if ( $rc_type == RC_LOG ) {
494 $logTargetText = $logTarget->getPrefixedText();
495 $comment = $this->cleanupForIRC( str_replace( $logTargetText, "\00302$logTargetText\00310", $rc_comment ) );
496 $flag = $logAction;
497 } else {
498 $comment = $this->cleanupForIRC( $rc_comment );
499 $flag = ($rc_minor ? "M" : "") . ($rc_new ? "N" : "");
500 }
501 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
502 # no colour (\003) switches back to the term default
503 $fullString = "\00314[[\00307$title\00314]]\0034 $flag\00310 " .
504 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
505 return $fullString;
506 }
507
508 }
509 ?>