| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
use MediaWiki\Cache\BacklinkCache; |
| 22 |
use MediaWiki\MediaWikiServices; |
| 23 |
use MediaWiki\Page\PageIdentity; |
| 24 |
use MediaWiki\Revision\RevisionRecord; |
| 25 |
use MediaWiki\Title\Title; |
| 26 |
use MediaWiki\User\User; |
| 27 |
use MediaWiki\User\UserIdentity; |
| 28 |
use Wikimedia\Rdbms\IDBAccessObject; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class CategoryMembershipChange { |
| 39 |
|
| 40 |
private const CATEGORY_ADDITION = 1; |
| 41 |
private const CATEGORY_REMOVAL = -1; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private $timestamp; |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
private $pageTitle; |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
private $revision; |
| 57 |
|
| 58 |
|
| 59 |
private $forImport; |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private $numTemplateLinks = 0; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
private $newForCategorizationCallback = null; |
| 72 |
|
| 73 |
|
| 74 |
private $backlinkCache; |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public function __construct( |
| 83 |
Title $pageTitle, BacklinkCache $backlinkCache, ?RevisionRecord $revision = null, bool $forImport = false |
| 84 |
) { |
| 85 |
|
| 86 |
$this->pageTitle = $pageTitle; |
| 87 |
$this->revision = $revision; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
if ( $revision === null || $forImport ) { |
| 94 |
$this->timestamp = wfTimestampNow(); |
| 95 |
} else { |
| 96 |
$this->timestamp = $revision->getTimestamp(); |
| 97 |
} |
| 98 |
$this->newForCategorizationCallback = [ RecentChange::class, 'newForCategorization' ]; |
| 99 |
$this->backlinkCache = $backlinkCache; |
| 100 |
$this->forImport = $forImport; |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
public function overrideNewForCategorizationCallback( callable $callback ) { |
| 111 |
if ( !defined( 'MW_PHPUNIT_TEST' ) ) { |
| 112 |
throw new LogicException( 'Cannot override newForCategorization callback in operation.' ); |
| 113 |
} |
| 114 |
$this->newForCategorizationCallback = $callback; |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
public function checkTemplateLinks() { |
| 121 |
$this->numTemplateLinks = $this->backlinkCache->getNumLinks( 'templatelinks' ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function triggerCategoryAddedNotification( PageIdentity $categoryPage ) { |
| 130 |
$this->createRecentChangesEntry( $categoryPage, self::CATEGORY_ADDITION ); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public function triggerCategoryRemovedNotification( PageIdentity $categoryPage ) { |
| 139 |
$this->createRecentChangesEntry( $categoryPage, self::CATEGORY_REMOVAL ); |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
private function createRecentChangesEntry( PageIdentity $categoryPage, $type ) { |
| 149 |
$this->notifyCategorization( |
| 150 |
$this->timestamp, |
| 151 |
$categoryPage, |
| 152 |
$this->getUser(), |
| 153 |
$this->getChangeMessageText( |
| 154 |
$type, |
| 155 |
$this->pageTitle->getPrefixedText(), |
| 156 |
$this->numTemplateLinks |
| 157 |
), |
| 158 |
$this->pageTitle, |
| 159 |
$this->getPreviousRevisionTimestamp(), |
| 160 |
$this->revision, |
| 161 |
$this->forImport, |
| 162 |
$type === self::CATEGORY_ADDITION |
| 163 |
); |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
private function notifyCategorization( |
| 178 |
$timestamp, |
| 179 |
PageIdentity $categoryPage, |
| 180 |
?UserIdentity $user, |
| 181 |
$comment, |
| 182 |
PageIdentity $page, |
| 183 |
$lastTimestamp, |
| 184 |
$revision, |
| 185 |
bool $forImport, |
| 186 |
$added |
| 187 |
) { |
| 188 |
$deleted = $revision ? $revision->getVisibility() & RevisionRecord::SUPPRESSED_USER : 0; |
| 189 |
$newRevId = $revision ? $revision->getId() : 0; |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
$bot = 1; |
| 197 |
$lastRevId = 0; |
| 198 |
$ip = ''; |
| 199 |
|
| 200 |
|
| 201 |
if ( $revision !== null ) { |
| 202 |
$revisionStore = MediaWikiServices::getInstance()->getRevisionStore(); |
| 203 |
|
| 204 |
$correspondingRc = $revisionStore->getRecentChange( $this->revision ) ?? |
| 205 |
$revisionStore->getRecentChange( $this->revision, IDBAccessObject::READ_LATEST ); |
| 206 |
if ( $correspondingRc !== null ) { |
| 207 |
$bot = $correspondingRc->getAttribute( 'rc_bot' ) ?: 0; |
| 208 |
$ip = $correspondingRc->getAttribute( 'rc_ip' ) ?: ''; |
| 209 |
$lastRevId = $correspondingRc->getAttribute( 'rc_last_oldid' ) ?: 0; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
$rc = ( $this->newForCategorizationCallback )( |
| 215 |
$timestamp, |
| 216 |
$categoryPage, |
| 217 |
$user, |
| 218 |
$comment, |
| 219 |
$page, |
| 220 |
$lastRevId, |
| 221 |
$newRevId, |
| 222 |
$lastTimestamp, |
| 223 |
$bot, |
| 224 |
$ip, |
| 225 |
$deleted, |
| 226 |
$added, |
| 227 |
$forImport |
| 228 |
); |
| 229 |
$rc->save(); |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
private function getUser(): ?UserIdentity { |
| 244 |
if ( $this->revision ) { |
| 245 |
$user = $this->revision->getUser( RevisionRecord::RAW ); |
| 246 |
if ( $user ) { |
| 247 |
return $user; |
| 248 |
} |
| 249 |
} |
| 250 |
|
| 251 |
$username = wfMessage( 'autochange-username' )->inContentLanguage()->text(); |
| 252 |
|
| 253 |
$user = User::newSystemUser( $username ); |
| 254 |
if ( $user && !$user->isRegistered() ) { |
| 255 |
$user->addToDatabase(); |
| 256 |
} |
| 257 |
|
| 258 |
return $user ?: null; |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) { |
| 278 |
$array = [ |
| 279 |
self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category', |
| 280 |
self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category', |
| 281 |
]; |
| 282 |
|
| 283 |
$msgKey = $array[$type]; |
| 284 |
|
| 285 |
if ( intval( $numTemplateLinks ) > 0 ) { |
| 286 |
$msgKey .= '-bundled'; |
| 287 |
} |
| 288 |
|
| 289 |
return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text(); |
| 290 |
} |
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
private function getPreviousRevisionTimestamp() { |
| 299 |
$rl = MediaWikiServices::getInstance()->getRevisionLookup(); |
| 300 |
$latestRev = $rl->getRevisionByTitle( $this->pageTitle ); |
| 301 |
if ( $latestRev ) { |
| 302 |
$previousRev = $rl->getPreviousRevision( $latestRev ); |
| 303 |
if ( $previousRev ) { |
| 304 |
return $previousRev->getTimestamp(); |
| 305 |
} |
| 306 |
} |
| 307 |
return null; |
| 308 |
} |
| 309 |
|
| 310 |
} |