3 * Helper class for category membership changes
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 class CategoryMembershipChange
{
28 const CATEGORY_ADDITION
= 1;
29 const CATEGORY_REMOVAL
= -1;
32 * @var string Current timestamp, set during CategoryMembershipChange::__construct()
37 * @var Title Title instance of the categorized page
42 * @var Revision|null Latest Revision instance of the categorized page
48 * Number of pages this WikiPage is embedded by
49 * Set by CategoryMembershipChange::checkTemplateLinks()
51 private $numTemplateLinks = 0;
56 private $newForCategorizationCallback = null;
59 * @param Title $pageTitle Title instance of the categorized page
60 * @param Revision|null $revision Latest Revision instance of the categorized page
64 public function __construct( Title
$pageTitle, Revision
$revision = null ) {
65 $this->pageTitle
= $pageTitle;
66 if ( $revision === null ) {
67 $this->timestamp
= wfTimestampNow();
69 $this->timestamp
= $revision->getTimestamp();
71 $this->revision
= $revision;
72 $this->newForCategorizationCallback
= [ RecentChange
::class, 'newForCategorization' ];
76 * Overrides the default new for categorization callback
77 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
79 * @param callable $callback
80 * @see RecentChange::newForCategorization for callback signiture
84 public function overrideNewForCategorizationCallback( callable
$callback ) {
85 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
86 throw new MWException( 'Cannot override newForCategorization callback in operation.' );
88 $this->newForCategorizationCallback
= $callback;
92 * Determines the number of template links for recursive link updates
94 public function checkTemplateLinks() {
95 $this->numTemplateLinks
= $this->pageTitle
->getBacklinkCache()->getNumLinks( 'templatelinks' );
99 * Create a recentchanges entry for category additions
101 * @param Title $categoryTitle
103 public function triggerCategoryAddedNotification( Title
$categoryTitle ) {
104 $this->createRecentChangesEntry( $categoryTitle, self
::CATEGORY_ADDITION
);
108 * Create a recentchanges entry for category removals
110 * @param Title $categoryTitle
112 public function triggerCategoryRemovedNotification( Title
$categoryTitle ) {
113 $this->createRecentChangesEntry( $categoryTitle, self
::CATEGORY_REMOVAL
);
117 * Create a recentchanges entry using RecentChange::notifyCategorization()
119 * @param Title $categoryTitle
122 private function createRecentChangesEntry( Title
$categoryTitle, $type ) {
123 $this->notifyCategorization(
127 $this->getChangeMessageText(
129 $this->pageTitle
->getPrefixedText(),
130 $this->numTemplateLinks
133 $this->getPreviousRevisionTimestamp(),
135 $type === self
::CATEGORY_ADDITION
140 * @param string $timestamp Timestamp of the recent change to occur in TS_MW format
141 * @param Title $categoryTitle Title of the category a page is being added to or removed from
142 * @param User|null $user User object of the user that made the change
143 * @param string $comment Change summary
144 * @param Title $pageTitle Title of the page that is being added or removed
145 * @param string $lastTimestamp Parent revision timestamp of this change in TS_MW format
146 * @param Revision|null $revision
147 * @param bool $added true, if the category was added, false for removed
149 * @throws MWException
151 private function notifyCategorization(
153 Title
$categoryTitle,
161 $deleted = $revision ?
$revision->getVisibility() & Revision
::SUPPRESSED_USER
: 0;
162 $newRevId = $revision ?
$revision->getId() : 0;
165 * T109700 - Default bot flag to true when there is no corresponding RC entry
166 * This means all changes caused by parser functions & Lua on reparse are marked as bot
167 * Also in the case no RC entry could be found due to replica DB lag
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
);
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;
186 /** @var RecentChange $rc */
187 $rc = ( $this->newForCategorizationCallback
)(
205 * Get the user associated with this change.
207 * If there is no revision associated with the change and thus no editing user
208 * fallback to a default.
210 * False will be returned if the user name specified in the
211 * 'autochange-username' message is invalid.
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 );
221 return User
::newFromId( $userId );
225 $username = wfMessage( 'autochange-username' )->inContentLanguage()->text();
226 $user = User
::newFromName( $username );
227 # User::newFromName() can return false on a badly configured wiki.
228 if ( $user && !$user->isLoggedIn() ) {
229 $user->addToDatabase();
236 * Returns the change message according to the type of category membership change
238 * The message keys created in this method may be one of:
239 * - recentchanges-page-added-to-category
240 * - recentchanges-page-added-to-category-bundled
241 * - recentchanges-page-removed-from-category
242 * - recentchanges-page-removed-from-category-bundled
244 * @param int $type may be CategoryMembershipChange::CATEGORY_ADDITION
245 * or CategoryMembershipChange::CATEGORY_REMOVAL
246 * @param string $prefixedText result of Title::->getPrefixedText()
247 * @param int $numTemplateLinks
251 private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) {
253 self
::CATEGORY_ADDITION
=> 'recentchanges-page-added-to-category',
254 self
::CATEGORY_REMOVAL
=> 'recentchanges-page-removed-from-category',
257 $msgKey = $array[$type];
259 if ( intval( $numTemplateLinks ) > 0 ) {
260 $msgKey .= '-bundled';
263 return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
267 * Returns the timestamp of the page's previous revision or null if the latest revision
268 * does not refer to a parent revision
270 * @return null|string
272 private function getPreviousRevisionTimestamp() {
273 $previousRev = Revision
::newFromId(
274 $this->pageTitle
->getPreviousRevisionID( $this->pageTitle
->getLatestRevID() )
277 return $previousRev ?
$previousRev->getTimestamp() : null;