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