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