MediaWiki  master
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 
65  private $backlinkCache;
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 ) {
136  $this->notifyCategorization(
137  $this->timestamp,
138  $categoryTitle,
139  $this->getUser(),
140  $this->getChangeMessageText(
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(
165  $timestamp,
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 
206  $timestamp,
207  $categoryTitle,
208  $user,
209  $comment,
210  $pageTitle,
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 }
CategoryMembershipChange\__construct
__construct(Title $pageTitle, BacklinkCache $backlinkCache, RevisionRecord $revision=null)
Definition: CategoryMembershipChange.php:74
MediaWiki\Revision\RevisionRecord
Page revision base class.
Definition: RevisionRecord.php:47
CategoryMembershipChange\$newForCategorizationCallback
callable null $newForCategorizationCallback
Definition: CategoryMembershipChange.php:62
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:200
CategoryMembershipChange\CATEGORY_ADDITION
const CATEGORY_ADDITION
Definition: CategoryMembershipChange.php:34
MediaWiki\Revision\RevisionStore
Service for looking up page revisions.
Definition: RevisionStore.php:88
CategoryMembershipChange\overrideNewForCategorizationCallback
overrideNewForCategorizationCallback(callable $callback)
Overrides the default new for categorization callback This is intended for use while testing and will...
Definition: CategoryMembershipChange.php:97
CategoryMembershipChange\$pageTitle
Title $pageTitle
Title instance of the categorized page.
Definition: CategoryMembershipChange.php:45
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:595
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1183
BacklinkCache
Class for fetching backlink lists, approximate backlink counts and partitions.
Definition: BacklinkCache.php:50
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
CategoryMembershipChange\$backlinkCache
BacklinkCache $backlinkCache
Definition: CategoryMembershipChange.php:65
CategoryMembershipChange\notifyCategorization
notifyCategorization( $timestamp, Title $categoryTitle, ?UserIdentity $user, $comment, Title $pageTitle, $lastTimestamp, $revision, $added)
Definition: CategoryMembershipChange.php:164
MWException
MediaWiki exception.
Definition: MWException.php:29
CategoryMembershipChange\getChangeMessageText
getChangeMessageText( $type, $prefixedText, $numTemplateLinks)
Returns the change message according to the type of category membership change.
Definition: CategoryMembershipChange.php:269
CategoryMembershipChange\triggerCategoryRemovedNotification
triggerCategoryRemovedNotification(Title $categoryTitle)
Create a recentchanges entry for category removals.
Definition: CategoryMembershipChange.php:125
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1694
CategoryMembershipChange\triggerCategoryAddedNotification
triggerCategoryAddedNotification(Title $categoryTitle)
Create a recentchanges entry for category additions.
Definition: CategoryMembershipChange.php:116
CategoryMembershipChange\$timestamp
string $timestamp
Current timestamp, set during CategoryMembershipChange::__construct()
Definition: CategoryMembershipChange.php:40
CategoryMembershipChange\getPreviousRevisionTimestamp
getPreviousRevisionTimestamp()
Returns the timestamp of the page's previous revision or null if the latest revision does not refer t...
Definition: CategoryMembershipChange.php:290
CategoryMembershipChange\getUser
getUser()
Get the user associated with this change.
Definition: CategoryMembershipChange.php:233
CategoryMembershipChange\createRecentChangesEntry
createRecentChangesEntry(Title $categoryTitle, $type)
Create a recentchanges entry using RecentChange::notifyCategorization()
Definition: CategoryMembershipChange.php:135
Title
Represents a title within MediaWiki.
Definition: Title.php:47
CategoryMembershipChange\CATEGORY_REMOVAL
const CATEGORY_REMOVAL
Definition: CategoryMembershipChange.php:35
MediaWiki\Revision\RevisionRecord\getId
getId( $wikiId=self::LOCAL)
Get revision ID.
Definition: RevisionRecord.php:279
MediaWiki\Revision\RevisionRecord\getTimestamp
getTimestamp()
MCR migration note: this replaced Revision::getTimestamp.
Definition: RevisionRecord.php:459
CategoryMembershipChange
Definition: CategoryMembershipChange.php:32
CategoryMembershipChange\$numTemplateLinks
int $numTemplateLinks
Number of pages this WikiPage is embedded by Set by CategoryMembershipChange::checkTemplateLinks()
Definition: CategoryMembershipChange.php:57
CategoryMembershipChange\$revision
RevisionRecord null $revision
Latest revision of the categorized page.
Definition: CategoryMembershipChange.php:50
CategoryMembershipChange\checkTemplateLinks
checkTemplateLinks()
Determines the number of template links for recursive link updates.
Definition: CategoryMembershipChange.php:107
MediaWiki\Revision\RevisionRecord\getVisibility
getVisibility()
Get the deletion bitfield of the revision.
Definition: RevisionRecord.php:448
$type
$type
Definition: testCompression.php:52