Merge "[debug] Remove BC code from AvroFormatter"
[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 // Never send an RC notification email about categorization changes
328 if ( $this->mAttribs['rc_type'] != RC_CATEGORIZE ) {
329 if ( Hooks::run( 'AbortEmailNotification', array( $editor, $title, $this ) ) ) {
330 # @todo FIXME: This would be better as an extension hook
331 $enotif = new EmailNotification();
332 $enotif->notifyOnPageChange(
333 $editor,
334 $title,
335 $this->mAttribs['rc_timestamp'],
336 $this->mAttribs['rc_comment'],
337 $this->mAttribs['rc_minor'],
338 $this->mAttribs['rc_last_oldid'],
339 $this->mExtra['pageStatus']
340 );
341 }
342 }
343 }
344
345 // Update the cached list of active users
346 if ( $this->mAttribs['rc_user'] > 0 ) {
347 JobQueueGroup::singleton()->lazyPush( RecentChangesUpdateJob::newCacheUpdateJob() );
348 }
349 }
350
351 /**
352 * Notify all the feeds about the change.
353 * @param array $feeds Optional feeds to send to, defaults to $wgRCFeeds
354 */
355 public function notifyRCFeeds( array $feeds = null ) {
356 global $wgRCFeeds;
357 if ( $feeds === null ) {
358 $feeds = $wgRCFeeds;
359 }
360
361 $performer = $this->getPerformer();
362
363 foreach ( $feeds as $feed ) {
364 $feed += array(
365 'omit_bots' => false,
366 'omit_anon' => false,
367 'omit_user' => false,
368 'omit_minor' => false,
369 'omit_patrolled' => false,
370 );
371
372 if (
373 ( $feed['omit_bots'] && $this->mAttribs['rc_bot'] ) ||
374 ( $feed['omit_anon'] && $performer->isAnon() ) ||
375 ( $feed['omit_user'] && !$performer->isAnon() ) ||
376 ( $feed['omit_minor'] && $this->mAttribs['rc_minor'] ) ||
377 ( $feed['omit_patrolled'] && $this->mAttribs['rc_patrolled'] ) ||
378 $this->mAttribs['rc_type'] == RC_EXTERNAL
379 ) {
380 continue;
381 }
382
383 $engine = self::getEngine( $feed['uri'] );
384
385 if ( isset( $this->mExtra['actionCommentIRC'] ) ) {
386 $actionComment = $this->mExtra['actionCommentIRC'];
387 } else {
388 $actionComment = null;
389 }
390
391 /** @var $formatter RCFeedFormatter */
392 $formatter = is_object( $feed['formatter'] ) ? $feed['formatter'] : new $feed['formatter']();
393 $line = $formatter->getLine( $feed, $this, $actionComment );
394 if ( !$line ) {
395 // T109544
396 // If a feed formatter returns null, this will otherwise cause an
397 // error in at least RedisPubSubFeedEngine.
398 // Not sure where/how this should best be handled.
399 continue;
400 }
401
402 $engine->send( $feed, $line );
403 }
404 }
405
406 /**
407 * Gets the stream engine object for a given URI from $wgRCEngines
408 *
409 * @param string $uri URI to get the engine object for
410 * @throws MWException
411 * @return RCFeedEngine The engine object
412 */
413 public static function getEngine( $uri ) {
414 global $wgRCEngines;
415
416 $scheme = parse_url( $uri, PHP_URL_SCHEME );
417 if ( !$scheme ) {
418 throw new MWException( __FUNCTION__ . ": Invalid stream logger URI: '$uri'" );
419 }
420
421 if ( !isset( $wgRCEngines[$scheme] ) ) {
422 throw new MWException( __FUNCTION__ . ": Unknown stream logger URI scheme: $scheme" );
423 }
424
425 return new $wgRCEngines[$scheme];
426 }
427
428 /**
429 * Mark a given change as patrolled
430 *
431 * @param RecentChange|int $change RecentChange or corresponding rc_id
432 * @param bool $auto For automatic patrol
433 * @return array See doMarkPatrolled(), or null if $change is not an existing rc_id
434 */
435 public static function markPatrolled( $change, $auto = false ) {
436 global $wgUser;
437
438 $change = $change instanceof RecentChange
439 ? $change
440 : RecentChange::newFromId( $change );
441
442 if ( !$change instanceof RecentChange ) {
443 return null;
444 }
445
446 return $change->doMarkPatrolled( $wgUser, $auto );
447 }
448
449 /**
450 * Mark this RecentChange as patrolled
451 *
452 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and
453 * 'markedaspatrollederror-noautopatrol' as errors
454 * @param User $user User object doing the action
455 * @param bool $auto For automatic patrol
456 * @return array Array of permissions errors, see Title::getUserPermissionsErrors()
457 */
458 public function doMarkPatrolled( User $user, $auto = false ) {
459 global $wgUseRCPatrol, $wgUseNPPatrol, $wgUseFilePatrol;
460 $errors = array();
461 // If recentchanges patrol is disabled, only new pages or new file versions
462 // can be patrolled, provided the appropriate config variable is set
463 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol || $this->getAttribute( 'rc_type' ) != RC_NEW ) &&
464 ( !$wgUseFilePatrol || !( $this->getAttribute( 'rc_type' ) == RC_LOG &&
465 $this->getAttribute( 'rc_log_type' ) == 'upload' ) ) ) {
466 $errors[] = array( 'rcpatroldisabled' );
467 }
468 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
469 $right = $auto ? 'autopatrol' : 'patrol';
470 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
471 if ( !Hooks::run( 'MarkPatrolled',
472 array( $this->getAttribute( 'rc_id' ), &$user, false, $auto ) )
473 ) {
474 $errors[] = array( 'hookaborted' );
475 }
476 // Users without the 'autopatrol' right can't patrol their
477 // own revisions
478 if ( $user->getName() === $this->getAttribute( 'rc_user_text' )
479 && !$user->isAllowed( 'autopatrol' )
480 ) {
481 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
482 }
483 if ( $errors ) {
484 return $errors;
485 }
486 // If the change was patrolled already, do nothing
487 if ( $this->getAttribute( 'rc_patrolled' ) ) {
488 return array();
489 }
490 // Actually set the 'patrolled' flag in RC
491 $this->reallyMarkPatrolled();
492 // Log this patrol event
493 PatrolLog::record( $this, $auto, $user );
494 Hooks::run(
495 'MarkPatrolledComplete',
496 array( $this->getAttribute( 'rc_id' ), &$user, false, $auto )
497 );
498
499 return array();
500 }
501
502 /**
503 * Mark this RecentChange patrolled, without error checking
504 * @return int Number of affected rows
505 */
506 public function reallyMarkPatrolled() {
507 $dbw = wfGetDB( DB_MASTER );
508 $dbw->update(
509 'recentchanges',
510 array(
511 'rc_patrolled' => 1
512 ),
513 array(
514 'rc_id' => $this->getAttribute( 'rc_id' )
515 ),
516 __METHOD__
517 );
518 // Invalidate the page cache after the page has been patrolled
519 // to make sure that the Patrol link isn't visible any longer!
520 $this->getTitle()->invalidateCache();
521
522 return $dbw->affectedRows();
523 }
524
525 /**
526 * Makes an entry in the database corresponding to an edit
527 *
528 * @param string $timestamp
529 * @param Title $title
530 * @param bool $minor
531 * @param User $user
532 * @param string $comment
533 * @param int $oldId
534 * @param string $lastTimestamp
535 * @param bool $bot
536 * @param string $ip
537 * @param int $oldSize
538 * @param int $newSize
539 * @param int $newId
540 * @param int $patrol
541 * @return RecentChange
542 */
543 public static function notifyEdit(
544 $timestamp, &$title, $minor, &$user, $comment, $oldId, $lastTimestamp,
545 $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0
546 ) {
547 $rc = new RecentChange;
548 $rc->mTitle = $title;
549 $rc->mPerformer = $user;
550 $rc->mAttribs = array(
551 'rc_timestamp' => $timestamp,
552 'rc_namespace' => $title->getNamespace(),
553 'rc_title' => $title->getDBkey(),
554 'rc_type' => RC_EDIT,
555 'rc_source' => self::SRC_EDIT,
556 'rc_minor' => $minor ? 1 : 0,
557 'rc_cur_id' => $title->getArticleID(),
558 'rc_user' => $user->getId(),
559 'rc_user_text' => $user->getName(),
560 'rc_comment' => $comment,
561 'rc_this_oldid' => $newId,
562 'rc_last_oldid' => $oldId,
563 'rc_bot' => $bot ? 1 : 0,
564 'rc_ip' => self::checkIPAddress( $ip ),
565 'rc_patrolled' => intval( $patrol ),
566 'rc_new' => 0, # obsolete
567 'rc_old_len' => $oldSize,
568 'rc_new_len' => $newSize,
569 'rc_deleted' => 0,
570 'rc_logid' => 0,
571 'rc_log_type' => null,
572 'rc_log_action' => '',
573 'rc_params' => ''
574 );
575
576 $rc->mExtra = array(
577 'prefixedDBkey' => $title->getPrefixedDBkey(),
578 'lastTimestamp' => $lastTimestamp,
579 'oldSize' => $oldSize,
580 'newSize' => $newSize,
581 'pageStatus' => 'changed'
582 );
583
584 DeferredUpdates::addCallableUpdate( function() use ( $rc ) {
585 $rc->save();
586 if ( $rc->mAttribs['rc_patrolled'] ) {
587 PatrolLog::record( $rc, true, $rc->getPerformer() );
588 }
589 } );
590
591 return $rc;
592 }
593
594 /**
595 * Makes an entry in the database corresponding to page creation
596 * Note: the title object must be loaded with the new id using resetArticleID()
597 *
598 * @param string $timestamp
599 * @param Title $title
600 * @param bool $minor
601 * @param User $user
602 * @param string $comment
603 * @param bool $bot
604 * @param string $ip
605 * @param int $size
606 * @param int $newId
607 * @param int $patrol
608 * @return RecentChange
609 */
610 public static function notifyNew(
611 $timestamp, &$title, $minor, &$user, $comment, $bot,
612 $ip = '', $size = 0, $newId = 0, $patrol = 0
613 ) {
614 $rc = new RecentChange;
615 $rc->mTitle = $title;
616 $rc->mPerformer = $user;
617 $rc->mAttribs = array(
618 'rc_timestamp' => $timestamp,
619 'rc_namespace' => $title->getNamespace(),
620 'rc_title' => $title->getDBkey(),
621 'rc_type' => RC_NEW,
622 'rc_source' => self::SRC_NEW,
623 'rc_minor' => $minor ? 1 : 0,
624 'rc_cur_id' => $title->getArticleID(),
625 'rc_user' => $user->getId(),
626 'rc_user_text' => $user->getName(),
627 'rc_comment' => $comment,
628 'rc_this_oldid' => $newId,
629 'rc_last_oldid' => 0,
630 'rc_bot' => $bot ? 1 : 0,
631 'rc_ip' => self::checkIPAddress( $ip ),
632 'rc_patrolled' => intval( $patrol ),
633 'rc_new' => 1, # obsolete
634 'rc_old_len' => 0,
635 'rc_new_len' => $size,
636 'rc_deleted' => 0,
637 'rc_logid' => 0,
638 'rc_log_type' => null,
639 'rc_log_action' => '',
640 'rc_params' => ''
641 );
642
643 $rc->mExtra = array(
644 'prefixedDBkey' => $title->getPrefixedDBkey(),
645 'lastTimestamp' => 0,
646 'oldSize' => 0,
647 'newSize' => $size,
648 'pageStatus' => 'created'
649 );
650
651 DeferredUpdates::addCallableUpdate( function() use ( $rc ) {
652 $rc->save();
653 if ( $rc->mAttribs['rc_patrolled'] ) {
654 PatrolLog::record( $rc, true, $rc->getPerformer() );
655 }
656 } );
657
658 return $rc;
659 }
660
661 /**
662 * @param string $timestamp
663 * @param Title $title
664 * @param User $user
665 * @param string $actionComment
666 * @param string $ip
667 * @param string $type
668 * @param string $action
669 * @param Title $target
670 * @param string $logComment
671 * @param string $params
672 * @param int $newId
673 * @param string $actionCommentIRC
674 * @return bool
675 */
676 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
677 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = ''
678 ) {
679 global $wgLogRestrictions;
680
681 # Don't add private logs to RC!
682 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
683 return false;
684 }
685 $rc = self::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
686 $target, $logComment, $params, $newId, $actionCommentIRC );
687 $rc->save();
688
689 return true;
690 }
691
692 /**
693 * @param string $timestamp
694 * @param Title $title
695 * @param User $user
696 * @param string $actionComment
697 * @param string $ip
698 * @param string $type
699 * @param string $action
700 * @param Title $target
701 * @param string $logComment
702 * @param string $params
703 * @param int $newId
704 * @param string $actionCommentIRC
705 * @param int $revId Id of associated revision, if any
706 * @return RecentChange
707 */
708 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
709 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '',
710 $revId = 0 ) {
711 global $wgRequest;
712
713 # # Get pageStatus for email notification
714 switch ( $type . '-' . $action ) {
715 case 'delete-delete':
716 $pageStatus = 'deleted';
717 break;
718 case 'move-move':
719 case 'move-move_redir':
720 $pageStatus = 'moved';
721 break;
722 case 'delete-restore':
723 $pageStatus = 'restored';
724 break;
725 case 'upload-upload':
726 $pageStatus = 'created';
727 break;
728 case 'upload-overwrite':
729 default:
730 $pageStatus = 'changed';
731 break;
732 }
733
734 // Allow unpatrolled status when an associated rev id is passed
735 // May be used in core by moves and uploads
736 $markPatrolled = ( $revId > 0 ) ? $user->isAllowed( 'autopatrol' ) : true;
737
738 $rc = new RecentChange;
739 $rc->mTitle = $target;
740 $rc->mPerformer = $user;
741 $rc->mAttribs = array(
742 'rc_timestamp' => $timestamp,
743 'rc_namespace' => $target->getNamespace(),
744 'rc_title' => $target->getDBkey(),
745 'rc_type' => RC_LOG,
746 'rc_source' => self::SRC_LOG,
747 'rc_minor' => 0,
748 'rc_cur_id' => $target->getArticleID(),
749 'rc_user' => $user->getId(),
750 'rc_user_text' => $user->getName(),
751 'rc_comment' => $logComment,
752 'rc_this_oldid' => $revId,
753 'rc_last_oldid' => 0,
754 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot', true ) : 0,
755 'rc_ip' => self::checkIPAddress( $ip ),
756 'rc_patrolled' => $markPatrolled ? 1 : 0,
757 'rc_new' => 0, # obsolete
758 'rc_old_len' => null,
759 'rc_new_len' => null,
760 'rc_deleted' => 0,
761 'rc_logid' => $newId,
762 'rc_log_type' => $type,
763 'rc_log_action' => $action,
764 'rc_params' => $params
765 );
766
767 $rc->mExtra = array(
768 'prefixedDBkey' => $title->getPrefixedDBkey(),
769 'lastTimestamp' => 0,
770 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
771 'pageStatus' => $pageStatus,
772 'actionCommentIRC' => $actionCommentIRC
773 );
774
775 return $rc;
776 }
777
778 /**
779 * Constructs a RecentChange object for the given categorization
780 * This does not call save() on the object and thus does not write to the db
781 *
782 * @since 1.27
783 *
784 * @param string $timestamp Timestamp of the recent change to occur
785 * @param Title $categoryTitle Title of the category a page is being added to or removed from
786 * @param User $user User object of the user that made the change
787 * @param string $comment Change summary
788 * @param Title $pageTitle Title of the page that is being added or removed
789 * @param int $oldRevId Parent revision ID of this change
790 * @param int $newRevId Revision ID of this change
791 * @param string $lastTimestamp Parent revision timestamp of this change
792 * @param bool $bot true, if the change was made by a bot
793 * @param string $ip IP address of the user, if the change was made anonymously
794 * @param int $deleted Indicates whether the change has been deleted
795 *
796 * @return RecentChange
797 */
798 public static function newForCategorization(
799 $timestamp,
800 Title $categoryTitle,
801 User $user = null,
802 $comment,
803 Title $pageTitle,
804 $oldRevId,
805 $newRevId,
806 $lastTimestamp,
807 $bot,
808 $ip = '',
809 $deleted = 0
810 ) {
811 $rc = new RecentChange;
812 $rc->mTitle = $categoryTitle;
813 $rc->mPerformer = $user;
814 $rc->mAttribs = array(
815 'rc_timestamp' => $timestamp,
816 'rc_namespace' => $categoryTitle->getNamespace(),
817 'rc_title' => $categoryTitle->getDBkey(),
818 'rc_type' => RC_CATEGORIZE,
819 'rc_source' => self::SRC_CATEGORIZE,
820 'rc_minor' => 0,
821 'rc_cur_id' => $pageTitle->getArticleID(),
822 'rc_user' => $user ? $user->getId() : 0,
823 'rc_user_text' => $user ? $user->getName() : '',
824 'rc_comment' => $comment,
825 'rc_this_oldid' => $newRevId,
826 'rc_last_oldid' => $oldRevId,
827 'rc_bot' => $bot ? 1 : 0,
828 'rc_ip' => self::checkIPAddress( $ip ),
829 'rc_patrolled' => 1, // Always patrolled, just like log entries
830 'rc_new' => 0, # obsolete
831 'rc_old_len' => 0,
832 'rc_new_len' => 0,
833 'rc_deleted' => $deleted,
834 'rc_logid' => 0,
835 'rc_log_type' => null,
836 'rc_log_action' => '',
837 'rc_params' => ''
838 );
839
840 $rc->mExtra = array(
841 'prefixedDBkey' => $categoryTitle->getPrefixedDBkey(),
842 'lastTimestamp' => $lastTimestamp,
843 'oldSize' => 0,
844 'newSize' => 0,
845 'pageStatus' => 'changed'
846 );
847
848 return $rc;
849 }
850
851 /**
852 * Initialises the members of this object from a mysql row object
853 *
854 * @param mixed $row
855 */
856 public function loadFromRow( $row ) {
857 $this->mAttribs = get_object_vars( $row );
858 $this->mAttribs['rc_timestamp'] = wfTimestamp( TS_MW, $this->mAttribs['rc_timestamp'] );
859 $this->mAttribs['rc_deleted'] = $row->rc_deleted; // MUST be set
860 }
861
862 /**
863 * Get an attribute value
864 *
865 * @param string $name Attribute name
866 * @return mixed
867 */
868 public function getAttribute( $name ) {
869 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : null;
870 }
871
872 /**
873 * @return array
874 */
875 public function getAttributes() {
876 return $this->mAttribs;
877 }
878
879 /**
880 * Gets the end part of the diff URL associated with this object
881 * Blank if no diff link should be displayed
882 * @param bool $forceCur
883 * @return string
884 */
885 public function diffLinkTrail( $forceCur ) {
886 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
887 $trail = "curid=" . (int)( $this->mAttribs['rc_cur_id'] ) .
888 "&oldid=" . (int)( $this->mAttribs['rc_last_oldid'] );
889 if ( $forceCur ) {
890 $trail .= '&diff=0';
891 } else {
892 $trail .= '&diff=' . (int)( $this->mAttribs['rc_this_oldid'] );
893 }
894 } else {
895 $trail = '';
896 }
897
898 return $trail;
899 }
900
901 /**
902 * Returns the change size (HTML).
903 * The lengths can be given optionally.
904 * @param int $old
905 * @param int $new
906 * @return string
907 */
908 public function getCharacterDifference( $old = 0, $new = 0 ) {
909 if ( $old === 0 ) {
910 $old = $this->mAttribs['rc_old_len'];
911 }
912 if ( $new === 0 ) {
913 $new = $this->mAttribs['rc_new_len'];
914 }
915 if ( $old === null || $new === null ) {
916 return '';
917 }
918
919 return ChangesList::showCharacterDifference( $old, $new );
920 }
921
922 private static function checkIPAddress( $ip ) {
923 global $wgRequest;
924 if ( $ip ) {
925 if ( !IP::isIPAddress( $ip ) ) {
926 throw new MWException( "Attempt to write \"" . $ip .
927 "\" as an IP address into recent changes" );
928 }
929 } else {
930 $ip = $wgRequest->getIP();
931 if ( !$ip ) {
932 $ip = '';
933 }
934 }
935
936 return $ip;
937 }
938
939 /**
940 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
941 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
942 * or rows which will be deleted soon shouldn't be included.
943 *
944 * @param mixed $timestamp MWTimestamp compatible timestamp
945 * @param int $tolerance Tolerance in seconds
946 * @return bool
947 */
948 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
949 global $wgRCMaxAge;
950
951 return wfTimestamp( TS_UNIX, $timestamp ) > time() - $tolerance - $wgRCMaxAge;
952 }
953
954 /**
955 * Parses and returns the rc_params attribute
956 *
957 * @since 1.26
958 *
959 * @return array|null
960 */
961 public function parseParams() {
962 $rcParams = $this->getAttribute( 'rc_params' );
963
964 MediaWiki\suppressWarnings();
965 $unserializedParams = unserialize( $rcParams );
966 MediaWiki\restoreWarnings();
967
968 return $unserializedParams;
969 }
970 }