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