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