MediaWiki REL1_37
CategoryMembershipChange.php
Go to the documentation of this file.
1<?php
2
7
33
34 private const CATEGORY_ADDITION = 1;
35 private const CATEGORY_REMOVAL = -1;
36
40 private $timestamp;
41
45 private $pageTitle;
46
50 private $revision;
51
57 private $numTemplateLinks = 0;
58
63
66
74 public function __construct(
76 ) {
77 $this->pageTitle = $pageTitle;
78 $this->revision = $revision;
79 if ( $revision === null ) {
80 $this->timestamp = wfTimestampNow();
81 } else {
82 $this->timestamp = $revision->getTimestamp();
83 }
84 $this->newForCategorizationCallback = [ RecentChange::class, 'newForCategorization' ];
85 $this->backlinkCache = $backlinkCache;
86 }
87
97 public function overrideNewForCategorizationCallback( callable $callback ) {
98 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
99 throw new MWException( 'Cannot override newForCategorization callback in operation.' );
100 }
101 $this->newForCategorizationCallback = $callback;
102 }
103
107 public function checkTemplateLinks() {
108 $this->numTemplateLinks = $this->backlinkCache->getNumLinks( 'templatelinks' );
109 }
110
116 public function triggerCategoryAddedNotification( Title $categoryTitle ) {
117 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_ADDITION );
118 }
119
125 public function triggerCategoryRemovedNotification( Title $categoryTitle ) {
126 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_REMOVAL );
127 }
128
135 private function createRecentChangesEntry( Title $categoryTitle, $type ) {
137 $this->timestamp,
138 $categoryTitle,
139 $this->getUser(),
141 $type,
142 $this->pageTitle->getPrefixedText(),
143 $this->numTemplateLinks
144 ),
145 $this->pageTitle,
146 $this->getPreviousRevisionTimestamp(),
147 $this->revision,
148 $type === self::CATEGORY_ADDITION
149 );
150 }
151
164 private function notifyCategorization(
166 Title $categoryTitle,
167 ?UserIdentity $user,
168 $comment,
170 $lastTimestamp,
171 $revision,
172 $added
173 ) {
174 $deleted = $revision ? $revision->getVisibility() & RevisionRecord::SUPPRESSED_USER : 0;
175 $newRevId = $revision ? $revision->getId() : 0;
176
182 $bot = 1;
183 $lastRevId = 0;
184 $ip = '';
185
186 # If no revision is given, the change was probably triggered by parser functions
187 if ( $revision !== null ) {
188 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
189
190 $correspondingRc = $revisionStore->getRecentChange( $this->revision );
191 if ( $correspondingRc === null ) {
192 $correspondingRc = $revisionStore->getRecentChange(
193 $this->revision,
194 RevisionStore::READ_LATEST
195 );
196 }
197 if ( $correspondingRc !== null ) {
198 $bot = $correspondingRc->getAttribute( 'rc_bot' ) ?: 0;
199 $ip = $correspondingRc->getAttribute( 'rc_ip' ) ?: '';
200 $lastRevId = $correspondingRc->getAttribute( 'rc_last_oldid' ) ?: 0;
201 }
202 }
203
207 $categoryTitle,
208 $user,
209 $comment,
211 $lastRevId,
212 $newRevId,
213 $lastTimestamp,
214 $bot,
215 $ip,
216 $deleted,
217 $added
218 );
219 $rc->save();
220 }
221
233 private function getUser(): ?UserIdentity {
234 if ( $this->revision ) {
235 $user = $this->revision->getUser( RevisionRecord::RAW );
236 if ( $user ) {
237 return $user;
238 }
239 }
240
241 $username = wfMessage( 'autochange-username' )->inContentLanguage()->text();
242
243 // TODO: use User::newSystemUser
244 $user = User::newFromName( $username );
245 # User::newFromName() can return false on a badly configured wiki.
246 if ( $user && !$user->isRegistered() ) {
247 $user->addToDatabase();
248 }
249
250 return $user ?: null;
251 }
252
269 private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) {
270 $array = [
271 self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category',
272 self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category',
273 ];
274
275 $msgKey = $array[$type];
276
277 if ( intval( $numTemplateLinks ) > 0 ) {
278 $msgKey .= '-bundled';
279 }
280
281 return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
282 }
283
290 private function getPreviousRevisionTimestamp() {
291 $rl = MediaWikiServices::getInstance()->getRevisionLookup();
292 $latestRev = $rl->getRevisionByTitle( $this->pageTitle );
293 if ( $latestRev ) {
294 $previousRev = $rl->getPreviousRevision( $latestRev );
295 if ( $previousRev ) {
296 return $previousRev->getTimestamp();
297 }
298 }
299 return null;
300 }
301
302}
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
if(ini_get('mbstring.func_overload')) if(!defined('MW_ENTRY_POINT'))
Pre-config setup: Before loading LocalSettings.php.
Definition Setup.php:88
Class for fetching backlink lists, approximate backlink counts and partitions.
getPreviousRevisionTimestamp()
Returns the timestamp of the page's previous revision or null if the latest revision does not refer t...
notifyCategorization( $timestamp, Title $categoryTitle, ?UserIdentity $user, $comment, Title $pageTitle, $lastTimestamp, $revision, $added)
getChangeMessageText( $type, $prefixedText, $numTemplateLinks)
Returns the change message according to the type of category membership change.
createRecentChangesEntry(Title $categoryTitle, $type)
Create a recentchanges entry using RecentChange::notifyCategorization()
triggerCategoryRemovedNotification(Title $categoryTitle)
Create a recentchanges entry for category removals.
triggerCategoryAddedNotification(Title $categoryTitle)
Create a recentchanges entry for category additions.
int $numTemplateLinks
Number of pages this WikiPage is embedded by Set by CategoryMembershipChange::checkTemplateLinks()
getUser()
Get the user associated with this change.
string $timestamp
Current timestamp, set during CategoryMembershipChange::__construct()
Title $pageTitle
Title instance of the categorized page.
RevisionRecord null $revision
Latest revision of the categorized page.
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)
checkTemplateLinks()
Determines the number of template links for recursive link updates.
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
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.
Service for looking up page revisions.
Represents a title within MediaWiki.
Definition Title.php:48
static newFromName( $name, $validate='valid')
Definition User.php:607
Interface for objects representing user identity.