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