Merge "Installer: Link 4.0 family of CC licenses"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / CategoryMembershipChangeJob.php
1 <?php
2 /**
3 * Updater for link tracking tables after a page edit.
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 */
22
23 /**
24 * Job to add recent change entries mentioning category membership changes
25 *
26 * Parameters include:
27 * - pageId : page ID
28 * - revTimestamp : timestamp of the triggering revision
29 *
30 * Category changes will be mentioned for revisions at/after the timestamp for this page
31 *
32 * @since 1.27
33 */
34 class CategoryMembershipChangeJob extends Job {
35 const ENQUEUE_FUDGE_SEC = 60;
36
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;
42 }
43
44 public function run() {
45 $page = WikiPage::newFromID( $this->params['pageId'], WikiPage::READ_LATEST );
46 if ( !$page ) {
47 $this->setLastError( "Could not find page #{$this->params['pageId']}" );
48 return false; // deleted?
49 }
50
51 $dbw = wfGetDB( DB_MASTER );
52
53 // Use a named lock so that jobs for this page see each others' changes
54 $fname = __METHOD__;
55 $lockKey = "CategoryMembershipUpdates:{$page->getId()}";
56 if ( !$dbw->lock( $lockKey, $fname, 10 ) ) {
57 $this->setLastError( "Could not acquire lock '$lockKey'" );
58 return false;
59 }
60
61 $unlocker = new ScopedCallback( function () use ( $dbw, $lockKey, $fname ) {
62 $dbw->unlock( $lockKey, $fname );
63 } );
64
65 // Sanity: clear any DB transaction snapshot
66 $dbw->commit( __METHOD__, 'flush' );
67
68 $cutoffUnix = wfTimestamp( TS_UNIX, $this->params['revTimestamp'] );
69 // Using ENQUEUE_FUDGE_SEC handles jobs inserted out of revision order due to the delay
70 // between COMMIT and actual enqueueing of the CategoryMembershipChangeJob job.
71 $cutoffUnix -= self::ENQUEUE_FUDGE_SEC;
72
73 // Get the newest revision that has a SRC_CATEGORIZE row...
74 $row = $dbw->selectRow(
75 array( 'revision', 'recentchanges' ),
76 array( 'rev_timestamp', 'rev_id' ),
77 array(
78 'rev_page' => $page->getId(),
79 'rev_timestamp >= ' . $dbw->addQuotes( $dbw->timestamp( $cutoffUnix ) )
80 ),
81 __METHOD__,
82 array( 'ORDER BY' => 'rev_timestamp DESC, rev_id DESC' ),
83 array(
84 'recentchanges' => array(
85 'INNER JOIN',
86 array(
87 'rc_this_oldid = rev_id',
88 'rc_source' => RecentChange::SRC_CATEGORIZE,
89 // Allow rc_cur_id or rc_timestamp index usage
90 'rc_cur_id = rev_page',
91 'rc_timestamp >= rev_timestamp'
92 )
93 )
94 )
95 );
96 // Only consider revisions newer than any such revision
97 if ( $row ) {
98 $cutoffUnix = wfTimestamp( TS_UNIX, $row->rev_timestamp );
99 $lastRevId = (int)$row->rev_id;
100 } else {
101 $lastRevId = 0;
102 }
103
104 // Find revisions to this page made around and after this revision which lack category
105 // notifications in recent changes. This lets jobs pick up were the last one left off.
106 $encCutoff = $dbw->addQuotes( $dbw->timestamp( $cutoffUnix ) );
107 $res = $dbw->select(
108 'revision',
109 Revision::selectFields(),
110 array(
111 'rev_page' => $page->getId(),
112 "rev_timestamp > $encCutoff" .
113 " OR (rev_timestamp = $encCutoff AND rev_id > $lastRevId)"
114 ),
115 __METHOD__,
116 array( 'ORDER BY' => 'rev_timestamp ASC, rev_id ASC' )
117 );
118
119 // Apply all category updates in revision timestamp order
120 foreach ( $res as $row ) {
121 $this->notifyUpdatesForRevision( $page, Revision::newFromRow( $row ) );
122 }
123
124 ScopedCallback::consume( $unlocker );
125
126 return true;
127 }
128
129 /**
130 * @param WikiPage $page
131 * @param Revision $newRev
132 * @throws MWException
133 */
134 protected function notifyUpdatesForRevision( WikiPage $page, Revision $newRev ) {
135 $config = RequestContext::getMain()->getConfig();
136 $title = $page->getTitle();
137
138 // Get the new revision
139 if ( !$newRev->getContent() ) {
140 return; // deleted?
141 }
142
143 // Get the prior revision (the same for null edits)
144 if ( $newRev->getParentId() ) {
145 $oldRev = Revision::newFromId( $newRev->getParentId(), Revision::READ_LATEST );
146 if ( !$oldRev->getContent() ) {
147 return; // deleted?
148 }
149 } else {
150 $oldRev = null;
151 }
152
153 // Parse the new revision and get the categories
154 $categoryChanges = $this->getExplicitCategoriesChanges( $title, $newRev, $oldRev );
155 list( $categoryInserts, $categoryDeletes ) = $categoryChanges;
156 if ( !$categoryInserts && !$categoryDeletes ) {
157 return; // nothing to do
158 }
159
160 $dbw = wfGetDB( DB_MASTER );
161 $catMembChange = new CategoryMembershipChange( $title, $newRev );
162 $catMembChange->checkTemplateLinks();
163
164 $batchSize = $config->get( 'UpdateRowsPerQuery' );
165 $insertCount = 0;
166
167 foreach ( $categoryInserts as $categoryName ) {
168 $categoryTitle = Title::makeTitle( NS_CATEGORY, $categoryName );
169 $catMembChange->triggerCategoryAddedNotification( $categoryTitle );
170 if ( $insertCount++ && ( $insertCount % $batchSize ) == 0 ) {
171 $dbw->commit( __METHOD__, 'flush' );
172 wfWaitForSlaves();
173 }
174 }
175
176 foreach ( $categoryDeletes as $categoryName ) {
177 $categoryTitle = Title::makeTitle( NS_CATEGORY, $categoryName );
178 $catMembChange->triggerCategoryRemovedNotification( $categoryTitle );
179 if ( $insertCount++ && ( $insertCount++ % $batchSize ) == 0 ) {
180 $dbw->commit( __METHOD__, 'flush' );
181 wfWaitForSlaves();
182 }
183 }
184 }
185
186 private function getExplicitCategoriesChanges(
187 Title $title, Revision $newRev, Revision $oldRev = null
188 ) {
189 // Inject the same timestamp for both revision parses to avoid seeing category changes
190 // due to time-based parser functions. Inject the same page title for the parses too.
191 // Note that REPEATABLE-READ makes template/file pages appear unchanged between parses.
192 $parseTimestamp = $newRev->getTimestamp();
193 // Parse the old rev and get the categories. Do not use link tables as that
194 // assumes these updates are perfectly FIFO and that link tables are always
195 // up to date, neither of which are true.
196 $oldCategories = $oldRev
197 ? $this->getCategoriesAtRev( $title, $oldRev, $parseTimestamp )
198 : array();
199 // Parse the new revision and get the categories
200 $newCategories = $this->getCategoriesAtRev( $title, $newRev, $parseTimestamp );
201
202 $categoryInserts = array_values( array_diff( $newCategories, $oldCategories ) );
203 $categoryDeletes = array_values( array_diff( $oldCategories, $newCategories ) );
204
205 return array( $categoryInserts, $categoryDeletes );
206 }
207
208 /**
209 * @param Title $title
210 * @param Revision $rev
211 * @param string $parseTimestamp TS_MW
212 *
213 * @return string[] category names
214 */
215 private function getCategoriesAtRev( Title $title, Revision $rev, $parseTimestamp ) {
216 $content = $rev->getContent();
217 $options = $content->getContentHandler()->makeParserOptions( 'canonical' );
218 $options->setTimestamp( $parseTimestamp );
219 // This could possibly use the parser cache if it checked the revision ID,
220 // but that's more complicated than it's worth.
221 $output = $content->getParserOutput( $title, $rev->getId(), $options );
222
223 // array keys will cast numeric category names to ints
224 // so we need to cast them back to strings to avoid breaking things!
225 return array_map( 'strval', array_keys( $output->getCategories() ) );
226 }
227
228 public function getDeduplicationInfo() {
229 $info = parent::getDeduplicationInfo();
230 unset( $info['params']['revTimestamp'] ); // first job wins
231
232 return $info;
233 }
234 }