22use Wikimedia\Purtle\RdfWriter;
23use Wikimedia\Purtle\RdfWriterFactory;
27require_once __DIR__ .
'/Maintenance.php';
45 private $categoriesRdf;
48 parent::__construct();
50 $this->addDescription(
"Generate RDF dump of categories in a wiki." );
52 $this->setBatchSize( 200 );
53 $this->addOption(
'output',
"Output file (default is stdout). Will be overwritten.",
55 $this->addOption(
'format',
"Set the dump format.",
false,
true );
67 [
'page',
'page_props',
'category' ],
74 $it->setFetchColumns( [
82 $it->addJoinConditions(
85 'LEFT JOIN', [
'pp_propname' =>
'hiddencat',
'pp_page = page_id' ]
88 'LEFT JOIN', [
'cat_title = page_title' ]
93 $it->setCaller( $fname );
108 [
'cl_from',
'cl_to' ],
109 $this->getBatchSize()
111 $it->addConditions( [
112 'cl_type' =>
'subcat',
115 $it->setFetchColumns( [
'cl_from',
'cl_to' ] );
116 $it->setCaller( $fname );
117 return new RecursiveIteratorIterator( $it );
124 $licenseUrl = $this->getConfig()->get( MainConfigNames::RightsUrl );
125 if ( str_starts_with( $licenseUrl,
'//' ) ) {
126 $licenseUrl =
'https:' . $licenseUrl;
128 $urlUtils = $this->getServiceContainer()->getUrlUtils();
129 $this->rdfWriter->about( $this->categoriesRdf->getDumpURI() )
130 ->a(
'schema',
'Dataset' )
131 ->a(
'owl',
'Ontology' )
132 ->say(
'cc',
'license' )->is( $licenseUrl )
133 ->say(
'schema',
'softwareVersion' )->value( CategoriesRdf::FORMAT_VERSION )
134 ->say(
'schema',
'dateModified' )
135 ->value(
wfTimestamp( TS_ISO_8601, $timestamp ),
'xsd',
'dateTime' )
136 ->say(
'schema',
'isPartOf' )->is( (
string)$urlUtils->expand(
'/',
PROTO_CANONICAL ) )
137 ->say(
'owl',
'imports' )->is( CategoriesRdf::OWL_URL );
141 $outFile = $this->getOption(
'output',
'php://stdout' );
143 if ( $outFile ===
'-' ) {
144 $outFile =
'php://stdout';
147 $output = fopen( $outFile,
'w' );
148 $this->rdfWriter = $this->createRdfWriter( $this->getOption(
'format',
'ttl' ) );
149 $this->categoriesRdf =
new CategoriesRdf( $this->rdfWriter );
151 $this->categoriesRdf->setupPrefixes();
152 $this->rdfWriter->start();
155 fwrite( $output, $this->rdfWriter->drain() );
157 $dbr = $this->getDB(
DB_REPLICA, [
'vslow' ] );
161 foreach ( $batch as $row ) {
162 $this->categoriesRdf->writeCategoryData(
164 $row->pp_propname ===
'hiddencat',
165 (
int)$row->cat_pages - (
int)$row->cat_subcats - (
int)$row->cat_files,
166 (
int)$row->cat_subcats
168 if ( $row->page_id ) {
169 $pages[$row->page_id] = $row->page_title;
174 $this->categoriesRdf->writeCategoryLinkData( $pages[$row->cl_from], $row->cl_to );
176 fwrite( $output, $this->rdfWriter->drain() );
179 if ( $outFile !==
'-' ) {
188 private function createRdfWriter( $format ) {
189 $factory =
new RdfWriterFactory();
190 return $factory->getWriter( $factory->getFormatName( $format ) );
196require_once RUN_MAINTENANCE_IF_MAIN;
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Allows iterating a large number of rows in batches transparently.
Maintenance script to provide RDF representation of the category tree.
getCategoryLinksIterator(IReadableDatabase $dbr, array $ids, $fname)
Get iterator for links for categories.
addDumpHeader( $timestamp)
getCategoryIterator(IReadableDatabase $dbr, $fname)
Produce row iterator for categories.
A class containing constants representing the names of configuration variables.