Merge "FauxRequest: don’t override getValues()"
[lhc/web/wiklou.git] / includes / changes / CategoryMembershipChange.php
1 <?php
2
3 use MediaWiki\MediaWikiServices;
4 use MediaWiki\Revision\RevisionRecord;
5
6 /**
7 * Helper class for category membership changes
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 * @author Kai Nissen
26 * @author Addshore
27 * @since 1.27
28 */
29
30 class CategoryMembershipChange {
31
32 const CATEGORY_ADDITION = 1;
33 const CATEGORY_REMOVAL = -1;
34
35 /**
36 * @var string Current timestamp, set during CategoryMembershipChange::__construct()
37 */
38 private $timestamp;
39
40 /**
41 * @var Title Title instance of the categorized page
42 */
43 private $pageTitle;
44
45 /**
46 * @var Revision|null Latest Revision instance of the categorized page
47 */
48 private $revision;
49
50 /**
51 * @var int
52 * Number of pages this WikiPage is embedded by
53 * Set by CategoryMembershipChange::checkTemplateLinks()
54 */
55 private $numTemplateLinks = 0;
56
57 /**
58 * @var callable|null
59 */
60 private $newForCategorizationCallback = null;
61
62 /**
63 * @param Title $pageTitle Title instance of the categorized page
64 * @param Revision|null $revision Latest Revision instance of the categorized page
65 *
66 * @throws MWException
67 */
68 public function __construct( Title $pageTitle, Revision $revision = null ) {
69 $this->pageTitle = $pageTitle;
70 if ( $revision === null ) {
71 $this->timestamp = wfTimestampNow();
72 } else {
73 $this->timestamp = $revision->getTimestamp();
74 }
75 $this->revision = $revision;
76 $this->newForCategorizationCallback = [ RecentChange::class, 'newForCategorization' ];
77 }
78
79 /**
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.
82 *
83 * @param callable $callback
84 * @see RecentChange::newForCategorization for callback signiture
85 *
86 * @throws MWException
87 */
88 public function overrideNewForCategorizationCallback( callable $callback ) {
89 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
90 throw new MWException( 'Cannot override newForCategorization callback in operation.' );
91 }
92 $this->newForCategorizationCallback = $callback;
93 }
94
95 /**
96 * Determines the number of template links for recursive link updates
97 */
98 public function checkTemplateLinks() {
99 $this->numTemplateLinks = $this->pageTitle->getBacklinkCache()->getNumLinks( 'templatelinks' );
100 }
101
102 /**
103 * Create a recentchanges entry for category additions
104 *
105 * @param Title $categoryTitle
106 */
107 public function triggerCategoryAddedNotification( Title $categoryTitle ) {
108 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_ADDITION );
109 }
110
111 /**
112 * Create a recentchanges entry for category removals
113 *
114 * @param Title $categoryTitle
115 */
116 public function triggerCategoryRemovedNotification( Title $categoryTitle ) {
117 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_REMOVAL );
118 }
119
120 /**
121 * Create a recentchanges entry using RecentChange::notifyCategorization()
122 *
123 * @param Title $categoryTitle
124 * @param int $type
125 */
126 private function createRecentChangesEntry( Title $categoryTitle, $type ) {
127 $this->notifyCategorization(
128 $this->timestamp,
129 $categoryTitle,
130 $this->getUser(),
131 $this->getChangeMessageText(
132 $type,
133 $this->pageTitle->getPrefixedText(),
134 $this->numTemplateLinks
135 ),
136 $this->pageTitle,
137 $this->getPreviousRevisionTimestamp(),
138 $this->revision,
139 $type === self::CATEGORY_ADDITION
140 );
141 }
142
143 /**
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
152 *
153 * @throws MWException
154 */
155 private function notifyCategorization(
156 $timestamp,
157 Title $categoryTitle,
158 User $user = null,
159 $comment,
160 Title $pageTitle,
161 $lastTimestamp,
162 $revision,
163 $added
164 ) {
165 $deleted = $revision ? $revision->getVisibility() & RevisionRecord::SUPPRESSED_USER : 0;
166 $newRevId = $revision ? $revision->getId() : 0;
167
168 /**
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
172 */
173 $bot = 1;
174 $lastRevId = 0;
175 $ip = '';
176
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 );
182 }
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;
187 }
188 }
189
190 /** @var RecentChange $rc */
191 $rc = ( $this->newForCategorizationCallback )(
192 $timestamp,
193 $categoryTitle,
194 $user,
195 $comment,
196 $pageTitle,
197 $lastRevId,
198 $newRevId,
199 $lastTimestamp,
200 $bot,
201 $ip,
202 $deleted,
203 $added
204 );
205 $rc->save();
206 }
207
208 /**
209 * Get the user associated with this change.
210 *
211 * If there is no revision associated with the change and thus no editing user
212 * fallback to a default.
213 *
214 * False will be returned if the user name specified in the
215 * 'autochange-username' message is invalid.
216 *
217 * @return User|bool
218 */
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 );
224 } else {
225 return User::newFromId( $userId );
226 }
227 }
228
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();
234 }
235
236 return $user;
237 }
238
239 /**
240 * Returns the change message according to the type of category membership change
241 *
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
247 *
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
252 *
253 * @return string
254 */
255 private function getChangeMessageText( $type, $prefixedText, $numTemplateLinks ) {
256 $array = [
257 self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category',
258 self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category',
259 ];
260
261 $msgKey = $array[$type];
262
263 if ( intval( $numTemplateLinks ) > 0 ) {
264 $msgKey .= '-bundled';
265 }
266
267 return wfMessage( $msgKey, $prefixedText )->inContentLanguage()->text();
268 }
269
270 /**
271 * Returns the timestamp of the page's previous revision or null if the latest revision
272 * does not refer to a parent revision
273 *
274 * @return null|string
275 */
276 private function getPreviousRevisionTimestamp() {
277 $rl = MediaWikiServices::getInstance()->getRevisionLookup();
278 $latestRev = $rl->getRevisionByTitle( $this->pageTitle );
279 if ( $latestRev ) {
280 $previousRev = $rl->getPreviousRevision( $latestRev );
281 if ( $previousRev ) {
282 return $previousRev->getTimestamp();
283 }
284 }
285 return null;
286 }
287
288 }