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