12use Wikimedia\Purtle\RdfWriter;
13use Wikimedia\Purtle\TurtleRdfWriter;
15use Wikimedia\Timestamp\TimestampFormat as TS;
18require_once __DIR__ .
'/Maintenance.php';
31 private const SPARQL_INSERT = <<<SPARQL
41 private const SPARQL_DELETE = <<<SPARQLD
61 private $categoriesRdf;
76 parent::__construct();
78 $this->
addDescription(
"Generate RDF dump of category changes in a wiki." );
81 $this->
addOption(
'output',
"Output file (default is stdout). Will be overwritten.",
false,
83 $this->
addOption(
'start',
'Starting timestamp (inclusive), in ISO or MediaWiki format.',
85 $this->
addOption(
'end',
'Ending timestamp (exclusive), in ISO or MediaWiki format.',
true,
94 $this->rdfWriter =
new TurtleRdfWriter();
104 $rcMaxAge = $this->
getConfig()->get( MainConfigNames::RCMaxAge );
106 if ( (
int)$now->getTimestamp( TS::UNIX ) - (
int)$startTS->getTimestamp( TS::UNIX ) > $rcMaxAge ) {
107 $this->
error(
"Start timestamp too old, maximum RC age is $rcMaxAge!" );
109 if ( (
int)$now->getTimestamp( TS::UNIX ) - (
int)$endTS->getTimestamp( TS::UNIX ) > $rcMaxAge ) {
110 $this->
error(
"End timestamp too old, maximum RC age is $rcMaxAge!" );
113 $this->startTS = $startTS->getTimestamp();
114 $this->endTS = $endTS->getTimestamp();
116 $outFile = $this->
getOption(
'output',
'php://stdout' );
117 if ( $outFile ===
'-' ) {
118 $outFile =
'php://stdout';
121 $output = fopen( $outFile,
'wb' );
123 $this->categoriesRdf->setupPrefixes();
124 $this->rdfWriter->start();
126 $prefixes = $this->
getRdf();
129 $prefixes = preg_replace( [
'/^@/m',
'/\s*[.]$/m' ],
'', $prefixes );
130 fwrite( $output, $prefixes );
150 fwrite( $output, $this->
updateTS( $this->endTS ) );
157 private function getInsertRdf() {
158 $rdfText = $this->
getRdf();
162 return sprintf( self::SPARQL_INSERT, $rdfText );
173 private function getCategoriesUpdate(
IReadableDatabase $dbr, $deleteUrls, $pages, $mark ) {
174 if ( !$deleteUrls ) {
179 $this->writeParentCategories( $dbr, $pages );
182 return "# $mark\n" . sprintf( self::SPARQL_DELETE, implode(
' ', $deleteUrls ) ) .
183 $this->getInsertRdf();
194 $this->categoriesRdf->writeCategoryLinkData( $pages[$row->cl_from], $row->lt_title );
204 $dumpUrl =
'<' . $this->categoriesRdf->getDumpURI() .
'>';
208 $dumpUrl schema:dateModified ?o .
211 $dumpUrl schema:dateModified ?o .
214 $dumpUrl schema:dateModified
"$ts"^^xsd:dateTime .
228 private function setupChangesIterator(
235 ->from(
'recentchanges' )
236 ->leftJoin(
'page_props',
null, [
'pp_propname' =>
'hiddencat',
'pp_page = rc_cur_id' ] )
237 ->leftJoin(
'category',
null, [
'cat_title = rc_title' ] )
238 ->select( array_merge( $columns, [
250 $this->addTimestampConditions( $it, $dbr );
261 $it = $this->setupChangesIterator( $dbr, [], $fname );
264 'rc_source' => RecentChange::SRC_NEW,
276 $it = $this->setupChangesIterator(
278 [
'page_title',
'page_namespace' ],
283 'rc_source' => RecentChange::SRC_LOG,
284 'rc_log_type' =>
'move',
286 $it->sqb->join(
'page',
null,
'rc_cur_id = page_id' );
287 $this->addIndex( $it );
300 ->from(
'recentchanges' )
301 ->select( [
'rc_cur_id',
'rc_title' ] )
304 'rc_source' => RecentChange::SRC_LOG,
305 'rc_log_type' =>
'delete',
306 'rc_log_action' =>
'delete',
309 'NOT EXISTS (SELECT * FROM page WHERE page_id = rc_cur_id)',
315 $this->addTimestampConditions( $it, $dbr );
316 $this->addIndex( $it );
327 $it = $this->setupChangesIterator( $dbr, [], $fname );
330 'rc_source' => RecentChange::SRC_LOG,
331 'rc_log_type' =>
'delete',
332 'rc_log_action' =>
'restore',
334 'EXISTS (SELECT page_id FROM page WHERE page_id = rc_cur_id)',
336 $this->addIndex( $it );
348 $it = $this->setupChangesIterator( $dbr, [], $fname );
353 $this->addIndex( $it );
364 $dbr->
expr(
'rc_timestamp',
'>=', $dbr->
timestamp( $this->startTS ) ),
365 $dbr->
expr(
'rc_timestamp',
'<', $dbr->
timestamp( $this->endTS ) ),
374 'USE INDEX' => [
'recentchanges' =>
'rc_source_name_timestamp' ]
387 ->select( [
'cl_from',
'lt_title' ] )
388 ->from(
'categorylinks' )
389 ->join(
'linktarget',
null,
'cl_target_id=lt_id' )
391 'cl_type' =>
'subcat',
395 $primaryKey = [
'cl_from',
'cl_target_id' ];
403 return new RecursiveIteratorIterator( $it );
411 return $this->rdfWriter->drain();
424 foreach ( $batch as $row ) {
426 $deleteUrls[] =
'<' . $this->categoriesRdf->labelToUrl( $row->rc_title ) .
'>';
427 $this->processed[$row->rc_cur_id] =
true;
429 fwrite( $output, $this->getCategoriesUpdate( $dbr, $deleteUrls, [],
"Deletes" ) );
437 private function writeCategoryData( $row ) {
438 $this->categoriesRdf->writeCategoryData(
440 $row->pp_propname ===
'hiddencat',
441 (
int)$row->cat_pages - (
int)$row->cat_subcats - (
int)$row->cat_files,
442 (
int)$row->cat_subcats
454 foreach ( $batch as $row ) {
455 $deleteUrls[] =
'<' . $this->categoriesRdf->labelToUrl( $row->rc_title ) .
'>';
457 if ( isset( $this->processed[$row->rc_cur_id] ) ) {
466 $row->rc_title = $row->page_title;
467 $this->writeCategoryData( $row );
468 $pages[$row->rc_cur_id] = $row->page_title;
469 $this->processed[$row->rc_cur_id] =
true;
472 fwrite( $output, $this->getCategoriesUpdate( $dbr, $deleteUrls, $pages,
"Moves" ) );
481 fwrite( $output,
"# Restores\n" );
486 foreach ( $batch as $row ) {
487 if ( isset( $this->processed[$row->rc_cur_id] ) ) {
491 $this->writeCategoryData( $row );
492 $pages[$row->rc_cur_id] = $row->rc_title;
493 $this->processed[$row->rc_cur_id] =
true;
500 $this->writeParentCategories( $dbr, $pages );
502 fwrite( $output, $this->getInsertRdf() );
511 fwrite( $output,
"# Additions\n" );
515 foreach ( $batch as $row ) {
516 if ( isset( $this->processed[$row->rc_cur_id] ) ) {
520 $this->writeCategoryData( $row );
521 $pages[$row->rc_cur_id] = $row->rc_title;
522 $this->processed[$row->rc_cur_id] =
true;
529 $this->writeParentCategories( $dbr, $pages );
530 fwrite( $output, $this->getInsertRdf() );
548 foreach ( $batch as $row ) {
551 if ( isset( $this->processed[$row->rc_cur_id] ) ) {
555 $this->writeCategoryData( $row );
556 $pages[$row->rc_cur_id] = $row->rc_title;
557 $this->processed[$row->rc_cur_id] =
true;
558 $deleteUrls[] =
'<' . $this->categoriesRdf->labelToUrl( $row->rc_title ) .
'>';
561 fwrite( $output, $this->getCategoriesUpdate( $dbr, $deleteUrls, $pages,
"Edits" ) );
571 $processedTitle = [];
587 foreach ( $batch as $row ) {
588 $childPages[$row->rc_cur_id] =
true;
589 $parentCats[$row->rc_title] =
true;
600 'rc_title' =>
'page_title',
607 ->leftJoin(
'page_props',
null, [
'pp_propname' =>
'hiddencat',
'pp_page = page_id' ] )
608 ->leftJoin(
'category',
null, [
'cat_title = page_title' ] )
609 ->where( [
'page_namespace' =>
NS_CATEGORY,
'page_id' => array_keys( $childPages ) ] )
610 ->caller( __METHOD__ )->fetchResultSet();
611 foreach ( $childRows as $row ) {
612 if ( isset( $this->processed[$row->page_id] ) ) {
616 $this->writeCategoryData( $row );
617 if ( $row->page_id ) {
618 $pages[$row->page_id] = $row->rc_title;
619 $deleteUrls[] =
'<' . $this->categoriesRdf->labelToUrl( $row->rc_title ) .
'>';
620 $this->processed[$row->page_id] =
true;
630 'rc_title' =>
'cat_title',
637 ->leftJoin(
'page',
null, [
'page_title = cat_title',
'page_namespace' =>
NS_CATEGORY ] )
638 ->leftJoin(
'page_props',
null, [
'pp_propname' =>
'hiddencat',
'pp_page = page_id' ] )
639 ->where( [
'cat_title' => array_map(
'strval', array_keys( $parentCats ) ) ] )
640 ->caller( __METHOD__ )->fetchResultSet();
641 foreach ( $parentRows as $row ) {
642 if ( $row->page_id && isset( $this->processed[$row->page_id] ) ) {
646 if ( isset( $processedTitle[$row->rc_title] ) ) {
650 $this->writeCategoryData( $row );
651 if ( $row->page_id ) {
652 $pages[$row->page_id] = $row->rc_title;
653 $deleteUrls[] =
'<' . $this->categoriesRdf->labelToUrl( $row->rc_title ) .
'>';
654 $this->processed[$row->page_id] =
true;
656 $processedTitle[$row->rc_title] =
true;
660 fwrite( $output, $this->getCategoriesUpdate( $dbr, $deleteUrls, $pages,
"Changes" ) );
667require_once RUN_MAINTENANCE_IF_MAIN;
wfTimestamp( $outputtype=TS::UNIX, $ts=0)
Get a timestamp string in one of various formats.
Maintenance script to provide RDF representation of the recent changes in category tree.
initialize()
Initialize external service classes.
handleRestores(IReadableDatabase $dbr, $output)
handleMoves(IReadableDatabase $dbr, $output)
getNewCatsIterator(IReadableDatabase $dbr, $fname)
Fetch newly created categories.
execute()
Do the actual work.
getDeletedCatsIterator(IReadableDatabase $dbr, $fname)
Fetch deleted categories.
getChangedCatsIterator(IReadableDatabase $dbr, $source, $fname)
Fetch categorization changes or edits.
handleCategorization(IReadableDatabase $dbr, $output)
Handles categorization changes.
true[] $processed
List of processed page IDs, so we don't try to process same thing twice.
getRestoredCatsIterator(IReadableDatabase $dbr, $fname)
Fetch restored categories.
getMovedCatsIterator(IReadableDatabase $dbr, $fname)
Fetch moved categories.
handleEdits(IReadableDatabase $dbr, $output)
Handle edits for category texts.
getRdf()
Get accumulated RDF.
updateTS( $timestamp)
Generate SPARQL Update code for updating dump timestamp.
handleAdds(IReadableDatabase $dbr, $output)
handleDeletes(IReadableDatabase $dbr, $output)
Handle category deletes.
__construct()
Default constructor.
getCategoryLinksIterator(IReadableDatabase $dbr, array $ids, $fname)
Get iterator for links for categories.
A class containing constants representing the names of configuration variables.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
addDescription( $text)
Set the description text.