MediaWiki  1.33.0
CategoryMembershipChange.php
Go to the documentation of this file.
1 <?php
27 
28  const CATEGORY_ADDITION = 1;
29  const CATEGORY_REMOVAL = -1;
30 
34  private $timestamp;
35 
39  private $pageTitle;
40 
44  private $revision;
45 
51  private $numTemplateLinks = 0;
52 
57 
64  public function __construct( Title $pageTitle, Revision $revision = null ) {
65  $this->pageTitle = $pageTitle;
66  if ( $revision === null ) {
67  $this->timestamp = wfTimestampNow();
68  } else {
69  $this->timestamp = $revision->getTimestamp();
70  }
71  $this->revision = $revision;
72  $this->newForCategorizationCallback = [ RecentChange::class, 'newForCategorization' ];
73  }
74 
84  public function overrideNewForCategorizationCallback( callable $callback ) {
85  if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
86  throw new MWException( 'Cannot override newForCategorization callback in operation.' );
87  }
88  $this->newForCategorizationCallback = $callback;
89  }
90 
94  public function checkTemplateLinks() {
95  $this->numTemplateLinks = $this->pageTitle->getBacklinkCache()->getNumLinks( 'templatelinks' );
96  }
97 
103  public function triggerCategoryAddedNotification( Title $categoryTitle ) {
104  $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_ADDITION );
105  }
106 
112  public function triggerCategoryRemovedNotification( Title $categoryTitle ) {
113  $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_REMOVAL );
114  }
115 
122  private function createRecentChangesEntry( Title $categoryTitle, $type ) {
123  $this->notifyCategorization(
124  $this->timestamp,
125  $categoryTitle,
126  $this->getUser(),
127  $this->getChangeMessageText(
128  $type,
129  $this->pageTitle->getPrefixedText(),
131  ),
132  $this->pageTitle,
136  );
137  }
138 
151  private function notifyCategorization(
152  $timestamp,
153  Title $categoryTitle,
154  User $user = null,
155  $comment,
157  $lastTimestamp,
158  $revision,
159  $added
160  ) {
162  $newRevId = $revision ? $revision->getId() : 0;
163 
169  $bot = 1;
170  $lastRevId = 0;
171  $ip = '';
172 
173  # If no revision is given, the change was probably triggered by parser functions
174  if ( $revision !== null ) {
175  $correspondingRc = $this->revision->getRecentChange();
176  if ( $correspondingRc === null ) {
177  $correspondingRc = $this->revision->getRecentChange( Revision::READ_LATEST );
178  }
179  if ( $correspondingRc !== null ) {
180  $bot = $correspondingRc->getAttribute( 'rc_bot' ) ?: 0;
181  $ip = $correspondingRc->getAttribute( 'rc_ip' ) ?: '';
182  $lastRevId = $correspondingRc->getAttribute( 'rc_last_oldid' ) ?: 0;
183  }
184  }
185 
188  $timestamp,
189  $categoryTitle,
190  $user,
191  $comment,
192  $pageTitle,
193  $lastRevId,
194  $newRevId,
195  $lastTimestamp,
196  $bot,
197  $ip,
198  $deleted,
199  $added
200  );
201  $rc->save();
202  }
203 
215  private function getUser() {
216  if ( $this->revision ) {
217  $userId = $this->revision->getUser( Revision::RAW );
218  if ( $userId === 0 ) {
219  return User::newFromName( $this->revision->getUserText( Revision::RAW ), false );
220  } else {
221  return User::newFromId( $userId );
222  }
223  }
224 
225  $username = wfMessage( 'autochange-username' )->inContentLanguage()->text();
227  # User::newFromName() can return false on a badly configured wiki.
228  if ( $user && !$user->isLoggedIn() ) {
229  $user->addToDatabase();
230  }
231 
232  return $user;
233  }
234 
251  private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) {
252  $array = [
253  self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category',
254  self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category',
255  ];
256 
257  $msgKey = $array[$type];
258 
259  if ( intval( $numTemplateLinks ) > 0 ) {
260  $msgKey .= '-bundled';
261  }
262 
263  return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
264  }
265 
272  private function getPreviousRevisionTimestamp() {
273  $previousRev = Revision::newFromId(
274  $this->pageTitle->getPreviousRevisionID( $this->pageTitle->getLatestRevID() )
275  );
276 
277  return $previousRev ? $previousRev->getTimestamp() : null;
278  }
279 
280 }
Revision\getTimestamp
getTimestamp()
Definition: Revision.php:994
$user
return true to allow those checks to and false if checking is done & $user
Definition: hooks.txt:1476
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:609
false
processing should stop and the error should be shown to the user * false
Definition: hooks.txt:187
Revision\newFromId
static newFromId( $id, $flags=0)
Load a page revision from a given revision ID number.
Definition: Revision.php:118
CategoryMembershipChange\$newForCategorizationCallback
callable null $newForCategorizationCallback
Definition: CategoryMembershipChange.php:56
CategoryMembershipChange\CATEGORY_ADDITION
const CATEGORY_ADDITION
Definition: CategoryMembershipChange.php:28
CategoryMembershipChange\overrideNewForCategorizationCallback
overrideNewForCategorizationCallback(callable $callback)
Overrides the default new for categorization callback This is intended for use while testing and will...
Definition: CategoryMembershipChange.php:84
CategoryMembershipChange\$pageTitle
Title $pageTitle
Title instance of the categorized page.
Definition: CategoryMembershipChange.php:39
User\newFromName
static newFromName( $name, $validate='valid')
Static factory method for creation from username.
Definition: User.php:585
Revision\getId
getId()
Get revision ID.
Definition: Revision.php:638
php
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
Revision
Definition: Revision.php:40
Revision\SUPPRESSED_USER
const SUPPRESSED_USER
Definition: Revision.php:50
MWException
MediaWiki exception.
Definition: MWException.php:26
CategoryMembershipChange\getChangeMessageText
getChangeMessageText( $type, $prefixedText, $numTemplateLinks)
Returns the change message according to the type of category membership change.
Definition: CategoryMembershipChange.php:251
CategoryMembershipChange\triggerCategoryRemovedNotification
triggerCategoryRemovedNotification(Title $categoryTitle)
Create a recentchanges entry for category removals.
Definition: CategoryMembershipChange.php:112
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1941
CategoryMembershipChange\triggerCategoryAddedNotification
triggerCategoryAddedNotification(Title $categoryTitle)
Create a recentchanges entry for category additions.
Definition: CategoryMembershipChange.php:103
CategoryMembershipChange\$timestamp
string $timestamp
Current timestamp, set during CategoryMembershipChange::__construct()
Definition: CategoryMembershipChange.php:34
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:272
CategoryMembershipChange\$revision
Revision null $revision
Latest Revision instance of the categorized page.
Definition: CategoryMembershipChange.php:44
CategoryMembershipChange\getUser
getUser()
Get the user associated with this change.
Definition: CategoryMembershipChange.php:215
CategoryMembershipChange\notifyCategorization
notifyCategorization( $timestamp, Title $categoryTitle, User $user=null, $comment, Title $pageTitle, $lastTimestamp, $revision, $added)
Definition: CategoryMembershipChange.php:151
CategoryMembershipChange\createRecentChangesEntry
createRecentChangesEntry(Title $categoryTitle, $type)
Create a recentchanges entry using RecentChange::notifyCategorization()
Definition: CategoryMembershipChange.php:122
Revision\getVisibility
getVisibility()
Get the deletion bitfield of the revision.
Definition: Revision.php:897
Revision\RAW
const RAW
Definition: Revision.php:56
Title
Represents a title within MediaWiki.
Definition: Title.php:40
CategoryMembershipChange\CATEGORY_REMOVAL
const CATEGORY_REMOVAL
Definition: CategoryMembershipChange.php:29
revision
In both all secondary updates will be triggered handle like object that caches derived data representing a revision
Definition: pageupdater.txt:78
CategoryMembershipChange\__construct
__construct(Title $pageTitle, Revision $revision=null)
Definition: CategoryMembershipChange.php:64
class
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
CategoryMembershipChange
Definition: CategoryMembershipChange.php:26
CategoryMembershipChange\$numTemplateLinks
int $numTemplateLinks
Number of pages this WikiPage is embedded by Set by CategoryMembershipChange::checkTemplateLinks()
Definition: CategoryMembershipChange.php:51
wfMessage
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
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:48
$username
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:780
CategoryMembershipChange\checkTemplateLinks
checkTemplateLinks()
Determines the number of template links for recursive link updates.
Definition: CategoryMembershipChange.php:94
$type
$type
Definition: testCompression.php:48