Merge "Don't check namespace in SpecialWantedtemplates"
[lhc/web/wiklou.git] / includes / changes / 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_namespace namespace #
30 * rc_title non-prefixed db key
31 * rc_type is new entry, used to determine whether updating is necessary
32 * rc_source string representation of change source
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 * oldSize text size before the change
56 * newSize text size after the change
57 * pageStatus status of the page: created, deleted, moved, restored, changed
58 *
59 * temporary: not stored in the database
60 * notificationtimestamp
61 * numberofWatchingusers
62 */
63 class RecentChange {
64 // Constants for the rc_source field. Extensions may also have
65 // their own source constants.
66 const SRC_EDIT = 'mw.edit';
67 const SRC_NEW = 'mw.new';
68 const SRC_LOG = 'mw.log';
69 const SRC_EXTERNAL = 'mw.external'; // obsolete
70
71 public $mAttribs = array();
72 public $mExtra = array();
73
74 /**
75 * @var Title
76 */
77 public $mTitle = false;
78
79 /**
80 * @var User
81 */
82 private $mPerformer = false;
83
84 public $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentChangesLinked
85 public $notificationtimestamp;
86
87 /**
88 * @var int Line number of recent change. Default -1.
89 */
90 public $counter = -1;
91
92 # Factory methods
93
94 /**
95 * @param mixed $row
96 * @return RecentChange
97 */
98 public static function newFromRow( $row ) {
99 $rc = new RecentChange;
100 $rc->loadFromRow( $row );
101
102 return $rc;
103 }
104
105 /**
106 * Parsing text to RC_* constants
107 * @since 1.24
108 * @param string|array $type
109 * @throws MWException
110 * @return int|array RC_TYPE
111 */
112 public static function parseToRCType( $type ) {
113 if ( is_array( $type ) ) {
114 $retval = array();
115 foreach ( $type as $t ) {
116 $retval[] = RecentChange::parseToRCType( $t );
117 }
118
119 return $retval;
120 }
121
122 switch ( $type ) {
123 case 'edit':
124 return RC_EDIT;
125 case 'new':
126 return RC_NEW;
127 case 'log':
128 return RC_LOG;
129 case 'external':
130 return RC_EXTERNAL;
131 default:
132 throw new MWException( "Unknown type '$type'" );
133 }
134 }
135
136 /**
137 * Parsing RC_* constants to human-readable test
138 * @since 1.24
139 * @param int $rcType
140 * @return string $type
141 */
142 public static function parseFromRCType( $rcType ) {
143 switch ( $rcType ) {
144 case RC_EDIT:
145 $type = 'edit';
146 break;
147 case RC_NEW:
148 $type = 'new';
149 break;
150 case RC_LOG:
151 $type = 'log';
152 break;
153 case RC_EXTERNAL:
154 $type = 'external';
155 break;
156 default:
157 $type = "$rcType";
158 }
159
160 return $type;
161 }
162
163 /**
164 * Obtain the recent change with a given rc_id value
165 *
166 * @param int $rcid The rc_id value to retrieve
167 * @return RecentChange|null
168 */
169 public static function newFromId( $rcid ) {
170 return self::newFromConds( array( 'rc_id' => $rcid ), __METHOD__ );
171 }
172
173 /**
174 * Find the first recent change matching some specific conditions
175 *
176 * @param array $conds Array of conditions
177 * @param mixed $fname Override the method name in profiling/logs
178 * @param array $options Query options
179 * @return RecentChange|null
180 */
181 public static function newFromConds( $conds, $fname = __METHOD__, $options = array() ) {
182 $dbr = wfGetDB( DB_SLAVE );
183 $row = $dbr->selectRow( 'recentchanges', self::selectFields(), $conds, $fname, $options );
184 if ( $row !== false ) {
185 return self::newFromRow( $row );
186 } else {
187 return null;
188 }
189 }
190
191 /**
192 * Return the list of recentchanges fields that should be selected to create
193 * a new recentchanges object.
194 * @return array
195 */
196 public static function selectFields() {
197 return array(
198 'rc_id',
199 'rc_timestamp',
200 'rc_user',
201 'rc_user_text',
202 'rc_namespace',
203 'rc_title',
204 'rc_comment',
205 'rc_minor',
206 'rc_bot',
207 'rc_new',
208 'rc_cur_id',
209 'rc_this_oldid',
210 'rc_last_oldid',
211 'rc_type',
212 'rc_source',
213 'rc_patrolled',
214 'rc_ip',
215 'rc_old_len',
216 'rc_new_len',
217 'rc_deleted',
218 'rc_logid',
219 'rc_log_type',
220 'rc_log_action',
221 'rc_params',
222 );
223 }
224
225 # Accessors
226
227 /**
228 * @param array $attribs
229 */
230 public function setAttribs( $attribs ) {
231 $this->mAttribs = $attribs;
232 }
233
234 /**
235 * @param array $extra
236 */
237 public function setExtra( $extra ) {
238 $this->mExtra = $extra;
239 }
240
241 /**
242 * @return Title
243 */
244 public function &getTitle() {
245 if ( $this->mTitle === false ) {
246 $this->mTitle = Title::makeTitle( $this->mAttribs['rc_namespace'], $this->mAttribs['rc_title'] );
247 }
248
249 return $this->mTitle;
250 }
251
252 /**
253 * Get the User object of the person who performed this change.
254 *
255 * @return User
256 */
257 public function getPerformer() {
258 if ( $this->mPerformer === false ) {
259 if ( $this->mAttribs['rc_user'] ) {
260 $this->mPerformer = User::newFromId( $this->mAttribs['rc_user'] );
261 } else {
262 $this->mPerformer = User::newFromName( $this->mAttribs['rc_user_text'], false );
263 }
264 }
265
266 return $this->mPerformer;
267 }
268
269 /**
270 * Writes the data in this object to the database
271 * @param bool $noudp
272 */
273 public function save( $noudp = false ) {
274 global $wgPutIPinRC, $wgUseEnotif, $wgShowUpdatedMarker, $wgContLang;
275
276 $dbw = wfGetDB( DB_MASTER );
277 if ( !is_array( $this->mExtra ) ) {
278 $this->mExtra = array();
279 }
280
281 if ( !$wgPutIPinRC ) {
282 $this->mAttribs['rc_ip'] = '';
283 }
284
285 # If our database is strict about IP addresses, use NULL instead of an empty string
286 if ( $dbw->strictIPs() && $this->mAttribs['rc_ip'] == '' ) {
287 unset( $this->mAttribs['rc_ip'] );
288 }
289
290 # Trim spaces on user supplied text
291 $this->mAttribs['rc_comment'] = trim( $this->mAttribs['rc_comment'] );
292
293 # Make sure summary is truncated (whole multibyte characters)
294 $this->mAttribs['rc_comment'] = $wgContLang->truncate( $this->mAttribs['rc_comment'], 255 );
295
296 # Fixup database timestamps
297 $this->mAttribs['rc_timestamp'] = $dbw->timestamp( $this->mAttribs['rc_timestamp'] );
298 $this->mAttribs['rc_id'] = $dbw->nextSequenceValue( 'recentchanges_rc_id_seq' );
299
300 ## If we are using foreign keys, an entry of 0 for the page_id will fail, so use NULL
301 if ( $dbw->cascadingDeletes() && $this->mAttribs['rc_cur_id'] == 0 ) {
302 unset( $this->mAttribs['rc_cur_id'] );
303 }
304
305 # Insert new row
306 $dbw->insert( 'recentchanges', $this->mAttribs, __METHOD__ );
307
308 # Set the ID
309 $this->mAttribs['rc_id'] = $dbw->insertId();
310
311 # Notify extensions
312 Hooks::run( 'RecentChange_save', array( &$this ) );
313
314 # Notify external application via UDP
315 if ( !$noudp ) {
316 $this->notifyRCFeeds();
317 }
318
319 # E-mail notifications
320 if ( $wgUseEnotif || $wgShowUpdatedMarker ) {
321 $editor = $this->getPerformer();
322 $title = $this->getTitle();
323
324 if ( Hooks::run( 'AbortEmailNotification', array( $editor, $title, $this ) ) ) {
325 # @todo FIXME: This would be better as an extension hook
326 $enotif = new EmailNotification();
327 $enotif->notifyOnPageChange( $editor, $title,
328 $this->mAttribs['rc_timestamp'],
329 $this->mAttribs['rc_comment'],
330 $this->mAttribs['rc_minor'],
331 $this->mAttribs['rc_last_oldid'],
332 $this->mExtra['pageStatus'] );
333 }
334 }
335
336 // Update the cached list of active users
337 if ( $this->mAttribs['rc_user'] > 0 ) {
338 JobQueueGroup::singleton()->lazyPush( RecentChangesUpdateJob::newCacheUpdateJob() );
339 }
340 }
341
342 /**
343 * Notify all the feeds about the change.
344 * @param array $feeds Optional feeds to send to, defaults to $wgRCFeeds
345 */
346 public function notifyRCFeeds( array $feeds = null ) {
347 global $wgRCFeeds;
348 if ( $feeds === null ) {
349 $feeds = $wgRCFeeds;
350 }
351
352 $performer = $this->getPerformer();
353
354 foreach ( $feeds as $feed ) {
355 $feed += array(
356 'omit_bots' => false,
357 'omit_anon' => false,
358 'omit_user' => false,
359 'omit_minor' => false,
360 'omit_patrolled' => false,
361 );
362
363 if (
364 ( $feed['omit_bots'] && $this->mAttribs['rc_bot'] ) ||
365 ( $feed['omit_anon'] && $performer->isAnon() ) ||
366 ( $feed['omit_user'] && !$performer->isAnon() ) ||
367 ( $feed['omit_minor'] && $this->mAttribs['rc_minor'] ) ||
368 ( $feed['omit_patrolled'] && $this->mAttribs['rc_patrolled'] ) ||
369 $this->mAttribs['rc_type'] == RC_EXTERNAL
370 ) {
371 continue;
372 }
373
374 $engine = self::getEngine( $feed['uri'] );
375
376 if ( isset( $this->mExtra['actionCommentIRC'] ) ) {
377 $actionComment = $this->mExtra['actionCommentIRC'];
378 } else {
379 $actionComment = null;
380 }
381
382 /** @var $formatter RCFeedFormatter */
383 $formatter = is_object( $feed['formatter'] ) ? $feed['formatter'] : new $feed['formatter']();
384 $line = $formatter->getLine( $feed, $this, $actionComment );
385 if ( !$line ) {
386 // T109544
387 // If a feed formatter returns null, this will otherwise cause an
388 // error in at least RedisPubSubFeedEngine.
389 // Not sure where/how this should best be handled.
390 continue;
391 }
392
393 $engine->send( $feed, $line );
394 }
395 }
396
397 /**
398 * Gets the stream engine object for a given URI from $wgRCEngines
399 *
400 * @param string $uri URI to get the engine object for
401 * @throws MWException
402 * @return RCFeedEngine The engine object
403 */
404 public static function getEngine( $uri ) {
405 global $wgRCEngines;
406
407 $scheme = parse_url( $uri, PHP_URL_SCHEME );
408 if ( !$scheme ) {
409 throw new MWException( __FUNCTION__ . ": Invalid stream logger URI: '$uri'" );
410 }
411
412 if ( !isset( $wgRCEngines[$scheme] ) ) {
413 throw new MWException( __FUNCTION__ . ": Unknown stream logger URI scheme: $scheme" );
414 }
415
416 return new $wgRCEngines[$scheme];
417 }
418
419 /**
420 * Mark a given change as patrolled
421 *
422 * @param RecentChange|int $change RecentChange or corresponding rc_id
423 * @param bool $auto For automatic patrol
424 * @return array See doMarkPatrolled(), or null if $change is not an existing rc_id
425 */
426 public static function markPatrolled( $change, $auto = false ) {
427 global $wgUser;
428
429 $change = $change instanceof RecentChange
430 ? $change
431 : RecentChange::newFromId( $change );
432
433 if ( !$change instanceof RecentChange ) {
434 return null;
435 }
436
437 return $change->doMarkPatrolled( $wgUser, $auto );
438 }
439
440 /**
441 * Mark this RecentChange as patrolled
442 *
443 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
444 * 'markedaspatrollederror-noautopatrol' as errors
445 * @param User $user User object doing the action
446 * @param bool $auto For automatic patrol
447 * @return array Array of permissions errors, see Title::getUserPermissionsErrors()
448 */
449 public function doMarkPatrolled( User $user, $auto = false ) {
450 global $wgUseRCPatrol, $wgUseNPPatrol;
451 $errors = array();
452 // If recentchanges patrol is disabled, only new pages
453 // can be patrolled
454 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol || $this->getAttribute( 'rc_type' ) != RC_NEW ) ) {
455 $errors[] = array( 'rcpatroldisabled' );
456 }
457 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
458 $right = $auto ? 'autopatrol' : 'patrol';
459 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
460 if ( !Hooks::run( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
461 $errors[] = array( 'hookaborted' );
462 }
463 // Users without the 'autopatrol' right can't patrol their
464 // own revisions
465 if ( $user->getName() === $this->getAttribute( 'rc_user_text' )
466 && !$user->isAllowed( 'autopatrol' )
467 ) {
468 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
469 }
470 if ( $errors ) {
471 return $errors;
472 }
473 // If the change was patrolled already, do nothing
474 if ( $this->getAttribute( 'rc_patrolled' ) ) {
475 return array();
476 }
477 // Actually set the 'patrolled' flag in RC
478 $this->reallyMarkPatrolled();
479 // Log this patrol event
480 PatrolLog::record( $this, $auto, $user );
481 Hooks::run( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
482
483 return array();
484 }
485
486 /**
487 * Mark this RecentChange patrolled, without error checking
488 * @return int Number of affected rows
489 */
490 public function reallyMarkPatrolled() {
491 $dbw = wfGetDB( DB_MASTER );
492 $dbw->update(
493 'recentchanges',
494 array(
495 'rc_patrolled' => 1
496 ),
497 array(
498 'rc_id' => $this->getAttribute( 'rc_id' )
499 ),
500 __METHOD__
501 );
502 // Invalidate the page cache after the page has been patrolled
503 // to make sure that the Patrol link isn't visible any longer!
504 $this->getTitle()->invalidateCache();
505
506 return $dbw->affectedRows();
507 }
508
509 /**
510 * Makes an entry in the database corresponding to an edit
511 *
512 * @param string $timestamp
513 * @param Title $title
514 * @param bool $minor
515 * @param User $user
516 * @param string $comment
517 * @param int $oldId
518 * @param string $lastTimestamp
519 * @param bool $bot
520 * @param string $ip
521 * @param int $oldSize
522 * @param int $newSize
523 * @param int $newId
524 * @param int $patrol
525 * @return RecentChange
526 */
527 public static function notifyEdit(
528 $timestamp, &$title, $minor, &$user, $comment, $oldId, $lastTimestamp,
529 $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0
530 ) {
531 $rc = new RecentChange;
532 $rc->mTitle = $title;
533 $rc->mPerformer = $user;
534 $rc->mAttribs = array(
535 'rc_timestamp' => $timestamp,
536 'rc_namespace' => $title->getNamespace(),
537 'rc_title' => $title->getDBkey(),
538 'rc_type' => RC_EDIT,
539 'rc_source' => self::SRC_EDIT,
540 'rc_minor' => $minor ? 1 : 0,
541 'rc_cur_id' => $title->getArticleID(),
542 'rc_user' => $user->getId(),
543 'rc_user_text' => $user->getName(),
544 'rc_comment' => $comment,
545 'rc_this_oldid' => $newId,
546 'rc_last_oldid' => $oldId,
547 'rc_bot' => $bot ? 1 : 0,
548 'rc_ip' => self::checkIPAddress( $ip ),
549 'rc_patrolled' => intval( $patrol ),
550 'rc_new' => 0, # obsolete
551 'rc_old_len' => $oldSize,
552 'rc_new_len' => $newSize,
553 'rc_deleted' => 0,
554 'rc_logid' => 0,
555 'rc_log_type' => null,
556 'rc_log_action' => '',
557 'rc_params' => ''
558 );
559
560 $rc->mExtra = array(
561 'prefixedDBkey' => $title->getPrefixedDBkey(),
562 'lastTimestamp' => $lastTimestamp,
563 'oldSize' => $oldSize,
564 'newSize' => $newSize,
565 'pageStatus' => 'changed'
566 );
567
568 DeferredUpdates::addCallableUpdate( function() use ( $rc ) {
569 $rc->save();
570 if ( $rc->mAttribs['rc_patrolled'] ) {
571 PatrolLog::record( $rc, true, $rc->getPerformer() );
572 }
573 } );
574
575 return $rc;
576 }
577
578 /**
579 * Makes an entry in the database corresponding to page creation
580 * Note: the title object must be loaded with the new id using resetArticleID()
581 *
582 * @param string $timestamp
583 * @param Title $title
584 * @param bool $minor
585 * @param User $user
586 * @param string $comment
587 * @param bool $bot
588 * @param string $ip
589 * @param int $size
590 * @param int $newId
591 * @param int $patrol
592 * @return RecentChange
593 */
594 public static function notifyNew(
595 $timestamp, &$title, $minor, &$user, $comment, $bot,
596 $ip = '', $size = 0, $newId = 0, $patrol = 0
597 ) {
598 $rc = new RecentChange;
599 $rc->mTitle = $title;
600 $rc->mPerformer = $user;
601 $rc->mAttribs = array(
602 'rc_timestamp' => $timestamp,
603 'rc_namespace' => $title->getNamespace(),
604 'rc_title' => $title->getDBkey(),
605 'rc_type' => RC_NEW,
606 'rc_source' => self::SRC_NEW,
607 'rc_minor' => $minor ? 1 : 0,
608 'rc_cur_id' => $title->getArticleID(),
609 'rc_user' => $user->getId(),
610 'rc_user_text' => $user->getName(),
611 'rc_comment' => $comment,
612 'rc_this_oldid' => $newId,
613 'rc_last_oldid' => 0,
614 'rc_bot' => $bot ? 1 : 0,
615 'rc_ip' => self::checkIPAddress( $ip ),
616 'rc_patrolled' => intval( $patrol ),
617 'rc_new' => 1, # obsolete
618 'rc_old_len' => 0,
619 'rc_new_len' => $size,
620 'rc_deleted' => 0,
621 'rc_logid' => 0,
622 'rc_log_type' => null,
623 'rc_log_action' => '',
624 'rc_params' => ''
625 );
626
627 $rc->mExtra = array(
628 'prefixedDBkey' => $title->getPrefixedDBkey(),
629 'lastTimestamp' => 0,
630 'oldSize' => 0,
631 'newSize' => $size,
632 'pageStatus' => 'created'
633 );
634
635 DeferredUpdates::addCallableUpdate( function() use ( $rc ) {
636 $rc->save();
637 if ( $rc->mAttribs['rc_patrolled'] ) {
638 PatrolLog::record( $rc, true, $rc->getPerformer() );
639 }
640 } );
641
642 return $rc;
643 }
644
645 /**
646 * @param string $timestamp
647 * @param Title $title
648 * @param User $user
649 * @param string $actionComment
650 * @param string $ip
651 * @param string $type
652 * @param string $action
653 * @param Title $target
654 * @param string $logComment
655 * @param string $params
656 * @param int $newId
657 * @param string $actionCommentIRC
658 * @return bool
659 */
660 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
661 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
662 ) {
663 global $wgLogRestrictions;
664
665 # Don't add private logs to RC!
666 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
667 return false;
668 }
669 $rc = self::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
670 $target, $logComment, $params, $newId, $actionCommentIRC );
671 $rc->save();
672
673 return true;
674 }
675
676 /**
677 * @param string $timestamp
678 * @param Title $title
679 * @param User $user
680 * @param string $actionComment
681 * @param string $ip
682 * @param string $type
683 * @param string $action
684 * @param Title $target
685 * @param string $logComment
686 * @param string $params
687 * @param int $newId
688 * @param string $actionCommentIRC
689 * @return RecentChange
690 */
691 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
692 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
693 global $wgRequest;
694
695 ## Get pageStatus for email notification
696 switch ( $type . '-' . $action ) {
697 case 'delete-delete':
698 $pageStatus = 'deleted';
699 break;
700 case 'move-move':
701 case 'move-move_redir':
702 $pageStatus = 'moved';
703 break;
704 case 'delete-restore':
705 $pageStatus = 'restored';
706 break;
707 case 'upload-upload':
708 $pageStatus = 'created';
709 break;
710 case 'upload-overwrite':
711 default:
712 $pageStatus = 'changed';
713 break;
714 }
715
716 $rc = new RecentChange;
717 $rc->mTitle = $target;
718 $rc->mPerformer = $user;
719 $rc->mAttribs = array(
720 'rc_timestamp' => $timestamp,
721 'rc_namespace' => $target->getNamespace(),
722 'rc_title' => $target->getDBkey(),
723 'rc_type' => RC_LOG,
724 'rc_source' => self::SRC_LOG,
725 'rc_minor' => 0,
726 'rc_cur_id' => $target->getArticleID(),
727 'rc_user' => $user->getId(),
728 'rc_user_text' => $user->getName(),
729 'rc_comment' => $logComment,
730 'rc_this_oldid' => 0,
731 'rc_last_oldid' => 0,
732 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot', true ) : 0,
733 'rc_ip' => self::checkIPAddress( $ip ),
734 'rc_patrolled' => 1,
735 'rc_new' => 0, # obsolete
736 'rc_old_len' => null,
737 'rc_new_len' => null,
738 'rc_deleted' => 0,
739 'rc_logid' => $newId,
740 'rc_log_type' => $type,
741 'rc_log_action' => $action,
742 'rc_params' => $params
743 );
744
745 $rc->mExtra = array(
746 'prefixedDBkey' => $title->getPrefixedDBkey(),
747 'lastTimestamp' => 0,
748 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
749 'pageStatus' => $pageStatus,
750 'actionCommentIRC' => $actionCommentIRC
751 );
752
753 return $rc;
754 }
755
756 /**
757 * Initialises the members of this object from a mysql row object
758 *
759 * @param mixed $row
760 */
761 public function loadFromRow( $row ) {
762 $this->mAttribs = get_object_vars( $row );
763 $this->mAttribs['rc_timestamp'] = wfTimestamp( TS_MW, $this->mAttribs['rc_timestamp'] );
764 $this->mAttribs['rc_deleted'] = $row->rc_deleted; // MUST be set
765 }
766
767 /**
768 * Get an attribute value
769 *
770 * @param string $name Attribute name
771 * @return mixed
772 */
773 public function getAttribute( $name ) {
774 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : null;
775 }
776
777 /**
778 * @return array
779 */
780 public function getAttributes() {
781 return $this->mAttribs;
782 }
783
784 /**
785 * Gets the end part of the diff URL associated with this object
786 * Blank if no diff link should be displayed
787 * @param bool $forceCur
788 * @return string
789 */
790 public function diffLinkTrail( $forceCur ) {
791 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
792 $trail = "curid=" . (int)( $this->mAttribs['rc_cur_id'] ) .
793 "&oldid=" . (int)( $this->mAttribs['rc_last_oldid'] );
794 if ( $forceCur ) {
795 $trail .= '&diff=0';
796 } else {
797 $trail .= '&diff=' . (int)( $this->mAttribs['rc_this_oldid'] );
798 }
799 } else {
800 $trail = '';
801 }
802
803 return $trail;
804 }
805
806 /**
807 * Returns the change size (HTML).
808 * The lengths can be given optionally.
809 * @param int $old
810 * @param int $new
811 * @return string
812 */
813 public function getCharacterDifference( $old = 0, $new = 0 ) {
814 if ( $old === 0 ) {
815 $old = $this->mAttribs['rc_old_len'];
816 }
817 if ( $new === 0 ) {
818 $new = $this->mAttribs['rc_new_len'];
819 }
820 if ( $old === null || $new === null ) {
821 return '';
822 }
823
824 return ChangesList::showCharacterDifference( $old, $new );
825 }
826
827 private static function checkIPAddress( $ip ) {
828 global $wgRequest;
829 if ( $ip ) {
830 if ( !IP::isIPAddress( $ip ) ) {
831 throw new MWException( "Attempt to write \"" . $ip .
832 "\" as an IP address into recent changes" );
833 }
834 } else {
835 $ip = $wgRequest->getIP();
836 if ( !$ip ) {
837 $ip = '';
838 }
839 }
840
841 return $ip;
842 }
843
844 /**
845 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
846 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
847 * or rows which will be deleted soon shouldn't be included.
848 *
849 * @param mixed $timestamp MWTimestamp compatible timestamp
850 * @param int $tolerance Tolerance in seconds
851 * @return bool
852 */
853 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
854 global $wgRCMaxAge;
855
856 return wfTimestamp( TS_UNIX, $timestamp ) > time() - $tolerance - $wgRCMaxAge;
857 }
858
859 /**
860 * Parses and returns the rc_params attribute
861 *
862 * @since 1.26
863 *
864 * @return array|null
865 */
866 public function parseParams() {
867 $rcParams = $this->getAttribute( 'rc_params' );
868
869 MediaWiki\suppressWarnings();
870 $unserializedParams = unserialize( $rcParams );
871 MediaWiki\restoreWarnings();
872
873 return $unserializedParams;
874 }
875 }