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