Merge "API: Use message-per-value for apihelp-query+siteinfo-param-prop"
[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 * @since 1.26
23 */
24
25 class CategoryMembershipChange {
26
27 const CATEGORY_ADDITION = 1;
28 const CATEGORY_REMOVAL = -1;
29
30 /** @var string Current timestamp, set during CategoryMembershipChange::__construct() */
31 private $timestamp;
32
33 /** @var Title Title instance of the categorized page */
34 private $pageTitle;
35
36 /** @var WikiPage WikiPage instance of the categorized page */
37 private $page;
38
39 /** @var Revision Latest Revision instance of the categorized page */
40 private $revision;
41
42 /**
43 * @var int
44 * Number of pages this WikiPage is embedded by; set by CategoryMembershipChange::setRecursive()
45 */
46 private $numTemplateLinks = 0;
47
48 /**
49 * @var User
50 * instance of the user that created CategoryMembershipChange::$revision
51 */
52 private $user;
53
54 /**
55 * @var null|RecentChange
56 * RecentChange that is referred to in CategoryMembershipChange::$revision
57 */
58 private $correspondingRC;
59
60 /**
61 * @param Title $pageTitle Title instance of the categorized page
62 * @param Revision $revision Latest Revision instance of the categorized page
63 * @throws MWException
64 */
65 public function __construct( Title $pageTitle, Revision $revision = null ) {
66 $this->pageTitle = $pageTitle;
67 $this->page = WikiPage::factory( $pageTitle );
68 $this->timestamp = wfTimestampNow();
69
70 # if no revision is given, the change was probably triggered by parser functions
71 if ( $revision ) {
72 $this->revision = $revision;
73 $this->correspondingRC = $this->revision->getRecentChange();
74 $this->user = $this->getRevisionUser();
75 } else {
76 $this->user = User::newFromId( 0 );
77 }
78 }
79
80 /**
81 * Determines the number of template links for recursive link updates
82 */
83 public function setRecursive() {
84 $this->numTemplateLinks = $this->pageTitle->getBacklinkCache()->getNumLinks( 'templatelinks' );
85 }
86
87 /**
88 * Create a recentchanges entry for category additions
89 * @param string $categoryName
90 */
91 public function pageAddedToCategory( $categoryName ) {
92 $this->createRecentChangesEntry( $categoryName, self::CATEGORY_ADDITION );
93 }
94
95 /**
96 * Create a recentchanges entry for category removals
97 * @param string $categoryName
98 */
99 public function pageRemovedFromCategory( $categoryName ) {
100 $this->createRecentChangesEntry( $categoryName, self::CATEGORY_REMOVAL );
101 }
102
103 /**
104 * Create a recentchanges entry using RecentChange::notifyCategorization()
105 * @param string $categoryName
106 * @param int $type
107 */
108 private function createRecentChangesEntry( $categoryName, $type ) {
109 $categoryTitle = Title::newFromText( $categoryName, NS_CATEGORY );
110 if ( !$categoryTitle ) {
111 return;
112 }
113
114 $previousRevTimestamp = $this->getPreviousRevisionTimestamp();
115 $unpatrolled = $this->revision ? $this->revision->isUnpatrolled() : 0;
116
117 $lastRevId = 0;
118 $bot = 0;
119 $ip = '';
120 if ( $this->correspondingRC ) {
121 $lastRevId = $this->correspondingRC->getAttribute( 'rc_last_oldid' ) ?: 0;
122 $bot = $this->correspondingRC->getAttribute( 'rc_bot' ) ?: 0;
123 $ip = $this->correspondingRC->getAttribute( 'rc_ip' ) ?: '';
124 }
125
126 RecentChange::notifyCategorization(
127 $this->timestamp,
128 $categoryTitle,
129 $this->user,
130 $this->getChangeMessage( $type, array(
131 'prefixedUrl' => $this->page->getTitle()->getPrefixedURL(),
132 'numTemplateLinks' => $this->numTemplateLinks
133 ) ),
134 $this->pageTitle,
135 $lastRevId,
136 $this->revision ? $this->revision->getId() : 0,
137 $previousRevTimestamp,
138 $bot,
139 $ip,
140 $unpatrolled ? 0 : 1,
141 $this->revision ? $this->revision->getVisibility() & Revision::SUPPRESSED_USER : 0
142 );
143 }
144
145 /**
146 * Get the user who created the revision. may be an anonymous IP
147 * @return User
148 */
149 private function getRevisionUser() {
150 $userId = $this->revision->getUser( Revision::RAW );
151 if ( $userId === 0 ) {
152 return User::newFromName( $this->revision->getUserText( Revision::RAW ), false );
153 } else {
154 return User::newFromId( $userId );
155 }
156 }
157
158 /**
159 * Returns the change message according to the type of category membership change
160 *
161 * The message keys created in this method may be one of:
162 * - recentchanges-page-added-to-category
163 * - recentchanges-page-added-to-category-bundled
164 * - recentchanges-page-removed-from-category
165 * - recentchanges-page-removed-from-category-bundled
166 *
167 * @param int $type may be CategoryMembershipChange::CATEGORY_ADDITION
168 * or CategoryMembershipChange::CATEGORY_REMOVAL
169 * @param array $params
170 * - prefixedUrl: result of Title::->getPrefixedURL()
171 * - numTemplateLinks
172 * @return string
173 */
174 private function getChangeMessage( $type, array $params ) {
175 $msgKey = 'recentchanges-';
176
177 switch ( $type ) {
178 case self::CATEGORY_ADDITION:
179 $msgKey .= 'page-added-to-category';
180 break;
181 case self::CATEGORY_REMOVAL:
182 $msgKey .= 'page-removed-from-category';
183 break;
184 }
185
186 if ( intval( $params['numTemplateLinks'] ) > 0 ) {
187 $msgKey .= '-bundled';
188 }
189
190 return wfMessage( $msgKey, $params )->inContentLanguage()->text();
191 }
192
193 /**
194 * Returns the timestamp of the page's previous revision or null if the latest revision
195 * does not refer to a parent revision
196 * @return null|string
197 */
198 private function getPreviousRevisionTimestamp() {
199 $latestRev = Revision::newFromId( $this->pageTitle->getLatestRevID() );
200 $previousRev = Revision::newFromId( $latestRev->getParentId() );
201
202 return $previousRev ? $previousRev->getTimestamp() : null;
203 }
204
205 }