Merge "Revert "Allow auto suggestion for subpages of Special:BotPasswords""
[lhc/web/wiklou.git] / includes / changes / CategoryMembershipChange.php
1 <?php
2 /**
3 * Helper class for category membership changes
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @author Kai Nissen
22 * @author Addshore
23 * @since 1.27
24 */
25
26 use Wikimedia\Assert\Assert;
27
28 class CategoryMembershipChange {
29
30 const CATEGORY_ADDITION = 1;
31 const CATEGORY_REMOVAL = -1;
32
33 /**
34 * @var string Current timestamp, set during CategoryMembershipChange::__construct()
35 */
36 private $timestamp;
37
38 /**
39 * @var Title Title instance of the categorized page
40 */
41 private $pageTitle;
42
43 /**
44 * @var Revision|null Latest Revision instance of the categorized page
45 */
46 private $revision;
47
48 /**
49 * @var int
50 * Number of pages this WikiPage is embedded by
51 * Set by CategoryMembershipChange::checkTemplateLinks()
52 */
53 private $numTemplateLinks = 0;
54
55 /**
56 * @var callable|null
57 */
58 private $newForCategorizationCallback = null;
59
60 /**
61 * @param Title $pageTitle Title instance of the categorized page
62 * @param Revision $revision Latest Revision instance of the categorized page
63 *
64 * @throws MWException
65 */
66 public function __construct( Title $pageTitle, Revision $revision = null ) {
67 $this->pageTitle = $pageTitle;
68 if ( $revision === null ) {
69 $this->timestamp = wfTimestampNow();
70 } else {
71 $this->timestamp = $revision->getTimestamp();
72 }
73 $this->revision = $revision;
74 $this->newForCategorizationCallback = [ 'RecentChange', 'newForCategorization' ];
75 }
76
77 /**
78 * Overrides the default new for categorization callback
79 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
80 *
81 * @param callable $callback
82 * @see RecentChange::newForCategorization for callback signiture
83 *
84 * @throws MWException
85 */
86 public function overrideNewForCategorizationCallback( $callback ) {
87 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
88 throw new MWException( 'Cannot override newForCategorization callback in operation.' );
89 }
90 Assert::parameterType( 'callable', $callback, '$callback' );
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( $type, [
131 'prefixedText' => $this->pageTitle->getPrefixedText(),
132 'numTemplateLinks' => $this->numTemplateLinks
133 ] ),
134 $this->pageTitle,
135 $this->getPreviousRevisionTimestamp(),
136 $this->revision
137 );
138 }
139
140 /**
141 * @param string $timestamp Timestamp of the recent change to occur in TS_MW format
142 * @param Title $categoryTitle Title of the category a page is being added to or removed from
143 * @param User $user User object of the user that made the change
144 * @param string $comment Change summary
145 * @param Title $pageTitle Title of the page that is being added or removed
146 * @param string $lastTimestamp Parent revision timestamp of this change in TS_MW format
147 * @param Revision|null $revision
148 *
149 * @throws MWException
150 */
151 private function notifyCategorization(
152 $timestamp,
153 Title $categoryTitle,
154 User $user = null,
155 $comment,
156 Title $pageTitle,
157 $lastTimestamp,
158 $revision
159 ) {
160 $deleted = $revision ? $revision->getVisibility() & Revision::SUPPRESSED_USER : 0;
161 $newRevId = $revision ? $revision->getId() : 0;
162
163 /**
164 * T109700 - Default bot flag to true when there is no corresponding RC entry
165 * This means all changes caused by parser functions & Lua on reparse are marked as bot
166 * Also in the case no RC entry could be found due to slave lag
167 */
168 $bot = 1;
169 $lastRevId = 0;
170 $ip = '';
171
172 # If no revision is given, the change was probably triggered by parser functions
173 if ( $revision !== null ) {
174 $correspondingRc = $this->revision->getRecentChange();
175 if ( $correspondingRc === null ) {
176 $correspondingRc = $this->revision->getRecentChange( Revision::READ_LATEST );
177 }
178 if ( $correspondingRc !== null ) {
179 $bot = $correspondingRc->getAttribute( 'rc_bot' ) ?: 0;
180 $ip = $correspondingRc->getAttribute( 'rc_ip' ) ?: '';
181 $lastRevId = $correspondingRc->getAttribute( 'rc_last_oldid' ) ?: 0;
182 }
183 }
184
185 /** @var RecentChange $rc */
186 $rc = call_user_func_array(
187 $this->newForCategorizationCallback,
188 [
189 $timestamp,
190 $categoryTitle,
191 $user,
192 $comment,
193 $pageTitle,
194 $lastRevId,
195 $newRevId,
196 $lastTimestamp,
197 $bot,
198 $ip,
199 $deleted
200 ]
201 );
202 $rc->save();
203 }
204
205 /**
206 * Get the user associated with this change.
207 *
208 * If there is no revision associated with the change and thus no editing user
209 * fallback to a default.
210 *
211 * False will be returned if the user name specified in the
212 * 'autochange-username' message is invalid.
213 *
214 * @return User|bool
215 */
216 private function getUser() {
217 if ( $this->revision ) {
218 $userId = $this->revision->getUser( Revision::RAW );
219 if ( $userId === 0 ) {
220 return User::newFromName( $this->revision->getUserText( Revision::RAW ), false );
221 } else {
222 return User::newFromId( $userId );
223 }
224 }
225
226 $username = wfMessage( 'autochange-username' )->inContentLanguage()->text();
227 $user = User::newFromName( $username );
228 # User::newFromName() can return false on a badly configured wiki.
229 if ( $user && !$user->isLoggedIn() ) {
230 $user->addToDatabase();
231 }
232
233 return $user;
234 }
235
236 /**
237 * Returns the change message according to the type of category membership change
238 *
239 * The message keys created in this method may be one of:
240 * - recentchanges-page-added-to-category
241 * - recentchanges-page-added-to-category-bundled
242 * - recentchanges-page-removed-from-category
243 * - recentchanges-page-removed-from-category-bundled
244 *
245 * @param int $type may be CategoryMembershipChange::CATEGORY_ADDITION
246 * or CategoryMembershipChange::CATEGORY_REMOVAL
247 * @param array $params
248 * - prefixedText: result of Title::->getPrefixedText()
249 *
250 * @return string
251 */
252 private function getChangeMessageText( $type, array $params ) {
253 $array = [
254 self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category',
255 self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category',
256 ];
257
258 $msgKey = $array[$type];
259
260 if ( intval( $params['numTemplateLinks'] ) > 0 ) {
261 $msgKey .= '-bundled';
262 }
263
264 return wfMessage( $msgKey, $params )->inContentLanguage()->text();
265 }
266
267 /**
268 * Returns the timestamp of the page's previous revision or null if the latest revision
269 * does not refer to a parent revision
270 *
271 * @return null|string
272 */
273 private function getPreviousRevisionTimestamp() {
274 $previousRev = Revision::newFromId(
275 $this->pageTitle->getPreviousRevisionID( $this->pageTitle->getLatestRevID() )
276 );
277
278 return $previousRev ? $previousRev->getTimestamp() : null;
279 }
280
281 }