MediaWiki  master
CategoryMembershipChange.php
Go to the documentation of this file.
1 <?php
2 
4 
30 
31  const CATEGORY_ADDITION = 1;
32  const CATEGORY_REMOVAL = -1;
33 
37  private $timestamp;
38 
42  private $pageTitle;
43 
47  private $revision;
48 
54  private $numTemplateLinks = 0;
55 
60 
68  $this->pageTitle = $pageTitle;
69  if ( $revision === null ) {
70  $this->timestamp = wfTimestampNow();
71  } else {
72  $this->timestamp = $revision->getTimestamp();
73  }
74  $this->revision = $revision;
75  $this->newForCategorizationCallback = [ RecentChange::class, 'newForCategorization' ];
76  }
77 
87  public function overrideNewForCategorizationCallback( callable $callback ) {
88  if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
89  throw new MWException( 'Cannot override newForCategorization callback in operation.' );
90  }
91  $this->newForCategorizationCallback = $callback;
92  }
93 
97  public function checkTemplateLinks() {
98  $this->numTemplateLinks = $this->pageTitle->getBacklinkCache()->getNumLinks( 'templatelinks' );
99  }
100 
106  public function triggerCategoryAddedNotification( Title $categoryTitle ) {
107  $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_ADDITION );
108  }
109 
115  public function triggerCategoryRemovedNotification( Title $categoryTitle ) {
116  $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_REMOVAL );
117  }
118 
125  private function createRecentChangesEntry( Title $categoryTitle, $type ) {
126  $this->notifyCategorization(
127  $this->timestamp,
128  $categoryTitle,
129  $this->getUser(),
130  $this->getChangeMessageText(
131  $type,
132  $this->pageTitle->getPrefixedText(),
134  ),
135  $this->pageTitle,
138  $type === self::CATEGORY_ADDITION
139  );
140  }
141 
154  private function notifyCategorization(
155  $timestamp,
156  Title $categoryTitle,
157  User $user = null,
158  $comment,
160  $lastTimestamp,
161  $revision,
162  $added
163  ) {
164  $deleted = $revision ? $revision->getVisibility() & RevisionRecord::SUPPRESSED_USER : 0;
165  $newRevId = $revision ? $revision->getId() : 0;
166 
172  $bot = 1;
173  $lastRevId = 0;
174  $ip = '';
175 
176  # If no revision is given, the change was probably triggered by parser functions
177  if ( $revision !== null ) {
178  $correspondingRc = $this->revision->getRecentChange();
179  if ( $correspondingRc === null ) {
180  $correspondingRc = $this->revision->getRecentChange( Revision::READ_LATEST );
181  }
182  if ( $correspondingRc !== null ) {
183  $bot = $correspondingRc->getAttribute( 'rc_bot' ) ?: 0;
184  $ip = $correspondingRc->getAttribute( 'rc_ip' ) ?: '';
185  $lastRevId = $correspondingRc->getAttribute( 'rc_last_oldid' ) ?: 0;
186  }
187  }
188 
191  $timestamp,
192  $categoryTitle,
193  $user,
194  $comment,
195  $pageTitle,
196  $lastRevId,
197  $newRevId,
198  $lastTimestamp,
199  $bot,
200  $ip,
201  $deleted,
202  $added
203  );
204  $rc->save();
205  }
206 
218  private function getUser() {
219  if ( $this->revision ) {
220  $userId = $this->revision->getUser( RevisionRecord::RAW );
221  if ( $userId === 0 ) {
222  return User::newFromName( $this->revision->getUserText( RevisionRecord::RAW ), false );
223  } else {
224  return User::newFromId( $userId );
225  }
226  }
227 
228  $username = wfMessage( 'autochange-username' )->inContentLanguage()->text();
230  # User::newFromName() can return false on a badly configured wiki.
231  if ( $user && !$user->isLoggedIn() ) {
232  $user->addToDatabase();
233  }
234 
235  return $user;
236  }
237 
254  private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) {
255  $array = [
256  self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category',
257  self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category',
258  ];
259 
260  $msgKey = $array[$type];
261 
262  if ( intval( $numTemplateLinks ) > 0 ) {
263  $msgKey .= '-bundled';
264  }
265 
266  return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
267  }
268 
275  private function getPreviousRevisionTimestamp() {
276  $previousRev = Revision::newFromId(
277  $this->pageTitle->getPreviousRevisionID( $this->pageTitle->getLatestRevID() )
278  );
279 
280  return $previousRev ? $previousRev->getTimestamp() : null;
281  }
282 
283 }
string $timestamp
Current timestamp, set during CategoryMembershipChange::__construct()
getUser()
Get the user associated with this change.
__construct(Title $pageTitle, Revision $revision=null)
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
getTimestamp()
Definition: Revision.php:994
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
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
getId()
Get revision ID.
Definition: Revision.php:638
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.
overrideNewForCategorizationCallback(callable $callback)
Overrides the default new for categorization callback This is intended for use while testing and will...
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:767
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:897
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:767
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:559
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:52
triggerCategoryRemovedNotification(Title $categoryTitle)
Create a recentchanges entry for category removals.
In both all secondary updates will be triggered handle like object that caches derived data representing a revision
Definition: pageupdater.txt:78
checkTemplateLinks()
Determines the number of template links for recursive link updates.
notifyCategorization( $timestamp, Title $categoryTitle, User $user=null, $comment, Title $pageTitle, $lastTimestamp, $revision, $added)
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:535
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:118
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1454
triggerCategoryAddedNotification(Title $categoryTitle)
Create a recentchanges entry for category additions.