3 * Helper class for category membership changes
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.
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.
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
25 class CategoryMembershipChange
{
27 const CATEGORY_ADDITION
= 1;
28 const CATEGORY_REMOVAL
= -1;
30 /** @var string Current timestamp, set during CategoryMembershipChange::__construct() */
33 /** @var Title Title instance of the categorized page */
36 /** @var WikiPage WikiPage instance of the categorized page */
39 /** @var Revision Latest Revision instance of the categorized page */
44 * Number of pages this WikiPage is embedded by; set by CategoryMembershipChange::setRecursive()
46 private $numTemplateLinks = 0;
50 * instance of the user that created CategoryMembershipChange::$revision
55 * @var null|RecentChange
56 * RecentChange that is referred to in CategoryMembershipChange::$revision
58 private $correspondingRC;
61 * @param Title $pageTitle Title instance of the categorized page
62 * @param Revision $revision Latest Revision instance of the categorized page
65 public function __construct( Title
$pageTitle, Revision
$revision = null ) {
66 $this->pageTitle
= $pageTitle;
67 $this->page
= WikiPage
::factory( $pageTitle );
68 $this->timestamp
= wfTimestampNow();
70 # if no revision is given, the change was probably triggered by parser functions
72 $this->revision
= $revision;
73 $this->correspondingRC
= $this->revision
->getRecentChange();
74 $this->user
= $this->getRevisionUser();
76 $this->user
= User
::newFromId( 0 );
81 * Determines the number of template links for recursive link updates
83 public function setRecursive() {
84 $this->numTemplateLinks
= $this->pageTitle
->getBacklinkCache()->getNumLinks( 'templatelinks' );
88 * Create a recentchanges entry for category additions
89 * @param string $categoryName
91 public function pageAddedToCategory( $categoryName ) {
92 $this->createRecentChangesEntry( $categoryName, self
::CATEGORY_ADDITION
);
96 * Create a recentchanges entry for category removals
97 * @param string $categoryName
99 public function pageRemovedFromCategory( $categoryName ) {
100 $this->createRecentChangesEntry( $categoryName, self
::CATEGORY_REMOVAL
);
104 * Create a recentchanges entry using RecentChange::notifyCategorization()
105 * @param string $categoryName
108 private function createRecentChangesEntry( $categoryName, $type ) {
109 $categoryTitle = Title
::newFromText( $categoryName, NS_CATEGORY
);
110 if ( !$categoryTitle ) {
114 $previousRevTimestamp = $this->getPreviousRevisionTimestamp();
115 $unpatrolled = $this->revision ?
$this->revision
->isUnpatrolled() : 0;
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' ) ?
: '';
126 RecentChange
::notifyCategorization(
130 $this->getChangeMessage( $type, array(
131 'prefixedUrl' => $this->page
->getTitle()->getPrefixedURL(),
132 'numTemplateLinks' => $this->numTemplateLinks
136 $this->revision ?
$this->revision
->getId() : 0,
137 $previousRevTimestamp,
140 $unpatrolled ?
0 : 1,
141 $this->revision ?
$this->revision
->getVisibility() & Revision
::SUPPRESSED_USER
: 0
146 * Get the user who created the revision. may be an anonymous IP
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 );
154 return User
::newFromId( $userId );
159 * Returns the change message according to the type of category membership change
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
167 * @param int $type may be CategoryMembershipChange::CATEGORY_ADDITION
168 * or CategoryMembershipChange::CATEGORY_REMOVAL
169 * @param array $params
170 * - prefixedUrl: result of Title::->getPrefixedURL()
174 private function getChangeMessage( $type, array $params ) {
175 $msgKey = 'recentchanges-';
178 case self
::CATEGORY_ADDITION
:
179 $msgKey .= 'page-added-to-category';
181 case self
::CATEGORY_REMOVAL
:
182 $msgKey .= 'page-removed-from-category';
186 if ( intval( $params['numTemplateLinks'] ) > 0 ) {
187 $msgKey .= '-bundled';
190 return wfMessage( $msgKey, $params )->inContentLanguage()->text();
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
198 private function getPreviousRevisionTimestamp() {
199 $latestRev = Revision
::newFromId( $this->pageTitle
->getLatestRevID() );
200 $previousRev = Revision
::newFromId( $latestRev->getParentId() );
202 return $previousRev ?
$previousRev->getTimestamp() : null;