Merge "Remove recentchanges.rc_cur_time from sql statements"
[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
68 // Constants for the rc_source field. Extensions may also have
69 // their own source constants.
70 const SRC_EDIT = 'mw.edit';
71 const SRC_NEW = 'mw.new';
72 const SRC_LOG = 'mw.log';
73 const SRC_EXTERNAL = 'mw.external'; // obsolete
74
75 var $mAttribs = array(), $mExtra = array();
76
77 /**
78 * @var Title
79 */
80 var $mTitle = false;
81
82 /**
83 * @var User
84 */
85 private $mPerformer = false;
86
87 /**
88 * @var Title
89 */
90 var $mMovedToTitle = false;
91 var $numberofWatchingusers = 0; # Dummy to prevent error message in SpecialRecentchangeslinked
92 var $notificationtimestamp;
93
94 # Factory methods
95
96 /**
97 * @param $row
98 * @return RecentChange
99 */
100 public static function newFromRow( $row ) {
101 $rc = new RecentChange;
102 $rc->loadFromRow( $row );
103 return $rc;
104 }
105
106 /**
107 * @deprecated in 1.22
108 * @param $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 return $rc;
118 }
119
120 /**
121 * Obtain the recent change with a given rc_id value
122 *
123 * @param int $rcid rc_id value to retrieve
124 * @return RecentChange
125 */
126 public static function newFromId( $rcid ) {
127 return self::newFromConds( array( 'rc_id' => $rcid ), __METHOD__ );
128 }
129
130 /**
131 * Find the first recent change matching some specific conditions
132 *
133 * @param array $conds of conditions
134 * @param $fname Mixed: override the method name in profiling/logs
135 * @param $options Array Query options
136 * @return RecentChange
137 */
138 public static function newFromConds( $conds, $fname = __METHOD__, $options = array() ) {
139 $dbr = wfGetDB( DB_SLAVE );
140 $row = $dbr->selectRow( 'recentchanges', self::selectFields(), $conds, $fname, $options );
141 if ( $row !== false ) {
142 return self::newFromRow( $row );
143 } else {
144 return null;
145 }
146 }
147
148 /**
149 * Return the list of recentchanges fields that should be selected to create
150 * a new recentchanges object.
151 * @return array
152 */
153 public static function selectFields() {
154 return array(
155 'rc_id',
156 'rc_timestamp',
157 'rc_user',
158 'rc_user_text',
159 'rc_namespace',
160 'rc_title',
161 'rc_comment',
162 'rc_minor',
163 'rc_bot',
164 'rc_new',
165 'rc_cur_id',
166 'rc_this_oldid',
167 'rc_last_oldid',
168 'rc_type',
169 'rc_source',
170 'rc_patrolled',
171 'rc_ip',
172 'rc_old_len',
173 'rc_new_len',
174 'rc_deleted',
175 'rc_logid',
176 'rc_log_type',
177 'rc_log_action',
178 'rc_params',
179 );
180 }
181
182 # Accessors
183
184 /**
185 * @param $attribs array
186 */
187 public function setAttribs( $attribs ) {
188 $this->mAttribs = $attribs;
189 }
190
191 /**
192 * @param $extra array
193 */
194 public function setExtra( $extra ) {
195 $this->mExtra = $extra;
196 }
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 return $this->mTitle;
207 }
208
209 /**
210 * Get the User object of the person who performed this change.
211 *
212 * @return User
213 */
214 public function getPerformer() {
215 if ( $this->mPerformer === false ) {
216 if ( $this->mAttribs['rc_user'] ) {
217 $this->mPerformer = User::newFromID( $this->mAttribs['rc_user'] );
218 } else {
219 $this->mPerformer = User::newFromName( $this->mAttribs['rc_user_text'], false );
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 return IRCColourfulRCFeedFormatter::cleanupForIRC( $text );
384 }
385
386 /**
387 * Mark a given change as patrolled
388 *
389 * @param $change Mixed: RecentChange or corresponding rc_id
390 * @param $auto Boolean: for automatic patrol
391 * @return Array See doMarkPatrolled(), or null if $change is not an existing rc_id
392 */
393 public static function markPatrolled( $change, $auto = false ) {
394 global $wgUser;
395
396 $change = $change instanceof RecentChange
397 ? $change
398 : RecentChange::newFromId( $change );
399
400 if ( !$change instanceof RecentChange ) {
401 return null;
402 }
403 return $change->doMarkPatrolled( $wgUser, $auto );
404 }
405
406 /**
407 * Mark this RecentChange as patrolled
408 *
409 * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and 'markedaspatrollederror-noautopatrol' as errors
410 * @param $user User object doing the action
411 * @param $auto Boolean: for automatic patrol
412 * @return array of permissions errors, see Title::getUserPermissionsErrors()
413 */
414 public function doMarkPatrolled( User $user, $auto = false ) {
415 global $wgUseRCPatrol, $wgUseNPPatrol;
416 $errors = array();
417 // If recentchanges patrol is disabled, only new pages
418 // can be patrolled
419 if ( !$wgUseRCPatrol && ( !$wgUseNPPatrol || $this->getAttribute( 'rc_type' ) != RC_NEW ) ) {
420 $errors[] = array( 'rcpatroldisabled' );
421 }
422 // Automatic patrol needs "autopatrol", ordinary patrol needs "patrol"
423 $right = $auto ? 'autopatrol' : 'patrol';
424 $errors = array_merge( $errors, $this->getTitle()->getUserPermissionsErrors( $right, $user ) );
425 if ( !wfRunHooks( 'MarkPatrolled', array( $this->getAttribute( 'rc_id' ), &$user, false ) ) ) {
426 $errors[] = array( 'hookaborted' );
427 }
428 // Users without the 'autopatrol' right can't patrol their
429 // own revisions
430 if ( $user->getName() == $this->getAttribute( 'rc_user_text' ) && !$user->isAllowed( 'autopatrol' ) ) {
431 $errors[] = array( 'markedaspatrollederror-noautopatrol' );
432 }
433 if ( $errors ) {
434 return $errors;
435 }
436 // If the change was patrolled already, do nothing
437 if ( $this->getAttribute( 'rc_patrolled' ) ) {
438 return array();
439 }
440 // Actually set the 'patrolled' flag in RC
441 $this->reallyMarkPatrolled();
442 // Log this patrol event
443 PatrolLog::record( $this, $auto, $user );
444 wfRunHooks( 'MarkPatrolledComplete', array( $this->getAttribute( 'rc_id' ), &$user, false ) );
445 return array();
446 }
447
448 /**
449 * Mark this RecentChange patrolled, without error checking
450 * @return Integer: number of affected rows
451 */
452 public function reallyMarkPatrolled() {
453 $dbw = wfGetDB( DB_MASTER );
454 $dbw->update(
455 'recentchanges',
456 array(
457 'rc_patrolled' => 1
458 ),
459 array(
460 'rc_id' => $this->getAttribute( 'rc_id' )
461 ),
462 __METHOD__
463 );
464 // Invalidate the page cache after the page has been patrolled
465 // to make sure that the Patrol link isn't visible any longer!
466 $this->getTitle()->invalidateCache();
467 return $dbw->affectedRows();
468 }
469
470 /**
471 * Makes an entry in the database corresponding to an edit
472 *
473 * @param $timestamp
474 * @param $title Title
475 * @param $minor
476 * @param $user User
477 * @param $comment
478 * @param $oldId
479 * @param $lastTimestamp
480 * @param $bot
481 * @param $ip string
482 * @param $oldSize int
483 * @param $newSize int
484 * @param $newId int
485 * @param $patrol int
486 * @return RecentChange
487 */
488 public static function notifyEdit( $timestamp, &$title, $minor, &$user, $comment, $oldId,
489 $lastTimestamp, $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0 ) {
490 $rc = new RecentChange;
491 $rc->mTitle = $title;
492 $rc->mPerformer = $user;
493 $rc->mAttribs = array(
494 'rc_timestamp' => $timestamp,
495 'rc_namespace' => $title->getNamespace(),
496 'rc_title' => $title->getDBkey(),
497 'rc_type' => RC_EDIT,
498 'rc_source' => self::SRC_EDIT,
499 'rc_minor' => $minor ? 1 : 0,
500 'rc_cur_id' => $title->getArticleID(),
501 'rc_user' => $user->getId(),
502 'rc_user_text' => $user->getName(),
503 'rc_comment' => $comment,
504 'rc_this_oldid' => $newId,
505 'rc_last_oldid' => $oldId,
506 'rc_bot' => $bot ? 1 : 0,
507 'rc_ip' => self::checkIPAddress( $ip ),
508 'rc_patrolled' => intval( $patrol ),
509 'rc_new' => 0, # obsolete
510 'rc_old_len' => $oldSize,
511 'rc_new_len' => $newSize,
512 'rc_deleted' => 0,
513 'rc_logid' => 0,
514 'rc_log_type' => null,
515 'rc_log_action' => '',
516 'rc_params' => ''
517 );
518
519 $rc->mExtra = array(
520 'prefixedDBkey' => $title->getPrefixedDBkey(),
521 'lastTimestamp' => $lastTimestamp,
522 'oldSize' => $oldSize,
523 'newSize' => $newSize,
524 'pageStatus' => 'changed'
525 );
526 $rc->save();
527 return $rc;
528 }
529
530 /**
531 * Makes an entry in the database corresponding to page creation
532 * Note: the title object must be loaded with the new id using resetArticleID()
533 * @todo Document parameters and return
534 *
535 * @param $timestamp
536 * @param $title Title
537 * @param $minor
538 * @param $user User
539 * @param $comment
540 * @param $bot
541 * @param $ip string
542 * @param $size int
543 * @param $newId int
544 * @param $patrol int
545 * @return RecentChange
546 */
547 public static function notifyNew( $timestamp, &$title, $minor, &$user, $comment, $bot,
548 $ip = '', $size = 0, $newId = 0, $patrol = 0 ) {
549 $rc = new RecentChange;
550 $rc->mTitle = $title;
551 $rc->mPerformer = $user;
552 $rc->mAttribs = array(
553 'rc_timestamp' => $timestamp,
554 'rc_namespace' => $title->getNamespace(),
555 'rc_title' => $title->getDBkey(),
556 'rc_type' => RC_NEW,
557 'rc_source' => self::SRC_NEW,
558 'rc_minor' => $minor ? 1 : 0,
559 'rc_cur_id' => $title->getArticleID(),
560 'rc_user' => $user->getId(),
561 'rc_user_text' => $user->getName(),
562 'rc_comment' => $comment,
563 'rc_this_oldid' => $newId,
564 'rc_last_oldid' => 0,
565 'rc_bot' => $bot ? 1 : 0,
566 'rc_ip' => self::checkIPAddress( $ip ),
567 'rc_patrolled' => intval( $patrol ),
568 'rc_new' => 1, # obsolete
569 'rc_old_len' => 0,
570 'rc_new_len' => $size,
571 'rc_deleted' => 0,
572 'rc_logid' => 0,
573 'rc_log_type' => null,
574 'rc_log_action' => '',
575 'rc_params' => ''
576 );
577
578 $rc->mExtra = array(
579 'prefixedDBkey' => $title->getPrefixedDBkey(),
580 'lastTimestamp' => 0,
581 'oldSize' => 0,
582 'newSize' => $size,
583 'pageStatus' => 'created'
584 );
585 $rc->save();
586 return $rc;
587 }
588
589 /**
590 * @param $timestamp
591 * @param $title
592 * @param $user
593 * @param $actionComment
594 * @param $ip string
595 * @param $type
596 * @param $action
597 * @param $target
598 * @param $logComment
599 * @param $params
600 * @param $newId int
601 * @param $actionCommentIRC string
602 * @return bool
603 */
604 public static function notifyLog( $timestamp, &$title, &$user, $actionComment, $ip, $type,
605 $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' )
606 {
607 global $wgLogRestrictions;
608 # Don't add private logs to RC!
609 if ( isset( $wgLogRestrictions[$type] ) && $wgLogRestrictions[$type] != '*' ) {
610 return false;
611 }
612 $rc = self::newLogEntry( $timestamp, $title, $user, $actionComment, $ip, $type, $action,
613 $target, $logComment, $params, $newId, $actionCommentIRC );
614 $rc->save();
615 return true;
616 }
617
618 /**
619 * @param $timestamp
620 * @param $title Title
621 * @param $user User
622 * @param $actionComment
623 * @param $ip string
624 * @param $type
625 * @param $action
626 * @param $target Title
627 * @param $logComment
628 * @param $params
629 * @param $newId int
630 * @param $actionCommentIRC string
631 * @return RecentChange
632 */
633 public static function newLogEntry( $timestamp, &$title, &$user, $actionComment, $ip,
634 $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '' ) {
635 global $wgRequest;
636
637 ## Get pageStatus for email notification
638 switch ( $type . '-' . $action ) {
639 case 'delete-delete':
640 $pageStatus = 'deleted';
641 break;
642 case 'move-move':
643 case 'move-move_redir':
644 $pageStatus = 'moved';
645 break;
646 case 'delete-restore':
647 $pageStatus = 'restored';
648 break;
649 case 'upload-upload':
650 $pageStatus = 'created';
651 break;
652 case 'upload-overwrite':
653 default:
654 $pageStatus = 'changed';
655 break;
656 }
657
658 $rc = new RecentChange;
659 $rc->mTitle = $target;
660 $rc->mPerformer = $user;
661 $rc->mAttribs = array(
662 'rc_timestamp' => $timestamp,
663 'rc_namespace' => $target->getNamespace(),
664 'rc_title' => $target->getDBkey(),
665 'rc_type' => RC_LOG,
666 'rc_source' => self::SRC_LOG,
667 'rc_minor' => 0,
668 'rc_cur_id' => $target->getArticleID(),
669 'rc_user' => $user->getId(),
670 'rc_user_text' => $user->getName(),
671 'rc_comment' => $logComment,
672 'rc_this_oldid' => 0,
673 'rc_last_oldid' => 0,
674 'rc_bot' => $user->isAllowed( 'bot' ) ? $wgRequest->getBool( 'bot', true ) : 0,
675 'rc_ip' => self::checkIPAddress( $ip ),
676 'rc_patrolled' => 1,
677 'rc_new' => 0, # obsolete
678 'rc_old_len' => null,
679 'rc_new_len' => null,
680 'rc_deleted' => 0,
681 'rc_logid' => $newId,
682 'rc_log_type' => $type,
683 'rc_log_action' => $action,
684 'rc_params' => $params
685 );
686
687 $rc->mExtra = array(
688 'prefixedDBkey' => $title->getPrefixedDBkey(),
689 'lastTimestamp' => 0,
690 'actionComment' => $actionComment, // the comment appended to the action, passed from LogPage
691 'pageStatus' => $pageStatus,
692 'actionCommentIRC' => $actionCommentIRC
693 );
694 return $rc;
695 }
696
697 /**
698 * Initialises the members of this object from a mysql row object
699 *
700 * @param $row
701 */
702 public function loadFromRow( $row ) {
703 $this->mAttribs = get_object_vars( $row );
704 $this->mAttribs['rc_timestamp'] = wfTimestamp( TS_MW, $this->mAttribs['rc_timestamp'] );
705 $this->mAttribs['rc_deleted'] = $row->rc_deleted; // MUST be set
706 }
707
708 /**
709 * Makes a pseudo-RC entry from a cur row
710 *
711 * @deprecated in 1.22
712 * @param $row
713 */
714 public function loadFromCurRow( $row ) {
715 wfDeprecated( __METHOD__, '1.22' );
716 $this->mAttribs = array(
717 'rc_timestamp' => wfTimestamp( TS_MW, $row->rev_timestamp ),
718 'rc_user' => $row->rev_user,
719 'rc_user_text' => $row->rev_user_text,
720 'rc_namespace' => $row->page_namespace,
721 'rc_title' => $row->page_title,
722 'rc_comment' => $row->rev_comment,
723 'rc_minor' => $row->rev_minor_edit ? 1 : 0,
724 'rc_type' => $row->page_is_new ? RC_NEW : RC_EDIT,
725 'rc_source' => $row->page_is_new ? self::SRC_NEW : self::SRC_EDIT,
726 'rc_cur_id' => $row->page_id,
727 'rc_this_oldid' => $row->rev_id,
728 'rc_last_oldid' => isset( $row->rc_last_oldid ) ? $row->rc_last_oldid : 0,
729 'rc_bot' => 0,
730 'rc_ip' => '',
731 'rc_id' => $row->rc_id,
732 'rc_patrolled' => $row->rc_patrolled,
733 'rc_new' => $row->page_is_new, # obsolete
734 'rc_old_len' => $row->rc_old_len,
735 'rc_new_len' => $row->rc_new_len,
736 'rc_params' => isset( $row->rc_params ) ? $row->rc_params : '',
737 'rc_log_type' => isset( $row->rc_log_type ) ? $row->rc_log_type : null,
738 'rc_log_action' => isset( $row->rc_log_action ) ? $row->rc_log_action : null,
739 'rc_logid' => isset( $row->rc_logid ) ? $row->rc_logid : 0,
740 'rc_deleted' => $row->rc_deleted // MUST be set
741 );
742 }
743
744 /**
745 * Get an attribute value
746 *
747 * @param string $name Attribute name
748 * @return mixed
749 */
750 public function getAttribute( $name ) {
751 return isset( $this->mAttribs[$name] ) ? $this->mAttribs[$name] : null;
752 }
753
754 /**
755 * @return array
756 */
757 public function getAttributes() {
758 return $this->mAttribs;
759 }
760
761 /**
762 * Gets the end part of the diff URL associated with this object
763 * Blank if no diff link should be displayed
764 * @param $forceCur
765 * @return string
766 */
767 public function diffLinkTrail( $forceCur ) {
768 if ( $this->mAttribs['rc_type'] == RC_EDIT ) {
769 $trail = "curid=" . (int)( $this->mAttribs['rc_cur_id'] ) .
770 "&oldid=" . (int)( $this->mAttribs['rc_last_oldid'] );
771 if ( $forceCur ) {
772 $trail .= '&diff=0';
773 } else {
774 $trail .= '&diff=' . (int)( $this->mAttribs['rc_this_oldid'] );
775 }
776 } else {
777 $trail = '';
778 }
779 return $trail;
780 }
781
782 /**
783 * Returns the change size (HTML).
784 * The lengths can be given optionally.
785 * @param $old int
786 * @param $new int
787 * @return string
788 */
789 public function getCharacterDifference( $old = 0, $new = 0 ) {
790 if ( $old === 0 ) {
791 $old = $this->mAttribs['rc_old_len'];
792 }
793 if ( $new === 0 ) {
794 $new = $this->mAttribs['rc_new_len'];
795 }
796 if ( $old === null || $new === null ) {
797 return '';
798 }
799 return ChangesList::showCharacterDifference( $old, $new );
800 }
801
802 /**
803 * Purge expired changes from the recentchanges table
804 * @since 1.22
805 */
806 public static function purgeExpiredChanges() {
807 if ( wfReadOnly() ) {
808 return;
809 }
810
811 $method = __METHOD__;
812 $dbw = wfGetDB( DB_MASTER );
813 $dbw->onTransactionIdle( function() use ( $dbw, $method ) {
814 global $wgRCMaxAge;
815
816 $cutoff = $dbw->timestamp( time() - $wgRCMaxAge );
817 $dbw->delete(
818 'recentchanges',
819 array( 'rc_timestamp < ' . $dbw->addQuotes( $cutoff ) ),
820 $method
821 );
822 } );
823 }
824
825 private static function checkIPAddress( $ip ) {
826 global $wgRequest;
827 if ( $ip ) {
828 if ( !IP::isIPAddress( $ip ) ) {
829 throw new MWException( "Attempt to write \"" . $ip . "\" as an IP address into recent changes" );
830 }
831 } else {
832 $ip = $wgRequest->getIP();
833 if ( !$ip ) {
834 $ip = '';
835 }
836 }
837 return $ip;
838 }
839
840 /**
841 * Check whether the given timestamp is new enough to have a RC row with a given tolerance
842 * as the recentchanges table might not be cleared out regularly (so older entries might exist)
843 * or rows which will be deleted soon shouldn't be included.
844 *
845 * @param $timestamp mixed MWTimestamp compatible timestamp
846 * @param $tolerance integer Tolerance in seconds
847 * @return bool
848 */
849 public static function isInRCLifespan( $timestamp, $tolerance = 0 ) {
850 global $wgRCMaxAge;
851 return wfTimestamp( TS_UNIX, $timestamp ) > time() - $tolerance - $wgRCMaxAge;
852 }
853 }