Merge "database: Simplify selectSQLText() and fix USE INDEX bug"
[lhc/web/wiklou.git] / includes / RecentChange.php
1 <?php
2 /**
3 * Utility class for creating and accessing recent change entries.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Utility class for creating new RC entries
25 *
26 * mAttribs:
27 * rc_id id of the row in the recentchanges table
28 * rc_timestamp time the entry was made
29 * rc_cur_time timestamp on the cur row
30 * rc_namespace namespace #
31 * rc_title non-prefixed db key
32 * rc_type is new entry, used to determine whether updating is necessary
33 * rc_minor is minor
34 * rc_cur_id page_id of associated page entry
35 * rc_user user id who made the entry
36 * rc_user_text user name who made the entry
37 * rc_comment edit summary
38 * rc_this_oldid rev_id associated with this entry (or zero)
39 * rc_last_oldid rev_id associated with the entry before this one (or zero)
40 * rc_bot is bot, hidden
41 * rc_ip IP address of the user in dotted quad notation
42 * rc_new obsolete, use rc_type==RC_NEW
43 * rc_patrolled boolean whether or not someone has marked this edit as patrolled
44 * rc_old_len integer byte length of the text before the edit
45 * rc_new_len the same after the edit
46 * rc_deleted partial deletion
47 * rc_logid the log_id value for this log entry (or zero)
48 * rc_log_type the log type (or null)
49 * rc_log_action the log action (or null)
50 * rc_params log params
51 *
52 * mExtra:
53 * prefixedDBkey prefixed db key, used by external app via msg queue
54 * lastTimestamp timestamp of previous entry, used in WHERE clause during update
55 * lang the interwiki prefix, automatically set in save()
56 * oldSize text size before the change
57 * newSize text size after the change
58 * pageStatus status of the page: created, deleted, moved, restored, changed
59 *
60 * temporary: not stored in the database
61 * notificationtimestamp
62 * numberofWatchingusers
63 *
64 * @todo document functions and variables
65 */
66 class RecentChange {
67 var $mAttribs = array(), $mExtra = array();
68
69 /**
70 * @var Title
71 */
72 var $mTitle = false;
73
74 /**
75 * @var User
76 */
77 private $mPerformer = false;
78
79 /**
80 * @var Title
81 */
82 var $mMovedToTitle = false;
83 var $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentchangeslinked
84 var $notificationtimestamp;
85
86 # Factory methods
87
88 /**
89 * @param $row
90 * @return RecentChange
91 */
92 public static function newFromRow( $row ) {
93 $rc = new RecentChange;
94 $rc->loadFromRow( $row );
95 return $rc;
96 }
97
98 /**
99 * @deprecated in 1.22
100 * @param $row
101 * @return RecentChange
102 */
103 public static function newFromCurRow( $row ) {
104 wfDeprecated( __METHOD__, '1.22' );
105 $rc = new RecentChange;
106 $rc->loadFromCurRow( $row );
107 $rc->notificationtimestamp = false;
108 $rc->numberofWatchingusers = false;
109 return $rc;
110 }
111
112 /**
113 * Obtain the recent change with a given rc_id value
114 *
115 * @param int $rcid rc_id value to retrieve
116 * @return RecentChange
117 */
118 public static function newFromId( $rcid ) {
119 return self::newFromConds( array( 'rc_id' => $rcid ), __METHOD__ );
120 }
121
122 /**
123 * Find the first recent change matching some specific conditions
124 *
125 * @param array $conds of conditions
126 * @param $fname Mixed: override the method name in profiling/logs
127 * @param $options Array Query options
128 * @return RecentChange
129 */
130 public static function newFromConds( $conds, $fname = __METHOD__, $options = array() ) {
131 $dbr = wfGetDB( DB_SLAVE );
132 $row = $dbr->selectRow( 'recentchanges', self::selectFields(), $conds, $fname, $options );
133 if ( $row !== false ) {
134 return self::newFromRow( $row );
135 } else {
136 return null;
137 }
138 }
139
140 /**
141 * Return the list of recentchanges fields that should be selected to create
142 * a new recentchanges object.
143 * @return array
144 */
145 public static function selectFields() {
146 return array(
147 'rc_id',
148 'rc_timestamp',
149 'rc_cur_time',
150 'rc_user',
151 'rc_user_text',
152 'rc_namespace',
153 'rc_title',
154 'rc_comment',
155 'rc_minor',
156 'rc_bot',
157 'rc_new',
158 'rc_cur_id',
159 'rc_this_oldid',
160 'rc_last_oldid',
161 'rc_type',
162 'rc_patrolled',
163 'rc_ip',
164 'rc_old_len',
165 'rc_new_len',
166 'rc_deleted',
167 'rc_logid',
168 'rc_log_type',
169 'rc_log_action',
170 'rc_params',
171 );
172 }
173
174 # Accessors
175
176 /**
177 * @param $attribs array
178 */
179 public function setAttribs( $attribs ) {
180 $this->mAttribs = $attribs;
181 }
182
183 /**
184 * @param $extra array
185 */
186 public function setExtra( $extra ) {
187 $this->mExtra = $extra;
188 }
189
190 /**
191 *
192 * @return Title
193 */
194 public function &getTitle() {
195 if ( $this->mTitle === false ) {
196 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
197 }
198 return $this->mTitle;
199 }
200
201 /**
202 * Get the User object of the person who performed this change.
203 *
204 * @return User
205 */
206 public function getPerformer() {
207 if ( $this->mPerformer === false ) {
208 if ( $this->mAttribs['rc_user'] ) {
209 $this->mPerformer = User::newFromID( $this->mAttribs['rc_user'] );
210 } else {
211 $this->mPerformer = User::newFromName( $this->mAttribs['rc_user_text'], false );
212 }
213 }
214 return $this->mPerformer;
215 }
216
217 /**
218 * Writes the data in this object to the database
219 * @param $noudp bool
220 */
221 public function save( $noudp = false ) {
222 global $wgLocalInterwiki, $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang;
223
224 $dbw = wfGetDB( DB_MASTER );
225 if ( !is_array( $this->mExtra ) ) {
226 $this->mExtra = array();
227 }
228 $this->mExtra['lang'] = $wgLocalInterwiki;
229
230 if ( !$wgPutIPinRC ) {
231 $this->mAttribs['rc_ip'] = '';
232 }
233
234 # If our database is strict about IP addresses, use NULL instead of an empty string
235 if ( $dbw->strictIPs() and $this->mAttribs['rc_ip'] == '' ) {
236 unset( $this->mAttribs['rc_ip'] );
237 }
238
239 # Trim spaces on user supplied text
240 $this->mAttribs['rc_comment'] = trim( $this->mAttribs['rc_comment'] );
241
242 # Make sure summary is truncated (whole multibyte characters)
243 $this->mAttribs['rc_comment'] = $wgContLang->truncate( $this->mAttribs['rc_comment'], 255 );
244
245 # Fixup database timestamps
246 $this->mAttribs['rc_timestamp'] = $dbw->timestamp( $this->mAttribs['rc_timestamp'] );
247 $this->mAttribs['rc_cur_time'] = $dbw->timestamp( $this->mAttribs['rc_cur_time'] );
248 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
249
250 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
251 if ( $dbw->cascadingDeletes() and $this->mAttribs['rc_cur_id'] == 0 ) {
252 unset( $this->mAttribs['rc_cur_id'] );
253 }
254
255 # Insert new row
256 $dbw->insert( 'recentchanges', $this->mAttribs, __METHOD__ );
257
258 # Set the ID
259 $this->mAttribs['rc_id'] = $dbw->insertId();
260
261 # Notify extensions
262 wfRunHooks( 'RecentChange_save', array( &$this ) );
263
264 # Notify external application via UDP
265 if ( !$noudp ) {
266 $this->notifyRC2UDP();
267 }
268
269 # E-mail notifications
270 if ( $wgUseEnotif || $wgShowUpdatedMarker ) {
271 $editor = $this->getPerformer();
272 $title = $this->getTitle();
273
274 if ( wfRunHooks( 'AbortEmailNotification', array( $editor, $title ) ) ) {
275 # @todo FIXME: This would be better as an extension hook
276 $enotif = new EmailNotification();
277 $enotif->notifyOnPageChange( $editor, $title,
278 $this->mAttribs['rc_timestamp'],
279 $this->mAttribs['rc_comment'],
280 $this->mAttribs['rc_minor'],
281 $this->mAttribs['rc_last_oldid'],
282 $this->mExtra['pageStatus'] );
283 }
284 }
285 }
286
287 public function notifyRC2UDP() {
288 global $wgRC2UDPAddress, $wgRC2UDPOmitBots;
289 # Notify external application via UDP
290 # Omit RC_EXTERNAL changes: bots and tools can get these edits from the feed of the external wiki
291 if ( $wgRC2UDPAddress && $this->mAttribs['rc_type'] != RC_EXTERNAL &&
292 ( !$this->mAttribs['rc_bot'] || !$wgRC2UDPOmitBots ) ) {
293 self::sendToUDP( $this->getIRCLine() );
294 }
295 }
296
297 /**
298 * Send some text to UDP.
299 * @see RecentChange::cleanupForIRC
300 * @param string $line text to send
301 * @param string $address defaults to $wgRC2UDPAddress.
302 * @param string $prefix defaults to $wgRC2UDPPrefix.
303 * @param int $port defaults to $wgRC2UDPPort. (Since 1.17)
304 * @return Boolean: success
305 */
306 public static function sendToUDP( $line, $address = '', $prefix = '', $port = '' ) {
307 global $wgRC2UDPAddress, $wgRC2UDPPrefix, $wgRC2UDPPort;
308 # Assume default for standard RC case
309 $address = $address ? $address : $wgRC2UDPAddress;
310 $prefix = $prefix ? $prefix : $wgRC2UDPPrefix;
311 $port = $port ? $port : $wgRC2UDPPort;
312 # Notify external application via UDP
313 if ( $address ) {
314 $conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
315 if ( $conn ) {
316 $line = $prefix . $line;
317 wfDebug( __METHOD__ . ": sending UDP line: $line\n" );
318 socket_sendto( $conn, $line, strlen( $line ), 0, $address, $port );
319 socket_close( $conn );
320 return true;
321 } else {
322 wfDebug( __METHOD__ . ": failed to create UDP socket\n" );
323 }
324 }
325 return false;
326 }
327
328 /**
329 * Remove newlines, carriage returns and decode html entities
330 * @param $text String
331 * @return String
332 */
333 public static function cleanupForIRC( $text ) {
334 return Sanitizer::decodeCharReferences( str_replace( array( "\n", "\r" ), array( " ", "" ), $text ) );
335 }
336
337 /**
338 * Mark a given change as patrolled
339 *
340 * @param $change Mixed: RecentChange or corresponding rc_id
341 * @param $auto Boolean: for automatic patrol
342 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
343 */
344 public static function markPatrolled( $change, $auto = false ) {
345 global $wgUser;
346
347 $change = $change instanceof RecentChange
348 ? $change
349 : RecentChange::newFromId( $change );
350
351 if ( !$change instanceof RecentChange ) {
352 return null;
353 }
354 return $change->doMarkPatrolled( $wgUser, $auto );
355 }
356
357 /**
358 * Mark this RecentChange as patrolled
359 *
360 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
361 * @param $user User object doing the action
362 * @param $auto Boolean: for automatic patrol
363 * @return array of permissions errors, see Title::getUserPermissionsErrors()
364 */
365 public function doMarkPatrolled( User $user, $auto = false ) {
366 global $wgUseRCPatrol, $wgUseNPPatrol;
367 $errors = array();
368 // If recentchanges patrol is disabled, only new pages
369 // can be patrolled
370 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol || $this->getAttribute( 'rc_type' ) != RC_NEW ) ) {
371 $errors[] = array( 'rcpatroldisabled' );
372 }
373 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
374 $right = $auto ? 'autopatrol' : 'patrol';
375 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
376 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
377 $errors[] = array( 'hookaborted' );
378 }
379 // Users without the 'autopatrol' right can't patrol their
380 // own revisions
381 if ( $user->getName() == $this->getAttribute( 'rc_user_text' ) && !$user->isAllowed( 'autopatrol' ) ) {
382 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
383 }
384 if ( $errors ) {
385 return $errors;
386 }
387 // If the change was patrolled already, do nothing
388 if ( $this->getAttribute( 'rc_patrolled' ) ) {
389 return array();
390 }
391 // Actually set the 'patrolled' flag in RC
392 $this->reallyMarkPatrolled();
393 // Log this patrol event
394 PatrolLog::record( $this, $auto, $user );
395 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
396 return array();
397 }
398
399 /**
400 * Mark this RecentChange patrolled, without error checking
401 * @return Integer: number of affected rows
402 */
403 public function reallyMarkPatrolled() {
404 $dbw = wfGetDB( DB_MASTER );
405 $dbw->update(
406 'recentchanges',
407 array(
408 'rc_patrolled' => 1
409 ),
410 array(
411 'rc_id' => $this->getAttribute( 'rc_id' )
412 ),
413 __METHOD__
414 );
415 // Invalidate the page cache after the page has been patrolled
416 // to make sure that the Patrol link isn't visible any longer!
417 $this->getTitle()->invalidateCache();
418 return $dbw->affectedRows();
419 }
420
421 /**
422 * Makes an entry in the database corresponding to an edit
423 *
424 * @param $timestamp
425 * @param $title Title
426 * @param $minor
427 * @param $user User
428 * @param $comment
429 * @param $oldId
430 * @param $lastTimestamp
431 * @param $bot
432 * @param $ip string
433 * @param $oldSize int
434 * @param $newSize int
435 * @param $newId int
436 * @param $patrol int
437 * @return RecentChange
438 */
439 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
440 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
441 $rc = new RecentChange;
442 $rc->mTitle = $title;
443 $rc->mPerformer = $user;
444 $rc->mAttribs = array(
445 'rc_timestamp' => $timestamp,
446 'rc_cur_time' => $timestamp,
447 'rc_namespace' => $title->getNamespace(),
448 'rc_title' => $title->getDBkey(),
449 'rc_type' => RC_EDIT,
450 'rc_minor' => $minor ? 1 : 0,
451 'rc_cur_id' => $title->getArticleID(),
452 'rc_user' => $user->getId(),
453 'rc_user_text' => $user->getName(),
454 'rc_comment' => $comment,
455 'rc_this_oldid' => $newId,
456 'rc_last_oldid' => $oldId,
457 'rc_bot' => $bot ? 1 : 0,
458 'rc_ip' => self::checkIPAddress( $ip ),
459 'rc_patrolled' => intval( $patrol ),
460 'rc_new' => 0, # obsolete
461 'rc_old_len' => $oldSize,
462 'rc_new_len' => $newSize,
463 'rc_deleted' => 0,
464 'rc_logid' => 0,
465 'rc_log_type' => null,
466 'rc_log_action' => '',
467 'rc_params' => ''
468 );
469
470 $rc->mExtra = array(
471 'prefixedDBkey' => $title->getPrefixedDBkey(),
472 'lastTimestamp' => $lastTimestamp,
473 'oldSize' => $oldSize,
474 'newSize' => $newSize,
475 'pageStatus' => 'changed'
476 );
477 $rc->save();
478 return $rc;
479 }
480
481 /**
482 * Makes an entry in the database corresponding to page creation
483 * Note: the title object must be loaded with the new id using resetArticleID()
484 * @todo Document parameters and return
485 *
486 * @param $timestamp
487 * @param $title Title
488 * @param $minor
489 * @param $user User
490 * @param $comment
491 * @param $bot
492 * @param $ip string
493 * @param $size int
494 * @param $newId int
495 * @param $patrol int
496 * @return RecentChange
497 */
498 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
499 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
500 $rc = new RecentChange;
501 $rc->mTitle = $title;
502 $rc->mPerformer = $user;
503 $rc->mAttribs = array(
504 'rc_timestamp' => $timestamp,
505 'rc_cur_time' => $timestamp,
506 'rc_namespace' => $title->getNamespace(),
507 'rc_title' => $title->getDBkey(),
508 'rc_type' => RC_NEW,
509 'rc_minor' => $minor ? 1 : 0,
510 'rc_cur_id' => $title->getArticleID(),
511 'rc_user' => $user->getId(),
512 'rc_user_text' => $user->getName(),
513 'rc_comment' => $comment,
514 'rc_this_oldid' => $newId,
515 'rc_last_oldid' => 0,
516 'rc_bot' => $bot ? 1 : 0,
517 'rc_ip' => self::checkIPAddress( $ip ),
518 'rc_patrolled' => intval( $patrol ),
519 'rc_new' => 1, # obsolete
520 'rc_old_len' => 0,
521 'rc_new_len' => $size,
522 'rc_deleted' => 0,
523 'rc_logid' => 0,
524 'rc_log_type' => null,
525 'rc_log_action' => '',
526 'rc_params' => ''
527 );
528
529 $rc->mExtra = array(
530 'prefixedDBkey' => $title->getPrefixedDBkey(),
531 'lastTimestamp' => 0,
532 'oldSize' => 0,
533 'newSize' => $size,
534 'pageStatus' => 'created'
535 );
536 $rc->save();
537 return $rc;
538 }
539
540 /**
541 * @param $timestamp
542 * @param $title
543 * @param $user
544 * @param $actionComment
545 * @param $ip string
546 * @param $type
547 * @param $action
548 * @param $target
549 * @param $logComment
550 * @param $params
551 * @param $newId int
552 * @param $actionCommentIRC string
553 * @return bool
554 */
555 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
556 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' )
557 {
558 global $wgLogRestrictions;
559 # Don't add private logs to RC!
560 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
561 return false;
562 }
563 $rc = self::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
564 $target, $logComment, $params, $newId, $actionCommentIRC );
565 $rc->save();
566 return true;
567 }
568
569 /**
570 * @param $timestamp
571 * @param $title Title
572 * @param $user User
573 * @param $actionComment
574 * @param $ip string
575 * @param $type
576 * @param $action
577 * @param $target Title
578 * @param $logComment
579 * @param $params
580 * @param $newId int
581 * @param $actionCommentIRC string
582 * @return RecentChange
583 */
584 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
585 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
586 global $wgRequest;
587
588 ## Get pageStatus for email notification
589 switch ( $type . '-' . $action ) {
590 case 'delete-delete':
591 $pageStatus = 'deleted';
592 break;
593 case 'move-move':
594 case 'move-move_redir':
595 $pageStatus = 'moved';
596 break;
597 case 'delete-restore':
598 $pageStatus = 'restored';
599 break;
600 case 'upload-upload':
601 $pageStatus = 'created';
602 break;
603 case 'upload-overwrite':
604 default:
605 $pageStatus = 'changed';
606 break;
607 }
608
609 $rc = new RecentChange;
610 $rc->mTitle = $target;
611 $rc->mPerformer = $user;
612 $rc->mAttribs = array(
613 'rc_timestamp' => $timestamp,
614 'rc_cur_time' => $timestamp,
615 'rc_namespace' => $target->getNamespace(),
616 'rc_title' => $target->getDBkey(),
617 'rc_type' => RC_LOG,
618 'rc_minor' => 0,
619 'rc_cur_id' => $target->getArticleID(),
620 'rc_user' => $user->getId(),
621 'rc_user_text' => $user->getName(),
622 'rc_comment' => $logComment,
623 'rc_this_oldid' => 0,
624 'rc_last_oldid' => 0,
625 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot', true ) : 0,
626 'rc_ip' => self::checkIPAddress( $ip ),
627 'rc_patrolled' => 1,
628 'rc_new' => 0, # obsolete
629 'rc_old_len' => null,
630 'rc_new_len' => null,
631 'rc_deleted' => 0,
632 'rc_logid' => $newId,
633 'rc_log_type' => $type,
634 'rc_log_action' => $action,
635 'rc_params' => $params
636 );
637
638 $rc->mExtra = array(
639 'prefixedDBkey' => $title->getPrefixedDBkey(),
640 'lastTimestamp' => 0,
641 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
642 'pageStatus' => $pageStatus,
643 'actionCommentIRC' => $actionCommentIRC
644 );
645 return $rc;
646 }
647
648 /**
649 * Initialises the members of this object from a mysql row object
650 *
651 * @param $row
652 */
653 public function loadFromRow( $row ) {
654 $this->mAttribs = get_object_vars( $row );
655 $this->mAttribs['rc_timestamp'] = wfTimestamp( TS_MW, $this->mAttribs['rc_timestamp'] );
656 $this->mAttribs['rc_deleted'] = $row->rc_deleted; // MUST be set
657 }
658
659 /**
660 * Makes a pseudo-RC entry from a cur row
661 *
662 * @deprected in 1.22
663 * @param $row
664 */
665 public function loadFromCurRow( $row ) {
666 wfDeprecated( __METHOD__, '1.22' );
667 $this->mAttribs = array(
668 'rc_timestamp' => wfTimestamp( TS_MW, $row->rev_timestamp ),
669 'rc_cur_time' => $row->rev_timestamp,
670 'rc_user' => $row->rev_user,
671 'rc_user_text' => $row->rev_user_text,
672 'rc_namespace' => $row->page_namespace,
673 'rc_title' => $row->page_title,
674 'rc_comment' => $row->rev_comment,
675 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
676 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
677 'rc_cur_id' => $row->page_id,
678 'rc_this_oldid' => $row->rev_id,
679 'rc_last_oldid' => isset( $row->rc_last_oldid ) ? $row->rc_last_oldid : 0,
680 'rc_bot' => 0,
681 'rc_ip' => '',
682 'rc_id' => $row->rc_id,
683 'rc_patrolled' => $row->rc_patrolled,
684 'rc_new' => $row->page_is_new, # obsolete
685 'rc_old_len' => $row->rc_old_len,
686 'rc_new_len' => $row->rc_new_len,
687 'rc_params' => isset( $row->rc_params ) ? $row->rc_params : '',
688 'rc_log_type' => isset( $row->rc_log_type ) ? $row->rc_log_type : null,
689 'rc_log_action' => isset( $row->rc_log_action ) ? $row->rc_log_action : null,
690 'rc_logid' => isset( $row->rc_logid ) ? $row->rc_logid : 0,
691 'rc_deleted' => $row->rc_deleted // MUST be set
692 );
693 }
694
695 /**
696 * Get an attribute value
697 *
698 * @param string $name Attribute name
699 * @return mixed
700 */
701 public function getAttribute( $name ) {
702 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : null;
703 }
704
705 /**
706 * @return array
707 */
708 public function getAttributes() {
709 return $this->mAttribs;
710 }
711
712 /**
713 * Gets the end part of the diff URL associated with this object
714 * Blank if no diff link should be displayed
715 * @param $forceCur
716 * @return string
717 */
718 public function diffLinkTrail( $forceCur ) {
719 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
720 $trail = "curid=" . (int)( $this->mAttribs['rc_cur_id'] ) .
721 "&oldid=" . (int)( $this->mAttribs['rc_last_oldid'] );
722 if ( $forceCur ) {
723 $trail .= '&diff=0';
724 } else {
725 $trail .= '&diff=' . (int)( $this->mAttribs['rc_this_oldid'] );
726 }
727 } else {
728 $trail = '';
729 }
730 return $trail;
731 }
732
733 /**
734 * @return string
735 */
736 public function getIRCLine() {
737 global $wgUseRCPatrol, $wgUseNPPatrol, $wgRC2UDPInterwikiPrefix, $wgLocalInterwiki,
738 $wgCanonicalServer, $wgScript;
739
740 if ( $this->mAttribs['rc_type'] == RC_LOG ) {
741 // Don't use SpecialPage::getTitleFor, backwards compatibility with
742 // IRC API which expects "Log".
743 $titleObj = Title::newFromText( 'Log/' . $this->mAttribs['rc_log_type'], NS_SPECIAL );
744 } else {
745 $titleObj =& $this->getTitle();
746 }
747 $title = $titleObj->getPrefixedText();
748 $title = self::cleanupForIRC( $title );
749
750 if ( $this->mAttribs['rc_type'] == RC_LOG ) {
751 $url = '';
752 } else {
753 $url = $wgCanonicalServer . $wgScript;
754 if ( $this->mAttribs['rc_type'] == RC_NEW ) {
755 $query = '?oldid=' . $this->mAttribs['rc_this_oldid'];
756 } else {
757 $query = '?diff=' . $this->mAttribs['rc_this_oldid'] . '&oldid=' . $this->mAttribs['rc_last_oldid'];
758 }
759 if ( $wgUseRCPatrol || ( $this->mAttribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) {
760 $query .= '&rcid=' . $this->mAttribs['rc_id'];
761 }
762 // HACK: We need this hook for WMF's secure server setup
763 wfRunHooks( 'IRCLineURL', array( &$url, &$query ) );
764 $url .= $query;
765 }
766
767 if ( $this->mAttribs['rc_old_len'] !== null && $this->mAttribs['rc_new_len'] !== null ) {
768 $szdiff = $this->mAttribs['rc_new_len'] - $this->mAttribs['rc_old_len'];
769 if ( $szdiff < -500 ) {
770 $szdiff = "\002$szdiff\002";
771 } elseif ( $szdiff >= 0 ) {
772 $szdiff = '+' . $szdiff;
773 }
774 // @todo i18n with parentheses in content language?
775 $szdiff = '(' . $szdiff . ')';
776 } else {
777 $szdiff = '';
778 }
779
780 $user = self::cleanupForIRC( $this->mAttribs['rc_user_text'] );
781
782 if ( $this->mAttribs['rc_type'] == RC_LOG ) {
783 $targetText = $this->getTitle()->getPrefixedText();
784 $comment = self::cleanupForIRC( str_replace( "[[$targetText]]", "[[\00302$targetText\00310]]", $this->mExtra['actionCommentIRC'] ) );
785 $flag = $this->mAttribs['rc_log_action'];
786 } else {
787 $comment = self::cleanupForIRC( $this->mAttribs['rc_comment'] );
788 $flag = '';
789 if ( !$this->mAttribs['rc_patrolled'] && ( $wgUseRCPatrol || $this->mAttribs['rc_type'] == RC_NEW && $wgUseNPPatrol ) ) {
790 $flag .= '!';
791 }
792 $flag .= ( $this->mAttribs['rc_type'] == RC_NEW ? "N" : "" ) . ( $this->mAttribs['rc_minor'] ? "M" : "" ) . ( $this->mAttribs['rc_bot'] ? "B" : "" );
793 }
794
795 if ( $wgRC2UDPInterwikiPrefix === true && $wgLocalInterwiki !== false ) {
796 $prefix = $wgLocalInterwiki;
797 } elseif ( $wgRC2UDPInterwikiPrefix ) {
798 $prefix = $wgRC2UDPInterwikiPrefix;
799 } else {
800 $prefix = false;
801 }
802 if ( $prefix !== false ) {
803 $titleString = "\00314[[\00303$prefix:\00307$title\00314]]";
804 } else {
805 $titleString = "\00314[[\00307$title\00314]]";
806 }
807
808 # see http://www.irssi.org/documentation/formats for some colour codes. prefix is \003,
809 # no colour (\003) switches back to the term default
810 $fullString = "$titleString\0034 $flag\00310 " .
811 "\00302$url\003 \0035*\003 \00303$user\003 \0035*\003 $szdiff \00310$comment\003\n";
812
813 return $fullString;
814 }
815
816 /**
817 * Returns the change size (HTML).
818 * The lengths can be given optionally.
819 * @param $old int
820 * @param $new int
821 * @return string
822 */
823 public function getCharacterDifference( $old = 0, $new = 0 ) {
824 if ( $old === 0 ) {
825 $old = $this->mAttribs['rc_old_len'];
826 }
827 if ( $new === 0 ) {
828 $new = $this->mAttribs['rc_new_len'];
829 }
830 if ( $old === null || $new === null ) {
831 return '';
832 }
833 return ChangesList::showCharacterDifference( $old, $new );
834 }
835
836 /**
837 * Purge expired changes from the recentchanges table
838 * @since 1.22
839 */
840 public static function purgeExpiredChanges() {
841 if ( wfReadOnly() ) {
842 return;
843 }
844
845 $method = __METHOD__;
846 $dbw = wfGetDB( DB_MASTER );
847 $dbw->onTransactionIdle( function() use ( $dbw, $method ) {
848 global $wgRCMaxAge;
849
850 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
851 $dbw->delete(
852 'recentchanges',
853 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
854 $method
855 );
856 } );
857 }
858
859 private static function checkIPAddress( $ip ) {
860 global $wgRequest;
861 if ( $ip ) {
862 if ( !IP::isIPAddress( $ip ) ) {
863 throw new MWException( "Attempt to write \"" . $ip . "\" as an IP address into recent changes" );
864 }
865 } else {
866 $ip = $wgRequest->getIP();
867 if ( !$ip ) {
868 $ip = '';
869 }
870 }
871 return $ip;
872 }
873
874 /**
875 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
876 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
877 * or rows which will be deleted soon shouldn't be included.
878 *
879 * @param $timestamp mixed MWTimestamp compatible timestamp
880 * @param $tolerance integer Tolerance in seconds
881 * @return bool
882 */
883 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
884 global $wgRCMaxAge;
885 return wfTimestamp( TS_UNIX, $timestamp ) > time() - $tolerance - $wgRCMaxAge;
886 }
887 }