MediaWiki master
dumpCategoriesAsRdf.php
Go to the documentation of this file.
1<?php
23use Wikimedia\Purtle\RdfWriter;
24use Wikimedia\Purtle\RdfWriterFactory;
26
27// @codeCoverageIgnoreStart
28require_once __DIR__ . '/Maintenance.php';
29// @codeCoverageIgnoreEnd
30
41 private $rdfWriter;
46 private $categoriesRdf;
47
48 public function __construct() {
49 parent::__construct();
50
51 $this->addDescription( "Generate RDF dump of categories in a wiki." );
52
53 $this->setBatchSize( 200 );
54 $this->addOption( 'output', "Output file (default is stdout). Will be overwritten.",
55 false, true );
56 $this->addOption( 'format', "Set the dump format.", false, true );
57 }
58
65 public function getCategoryIterator( IReadableDatabase $dbr, $fname ) {
66 $it = new BatchRowIterator(
67 $dbr,
69 ->from( 'page' )
70 ->leftJoin( 'page_props', null, [ 'pp_propname' => 'hiddencat', 'pp_page = page_id' ] )
71 ->leftJoin( 'category', null, [ 'cat_title = page_title' ] )
72 ->select( [
73 'page_title',
74 'page_id',
75 'pp_propname',
76 'cat_pages',
77 'cat_subcats',
78 'cat_files'
79 ] )
80 ->where( [
81 'page_namespace' => NS_CATEGORY,
82 ] )
83 ->caller( $fname ),
84 [ 'page_title' ],
85 $this->getBatchSize()
86 );
87 return $it;
88 }
89
97 public function getCategoryLinksIterator( IReadableDatabase $dbr, array $ids, $fname ) {
98 $it = new BatchRowIterator(
99 $dbr,
101 ->from( 'categorylinks' )
102 ->select( [ 'cl_from', 'cl_to' ] )
103 ->where( [
104 'cl_type' => 'subcat',
105 'cl_from' => $ids
106 ] )
107 ->caller( $fname ),
108 [ 'cl_from', 'cl_to' ],
109 $this->getBatchSize()
110 );
111 return new RecursiveIteratorIterator( $it );
112 }
113
117 public function addDumpHeader( $timestamp ) {
118 $licenseUrl = $this->getConfig()->get( MainConfigNames::RightsUrl );
119 if ( str_starts_with( $licenseUrl, '//' ) ) {
120 $licenseUrl = 'https:' . $licenseUrl;
121 }
122 $urlUtils = $this->getServiceContainer()->getUrlUtils();
123 $this->rdfWriter->about( $this->categoriesRdf->getDumpURI() )
124 ->a( 'schema', 'Dataset' )
125 ->a( 'owl', 'Ontology' )
126 ->say( 'cc', 'license' )->is( $licenseUrl )
127 ->say( 'schema', 'softwareVersion' )->value( CategoriesRdf::FORMAT_VERSION )
128 ->say( 'schema', 'dateModified' )
129 ->value( wfTimestamp( TS_ISO_8601, $timestamp ), 'xsd', 'dateTime' )
130 ->say( 'schema', 'isPartOf' )->is( (string)$urlUtils->expand( '/', PROTO_CANONICAL ) )
131 ->say( 'owl', 'imports' )->is( CategoriesRdf::OWL_URL );
132 }
133
134 public function execute() {
135 $outFile = $this->getOption( 'output', 'php://stdout' );
136
137 if ( $outFile === '-' ) {
138 $outFile = 'php://stdout';
139 }
140
141 $output = fopen( $outFile, 'w' );
142 $this->rdfWriter = $this->createRdfWriter( $this->getOption( 'format', 'ttl' ) );
143 $this->categoriesRdf = new CategoriesRdf( $this->rdfWriter );
144
145 $this->categoriesRdf->setupPrefixes();
146 $this->rdfWriter->start();
147
148 $this->addDumpHeader( time() );
149 fwrite( $output, $this->rdfWriter->drain() );
150
151 $dbr = $this->getDB( DB_REPLICA, [ 'vslow' ] );
152
153 foreach ( $this->getCategoryIterator( $dbr, __METHOD__ ) as $batch ) {
154 $pages = [];
155 foreach ( $batch as $row ) {
156 $this->categoriesRdf->writeCategoryData(
157 $row->page_title,
158 $row->pp_propname === 'hiddencat',
159 (int)$row->cat_pages - (int)$row->cat_subcats - (int)$row->cat_files,
160 (int)$row->cat_subcats
161 );
162 if ( $row->page_id ) {
163 $pages[$row->page_id] = $row->page_title;
164 }
165 }
166
167 foreach ( $this->getCategoryLinksIterator( $dbr, array_keys( $pages ), __METHOD__ ) as $row ) {
168 $this->categoriesRdf->writeCategoryLinkData( $pages[$row->cl_from], $row->cl_to );
169 }
170 fwrite( $output, $this->rdfWriter->drain() );
171 }
172 fflush( $output );
173 if ( $outFile !== '-' ) {
174 fclose( $output );
175 }
176 }
177
182 private function createRdfWriter( $format ) {
183 $factory = new RdfWriterFactory();
184 return $factory->getWriter( $factory->getFormatName( $format ) );
185 }
186}
187
188// @codeCoverageIgnoreStart
189$maintClass = DumpCategoriesAsRdf::class;
190require_once RUN_MAINTENANCE_IF_MAIN;
191// @codeCoverageIgnoreEnd
const PROTO_CANONICAL
Definition Defines.php:216
const NS_CATEGORY
Definition Defines.php:79
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.
execute()
Do the actual work.
getCategoryIterator(IReadableDatabase $dbr, $fname)
Produce row iterator for categories.
__construct()
Default constructor.
Helper class to produce RDF representation of 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.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
A database connection without write operations.
newSelectQueryBuilder()
Create an empty SelectQueryBuilder which can be used to run queries against this connection.
const DB_REPLICA
Definition defines.php:26