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
22 * @author Adam Shorland
26 use Wikimedia\Assert\Assert
;
28 class CategoryMembershipChange
{
30 const CATEGORY_ADDITION
= 1;
31 const CATEGORY_REMOVAL
= -1;
34 * @var string Current timestamp, set during CategoryMembershipChange::__construct()
39 * @var Title Title instance of the categorized page
44 * @var Revision|null Latest Revision instance of the categorized page
50 * Number of pages this WikiPage is embedded by; set by CategoryMembershipChange::setRecursive()
52 private $numTemplateLinks = 0;
57 private $newForCategorizationCallback = null;
60 * @param Title $pageTitle Title instance of the categorized page
61 * @param Revision $revision Latest Revision instance of the categorized page
65 public function __construct( Title
$pageTitle, Revision
$revision = null ) {
66 $this->pageTitle
= $pageTitle;
67 $this->timestamp
= wfTimestampNow();
68 $this->revision
= $revision;
69 $this->newForCategorizationCallback
= array( 'RecentChange', 'newForCategorization' );
73 * Overrides the default new for categorization callback
74 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
76 * @param callable $callback
77 * @see RecentChange::newForCategorization for callback signiture
81 public function overrideNewForCategorizationCallback( $callback ) {
82 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
83 throw new MWException( 'Cannot override newForCategorization callback in operation.' );
85 Assert
::parameterType( 'callable', $callback, '$callback' );
86 $this->newForCategorizationCallback
= $callback;
90 * Determines the number of template links for recursive link updates
92 public function checkTemplateLinks() {
93 $this->numTemplateLinks
= $this->pageTitle
->getBacklinkCache()->getNumLinks( 'templatelinks' );
97 * Create a recentchanges entry for category additions
99 * @param Title $categoryTitle
101 public function triggerCategoryAddedNotification( Title
$categoryTitle ) {
102 $this->createRecentChangesEntry( $categoryTitle, self
::CATEGORY_ADDITION
);
106 * Create a recentchanges entry for category removals
108 * @param Title $categoryTitle
110 public function triggerCategoryRemovedNotification( Title
$categoryTitle ) {
111 $this->createRecentChangesEntry( $categoryTitle, self
::CATEGORY_REMOVAL
);
115 * Create a recentchanges entry using RecentChange::notifyCategorization()
117 * @param Title $categoryTitle
120 private function createRecentChangesEntry( Title
$categoryTitle, $type ) {
121 $this->notifyCategorization(
125 $this->getChangeMessageText( $type, array(
126 'prefixedText' => $this->pageTitle
->getPrefixedText(),
127 'numTemplateLinks' => $this->numTemplateLinks
130 $this->getPreviousRevisionTimestamp(),
136 * @param string $timestamp Timestamp of the recent change to occur in TS_MW format
137 * @param Title $categoryTitle Title of the category a page is being added to or removed from
138 * @param User $user User object of the user that made the change
139 * @param string $comment Change summary
140 * @param Title $pageTitle Title of the page that is being added or removed
141 * @param string $lastTimestamp Parent revision timestamp of this change in TS_MW format
142 * @param Revision|null $revision
144 * @throws MWException
146 private function notifyCategorization(
148 Title
$categoryTitle,
155 $deleted = $revision ?
$revision->getVisibility() & Revision
::SUPPRESSED_USER
: 0;
156 $newRevId = $revision ?
$revision->getId() : 0;
159 * T109700 - Default bot flag to true when there is no corresponding RC entry
160 * This means all changes caused by parser functions & Lua on reparse are marked as bot
161 * Also in the case no RC entry could be found due to slave lag
167 # If no revision is given, the change was probably triggered by parser functions
168 if ( $revision !== null ) {
169 $correspondingRc = $this->revision
->getRecentChange();
170 if ( $correspondingRc === null ) {
171 $correspondingRc = $this->revision
->getRecentChange( Revision
::READ_LATEST
);
173 if ( $correspondingRc !== null ) {
174 $bot = $correspondingRc->getAttribute( 'rc_bot' ) ?
: 0;
175 $ip = $correspondingRc->getAttribute( 'rc_ip' ) ?
: '';
176 $lastRevId = $correspondingRc->getAttribute( 'rc_last_oldid' ) ?
: 0;
180 $rc = call_user_func_array(
181 $this->newForCategorizationCallback
,
200 * Get the user associated with this change.
202 * If there is no revision associated with the change and thus no editing user
203 * fallback to a default.
205 * False will be returned if the user name specified in the
206 * 'autochange-username' message is invalid.
210 private function getUser() {
211 if ( $this->revision
) {
212 $userId = $this->revision
->getUser( Revision
::RAW
);
213 if ( $userId === 0 ) {
214 return User
::newFromName( $this->revision
->getUserText( Revision
::RAW
), false );
216 return User
::newFromId( $userId );
220 $username = wfMessage( 'autochange-username' )->inContentLanguage()->text();
221 $user = User
::newFromName( $username );
222 # User::newFromName() can return false on a badly configured wiki.
223 if ( $user && !$user->isLoggedIn() ) {
224 $user->addToDatabase();
231 * Returns the change message according to the type of category membership change
233 * The message keys created in this method may be one of:
234 * - recentchanges-page-added-to-category
235 * - recentchanges-page-added-to-category-bundled
236 * - recentchanges-page-removed-from-category
237 * - recentchanges-page-removed-from-category-bundled
239 * @param int $type may be CategoryMembershipChange::CATEGORY_ADDITION
240 * or CategoryMembershipChange::CATEGORY_REMOVAL
241 * @param array $params
242 * - prefixedUrl: result of Title::->getPrefixedURL()
246 private function getChangeMessageText( $type, array $params ) {
248 self
::CATEGORY_ADDITION
=> 'recentchanges-page-added-to-category',
249 self
::CATEGORY_REMOVAL
=> 'recentchanges-page-removed-from-category',
252 $msgKey = $array[$type];
254 if ( intval( $params['numTemplateLinks'] ) > 0 ) {
255 $msgKey .= '-bundled';
258 return wfMessage( $msgKey, $params )->inContentLanguage()->text();
262 * Returns the timestamp of the page's previous revision or null if the latest revision
263 * does not refer to a parent revision
265 * @return null|string
267 private function getPreviousRevisionTimestamp() {
268 $previousRev = Revision
::newFromId(
269 $this->pageTitle
->getPreviousRevisionID( $this->pageTitle
->getLatestRevID() )
272 return $previousRev ?
$previousRev->getTimestamp() : null;