3 use MediaWiki\MediaWikiServices
;
4 use MediaWiki\Revision\RevisionRecord
;
7 * Helper class for category membership changes
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
30 class CategoryMembershipChange
{
32 const CATEGORY_ADDITION
= 1;
33 const CATEGORY_REMOVAL
= -1;
36 * @var string Current timestamp, set during CategoryMembershipChange::__construct()
41 * @var Title Title instance of the categorized page
46 * @var Revision|null Latest Revision instance of the categorized page
52 * Number of pages this WikiPage is embedded by
53 * Set by CategoryMembershipChange::checkTemplateLinks()
55 private $numTemplateLinks = 0;
60 private $newForCategorizationCallback = null;
63 * @param Title $pageTitle Title instance of the categorized page
64 * @param Revision|null $revision Latest Revision instance of the categorized page
68 public function __construct( Title
$pageTitle, Revision
$revision = null ) {
69 $this->pageTitle
= $pageTitle;
70 if ( $revision === null ) {
71 $this->timestamp
= wfTimestampNow();
73 $this->timestamp
= $revision->getTimestamp();
75 $this->revision
= $revision;
76 $this->newForCategorizationCallback
= [ RecentChange
::class, 'newForCategorization' ];
80 * Overrides the default new for categorization callback
81 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
83 * @param callable $callback
84 * @see RecentChange::newForCategorization for callback signiture
88 public function overrideNewForCategorizationCallback( callable
$callback ) {
89 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
90 throw new MWException( 'Cannot override newForCategorization callback in operation.' );
92 $this->newForCategorizationCallback
= $callback;
96 * Determines the number of template links for recursive link updates
98 public function checkTemplateLinks() {
99 $this->numTemplateLinks
= $this->pageTitle
->getBacklinkCache()->getNumLinks( 'templatelinks' );
103 * Create a recentchanges entry for category additions
105 * @param Title $categoryTitle
107 public function triggerCategoryAddedNotification( Title
$categoryTitle ) {
108 $this->createRecentChangesEntry( $categoryTitle, self
::CATEGORY_ADDITION
);
112 * Create a recentchanges entry for category removals
114 * @param Title $categoryTitle
116 public function triggerCategoryRemovedNotification( Title
$categoryTitle ) {
117 $this->createRecentChangesEntry( $categoryTitle, self
::CATEGORY_REMOVAL
);
121 * Create a recentchanges entry using RecentChange::notifyCategorization()
123 * @param Title $categoryTitle
126 private function createRecentChangesEntry( Title
$categoryTitle, $type ) {
127 $this->notifyCategorization(
131 $this->getChangeMessageText(
133 $this->pageTitle
->getPrefixedText(),
134 $this->numTemplateLinks
137 $this->getPreviousRevisionTimestamp(),
139 $type === self
::CATEGORY_ADDITION
144 * @param string $timestamp Timestamp of the recent change to occur in TS_MW format
145 * @param Title $categoryTitle Title of the category a page is being added to or removed from
146 * @param User|null $user User object of the user that made the change
147 * @param string $comment Change summary
148 * @param Title $pageTitle Title of the page that is being added or removed
149 * @param string $lastTimestamp Parent revision timestamp of this change in TS_MW format
150 * @param Revision|null $revision
151 * @param bool $added true, if the category was added, false for removed
153 * @throws MWException
155 private function notifyCategorization(
157 Title
$categoryTitle,
165 $deleted = $revision ?
$revision->getVisibility() & RevisionRecord
::SUPPRESSED_USER
: 0;
166 $newRevId = $revision ?
$revision->getId() : 0;
169 * T109700 - Default bot flag to true when there is no corresponding RC entry
170 * This means all changes caused by parser functions & Lua on reparse are marked as bot
171 * Also in the case no RC entry could be found due to replica DB lag
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
);
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;
190 /** @var RecentChange $rc */
191 $rc = ( $this->newForCategorizationCallback
)(
209 * Get the user associated with this change.
211 * If there is no revision associated with the change and thus no editing user
212 * fallback to a default.
214 * False will be returned if the user name specified in the
215 * 'autochange-username' message is invalid.
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 );
225 return User
::newFromId( $userId );
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();
240 * Returns the change message according to the type of category membership change
242 * The message keys created in this method may be one of:
243 * - recentchanges-page-added-to-category
244 * - recentchanges-page-added-to-category-bundled
245 * - recentchanges-page-removed-from-category
246 * - recentchanges-page-removed-from-category-bundled
248 * @param int $type may be CategoryMembershipChange::CATEGORY_ADDITION
249 * or CategoryMembershipChange::CATEGORY_REMOVAL
250 * @param string $prefixedText result of Title::->getPrefixedText()
251 * @param int $numTemplateLinks
255 private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) {
257 self
::CATEGORY_ADDITION
=> 'recentchanges-page-added-to-category',
258 self
::CATEGORY_REMOVAL
=> 'recentchanges-page-removed-from-category',
261 $msgKey = $array[$type];
263 if ( intval( $numTemplateLinks ) > 0 ) {
264 $msgKey .= '-bundled';
267 return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
271 * Returns the timestamp of the page's previous revision or null if the latest revision
272 * does not refer to a parent revision
274 * @return null|string
276 private function getPreviousRevisionTimestamp() {
277 $rl = MediaWikiServices
::getInstance()->getRevisionLookup();
278 $latestRev = $rl->getRevisionByTitle( $this->pageTitle
);
280 $previousRev = $rl->getPreviousRevision( $latestRev );
281 if ( $previousRev ) {
282 return $previousRev->getTimestamp();