Add Blob to accepted types for uploads
[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 $this->timestamp = wfTimestampNow();
69 $this->revision = $revision;
70 $this->newForCategorizationCallback = array( 'RecentChange', 'newForCategorization' );
71 }
72
73 /**
74 * Overrides the default new for categorization callback
75 * This is intended for use while testing and will fail if MW_PHPUNIT_TEST is not defined.
76 *
77 * @param callable $callback
78 * @see RecentChange::newForCategorization for callback signiture
79 *
80 * @throws MWException
81 */
82 public function overrideNewForCategorizationCallback( $callback ) {
83 if ( !defined( 'MW_PHPUNIT_TEST' ) ) {
84 throw new MWException( 'Cannot override newForCategorization callback in operation.' );
85 }
86 Assert::parameterType( 'callable', $callback, '$callback' );
87 $this->newForCategorizationCallback = $callback;
88 }
89
90 /**
91 * Determines the number of template links for recursive link updates
92 */
93 public function checkTemplateLinks() {
94 $this->numTemplateLinks = $this->pageTitle->getBacklinkCache()->getNumLinks( 'templatelinks' );
95 }
96
97 /**
98 * Create a recentchanges entry for category additions
99 *
100 * @param Title $categoryTitle
101 */
102 public function triggerCategoryAddedNotification( Title $categoryTitle ) {
103 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_ADDITION );
104 }
105
106 /**
107 * Create a recentchanges entry for category removals
108 *
109 * @param Title $categoryTitle
110 */
111 public function triggerCategoryRemovedNotification( Title $categoryTitle ) {
112 $this->createRecentChangesEntry( $categoryTitle, self::CATEGORY_REMOVAL );
113 }
114
115 /**
116 * Create a recentchanges entry using RecentChange::notifyCategorization()
117 *
118 * @param Title $categoryTitle
119 * @param int $type
120 */
121 private function createRecentChangesEntry( Title $categoryTitle, $type ) {
122 $this->notifyCategorization(
123 $this->timestamp,
124 $categoryTitle,
125 $this->getUser(),
126 $this->getChangeMessageText( $type, array(
127 'prefixedText' => $this->pageTitle->getPrefixedText(),
128 'numTemplateLinks' => $this->numTemplateLinks
129 ) ),
130 $this->pageTitle,
131 $this->getPreviousRevisionTimestamp(),
132 $this->revision
133 );
134 }
135
136 /**
137 * @param string $timestamp Timestamp of the recent change to occur in TS_MW format
138 * @param Title $categoryTitle Title of the category a page is being added to or removed from
139 * @param User $user User object of the user that made the change
140 * @param string $comment Change summary
141 * @param Title $pageTitle Title of the page that is being added or removed
142 * @param string $lastTimestamp Parent revision timestamp of this change in TS_MW format
143 * @param Revision|null $revision
144 *
145 * @throws MWException
146 */
147 private function notifyCategorization(
148 $timestamp,
149 Title $categoryTitle,
150 User $user = null,
151 $comment,
152 Title $pageTitle,
153 $lastTimestamp,
154 $revision
155 ) {
156 $deleted = $revision ? $revision->getVisibility() & Revision::SUPPRESSED_USER : 0;
157 $newRevId = $revision ? $revision->getId() : 0;
158
159 /**
160 * T109700 - Default bot flag to true when there is no corresponding RC entry
161 * This means all changes caused by parser functions & Lua on reparse are marked as bot
162 * Also in the case no RC entry could be found due to slave lag
163 */
164 $bot = 1;
165 $lastRevId = 0;
166 $ip = '';
167
168 # If no revision is given, the change was probably triggered by parser functions
169 if ( $revision !== null ) {
170 $correspondingRc = $this->revision->getRecentChange();
171 if ( $correspondingRc === null ) {
172 $correspondingRc = $this->revision->getRecentChange( Revision::READ_LATEST );
173 }
174 if ( $correspondingRc !== null ) {
175 $bot = $correspondingRc->getAttribute( 'rc_bot' ) ?: 0;
176 $ip = $correspondingRc->getAttribute( 'rc_ip' ) ?: '';
177 $lastRevId = $correspondingRc->getAttribute( 'rc_last_oldid' ) ?: 0;
178 }
179 }
180
181 /** @var RecentChange $rc */
182 $rc = call_user_func_array(
183 $this->newForCategorizationCallback,
184 array(
185 $timestamp,
186 $categoryTitle,
187 $user,
188 $comment,
189 $pageTitle,
190 $lastRevId,
191 $newRevId,
192 $lastTimestamp,
193 $bot,
194 $ip,
195 $deleted
196 )
197 );
198 $rc->save();
199 }
200
201 /**
202 * Get the user associated with this change.
203 *
204 * If there is no revision associated with the change and thus no editing user
205 * fallback to a default.
206 *
207 * False will be returned if the user name specified in the
208 * 'autochange-username' message is invalid.
209 *
210 * @return User|bool
211 */
212 private function getUser() {
213 if ( $this->revision ) {
214 $userId = $this->revision->getUser( Revision::RAW );
215 if ( $userId === 0 ) {
216 return User::newFromName( $this->revision->getUserText( Revision::RAW ), false );
217 } else {
218 return User::newFromId( $userId );
219 }
220 }
221
222 $username = wfMessage( 'autochange-username' )->inContentLanguage()->text();
223 $user = User::newFromName( $username );
224 # User::newFromName() can return false on a badly configured wiki.
225 if ( $user && !$user->isLoggedIn() ) {
226 $user->addToDatabase();
227 }
228
229 return $user;
230 }
231
232 /**
233 * Returns the change message according to the type of category membership change
234 *
235 * The message keys created in this method may be one of:
236 * - recentchanges-page-added-to-category
237 * - recentchanges-page-added-to-category-bundled
238 * - recentchanges-page-removed-from-category
239 * - recentchanges-page-removed-from-category-bundled
240 *
241 * @param int $type may be CategoryMembershipChange::CATEGORY_ADDITION
242 * or CategoryMembershipChange::CATEGORY_REMOVAL
243 * @param array $params
244 * - prefixedText: result of Title::->getPrefixedText()
245 *
246 * @return string
247 */
248 private function getChangeMessageText( $type, array $params ) {
249 $array = array(
250 self::CATEGORY_ADDITION => 'recentchanges-page-added-to-category',
251 self::CATEGORY_REMOVAL => 'recentchanges-page-removed-from-category',
252 );
253
254 $msgKey = $array[$type];
255
256 if ( intval( $params['numTemplateLinks'] ) > 0 ) {
257 $msgKey .= '-bundled';
258 }
259
260 return wfMessage( $msgKey, $params )->inContentLanguage()->text();
261 }
262
263 /**
264 * Returns the timestamp of the page's previous revision or null if the latest revision
265 * does not refer to a parent revision
266 *
267 * @return null|string
268 */
269 private function getPreviousRevisionTimestamp() {
270 $previousRev = Revision::newFromId(
271 $this->pageTitle->getPreviousRevisionID( $this->pageTitle->getLatestRevID() )
272 );
273
274 return $previousRev ? $previousRev->getTimestamp() : null;
275 }
276
277 }