Add partial support for running Parsoid selser tests
[mediawiki.git] / maintenance / categoryChangesAsRdf.php
blob1f95a98d0cf8cb7342242bf0ae65d86b3ab7e492
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
19 use Wikimedia\Purtle\RdfWriter;
20 use Wikimedia\Purtle\TurtleRdfWriter;
21 use Wikimedia\Rdbms\IDatabase;
23 require_once __DIR__ . '/Maintenance.php';
25 /**
26 * Maintenance script to provide RDF representation of the recent changes in category tree.
28 * @ingroup Maintenance
29 * @since 1.30
31 class CategoryChangesAsRdf extends Maintenance {
32 /**
33 * Insert query
35 private const SPARQL_INSERT = <<<SPARQL
36 INSERT DATA {
40 SPARQL;
42 /**
43 * Delete query
45 private const SPARQL_DELETE = <<<SPARQLD
46 DELETE {
47 ?category ?x ?y
48 } WHERE {
49 ?category ?x ?y
50 VALUES ?category {
55 SPARQLD;
57 /**
58 * @var RdfWriter
60 private $rdfWriter;
61 /**
62 * Categories RDF helper.
63 * @var CategoriesRdf
65 private $categoriesRdf;
67 private $startTS;
68 private $endTS;
70 /**
71 * List of processed page IDs,
72 * so we don't try to process same thing twice
73 * @var true[]
75 protected $processed = [];
77 public function __construct() {
78 parent::__construct();
80 $this->addDescription( "Generate RDF dump of category changes in a wiki." );
82 $this->setBatchSize( 200 );
83 $this->addOption( 'output', "Output file (default is stdout). Will be overwritten.", false,
84 true, 'o' );
85 $this->addOption( 'start', 'Starting timestamp (inclusive), in ISO or Mediawiki format.',
86 true, true, 's' );
87 $this->addOption( 'end', 'Ending timestamp (exclusive), in ISO or Mediawiki format.', true,
88 true, 'e' );
91 /**
92 * Initialize external service classes.
94 public function initialize() {
95 // SPARQL Update syntax is close to Turtle format, so we can use Turtle writer.
96 $this->rdfWriter = new TurtleRdfWriter();
97 $this->categoriesRdf = new CategoriesRdf( $this->rdfWriter );
100 public function execute() {
101 $this->initialize();
102 $startTS = new MWTimestamp( $this->getOption( "start" ) );
104 $endTS = new MWTimestamp( $this->getOption( "end" ) );
105 $now = new MWTimestamp();
106 $rcMaxAge = $this->getConfig()->get( 'RCMaxAge' );
108 if ( (int)$now->getTimestamp( TS_UNIX ) - (int)$startTS->getTimestamp( TS_UNIX ) > $rcMaxAge ) {
109 $this->error( "Start timestamp too old, maximum RC age is $rcMaxAge!" );
111 if ( (int)$now->getTimestamp( TS_UNIX ) - (int)$endTS->getTimestamp( TS_UNIX ) > $rcMaxAge ) {
112 $this->error( "End timestamp too old, maximum RC age is $rcMaxAge!" );
115 $this->startTS = $startTS->getTimestamp();
116 $this->endTS = $endTS->getTimestamp();
118 $outFile = $this->getOption( 'output', 'php://stdout' );
119 if ( $outFile === '-' ) {
120 $outFile = 'php://stdout';
123 $output = fopen( $outFile, 'wb' );
125 $this->categoriesRdf->setupPrefixes();
126 $this->rdfWriter->start();
128 $prefixes = $this->getRdf();
129 // We have to strip @ from prefix, since SPARQL UPDATE doesn't use them
130 // Also strip dot at the end.
131 $prefixes = preg_replace( [ '/^@/m', '/\s*[.]$/m' ], '', $prefixes );
132 fwrite( $output, $prefixes );
134 $dbr = $this->getDB( DB_REPLICA, [ 'vslow' ] );
136 // Deletes go first because if the page was deleted, other changes
137 // do not matter. This only gets true deletes, i.e. not pages that were restored.
138 $this->handleDeletes( $dbr, $output );
139 // Moves go before additions because if category is moved, we should not process creation
140 // as it would produce wrong data - because create row has old title
141 $this->handleMoves( $dbr, $output );
142 // We need to handle restores too since delete may have happened in previous update.
143 $this->handleRestores( $dbr, $output );
144 // Process newly added pages
145 $this->handleAdds( $dbr, $output );
146 // Process page edits
147 $this->handleEdits( $dbr, $output );
148 // Process categorization changes
149 $this->handleCategorization( $dbr, $output );
151 // Update timestamp
152 fwrite( $output, $this->updateTS( $this->endTS ) );
156 * Get the text of SPARQL INSERT DATA clause
157 * @return string
159 private function getInsertRdf() {
160 $rdfText = $this->getRdf();
161 if ( !$rdfText ) {
162 return "";
164 return sprintf( self::SPARQL_INSERT, $rdfText );
168 * Get SPARQL for updating set of categories
169 * @param IDatabase $dbr
170 * @param string[] $deleteUrls List of URIs to be deleted, with <>
171 * @param string[] $pages List of categories: id => title
172 * @param string $mark Marks which operation requests the query
173 * @return string SPARQL query
175 private function getCategoriesUpdate( IDatabase $dbr, $deleteUrls, $pages, $mark ) {
176 if ( empty( $deleteUrls ) ) {
177 return "";
180 if ( !empty( $pages ) ) {
181 $this->writeParentCategories( $dbr, $pages );
184 return "# $mark\n" . sprintf( self::SPARQL_DELETE, implode( ' ', $deleteUrls ) ) .
185 $this->getInsertRdf();
189 * Write parent data for a set of categories.
190 * The list has the child categories.
191 * @param IDatabase $dbr
192 * @param string[] $pages List of child categories: id => title
194 private function writeParentCategories( IDatabase $dbr, $pages ) {
195 foreach ( $this->getCategoryLinksIterator( $dbr, array_keys( $pages ), __METHOD__ ) as $row ) {
196 $this->categoriesRdf->writeCategoryLinkData( $pages[$row->cl_from], $row->cl_to );
201 * Generate SPARQL Update code for updating dump timestamp
202 * @param string|int $timestamp Timestamp for last change
203 * @return string SPARQL Update query for timestamp.
205 public function updateTS( $timestamp ) {
206 $dumpUrl = '<' . $this->categoriesRdf->getDumpURI() . '>';
207 $ts = wfTimestamp( TS_ISO_8601, $timestamp );
208 $tsQuery = <<<SPARQL
209 DELETE {
210 $dumpUrl schema:dateModified ?o .
212 WHERE {
213 $dumpUrl schema:dateModified ?o .
215 INSERT DATA {
216 $dumpUrl schema:dateModified "$ts"^^xsd:dateTime .
219 SPARQL;
220 return $tsQuery;
224 * Set up standard iterator for retrieving category changes.
225 * @param IDatabase $dbr
226 * @param string[] $columns List of additional fields to get
227 * @param string[] $extra_tables List of additional tables to join
228 * @param string $fname Name of the calling function
229 * @return BatchRowIterator
231 private function setupChangesIterator(
232 IDatabase $dbr,
233 array $columns = [],
234 array $extra_tables = [],
235 $fname = __METHOD__
237 $tables = [ 'recentchanges', 'page_props', 'category' ];
238 if ( $extra_tables ) {
239 $tables = array_merge( $tables, $extra_tables );
241 $it = new BatchRowIterator( $dbr,
242 $tables,
243 [ 'rc_timestamp' ],
244 $this->mBatchSize
246 $this->addTimestampConditions( $it, $dbr );
247 $it->addJoinConditions(
249 'page_props' => [
250 'LEFT JOIN', [ 'pp_propname' => 'hiddencat', 'pp_page = rc_cur_id' ]
252 'category' => [
253 'LEFT JOIN', [ 'cat_title = rc_title' ]
257 $it->setFetchColumns( array_merge( $columns, [
258 'rc_title',
259 'rc_cur_id',
260 'pp_propname',
261 'cat_pages',
262 'cat_subcats',
263 'cat_files'
264 ] ) );
265 $it->setCaller( $fname );
266 return $it;
270 * Fetch newly created categories
271 * @param IDatabase $dbr
272 * @param string $fname Name of the calling function
273 * @return BatchRowIterator
275 protected function getNewCatsIterator( IDatabase $dbr, $fname ) {
276 $it = $this->setupChangesIterator( $dbr, [], [], $fname );
277 $it->addConditions( [
278 'rc_namespace' => NS_CATEGORY,
279 'rc_new' => 1,
280 ] );
281 return $it;
285 * Fetch moved categories
286 * @param IDatabase $dbr
287 * @param string $fname Name of the calling function
288 * @return BatchRowIterator
290 protected function getMovedCatsIterator( IDatabase $dbr, $fname ) {
291 $it = $this->setupChangesIterator(
292 $dbr,
293 [ 'page_title', 'page_namespace' ],
294 [ 'page' ],
295 $fname
297 $it->addConditions( [
298 'rc_namespace' => NS_CATEGORY,
299 'rc_new' => 0,
300 'rc_log_type' => 'move',
301 'rc_type' => RC_LOG,
302 ] );
303 $it->addJoinConditions( [
304 'page' => [ 'JOIN', 'rc_cur_id = page_id' ],
305 ] );
306 $this->addIndex( $it, $dbr );
307 return $it;
311 * Fetch deleted categories
312 * @param IDatabase $dbr
313 * @param string $fname Name of the calling function
314 * @return BatchRowIterator
316 protected function getDeletedCatsIterator( IDatabase $dbr, $fname ) {
317 $it = new BatchRowIterator( $dbr,
318 'recentchanges',
319 [ 'rc_timestamp' ],
320 $this->mBatchSize
322 $this->addTimestampConditions( $it, $dbr );
323 $it->addConditions( [
324 'rc_namespace' => NS_CATEGORY,
325 'rc_new' => 0,
326 'rc_log_type' => 'delete',
327 'rc_log_action' => 'delete',
328 'rc_type' => RC_LOG,
329 // We will fetch ones that do not have page record. If they do,
330 // this means they were restored, thus restoring handler will pick it up.
331 'NOT EXISTS (SELECT * FROM page WHERE page_id = rc_cur_id)',
332 ] );
333 $this->addIndex( $it, $dbr );
334 $it->setFetchColumns( [ 'rc_cur_id', 'rc_title' ] );
335 $it->setCaller( $fname );
336 return $it;
340 * Fetch restored categories
341 * @param IDatabase $dbr
342 * @param string $fname Name of the calling function
343 * @return BatchRowIterator
345 protected function getRestoredCatsIterator( IDatabase $dbr, $fname ) {
346 $it = $this->setupChangesIterator( $dbr, [], [], $fname );
347 $it->addConditions( [
348 'rc_namespace' => NS_CATEGORY,
349 'rc_new' => 0,
350 'rc_log_type' => 'delete',
351 'rc_log_action' => 'restore',
352 'rc_type' => RC_LOG,
353 // We will only fetch ones that have page record
354 'EXISTS (SELECT page_id FROM page WHERE page_id = rc_cur_id)',
355 ] );
356 $this->addIndex( $it, $dbr );
357 return $it;
361 * Fetch categorization changes or edits
362 * @param IDatabase $dbr
363 * @param int $type
364 * @param string $fname Name of the calling function
365 * @return BatchRowIterator
367 protected function getChangedCatsIterator( IDatabase $dbr, $type, $fname ) {
368 $it = $this->setupChangesIterator( $dbr, [], [], $fname );
369 $it->addConditions( [
370 'rc_namespace' => NS_CATEGORY,
371 'rc_new' => 0,
372 'rc_type' => $type,
373 ] );
374 $this->addIndex( $it, $dbr );
375 return $it;
379 * Add timestamp limits to iterator
380 * @param BatchRowIterator $it Iterator
381 * @param IDatabase $dbr
383 private function addTimestampConditions( BatchRowIterator $it, IDatabase $dbr ) {
384 $it->addConditions( [
385 'rc_timestamp >= ' . $dbr->addQuotes( $dbr->timestamp( $this->startTS ) ),
386 'rc_timestamp < ' . $dbr->addQuotes( $dbr->timestamp( $this->endTS ) ),
387 ] );
391 * Need to force index, somehow on terbium the optimizer chooses wrong one
392 * @param BatchRowIterator $it
393 * @param IDatabase $dbr
395 private function addIndex( BatchRowIterator $it, IDatabase $dbr ) {
396 $it->addOptions( [
397 'USE INDEX' => [ 'recentchanges' => 'rc_new_name_timestamp' ]
398 ] );
402 * Get iterator for links for categories.
403 * @param IDatabase $dbr
404 * @param int[] $ids List of page IDs
405 * @param string $fname Name of the calling function
406 * @return Traversable
408 protected function getCategoryLinksIterator( IDatabase $dbr, array $ids, $fname ) {
409 $it = new BatchRowIterator(
410 $dbr,
411 'categorylinks',
412 [ 'cl_from', 'cl_to' ],
413 $this->mBatchSize
415 $it->addConditions( [
416 'cl_type' => 'subcat',
417 'cl_from' => $ids
418 ] );
419 $it->setFetchColumns( [ 'cl_from', 'cl_to' ] );
420 $it->setCaller( $fname );
421 return new RecursiveIteratorIterator( $it );
425 * Get accumulated RDF.
426 * @return string
428 public function getRdf() {
429 return $this->rdfWriter->drain();
433 * Handle category deletes.
434 * @param IDatabase $dbr
435 * @param resource $output File to write the output
437 public function handleDeletes( IDatabase $dbr, $output ) {
438 // This only does "true" deletes - i.e. those that the page stays deleted
440 foreach ( $this->getDeletedCatsIterator( $dbr, __METHOD__ ) as $batch ) {
441 $deleteUrls = [];
442 foreach ( $batch as $row ) {
443 // This can produce duplicates, we don't care
444 $deleteUrls[] = '<' . $this->categoriesRdf->labelToUrl( $row->rc_title ) . '>';
445 $this->processed[$row->rc_cur_id] = true;
447 fwrite( $output, $this->getCategoriesUpdate( $dbr, $deleteUrls, [], "Deletes" ) );
452 * Write category data to RDF.
453 * @param stdclass $row Database row
455 private function writeCategoryData( $row ) {
456 $this->categoriesRdf->writeCategoryData(
457 $row->rc_title,
458 $row->pp_propname === 'hiddencat',
459 (int)$row->cat_pages - (int)$row->cat_subcats - (int)$row->cat_files,
460 (int)$row->cat_subcats
465 * @param IDatabase $dbr
466 * @param resource $output
468 public function handleMoves( IDatabase $dbr, $output ) {
469 foreach ( $this->getMovedCatsIterator( $dbr, __METHOD__ ) as $batch ) {
470 $pages = [];
471 $deleteUrls = [];
472 foreach ( $batch as $row ) {
473 $deleteUrls[] = '<' . $this->categoriesRdf->labelToUrl( $row->rc_title ) . '>';
475 if ( isset( $this->processed[$row->rc_cur_id] ) ) {
476 // We already captured this one before
477 continue;
480 if ( $row->page_namespace != NS_CATEGORY ) {
481 // If page was moved out of Category:, we'll just delete
482 continue;
484 $row->rc_title = $row->page_title;
485 $this->writeCategoryData( $row );
486 $pages[$row->rc_cur_id] = $row->page_title;
487 $this->processed[$row->rc_cur_id] = true;
490 fwrite( $output, $this->getCategoriesUpdate( $dbr, $deleteUrls, $pages, "Moves" ) );
495 * @param IDatabase $dbr
496 * @param resource $output
498 public function handleRestores( IDatabase $dbr, $output ) {
499 fwrite( $output, "# Restores\n" );
501 // This will only find those restores that were not deleted later.
502 foreach ( $this->getRestoredCatsIterator( $dbr, __METHOD__ ) as $batch ) {
503 $pages = [];
504 foreach ( $batch as $row ) {
505 if ( isset( $this->processed[$row->rc_cur_id] ) ) {
506 // We already captured this one before
507 continue;
509 $this->writeCategoryData( $row );
510 $pages[$row->rc_cur_id] = $row->rc_title;
511 $this->processed[$row->rc_cur_id] = true;
514 if ( empty( $pages ) ) {
515 continue;
518 $this->writeParentCategories( $dbr, $pages );
520 fwrite( $output, $this->getInsertRdf() );
525 * @param IDatabase $dbr
526 * @param resource $output
528 public function handleAdds( IDatabase $dbr, $output ) {
529 fwrite( $output, "# Additions\n" );
531 foreach ( $this->getNewCatsIterator( $dbr, __METHOD__ ) as $batch ) {
532 $pages = [];
533 foreach ( $batch as $row ) {
534 if ( isset( $this->processed[$row->rc_cur_id] ) ) {
535 // We already captured this one before
536 continue;
538 $this->writeCategoryData( $row );
539 $pages[$row->rc_cur_id] = $row->rc_title;
540 $this->processed[$row->rc_cur_id] = true;
543 if ( empty( $pages ) ) {
544 continue;
547 $this->writeParentCategories( $dbr, $pages );
548 fwrite( $output, $this->getInsertRdf() );
553 * Handle edits for category texts
554 * @param IDatabase $dbr
555 * @param resource $output
557 public function handleEdits( IDatabase $dbr, $output ) {
558 // Editing category can change hidden flag and add new parents.
559 // TODO: it's pretty expensive to update all edited categories, and most edits
560 // aren't actually interesting for us. Some way to know which are interesting?
561 // We can capture recategorization on the next step, but not change in hidden status.
563 foreach ( $this->getChangedCatsIterator( $dbr, RC_EDIT, __METHOD__ ) as $batch ) {
564 $pages = [];
565 $deleteUrls = [];
566 foreach ( $batch as $row ) {
567 // Note that on categorization event, cur_id points to
568 // the child page, not the parent category!
569 if ( isset( $this->processed[$row->rc_cur_id] ) ) {
570 // We already captured this one before
571 continue;
573 $this->writeCategoryData( $row );
574 $pages[$row->rc_cur_id] = $row->rc_title;
575 $this->processed[$row->rc_cur_id] = true;
576 $deleteUrls[] = '<' . $this->categoriesRdf->labelToUrl( $row->rc_title ) . '>';
579 fwrite( $output, $this->getCategoriesUpdate( $dbr, $deleteUrls, $pages, "Edits" ) );
584 * Handles categorization changes
585 * @param IDatabase $dbr
586 * @param resource $output
588 public function handleCategorization( IDatabase $dbr, $output ) {
589 $processedTitle = [];
591 // Categorization change can add new parents and change counts
592 // for the parent category.
594 foreach ( $this->getChangedCatsIterator( $dbr, RC_CATEGORIZE, __METHOD__ ) as $batch ) {
596 * Note that on categorization event, cur_id points to
597 * the child page, not the parent category!
598 * So we need to have a two-stage process, since we have ID from one
599 * category and title from another, and we need both for proper updates.
600 * TODO: For now, we do full update even though some data hasn't changed,
601 * e.g. parents for parent cat and counts for child cat.
603 $childPages = [];
604 $parentCats = [];
605 foreach ( $batch as $row ) {
606 $childPages[$row->rc_cur_id] = true;
607 $parentCats[$row->rc_title] = true;
610 $joinConditions = [
611 'page_props' => [
612 'LEFT JOIN',
613 [ 'pp_propname' => 'hiddencat', 'pp_page = page_id' ],
615 'category' => [
616 'LEFT JOIN',
617 [ 'cat_title = page_title' ],
621 $pages = [];
622 $deleteUrls = [];
624 if ( $childPages ) {
625 // Load child rows by ID
626 $childRows = $dbr->select(
627 [ 'page', 'page_props', 'category' ],
629 'page_id',
630 'rc_title' => 'page_title',
631 'pp_propname',
632 'cat_pages',
633 'cat_subcats',
634 'cat_files',
636 [ 'page_namespace' => NS_CATEGORY, 'page_id' => array_keys( $childPages ) ],
637 __METHOD__,
639 $joinConditions
641 foreach ( $childRows as $row ) {
642 if ( isset( $this->processed[$row->page_id] ) ) {
643 // We already captured this one before
644 continue;
646 $this->writeCategoryData( $row );
647 if ( $row->page_id ) {
648 $pages[$row->page_id] = $row->rc_title;
649 $deleteUrls[] = '<' . $this->categoriesRdf->labelToUrl( $row->rc_title ) . '>';
650 $this->processed[$row->page_id] = true;
655 if ( $parentCats ) {
656 // Load parent rows by title
657 $joinConditions = [
658 'page' => [
659 'LEFT JOIN',
660 [ 'page_title = cat_title', 'page_namespace' => NS_CATEGORY ],
662 'page_props' => [
663 'LEFT JOIN',
664 [ 'pp_propname' => 'hiddencat', 'pp_page = page_id' ],
668 $parentRows = $dbr->select(
669 [ 'category', 'page', 'page_props' ],
671 'page_id',
672 'rc_title' => 'cat_title',
673 'pp_propname',
674 'cat_pages',
675 'cat_subcats',
676 'cat_files',
678 [ 'cat_title' => array_map( 'strval', array_keys( $parentCats ) ) ],
679 __METHOD__,
681 $joinConditions
683 foreach ( $parentRows as $row ) {
684 if ( $row->page_id && isset( $this->processed[$row->page_id] ) ) {
685 // We already captured this one before
686 continue;
688 if ( isset( $processedTitle[$row->rc_title] ) ) {
689 // We already captured this one before
690 continue;
692 $this->writeCategoryData( $row );
693 if ( $row->page_id ) {
694 $pages[$row->page_id] = $row->rc_title;
695 $deleteUrls[] = '<' . $this->categoriesRdf->labelToUrl( $row->rc_title ) . '>';
696 $this->processed[$row->page_id] = true;
698 $processedTitle[$row->rc_title] = true;
702 fwrite( $output, $this->getCategoriesUpdate( $dbr, $deleteUrls, $pages, "Changes" ) );
707 $maintClass = CategoryChangesAsRdf::class;
708 require_once RUN_MAINTENANCE_IF_MAIN;