3 * Updater for link tracking tables after a page edit.
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
24 * Job to add recent change entries mentioning category membership changes
28 * - revTimestamp : timestamp of the triggering revision
30 * Category changes will be mentioned for revisions at/after the timestamp for this page
34 class CategoryMembershipChangeJob
extends Job
{
35 const ENQUEUE_FUDGE_SEC
= 60;
37 public function __construct( Title
$title, array $params ) {
38 parent
::__construct( 'categoryMembershipChange', $title, $params );
39 // Only need one job per page. Note that ENQUEUE_FUDGE_SEC handles races where an
40 // older revision job gets inserted while the newer revision job is de-duplicated.
41 $this->removeDuplicates
= true;
44 public function run() {
45 $page = WikiPage
::newFromID( $this->params
['pageId'], WikiPage
::READ_LATEST
);
47 $this->setLastError( "Could not find page #{$this->params['pageId']}" );
48 return false; // deleted?
51 $dbw = wfGetDB( DB_MASTER
);
53 // Use a named lock so that jobs for this page see each others' changes
54 $lockKey = "CategoryMembershipUpdates:{$page->getId()}";
55 $scopedLock = $dbw->getScopedLockAndFlush( $lockKey, __METHOD__
, 10 );
57 $this->setLastError( "Could not acquire lock '$lockKey'" );
61 $cutoffUnix = wfTimestamp( TS_UNIX
, $this->params
['revTimestamp'] );
62 // Using ENQUEUE_FUDGE_SEC handles jobs inserted out of revision order due to the delay
63 // between COMMIT and actual enqueueing of the CategoryMembershipChangeJob job.
64 $cutoffUnix -= self
::ENQUEUE_FUDGE_SEC
;
66 // Get the newest revision that has a SRC_CATEGORIZE row...
67 $row = $dbw->selectRow(
68 array( 'revision', 'recentchanges' ),
69 array( 'rev_timestamp', 'rev_id' ),
71 'rev_page' => $page->getId(),
72 'rev_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $cutoffUnix ) )
75 array( 'ORDER BY' => 'rev_timestamp DESC, rev_id DESC' ),
77 'recentchanges' => array(
80 'rc_this_oldid = rev_id',
81 'rc_source' => RecentChange
::SRC_CATEGORIZE
,
82 // Allow rc_cur_id or rc_timestamp index usage
83 'rc_cur_id = rev_page',
84 'rc_timestamp >= rev_timestamp'
89 // Only consider revisions newer than any such revision
91 $cutoffUnix = wfTimestamp( TS_UNIX
, $row->rev_timestamp
);
92 $lastRevId = (int)$row->rev_id
;
97 // Find revisions to this page made around and after this revision which lack category
98 // notifications in recent changes. This lets jobs pick up were the last one left off.
99 $encCutoff = $dbw->addQuotes( $dbw->timestamp( $cutoffUnix ) );
102 Revision
::selectFields(),
104 'rev_page' => $page->getId(),
105 "rev_timestamp > $encCutoff" .
106 " OR (rev_timestamp = $encCutoff AND rev_id > $lastRevId)"
109 array( 'ORDER BY' => 'rev_timestamp ASC, rev_id ASC' )
112 // Apply all category updates in revision timestamp order
113 foreach ( $res as $row ) {
114 $this->notifyUpdatesForRevision( $page, Revision
::newFromRow( $row ) );
121 * @param WikiPage $page
122 * @param Revision $newRev
123 * @throws MWException
125 protected function notifyUpdatesForRevision( WikiPage
$page, Revision
$newRev ) {
126 $config = RequestContext
::getMain()->getConfig();
127 $title = $page->getTitle();
129 // Get the new revision
130 if ( !$newRev->getContent() ) {
134 // Get the prior revision (the same for null edits)
135 if ( $newRev->getParentId() ) {
136 $oldRev = Revision
::newFromId( $newRev->getParentId(), Revision
::READ_LATEST
);
137 if ( !$oldRev->getContent() ) {
144 // Parse the new revision and get the categories
145 $categoryChanges = $this->getExplicitCategoriesChanges( $title, $newRev, $oldRev );
146 list( $categoryInserts, $categoryDeletes ) = $categoryChanges;
147 if ( !$categoryInserts && !$categoryDeletes ) {
148 return; // nothing to do
151 $dbw = wfGetDB( DB_MASTER
);
152 $catMembChange = new CategoryMembershipChange( $title, $newRev );
153 $catMembChange->checkTemplateLinks();
155 $batchSize = $config->get( 'UpdateRowsPerQuery' );
158 foreach ( $categoryInserts as $categoryName ) {
159 $categoryTitle = Title
::makeTitle( NS_CATEGORY
, $categoryName );
160 $catMembChange->triggerCategoryAddedNotification( $categoryTitle );
161 if ( $insertCount++
&& ( $insertCount %
$batchSize ) == 0 ) {
162 $dbw->commit( __METHOD__
, 'flush' );
163 wfGetLBFactory()->waitForReplication();
167 foreach ( $categoryDeletes as $categoryName ) {
168 $categoryTitle = Title
::makeTitle( NS_CATEGORY
, $categoryName );
169 $catMembChange->triggerCategoryRemovedNotification( $categoryTitle );
170 if ( $insertCount++
&& ( $insertCount++ %
$batchSize ) == 0 ) {
171 $dbw->commit( __METHOD__
, 'flush' );
172 wfGetLBFactory()->waitForReplication();
177 private function getExplicitCategoriesChanges(
178 Title
$title, Revision
$newRev, Revision
$oldRev = null
180 // Inject the same timestamp for both revision parses to avoid seeing category changes
181 // due to time-based parser functions. Inject the same page title for the parses too.
182 // Note that REPEATABLE-READ makes template/file pages appear unchanged between parses.
183 $parseTimestamp = $newRev->getTimestamp();
184 // Parse the old rev and get the categories. Do not use link tables as that
185 // assumes these updates are perfectly FIFO and that link tables are always
186 // up to date, neither of which are true.
187 $oldCategories = $oldRev
188 ?
$this->getCategoriesAtRev( $title, $oldRev, $parseTimestamp )
190 // Parse the new revision and get the categories
191 $newCategories = $this->getCategoriesAtRev( $title, $newRev, $parseTimestamp );
193 $categoryInserts = array_values( array_diff( $newCategories, $oldCategories ) );
194 $categoryDeletes = array_values( array_diff( $oldCategories, $newCategories ) );
196 return array( $categoryInserts, $categoryDeletes );
200 * @param Title $title
201 * @param Revision $rev
202 * @param string $parseTimestamp TS_MW
204 * @return string[] category names
206 private function getCategoriesAtRev( Title
$title, Revision
$rev, $parseTimestamp ) {
207 $content = $rev->getContent();
208 $options = $content->getContentHandler()->makeParserOptions( 'canonical' );
209 $options->setTimestamp( $parseTimestamp );
210 // This could possibly use the parser cache if it checked the revision ID,
211 // but that's more complicated than it's worth.
212 $output = $content->getParserOutput( $title, $rev->getId(), $options );
214 // array keys will cast numeric category names to ints
215 // so we need to cast them back to strings to avoid breaking things!
216 return array_map( 'strval', array_keys( $output->getCategories() ) );
219 public function getDeduplicationInfo() {
220 $info = parent
::getDeduplicationInfo();
221 unset( $info['params']['revTimestamp'] ); // first job wins