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