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