Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
32.09% |
69 / 215 |
|
30.00% |
12 / 40 |
CRAP | |
0.00% |
0 / 1 |
| RecentChange | |
32.24% |
69 / 214 |
|
30.00% |
12 / 40 |
1872.76 | |
0.00% |
0 / 1 |
| __construct | |
83.33% |
10 / 12 |
|
0.00% |
0 / 1 |
1.00 | |||
| newFromRow | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| parseToRCType | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
20 | |||
| parseFromRCType | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
| getChangeTypes | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| newFromId | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| newFromConds | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| getQueryInfo | |
0.00% |
0 / 41 |
|
0.00% |
0 / 1 |
6 | |||
| setAttribs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| setAttribute | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setExtra | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getTitle | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
| getPage | |
25.00% |
2 / 8 |
|
0.00% |
0 / 1 |
6.80 | |||
| getPerformerIdentity | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| save | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| notifyRCFeeds | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| markPatrolled | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| reallyMarkPatrolled | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
| notifyEdit | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| notifyNew | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
2 | |||
| newLogEntry | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
| getParam | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| loadFromRow | |
95.00% |
19 / 20 |
|
0.00% |
0 / 1 |
3 | |||
| isInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getAttribute | |
60.00% |
9 / 15 |
|
0.00% |
0 / 1 |
12.10 | |||
| getAttributes | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getExtra | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getExtras | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| diffLinkTrail | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
12 | |||
| getCharacterDifference | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
30 | |||
| isInRCLifespan | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| parseParams | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getTags | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| addTags | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| getEditResult | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| setEditResult | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getUserIdentityFromAnyId | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
156 | |||
| setHighlights | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getHighlights | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isHighlighted | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\RecentChanges; |
| 8 | |
| 9 | use InvalidArgumentException; |
| 10 | use MediaWiki\ChangeTags\Taggable; |
| 11 | use MediaWiki\Debug\DeprecationHelper; |
| 12 | use MediaWiki\MainConfigNames; |
| 13 | use MediaWiki\MediaWikiServices; |
| 14 | use MediaWiki\Page\PageIdentity; |
| 15 | use MediaWiki\Page\PageReference; |
| 16 | use MediaWiki\Page\PageReferenceValue; |
| 17 | use MediaWiki\Permissions\Authority; |
| 18 | use MediaWiki\Permissions\PermissionStatus; |
| 19 | use MediaWiki\Storage\EditResult; |
| 20 | use MediaWiki\Title\Title; |
| 21 | use MediaWiki\User\UserIdentity; |
| 22 | use MediaWiki\User\UserIdentityValue; |
| 23 | use RuntimeException; |
| 24 | use Wikimedia\Timestamp\TimestampFormat as TS; |
| 25 | |
| 26 | /** |
| 27 | * @defgroup RecentChanges Recent changes |
| 28 | * Discovery and review of recent edits and log events on the wiki. |
| 29 | * |
| 30 | * The Recent changes feature stores a temporary copy of the long-term |
| 31 | * `revision` and `logging` table rows which represent page edits and |
| 32 | * log actions respectively. |
| 33 | * |
| 34 | * Recent changes augments revision and logging rows with additional metadata |
| 35 | * that empower reviewers to efficiently find edits related to their |
| 36 | * interest, or edits that warrant a closer look. This includes page |
| 37 | * namespace, "minor" edit status, and user type. As well as metadata we |
| 38 | * don't store elsewhere, such the bot flag (rc_bot), edit type (page creation, |
| 39 | * edit, or something else), and patrolling state (rc_patrolled). |
| 40 | * |
| 41 | * The patrolled status facilitates edit review via the "mark as patrolled" |
| 42 | * button, in combination with filtering by patrol status via SpecialRecentChanges, |
| 43 | * SpecialWatchlist, and ApiQueryRecentChanges. |
| 44 | */ |
| 45 | |
| 46 | /** |
| 47 | * Utility class for creating and reading rows in the recentchanges table. |
| 48 | * |
| 49 | * mAttribs: |
| 50 | * rc_id id of the row in the recentchanges table |
| 51 | * rc_timestamp time the entry was made |
| 52 | * rc_namespace namespace # |
| 53 | * rc_title non-prefixed db key |
| 54 | * rc_source change source |
| 55 | * rc_minor is minor |
| 56 | * rc_cur_id page_id of associated page entry |
| 57 | * rc_user user id who made the entry |
| 58 | * rc_user_text user name who made the entry |
| 59 | * rc_comment edit summary |
| 60 | * rc_this_oldid rev_id associated with this entry (or zero) |
| 61 | * rc_last_oldid rev_id associated with the entry before this one (or zero) |
| 62 | * rc_bot is bot, hidden |
| 63 | * rc_ip IP address of the user in dotted quad notation |
| 64 | * rc_patrolled boolean whether or not someone has marked this edit as patrolled |
| 65 | * rc_old_len integer byte length of the text before the edit |
| 66 | * rc_new_len the same after the edit |
| 67 | * rc_deleted partial deletion |
| 68 | * rc_logid the log_id value for this log entry (or zero) |
| 69 | * rc_log_type the log type (or null) |
| 70 | * rc_log_action the log action (or null) |
| 71 | * rc_params log params |
| 72 | * |
| 73 | * mExtra: |
| 74 | * prefixedDBkey prefixed db key, used by external app via msg queue |
| 75 | * oldSize text size before the change |
| 76 | * newSize text size after the change |
| 77 | * pageStatus status of the page: created, deleted, moved, restored, changed |
| 78 | * |
| 79 | * temporary: not stored in the database |
| 80 | * notificationtimestamp |
| 81 | * numberofWatchingusers |
| 82 | * watchlistExpiry for temporary watchlist items |
| 83 | * |
| 84 | * @todo Deprecate access to mAttribs (direct or via getAttributes). Right now |
| 85 | * we're having to include both rc_comment and rc_comment_text/rc_comment_data |
| 86 | * so random crap works right. |
| 87 | * |
| 88 | * @ingroup RecentChanges |
| 89 | */ |
| 90 | class RecentChange implements Taggable { |
| 91 | use DeprecationHelper; |
| 92 | |
| 93 | private const CHANGE_TYPES = [ |
| 94 | 'edit' => RC_EDIT, |
| 95 | 'new' => RC_NEW, |
| 96 | 'log' => RC_LOG, |
| 97 | 'external' => RC_EXTERNAL, |
| 98 | 'categorize' => RC_CATEGORIZE, |
| 99 | ]; |
| 100 | |
| 101 | // Constants for the rc_source field. Extensions may also have |
| 102 | // their own source constants. |
| 103 | public const SRC_EDIT = 'mw.edit'; |
| 104 | public const SRC_NEW = 'mw.new'; |
| 105 | public const SRC_LOG = 'mw.log'; |
| 106 | public const SRC_CATEGORIZE = 'mw.categorize'; |
| 107 | |
| 108 | /** |
| 109 | * Values of the `rc_source` attribute that may be added by MediaWiki core. |
| 110 | * It can be useful in database queries to exclude extension-added RC entries. |
| 111 | * If you have a RecentChange object, use the isInternal() method on it instead |
| 112 | * of this constant. |
| 113 | * |
| 114 | * @var array<string> |
| 115 | */ |
| 116 | public const INTERNAL_SOURCES = [ |
| 117 | self::SRC_EDIT, |
| 118 | self::SRC_NEW, |
| 119 | self::SRC_LOG, |
| 120 | self::SRC_CATEGORIZE, |
| 121 | ]; |
| 122 | |
| 123 | public const PRC_UNPATROLLED = PatrolManager::PRC_UNPATROLLED; |
| 124 | public const PRC_PATROLLED = PatrolManager::PRC_PATROLLED; |
| 125 | public const PRC_AUTOPATROLLED = PatrolManager::PRC_AUTOPATROLLED; |
| 126 | |
| 127 | public const SEND_NONE = RecentChangeStore::SEND_NONE; |
| 128 | public const SEND_FEED = RecentChangeStore::SEND_FEED; |
| 129 | |
| 130 | /** Flag for RecentChange::getQueryInfo() */ |
| 131 | public const STRAIGHT_JOIN_ACTOR = 1; |
| 132 | |
| 133 | /** @var array */ |
| 134 | public $mAttribs = []; |
| 135 | /** @var array */ |
| 136 | public $mExtra = []; |
| 137 | |
| 138 | /** |
| 139 | * @var PageReference|null |
| 140 | */ |
| 141 | private $mPage = null; |
| 142 | |
| 143 | /** |
| 144 | * @var UserIdentity|null |
| 145 | */ |
| 146 | private $mPerformer = null; |
| 147 | |
| 148 | /** @var int */ |
| 149 | public $numberofWatchingusers = 0; |
| 150 | /** @var bool */ |
| 151 | public $notificationtimestamp; |
| 152 | |
| 153 | /** |
| 154 | * @var string|null The expiry time, if this is a temporary watchlist item. |
| 155 | */ |
| 156 | public $watchlistExpiry; |
| 157 | |
| 158 | /** |
| 159 | * @var int Line number of recent change. Default -1. |
| 160 | */ |
| 161 | public $counter = -1; |
| 162 | |
| 163 | /** |
| 164 | * @var array List of tags to apply |
| 165 | */ |
| 166 | private $tags = []; |
| 167 | |
| 168 | /** |
| 169 | * @var EditResult|null EditResult associated with the edit |
| 170 | */ |
| 171 | private $editResult = null; |
| 172 | |
| 173 | /** |
| 174 | * @var array<string,bool> Highlighted filters, for ChangesList. The key |
| 175 | * is the group name and then a slash and then the filter name. |
| 176 | */ |
| 177 | private $highlights = []; |
| 178 | |
| 179 | public function __construct( |
| 180 | ?PageReference $page = null, |
| 181 | ?UserIdentity $performer = null |
| 182 | ) { |
| 183 | $this->deprecatePublicPropertyFallback( |
| 184 | 'mTitle', |
| 185 | '1.37', |
| 186 | function () { |
| 187 | return Title::castFromPageReference( $this->mPage ); |
| 188 | }, |
| 189 | function ( ?Title $title ) { |
| 190 | $this->mPage = $title; |
| 191 | } |
| 192 | ); |
| 193 | |
| 194 | $this->mPage = $page; |
| 195 | $this->mPerformer = $performer; |
| 196 | } |
| 197 | |
| 198 | # Factory methods |
| 199 | |
| 200 | /** |
| 201 | * @deprecated since 1.45, use MediaWikiServices::getInstance() |
| 202 | * ->getRecentChangeFactory()->newRecentChangeFromRow() instead. |
| 203 | * |
| 204 | * @param mixed $row |
| 205 | * @return RecentChange |
| 206 | */ |
| 207 | public static function newFromRow( $row ) { |
| 208 | return MediaWikiServices::getInstance() |
| 209 | ->getRecentChangeFactory() |
| 210 | ->newRecentChangeFromRow( $row ); |
| 211 | } |
| 212 | |
| 213 | /** |
| 214 | * Parsing text to RC_* constants |
| 215 | * |
| 216 | * @deprecated since 1.46 |
| 217 | * @since 1.24 |
| 218 | * @param string|array $type Callers must make sure that the given types are valid RC types. |
| 219 | * @return int|array RC_TYPE |
| 220 | */ |
| 221 | public static function parseToRCType( $type ) { |
| 222 | wfDeprecated( __METHOD__, '1.46' ); |
| 223 | |
| 224 | if ( is_array( $type ) ) { |
| 225 | $retval = []; |
| 226 | foreach ( $type as $t ) { |
| 227 | $retval[] = self::parseToRCType( $t ); |
| 228 | } |
| 229 | |
| 230 | return $retval; |
| 231 | } |
| 232 | |
| 233 | if ( !array_key_exists( $type, self::CHANGE_TYPES ) ) { |
| 234 | throw new InvalidArgumentException( "Unknown type '$type'" ); |
| 235 | } |
| 236 | return self::CHANGE_TYPES[$type]; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * Parsing RC_* constants to human-readable test |
| 241 | * |
| 242 | * @deprecated since 1.46 |
| 243 | * @since 1.24 |
| 244 | * @param int $rcType |
| 245 | * @return string |
| 246 | */ |
| 247 | public static function parseFromRCType( $rcType ) { |
| 248 | wfDeprecated( __METHOD__, '1.46' ); |
| 249 | |
| 250 | return array_search( $rcType, self::CHANGE_TYPES, true ) ?: "$rcType"; |
| 251 | } |
| 252 | |
| 253 | /** |
| 254 | * Get an array of all change types |
| 255 | * |
| 256 | * @since 1.26 |
| 257 | * |
| 258 | * @return array |
| 259 | */ |
| 260 | public static function getChangeTypes() { |
| 261 | return array_keys( self::CHANGE_TYPES ); |
| 262 | } |
| 263 | |
| 264 | /** |
| 265 | * Obtain the recent change with a given rc_id value |
| 266 | * |
| 267 | * @deprecated since 1.45, use MediaWikiServices::getInstance() |
| 268 | * ->getRecentChangeLookup()->getRecentChangeById() instead. |
| 269 | * |
| 270 | * @param int $rcid The rc_id value to retrieve |
| 271 | * @return RecentChange|null |
| 272 | */ |
| 273 | public static function newFromId( $rcid ) { |
| 274 | return MediaWikiServices::getInstance() |
| 275 | ->getRecentChangeLookup() |
| 276 | ->getRecentChangeById( $rcid ); |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Find the first recent change matching some specific conditions |
| 281 | * |
| 282 | * @deprecated since 1.45, use MediaWikiServices::getInstance() |
| 283 | * ->getRecentChangeLookup()->getRecentChangeByConds() instead. |
| 284 | * |
| 285 | * @param array $conds Array of conditions |
| 286 | * @param mixed $fname Override the method name in profiling/logs @phan-mandatory-param |
| 287 | * @param int $dbType DB_* constant |
| 288 | * |
| 289 | * @return RecentChange|null |
| 290 | */ |
| 291 | public static function newFromConds( |
| 292 | $conds, |
| 293 | $fname = __METHOD__, |
| 294 | $dbType = DB_REPLICA |
| 295 | ) { |
| 296 | return MediaWikiServices::getInstance() |
| 297 | ->getRecentChangeLookup() |
| 298 | ->getRecentChangeByConds( $conds, $fname, $dbType === DB_PRIMARY ); |
| 299 | } |
| 300 | |
| 301 | /** |
| 302 | * Return the tables, fields, and join conditions to be selected to create |
| 303 | * a new recentchanges object. |
| 304 | * |
| 305 | * Since 1.34, rc_user and rc_user_text have not been present in the |
| 306 | * database, but they continue to be available in query results as |
| 307 | * aliases. |
| 308 | * |
| 309 | * @since 1.31 |
| 310 | * @param int $joinFlags May be STRAIGHT_JOIN_ACTOR to use a straight join |
| 311 | * on the actor table, preventing the database from placing the actor |
| 312 | * table first in the join. This is appropriate when there are no |
| 313 | * restrictive conditions on the actor table, and the conditions are |
| 314 | * potentially complex and unindexed, and the query is limited and |
| 315 | * ordered by timestamp. Since 1.45. |
| 316 | * |
| 317 | * @return array[] With three keys: |
| 318 | * - tables: (string[]) to include in the `$table` to `IDatabase->select()` or `SelectQueryBuilder::tables` |
| 319 | * - fields: (string[]) to include in the `$vars` to `IDatabase->select()` or `SelectQueryBuilder::fields` |
| 320 | * - joins: (array) to include in the `$join_conds` to `IDatabase->select()` or `SelectQueryBuilder::joinConds` |
| 321 | * @phan-return array{tables:string[],fields:string[],joins:array} |
| 322 | */ |
| 323 | public static function getQueryInfo( int $joinFlags = 0 ) { |
| 324 | return [ |
| 325 | 'tables' => [ |
| 326 | 'recentchanges', |
| 327 | 'recentchanges_actor' => 'actor', |
| 328 | 'recentchanges_comment' => 'comment', |
| 329 | ], |
| 330 | 'fields' => [ |
| 331 | 'rc_id', |
| 332 | 'rc_timestamp', |
| 333 | 'rc_namespace', |
| 334 | 'rc_title', |
| 335 | 'rc_minor', |
| 336 | 'rc_bot', |
| 337 | 'rc_cur_id', |
| 338 | 'rc_this_oldid', |
| 339 | 'rc_last_oldid', |
| 340 | 'rc_source', |
| 341 | 'rc_patrolled', |
| 342 | 'rc_ip', |
| 343 | 'rc_old_len', |
| 344 | 'rc_new_len', |
| 345 | 'rc_deleted', |
| 346 | 'rc_logid', |
| 347 | 'rc_log_type', |
| 348 | 'rc_log_action', |
| 349 | 'rc_params', |
| 350 | 'rc_actor', |
| 351 | 'rc_user' => 'recentchanges_actor.actor_user', |
| 352 | 'rc_user_text' => 'recentchanges_actor.actor_name', |
| 353 | 'rc_comment_text' => 'recentchanges_comment.comment_text', |
| 354 | 'rc_comment_data' => 'recentchanges_comment.comment_data', |
| 355 | 'rc_comment_id' => 'recentchanges_comment.comment_id', |
| 356 | ], |
| 357 | 'joins' => [ |
| 358 | // Optimizer sometimes refuses to pick up the correct join order (T311360) |
| 359 | 'recentchanges_actor' => [ |
| 360 | ( $joinFlags & self::STRAIGHT_JOIN_ACTOR ) ? 'STRAIGHT_JOIN' : 'JOIN', |
| 361 | 'actor_id=rc_actor' |
| 362 | ], |
| 363 | 'recentchanges_comment' => [ 'STRAIGHT_JOIN', 'comment_id=rc_comment_id' ], |
| 364 | ], |
| 365 | ]; |
| 366 | } |
| 367 | |
| 368 | # Accessors |
| 369 | |
| 370 | /** |
| 371 | * @param array $attribs |
| 372 | */ |
| 373 | public function setAttribs( $attribs ) { |
| 374 | $this->mAttribs = $attribs; |
| 375 | } |
| 376 | |
| 377 | /** |
| 378 | * @since 1.45 |
| 379 | * @param string $name |
| 380 | * @param mixed $value |
| 381 | */ |
| 382 | public function setAttribute( $name, $value ) { |
| 383 | $this->mAttribs[$name] = $value; |
| 384 | } |
| 385 | |
| 386 | /** |
| 387 | * @param array $extra |
| 388 | */ |
| 389 | public function setExtra( $extra ) { |
| 390 | $this->mExtra = $extra; |
| 391 | } |
| 392 | |
| 393 | /** |
| 394 | * @deprecated since 1.37, use getPage() instead. |
| 395 | * @return Title |
| 396 | */ |
| 397 | public function getTitle() { |
| 398 | $this->mPage = Title::castFromPageReference( $this->getPage() ); |
| 399 | return $this->mPage ?: Title::makeTitle( NS_SPECIAL, 'BadTitle' ); |
| 400 | } |
| 401 | |
| 402 | /** |
| 403 | * @since 1.37 |
| 404 | * @return ?PageReference |
| 405 | */ |
| 406 | public function getPage(): ?PageReference { |
| 407 | if ( !$this->mPage ) { |
| 408 | // NOTE: As per the 1.36 release, we always provide rc_title, |
| 409 | // even in cases where it doesn't really make sense. |
| 410 | // In the future, rc_title may be nullable, or we may use |
| 411 | // empty strings in entries that do not refer to a page. |
| 412 | if ( ( $this->mAttribs['rc_title'] ?? '' ) === '' ) { |
| 413 | return null; |
| 414 | } |
| 415 | |
| 416 | // XXX: We could use rc_cur_id to create a PageIdentityValue, |
| 417 | // at least if it's not a special page. |
| 418 | // However, RecentChangeFactory::createCategorizationRecentChange() puts the ID of the |
| 419 | // categorized page into rc_cur_id, but the title of the category page into rc_title. |
| 420 | $this->mPage = PageReferenceValue::localReference( |
| 421 | (int)$this->mAttribs['rc_namespace'], |
| 422 | $this->mAttribs['rc_title'] |
| 423 | ); |
| 424 | } |
| 425 | |
| 426 | return $this->mPage; |
| 427 | } |
| 428 | |
| 429 | /** |
| 430 | * Get the UserIdentity of the client that performed this change. |
| 431 | * |
| 432 | * @since 1.36 |
| 433 | * |
| 434 | * @return UserIdentity |
| 435 | */ |
| 436 | public function getPerformerIdentity(): UserIdentity { |
| 437 | if ( !$this->mPerformer ) { |
| 438 | $this->mPerformer = $this->getUserIdentityFromAnyId( |
| 439 | $this->mAttribs['rc_user'] ?? null, |
| 440 | $this->mAttribs['rc_user_text'] ?? null, |
| 441 | $this->mAttribs['rc_actor'] ?? null |
| 442 | ); |
| 443 | } |
| 444 | |
| 445 | return $this->mPerformer; |
| 446 | } |
| 447 | |
| 448 | /** |
| 449 | * Writes the data in this object to the database |
| 450 | * |
| 451 | * @deprecated since 1.45, use RecentChangeFactory::insertRecentChange() instead. |
| 452 | * |
| 453 | * @param bool $send self::SEND_FEED or self::SEND_NONE |
| 454 | */ |
| 455 | public function save( $send = self::SEND_FEED ) { |
| 456 | MediaWikiServices::getInstance()->getRecentChangeFactory()->insertRecentChange( $this, $send ); |
| 457 | } |
| 458 | |
| 459 | /** |
| 460 | * Notify all the feeds about the change. |
| 461 | * |
| 462 | * @deprecated since 1.45, use RecentChangeRCFeedNotifier::notifyRCFeeds() instead. |
| 463 | * |
| 464 | * @param array|null $feeds Optional feeds to send to, defaults to $wgRCFeeds |
| 465 | */ |
| 466 | public function notifyRCFeeds( ?array $feeds = null ) { |
| 467 | MediaWikiServices::getInstance()->getRecentChangeRCFeedNotifier()->notifyRCFeeds( $this, $feeds ); |
| 468 | } |
| 469 | |
| 470 | /** |
| 471 | * Mark this RecentChange as patrolled |
| 472 | * |
| 473 | * NOTE: Can also return 'rcpatroldisabled', 'hookaborted' and |
| 474 | * 'markedaspatrollederror-noautopatrol' as errors |
| 475 | * |
| 476 | * @deprecated since 1.45, use PatrolManager::markPatrolled() instead. |
| 477 | * |
| 478 | * @param Authority $performer User performing the action |
| 479 | * @param string|string[]|null $tags Change tags to add to the patrol log entry |
| 480 | * ($user should be able to add the specified tags before this is called) |
| 481 | * @return PermissionStatus |
| 482 | */ |
| 483 | public function markPatrolled( Authority $performer, $tags = null ): PermissionStatus { |
| 484 | return MediaWikiServices::getInstance() |
| 485 | ->getPatrolManager() |
| 486 | ->markPatrolled( $this, $performer, $tags ); |
| 487 | } |
| 488 | |
| 489 | /** |
| 490 | * Mark this RecentChange patrolled, without error checking |
| 491 | * |
| 492 | * @deprecated since 1.45, use PatrolManager::reallyMarkPatrolled() instead. |
| 493 | * |
| 494 | * @return int Number of database rows changed, usually 1, but 0 if |
| 495 | * another request already patrolled it in the mean time. |
| 496 | */ |
| 497 | public function reallyMarkPatrolled() { |
| 498 | return MediaWikiServices::getInstance() |
| 499 | ->getPatrolManager() |
| 500 | ->reallyMarkPatrolled( $this ); |
| 501 | } |
| 502 | |
| 503 | /** |
| 504 | * Makes an entry in the database corresponding to an edit |
| 505 | * |
| 506 | * @since 1.36 Added $editResult parameter |
| 507 | * @deprecated since 1.45, use RecentChangeFactory::createEditRecentChange() instead to create |
| 508 | * the log entry, then use RecentChangeFactory::insertRecentChange() to insert it into the database. |
| 509 | * |
| 510 | * @param string $timestamp |
| 511 | * @param PageIdentity $page |
| 512 | * @param bool $minor |
| 513 | * @param UserIdentity $user |
| 514 | * @param string $comment |
| 515 | * @param int $oldId |
| 516 | * @param string $lastTimestamp |
| 517 | * @param bool $bot |
| 518 | * @param string $ip |
| 519 | * @param int $oldSize |
| 520 | * @param int $newSize |
| 521 | * @param int $newId |
| 522 | * @param int $patrol |
| 523 | * @param string[] $tags |
| 524 | * @param EditResult|null $editResult EditResult associated with this edit. Can be safely |
| 525 | * skipped if the edit is not a revert. Used only for marking revert tags. |
| 526 | * |
| 527 | * @return RecentChange |
| 528 | */ |
| 529 | public static function notifyEdit( |
| 530 | $timestamp, $page, $minor, $user, $comment, $oldId, $lastTimestamp, |
| 531 | $bot, $ip = '', $oldSize = 0, $newSize = 0, $newId = 0, $patrol = 0, |
| 532 | $tags = [], ?EditResult $editResult = null |
| 533 | ) { |
| 534 | $rc = MediaWikiServices::getInstance()->getRecentChangeFactory() |
| 535 | ->createEditRecentChange( |
| 536 | $timestamp, $page, $minor, $user, $comment, $oldId, |
| 537 | $bot, $ip, $oldSize, $newSize, $newId, $patrol, $tags, $editResult |
| 538 | ); |
| 539 | $rc->save(); |
| 540 | return $rc; |
| 541 | } |
| 542 | |
| 543 | /** |
| 544 | * Makes an entry in the database corresponding to page creation |
| 545 | * @note $page must reflect the state of the database after the page creation. In particular, |
| 546 | * $page->getId() must return the newly assigned page ID. |
| 547 | * |
| 548 | * @deprecated since 1.45, use RecentChangeFactory::createNewPageRecentChange() instead to create |
| 549 | * the log entry, then use RecentChangeFactory::insertRecentChange() to insert it into the database. |
| 550 | * |
| 551 | * @param string $timestamp |
| 552 | * @param PageIdentity $page |
| 553 | * @param bool $minor |
| 554 | * @param UserIdentity $user |
| 555 | * @param string $comment |
| 556 | * @param bool $bot |
| 557 | * @param string $ip |
| 558 | * @param int $size |
| 559 | * @param int $newId |
| 560 | * @param int $patrol |
| 561 | * @param string[] $tags |
| 562 | * |
| 563 | * @return RecentChange |
| 564 | */ |
| 565 | public static function notifyNew( |
| 566 | $timestamp, |
| 567 | $page, $minor, $user, $comment, $bot, |
| 568 | $ip = '', $size = 0, $newId = 0, $patrol = 0, $tags = [] |
| 569 | ) { |
| 570 | $rc = MediaWikiServices::getInstance()->getRecentChangeFactory() |
| 571 | ->createNewPageRecentChange( |
| 572 | $timestamp, $page, $minor, $user, $comment, $bot, |
| 573 | $ip, $size, $newId, $patrol, $tags |
| 574 | ); |
| 575 | $rc->save(); |
| 576 | return $rc; |
| 577 | } |
| 578 | |
| 579 | /** |
| 580 | * @deprecated since 1.45, use RecentChangeFactory::createLogRecentChange() instead |
| 581 | * |
| 582 | * @param string $timestamp |
| 583 | * @param PageReference $logPage |
| 584 | * @param UserIdentity $user |
| 585 | * @param string $actionComment |
| 586 | * @param string $ip |
| 587 | * @param string $type |
| 588 | * @param string $action |
| 589 | * @param PageReference $target |
| 590 | * @param string $logComment |
| 591 | * @param string $params |
| 592 | * @param int $newId |
| 593 | * @param string $actionCommentIRC |
| 594 | * @param int $revId Id of associated revision, if any |
| 595 | * @param bool $isPatrollable Whether this log entry is patrollable |
| 596 | * @param bool|null $forceBotFlag Override the default behavior and set bot flag to |
| 597 | * the value of the argument. When omitted or null, it falls back to the global state. |
| 598 | * |
| 599 | * @return RecentChange |
| 600 | */ |
| 601 | public static function newLogEntry( $timestamp, |
| 602 | $logPage, $user, $actionComment, $ip, |
| 603 | $type, $action, $target, $logComment, $params, $newId = 0, $actionCommentIRC = '', |
| 604 | $revId = 0, $isPatrollable = false, $forceBotFlag = null |
| 605 | ) { |
| 606 | return MediaWikiServices::getInstance()->getRecentChangeFactory() |
| 607 | ->createLogRecentChange( |
| 608 | $timestamp, $logPage, $user, $actionComment, $ip, |
| 609 | $type, $action, $target, $logComment, $params, $newId, $actionCommentIRC, |
| 610 | $revId, $isPatrollable, $forceBotFlag |
| 611 | ); |
| 612 | } |
| 613 | |
| 614 | /** |
| 615 | * Get a parameter value |
| 616 | * |
| 617 | * @since 1.27 |
| 618 | * |
| 619 | * @param string $name parameter name |
| 620 | * @return mixed |
| 621 | */ |
| 622 | public function getParam( $name ) { |
| 623 | $params = $this->parseParams(); |
| 624 | return $params[$name] ?? null; |
| 625 | } |
| 626 | |
| 627 | /** |
| 628 | * Initialises the members of this object from a mysql row object |
| 629 | * |
| 630 | * @param mixed $row |
| 631 | */ |
| 632 | public function loadFromRow( $row ) { |
| 633 | $this->mAttribs = get_object_vars( $row ); |
| 634 | $this->mAttribs['rc_timestamp'] = wfTimestamp( TS::MW, $this->mAttribs['rc_timestamp'] ); |
| 635 | // rc_deleted MUST be set |
| 636 | $this->mAttribs['rc_deleted'] = $row->rc_deleted; |
| 637 | |
| 638 | $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 639 | $comment = MediaWikiServices::getInstance()->getCommentStore() |
| 640 | // Legacy because $row may have come from self::selectFields() |
| 641 | ->getCommentLegacy( $dbr, 'rc_comment', $row, true ) |
| 642 | ->text; |
| 643 | $this->mAttribs['rc_comment'] = &$comment; |
| 644 | $this->mAttribs['rc_comment_text'] = &$comment; |
| 645 | $this->mAttribs['rc_comment_data'] = null; |
| 646 | |
| 647 | $this->mPerformer = $this->getUserIdentityFromAnyId( |
| 648 | $row->rc_user ?? null, |
| 649 | $row->rc_user_text ?? null, |
| 650 | $row->rc_actor ?? null |
| 651 | ); |
| 652 | $this->mAttribs['rc_user'] = $this->mPerformer->getId(); |
| 653 | $this->mAttribs['rc_user_text'] = $this->mPerformer->getName(); |
| 654 | |
| 655 | // Watchlist expiry. |
| 656 | if ( isset( $row->we_expiry ) && $row->we_expiry ) { |
| 657 | $this->watchlistExpiry = wfTimestamp( TS::MW, $row->we_expiry ); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | /** |
| 662 | * Check if the change is internal (edit, new, log, categorize). External changes are |
| 663 | * those that are defined by external sources, such as extensions. |
| 664 | * |
| 665 | * @since 1.45 |
| 666 | * @return bool |
| 667 | */ |
| 668 | public function isInternal() { |
| 669 | return in_array( $this->getAttribute( 'rc_source' ), self::INTERNAL_SOURCES ); |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * Get an attribute value |
| 674 | * |
| 675 | * @param string $name Attribute name |
| 676 | * @return mixed |
| 677 | */ |
| 678 | public function getAttribute( $name ) { |
| 679 | if ( $name === 'rc_comment' ) { |
| 680 | return MediaWikiServices::getInstance()->getCommentStore() |
| 681 | ->getComment( 'rc_comment', $this->mAttribs, true )->text; |
| 682 | } |
| 683 | |
| 684 | if ( $name === 'rc_user' || $name === 'rc_user_text' || $name === 'rc_actor' ) { |
| 685 | $user = $this->getPerformerIdentity(); |
| 686 | |
| 687 | if ( $name === 'rc_user' ) { |
| 688 | return $user->getId(); |
| 689 | } |
| 690 | if ( $name === 'rc_user_text' ) { |
| 691 | return $user->getName(); |
| 692 | } |
| 693 | if ( $name === 'rc_actor' ) { |
| 694 | // NOTE: rc_actor exists in the database, but application logic should not use it. |
| 695 | wfDeprecatedMsg( 'Accessing deprecated field rc_actor', '1.36' ); |
| 696 | $actorStore = MediaWikiServices::getInstance()->getActorStore(); |
| 697 | $db = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 698 | return $actorStore->findActorId( $user, $db ); |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | return $this->mAttribs[$name] ?? null; |
| 703 | } |
| 704 | |
| 705 | /** |
| 706 | * @return array |
| 707 | */ |
| 708 | public function getAttributes() { |
| 709 | return $this->mAttribs; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * @internal |
| 714 | * |
| 715 | * @param string $name |
| 716 | * @return mixed |
| 717 | */ |
| 718 | public function getExtra( $name ) { |
| 719 | return $this->mExtra[$name] ?? null; |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * @internal |
| 724 | * |
| 725 | * @return array |
| 726 | */ |
| 727 | public function getExtras() { |
| 728 | return $this->mExtra; |
| 729 | } |
| 730 | |
| 731 | /** |
| 732 | * Gets the end part of the diff URL associated with this object |
| 733 | * Blank if no diff link should be displayed |
| 734 | * @param bool $forceCur |
| 735 | * @return string |
| 736 | */ |
| 737 | public function diffLinkTrail( $forceCur ) { |
| 738 | if ( $this->mAttribs['rc_source'] == self::SRC_EDIT ) { |
| 739 | $trail = "curid=" . (int)( $this->mAttribs['rc_cur_id'] ) . |
| 740 | "&oldid=" . (int)( $this->mAttribs['rc_last_oldid'] ); |
| 741 | if ( $forceCur ) { |
| 742 | $trail .= '&diff=0'; |
| 743 | } else { |
| 744 | $trail .= '&diff=' . (int)( $this->mAttribs['rc_this_oldid'] ); |
| 745 | } |
| 746 | } else { |
| 747 | $trail = ''; |
| 748 | } |
| 749 | |
| 750 | return $trail; |
| 751 | } |
| 752 | |
| 753 | /** |
| 754 | * Returns the change size (HTML). |
| 755 | * The lengths can be given optionally. |
| 756 | * @param int $old |
| 757 | * @param int $new |
| 758 | * @return string |
| 759 | */ |
| 760 | public function getCharacterDifference( $old = 0, $new = 0 ) { |
| 761 | if ( $old === 0 ) { |
| 762 | $old = $this->mAttribs['rc_old_len']; |
| 763 | } |
| 764 | if ( $new === 0 ) { |
| 765 | $new = $this->mAttribs['rc_new_len']; |
| 766 | } |
| 767 | if ( $old === null || $new === null ) { |
| 768 | return ''; |
| 769 | } |
| 770 | |
| 771 | return ChangesList::showCharacterDifference( $old, $new ); |
| 772 | } |
| 773 | |
| 774 | /** |
| 775 | * Check whether the given timestamp is new enough to have a RC row with a given tolerance |
| 776 | * as the recentchanges table might not be cleared out regularly (so older entries might exist) |
| 777 | * or rows which will be deleted soon shouldn't be included. |
| 778 | * |
| 779 | * @param mixed $timestamp MWTimestamp compatible timestamp |
| 780 | * @param int $tolerance Tolerance in seconds |
| 781 | * @return bool |
| 782 | */ |
| 783 | public static function isInRCLifespan( $timestamp, $tolerance = 0 ) { |
| 784 | $rcMaxAge = |
| 785 | MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::RCMaxAge ); |
| 786 | |
| 787 | return (int)wfTimestamp( TS::UNIX, $timestamp ) > time() - $tolerance - $rcMaxAge; |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Parses and returns the rc_params attribute |
| 792 | * |
| 793 | * @since 1.26 |
| 794 | * @return mixed|bool false on failed unserialization |
| 795 | */ |
| 796 | public function parseParams() { |
| 797 | $rcParams = $this->getAttribute( 'rc_params' ); |
| 798 | |
| 799 | // phpcs:ignore Generic.PHP.NoSilencedErrors.Discouraged |
| 800 | return @unserialize( $rcParams ); |
| 801 | } |
| 802 | |
| 803 | /** |
| 804 | * @internal |
| 805 | * |
| 806 | * @return string[] |
| 807 | */ |
| 808 | public function getTags(): array { |
| 809 | return $this->tags; |
| 810 | } |
| 811 | |
| 812 | /** |
| 813 | * Tags to append to the recent change, |
| 814 | * and associated revision/log |
| 815 | * |
| 816 | * @since 1.28 |
| 817 | * |
| 818 | * @param string|string[] $tags |
| 819 | */ |
| 820 | public function addTags( $tags ) { |
| 821 | if ( is_string( $tags ) ) { |
| 822 | $this->tags[] = $tags; |
| 823 | } else { |
| 824 | $this->tags = array_merge( $tags, $this->tags ); |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | /** |
| 829 | * @internal |
| 830 | * |
| 831 | * @return EditResult|null |
| 832 | */ |
| 833 | public function getEditResult(): ?EditResult { |
| 834 | return $this->editResult; |
| 835 | } |
| 836 | |
| 837 | /** |
| 838 | * Sets the EditResult associated with the edit. |
| 839 | * |
| 840 | * @since 1.36 |
| 841 | * |
| 842 | * @param EditResult|null $editResult |
| 843 | */ |
| 844 | public function setEditResult( ?EditResult $editResult ) { |
| 845 | $this->editResult = $editResult; |
| 846 | } |
| 847 | |
| 848 | /** |
| 849 | * @param string|int|null $userId |
| 850 | * @param string|null $userName |
| 851 | * @param string|int|null $actorId |
| 852 | * |
| 853 | * @return UserIdentity |
| 854 | */ |
| 855 | private function getUserIdentityFromAnyId( |
| 856 | $userId, |
| 857 | $userName, |
| 858 | $actorId = null |
| 859 | ): UserIdentity { |
| 860 | // XXX: Is this logic needed elsewhere? Should it be reusable? |
| 861 | |
| 862 | $userId = $userId !== null ? (int)$userId : null; |
| 863 | $actorId = $actorId !== null ? (int)$actorId : 0; |
| 864 | |
| 865 | $actorStore = MediaWikiServices::getInstance()->getActorStore(); |
| 866 | if ( $userName && $actorId ) { |
| 867 | // Likely the fields are coming from a join on actor table, |
| 868 | // so can definitely build a UserIdentityValue. |
| 869 | return $actorStore->newActorFromRowFields( $userId, $userName, $actorId ); |
| 870 | } |
| 871 | if ( $userId !== null ) { |
| 872 | if ( $userName !== null ) { |
| 873 | // NOTE: For IPs and external users, $userId will be 0. |
| 874 | $user = new UserIdentityValue( $userId, $userName ); |
| 875 | } else { |
| 876 | $user = $actorStore->getUserIdentityByUserId( $userId ); |
| 877 | |
| 878 | if ( !$user ) { |
| 879 | throw new RuntimeException( "User not found by ID: $userId" ); |
| 880 | } |
| 881 | } |
| 882 | } elseif ( $actorId > 0 ) { |
| 883 | $db = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 884 | $user = $actorStore->getActorById( $actorId, $db ); |
| 885 | |
| 886 | if ( !$user ) { |
| 887 | throw new RuntimeException( "User not found by actor ID: $actorId" ); |
| 888 | } |
| 889 | } elseif ( $userName !== null ) { |
| 890 | $user = $actorStore->getUserIdentityByName( $userName ); |
| 891 | |
| 892 | if ( !$user ) { |
| 893 | throw new RuntimeException( "User not found by name: $userName" ); |
| 894 | } |
| 895 | } else { |
| 896 | throw new RuntimeException( 'At least one of user ID, actor ID or user name must be given' ); |
| 897 | } |
| 898 | |
| 899 | return $user; |
| 900 | } |
| 901 | |
| 902 | /** |
| 903 | * @param array<string,bool> $highlights |
| 904 | */ |
| 905 | public function setHighlights( array $highlights ) { |
| 906 | $this->highlights = $highlights; |
| 907 | } |
| 908 | |
| 909 | /** |
| 910 | * @return array<string,bool> |
| 911 | */ |
| 912 | public function getHighlights() { |
| 913 | return $this->highlights; |
| 914 | } |
| 915 | |
| 916 | public function isHighlighted( string $key ): bool { |
| 917 | return $this->highlights[$key] ?? false; |
| 918 | } |
| 919 | } |
| 920 | |
| 921 | /** @deprecated class alias since 1.44 */ |
| 922 | class_alias( RecentChange::class, 'RecentChange' ); |