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