Merge "mw.widgets.CategorySelector: Indicate pending requests and abort useless ones"
[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 Adam Shorland
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; set by CategoryMembershipChange::setRecursive()
51 */
52 private $numTemplateLinks = 0;
53
54 /**
55 * @var callable|null
56 */
57 private $newForCategorizationCallback = null;
58
59 /**
60 * @param Title $pageTitle Title instance of the categorized page
61 * @param Revision $revision Latest Revision instance of the categorized page
62 *
63 * @throws MWException
64 */
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' );
70 }
71
72 /**
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.
75 *
76 * @param callable $callback
77 * @see RecentChange::newForCategorization for callback signiture
78 *
79 * @throws MWException
80 */
81 public function overrideNewForCategorizationCallback( $callback ) {
82 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
83 throw new MWException( 'Cannot override newForCategorization callback in operation.' );
84 }
85 Assert::parameterType( 'callable', $callback, '$callback' );
86 $this->newForCategorizationCallback = $callback;
87 }
88
89 /**
90 * Determines the number of template links for recursive link updates
91 */
92 public function checkTemplateLinks() {
93 $this->numTemplateLinks = $this->pageTitle->getBacklinkCache()->getNumLinks( 'templatelinks' );
94 }
95
96 /**
97 * Create a recentchanges entry for category additions
98 *
99 * @param Title $categoryTitle
100 */
101 public function triggerCategoryAddedNotification( Title $categoryTitle ) {
102 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_ADDITION );
103 }
104
105 /**
106 * Create a recentchanges entry for category removals
107 *
108 * @param Title $categoryTitle
109 */
110 public function triggerCategoryRemovedNotification( Title $categoryTitle ) {
111 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_REMOVAL );
112 }
113
114 /**
115 * Create a recentchanges entry using RecentChange::notifyCategorization()
116 *
117 * @param Title $categoryTitle
118 * @param int $type
119 */
120 private function createRecentChangesEntry( Title $categoryTitle, $type ) {
121 $this->notifyCategorization(
122 $this->timestamp,
123 $categoryTitle,
124 $this->getUser(),
125 $this->getChangeMessageText( $type, array(
126 'prefixedText' => $this->pageTitle->getPrefixedText(),
127 'numTemplateLinks' => $this->numTemplateLinks
128 ) ),
129 $this->pageTitle,
130 $this->getPreviousRevisionTimestamp(),
131 $this->revision
132 );
133 }
134
135 /**
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
143 *
144 * @throws MWException
145 */
146 private function notifyCategorization(
147 $timestamp,
148 Title $categoryTitle,
149 User $user = null,
150 $comment,
151 Title $pageTitle,
152 $lastTimestamp,
153 $revision
154 ) {
155 $deleted = $revision ? $revision->getVisibility() & Revision::SUPPRESSED_USER : 0;
156 $newRevId = $revision ? $revision->getId() : 0;
157
158 /**
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
162 */
163 $bot = 1;
164 $lastRevId = 0;
165 $ip = '';
166
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 );
172 }
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;
177 }
178 }
179
180 $rc = call_user_func_array(
181 $this->newForCategorizationCallback,
182 array(
183 $timestamp,
184 $categoryTitle,
185 $user,
186 $comment,
187 $pageTitle,
188 $lastRevId,
189 $newRevId,
190 $lastTimestamp,
191 $bot,
192 $ip,
193 $deleted
194 )
195 );
196 $rc->save();
197 }
198
199 /**
200 * Get the user associated with this change.
201 *
202 * If there is no revision associated with the change and thus no editing user
203 * fallback to a default.
204 *
205 * False will be returned if the user name specified in the
206 * 'autochange-username' message is invalid.
207 *
208 * @return User|bool
209 */
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 );
215 } else {
216 return User::newFromId( $userId );
217 }
218 }
219
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();
225 }
226
227 return $user;
228 }
229
230 /**
231 * Returns the change message according to the type of category membership change
232 *
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
238 *
239 * @param int $type may be CategoryMembershipChange::CATEGORY_ADDITION
240 * or CategoryMembershipChange::CATEGORY_REMOVAL
241 * @param array $params
242 * - prefixedUrl: result of Title::->getPrefixedURL()
243 *
244 * @return string
245 */
246 private function getChangeMessageText( $type, array $params ) {
247 $array = array(
248 self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category',
249 self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category',
250 );
251
252 $msgKey = $array[$type];
253
254 if ( intval( $params['numTemplateLinks'] ) > 0 ) {
255 $msgKey .= '-bundled';
256 }
257
258 return wfMessage( $msgKey, $params )->inContentLanguage()->text();
259 }
260
261 /**
262 * Returns the timestamp of the page's previous revision or null if the latest revision
263 * does not refer to a parent revision
264 *
265 * @return null|string
266 */
267 private function getPreviousRevisionTimestamp() {
268 $previousRev = Revision::newFromId(
269 $this->pageTitle->getPreviousRevisionID( $this->pageTitle->getLatestRevID() )
270 );
271
272 return $previousRev ? $previousRev->getTimestamp() : null;
273 }
274
275 }