MediaWiki  master
CategoryMembershipChange.php
Go to the documentation of this file.
1 <?php
2 
5 
31 
32  const CATEGORY_ADDITION = 1;
33  const CATEGORY_REMOVAL = -1;
34 
38  private $timestamp;
39 
43  private $pageTitle;
44 
48  private $revision;
49 
55  private $numTemplateLinks = 0;
56 
61 
68  public function __construct( Title $pageTitle, Revision $revision = null ) {
69  $this->pageTitle = $pageTitle;
70  if ( $revision === null ) {
71  $this->timestamp = wfTimestampNow();
72  } else {
73  $this->timestamp = $revision->getTimestamp();
74  }
75  $this->revision = $revision;
76  $this->newForCategorizationCallback = [ RecentChange::class, 'newForCategorization' ];
77  }
78 
88  public function overrideNewForCategorizationCallback( callable $callback ) {
89  if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
90  throw new MWException( 'Cannot override newForCategorization callback in operation.' );
91  }
92  $this->newForCategorizationCallback = $callback;
93  }
94 
98  public function checkTemplateLinks() {
99  $this->numTemplateLinks = $this->pageTitle->getBacklinkCache()->getNumLinks( 'templatelinks' );
100  }
101 
107  public function triggerCategoryAddedNotification( Title $categoryTitle ) {
108  $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_ADDITION );
109  }
110 
116  public function triggerCategoryRemovedNotification( Title $categoryTitle ) {
117  $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_REMOVAL );
118  }
119 
126  private function createRecentChangesEntry( Title $categoryTitle, $type ) {
127  $this->notifyCategorization(
128  $this->timestamp,
129  $categoryTitle,
130  $this->getUser(),
131  $this->getChangeMessageText(
132  $type,
133  $this->pageTitle->getPrefixedText(),
135  ),
136  $this->pageTitle,
139  $type === self::CATEGORY_ADDITION
140  );
141  }
142 
155  private function notifyCategorization(
156  $timestamp,
157  Title $categoryTitle,
158  ?User $user,
159  $comment,
161  $lastTimestamp,
162  $revision,
163  $added
164  ) {
165  $deleted = $revision ? $revision->getVisibility() & RevisionRecord::SUPPRESSED_USER : 0;
166  $newRevId = $revision ? $revision->getId() : 0;
167 
173  $bot = 1;
174  $lastRevId = 0;
175  $ip = '';
176 
177  # If no revision is given, the change was probably triggered by parser functions
178  if ( $revision !== null ) {
179  $correspondingRc = $this->revision->getRecentChange();
180  if ( $correspondingRc === null ) {
181  $correspondingRc = $this->revision->getRecentChange( Revision::READ_LATEST );
182  }
183  if ( $correspondingRc !== null ) {
184  $bot = $correspondingRc->getAttribute( 'rc_bot' ) ?: 0;
185  $ip = $correspondingRc->getAttribute( 'rc_ip' ) ?: '';
186  $lastRevId = $correspondingRc->getAttribute( 'rc_last_oldid' ) ?: 0;
187  }
188  }
189 
192  $timestamp,
193  $categoryTitle,
194  $user,
195  $comment,
196  $pageTitle,
197  $lastRevId,
198  $newRevId,
199  $lastTimestamp,
200  $bot,
201  $ip,
202  $deleted,
203  $added
204  );
205  $rc->save();
206  }
207 
219  private function getUser() {
220  if ( $this->revision ) {
221  $userId = $this->revision->getUser( RevisionRecord::RAW );
222  if ( $userId === 0 ) {
223  return User::newFromName( $this->revision->getUserText( RevisionRecord::RAW ), false );
224  } else {
225  return User::newFromId( $userId );
226  }
227  }
228 
229  $username = wfMessage( 'autochange-username' )->inContentLanguage()->text();
230  $user = User::newFromName( $username );
231  # User::newFromName() can return false on a badly configured wiki.
232  if ( $user && !$user->isLoggedIn() ) {
233  $user->addToDatabase();
234  }
235 
236  return $user;
237  }
238 
255  private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) {
256  $array = [
257  self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category',
258  self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category',
259  ];
260 
261  $msgKey = $array[$type];
262 
263  if ( intval( $numTemplateLinks ) > 0 ) {
264  $msgKey .= '-bundled';
265  }
266 
267  return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
268  }
269 
276  private function getPreviousRevisionTimestamp() {
277  $rl = MediaWikiServices::getInstance()->getRevisionLookup();
278  $latestRev = $rl->getRevisionByTitle( $this->pageTitle );
279  if ( $latestRev ) {
280  $previousRev = $rl->getPreviousRevision( $latestRev );
281  if ( $previousRev ) {
282  return $previousRev->getTimestamp();
283  }
284  }
285  return null;
286  }
287 
288 }
string $timestamp
Current timestamp, set during CategoryMembershipChange::__construct()
getUser()
Get the user associated with this change.
__construct(Title $pageTitle, Revision $revision=null)
getTimestamp()
Definition: Revision.php:798
int $numTemplateLinks
Number of pages this WikiPage is embedded by Set by CategoryMembershipChange::checkTemplateLinks() ...
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
getId()
Get revision ID.
Definition: Revision.php:442
getPreviousRevisionTimestamp()
Returns the timestamp of the page&#39;s previous revision or null if the latest revision does not refer t...
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
notifyCategorization( $timestamp, Title $categoryTitle, ?User $user, $comment, Title $pageTitle, $lastTimestamp, $revision, $added)
overrideNewForCategorizationCallback(callable $callback)
Overrides the default new for categorization callback This is intended for use while testing and will...
createRecentChangesEntry(Title $categoryTitle, $type)
Create a recentchanges entry using RecentChange::notifyCategorization()
Title $pageTitle
Title instance of the categorized page.
getChangeMessageText( $type, $prefixedText, $numTemplateLinks)
Returns the change message according to the type of category membership change.
Revision null $revision
Latest Revision instance of the categorized page.
getVisibility()
Get the deletion bitfield of the revision.
Definition: Revision.php:701
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:539
triggerCategoryRemovedNotification(Title $categoryTitle)
Create a recentchanges entry for category removals.
checkTemplateLinks()
Determines the number of template links for recursive link updates.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:515
triggerCategoryAddedNotification(Title $categoryTitle)
Create a recentchanges entry for category additions.