Merge "Type hint against LinkTarget in WatchedItemStore"
[lhc/web/wiklou.git] / includes / changes / CategoryMembershipChange.php
1 <?php
2
3 use MediaWiki\Storage\RevisionRecord;
4
5 /**
6 * Helper class for category membership changes
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @author Kai Nissen
25 * @author Addshore
26 * @since 1.27
27 */
28
29 class CategoryMembershipChange {
30
31 const CATEGORY_ADDITION = 1;
32 const CATEGORY_REMOVAL = -1;
33
34 /**
35 * @var string Current timestamp, set during CategoryMembershipChange::__construct()
36 */
37 private $timestamp;
38
39 /**
40 * @var Title Title instance of the categorized page
41 */
42 private $pageTitle;
43
44 /**
45 * @var Revision|null Latest Revision instance of the categorized page
46 */
47 private $revision;
48
49 /**
50 * @var int
51 * Number of pages this WikiPage is embedded by
52 * Set by CategoryMembershipChange::checkTemplateLinks()
53 */
54 private $numTemplateLinks = 0;
55
56 /**
57 * @var callable|null
58 */
59 private $newForCategorizationCallback = null;
60
61 /**
62 * @param Title $pageTitle Title instance of the categorized page
63 * @param Revision|null $revision Latest Revision instance of the categorized page
64 *
65 * @throws MWException
66 */
67 public function __construct( Title $pageTitle, Revision $revision = null ) {
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
78 /**
79 * Overrides the default new for categorization callback
80 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
81 *
82 * @param callable $callback
83 * @see RecentChange::newForCategorization for callback signiture
84 *
85 * @throws MWException
86 */
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
94 /**
95 * Determines the number of template links for recursive link updates
96 */
97 public function checkTemplateLinks() {
98 $this->numTemplateLinks = $this->pageTitle->getBacklinkCache()->getNumLinks( 'templatelinks' );
99 }
100
101 /**
102 * Create a recentchanges entry for category additions
103 *
104 * @param Title $categoryTitle
105 */
106 public function triggerCategoryAddedNotification( Title $categoryTitle ) {
107 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_ADDITION );
108 }
109
110 /**
111 * Create a recentchanges entry for category removals
112 *
113 * @param Title $categoryTitle
114 */
115 public function triggerCategoryRemovedNotification( Title $categoryTitle ) {
116 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_REMOVAL );
117 }
118
119 /**
120 * Create a recentchanges entry using RecentChange::notifyCategorization()
121 *
122 * @param Title $categoryTitle
123 * @param int $type
124 */
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(),
133 $this->numTemplateLinks
134 ),
135 $this->pageTitle,
136 $this->getPreviousRevisionTimestamp(),
137 $this->revision,
138 $type === self::CATEGORY_ADDITION
139 );
140 }
141
142 /**
143 * @param string $timestamp Timestamp of the recent change to occur in TS_MW format
144 * @param Title $categoryTitle Title of the category a page is being added to or removed from
145 * @param User|null $user User object of the user that made the change
146 * @param string $comment Change summary
147 * @param Title $pageTitle Title of the page that is being added or removed
148 * @param string $lastTimestamp Parent revision timestamp of this change in TS_MW format
149 * @param Revision|null $revision
150 * @param bool $added true, if the category was added, false for removed
151 *
152 * @throws MWException
153 */
154 private function notifyCategorization(
155 $timestamp,
156 Title $categoryTitle,
157 User $user = null,
158 $comment,
159 Title $pageTitle,
160 $lastTimestamp,
161 $revision,
162 $added
163 ) {
164 $deleted = $revision ? $revision->getVisibility() & RevisionRecord::SUPPRESSED_USER : 0;
165 $newRevId = $revision ? $revision->getId() : 0;
166
167 /**
168 * T109700 - Default bot flag to true when there is no corresponding RC entry
169 * This means all changes caused by parser functions & Lua on reparse are marked as bot
170 * Also in the case no RC entry could be found due to replica DB lag
171 */
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
189 /** @var RecentChange $rc */
190 $rc = ( $this->newForCategorizationCallback )(
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
207 /**
208 * Get the user associated with this change.
209 *
210 * If there is no revision associated with the change and thus no editing user
211 * fallback to a default.
212 *
213 * False will be returned if the user name specified in the
214 * 'autochange-username' message is invalid.
215 *
216 * @return User|bool
217 */
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();
229 $user = User::newFromName( $username );
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
238 /**
239 * Returns the change message according to the type of category membership change
240 *
241 * The message keys created in this method may be one of:
242 * - recentchanges-page-added-to-category
243 * - recentchanges-page-added-to-category-bundled
244 * - recentchanges-page-removed-from-category
245 * - recentchanges-page-removed-from-category-bundled
246 *
247 * @param int $type may be CategoryMembershipChange::CATEGORY_ADDITION
248 * or CategoryMembershipChange::CATEGORY_REMOVAL
249 * @param string $prefixedText result of Title::->getPrefixedText()
250 * @param int $numTemplateLinks
251 *
252 * @return string
253 */
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
269 /**
270 * Returns the timestamp of the page's previous revision or null if the latest revision
271 * does not refer to a parent revision
272 *
273 * @return null|string
274 */
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 }