MediaWiki 1.43.1
CategoryMembershipChange.php
Go to the documentation of this file.
1<?php
29
39
40 private const CATEGORY_ADDITION = 1;
41 private const CATEGORY_REMOVAL = -1;
42
46 private $timestamp;
47
51 private $pageTitle;
52
56 private $revision;
57
59 private $forImport;
60
66 private $numTemplateLinks = 0;
67
71 private $newForCategorizationCallback = null;
72
74 private $backlinkCache;
75
82 public function __construct(
83 Title $pageTitle, BacklinkCache $backlinkCache, ?RevisionRecord $revision = null, bool $forImport = false
84 ) {
85 // TODO: Update callers of this method to pass for import
86 $this->pageTitle = $pageTitle;
87 $this->revision = $revision;
88
89 // Use the current timestamp for creating the RC entry when dealing with imported revisions,
90 // since their timestamp may be significantly older than the current time.
91 // This ensures the resulting RC entry won't be immediately reaped by probabilistic RC purging if
92 // the imported revision is older than $wgRCMaxAge (T377392).
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
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
120 public function checkTemplateLinks() {
121 $this->numTemplateLinks = $this->backlinkCache->getNumLinks( 'templatelinks' );
122 }
123
129 public function triggerCategoryAddedNotification( PageIdentity $categoryPage ) {
130 $this->createRecentChangesEntry( $categoryPage, self::CATEGORY_ADDITION );
131 }
132
138 public function triggerCategoryRemovedNotification( PageIdentity $categoryPage ) {
139 $this->createRecentChangesEntry( $categoryPage, self::CATEGORY_REMOVAL );
140 }
141
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
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
196 $bot = 1;
197 $lastRevId = 0;
198 $ip = '';
199
200 # If no revision is given, the change was probably triggered by parser functions
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
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
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
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
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}
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:81
Helper class for category membership changes.
triggerCategoryAddedNotification(PageIdentity $categoryPage)
Create a recentchanges entry for category additions.
overrideNewForCategorizationCallback(callable $callback)
Overrides the default new for categorization callback This is intended for use while testing and will...
__construct(Title $pageTitle, BacklinkCache $backlinkCache, ?RevisionRecord $revision=null, bool $forImport=false)
checkTemplateLinks()
Determines the number of template links for recursive link updates.
triggerCategoryRemovedNotification(PageIdentity $categoryPage)
Create a recentchanges entry for category removals.
Class for fetching backlink lists, approximate backlink counts and partitions.
Service locator for MediaWiki core services.
Page revision base class.
getVisibility()
Get the deletion bitfield of the revision.
getTimestamp()
MCR migration note: this replaced Revision::getTimestamp.
getId( $wikiId=self::LOCAL)
Get revision ID.
Represents a title within MediaWiki.
Definition Title.php:78
internal since 1.36
Definition User.php:94
Interface for objects (potentially) representing an editable wiki page.
Interface for objects representing user identity.
isRegistered()
This must be equivalent to getId() != 0 and is provided for code readability.
Interface for database access objects.