MediaWiki 1.40.4
dumpCategoriesAsRdf.php
Go to the documentation of this file.
1<?php
22use Wikimedia\Purtle\RdfWriter;
23use Wikimedia\Purtle\RdfWriterFactory;
25
26require_once __DIR__ . '/Maintenance.php';
27
38 private $rdfWriter;
43 private $categoriesRdf;
44
45 public function __construct() {
46 parent::__construct();
47
48 $this->addDescription( "Generate RDF dump of categories in a wiki." );
49
50 $this->setBatchSize( 200 );
51 $this->addOption( 'output', "Output file (default is stdout). Will be overwritten.",
52 false, true );
53 $this->addOption( 'format', "Set the dump format.", false, true );
54 }
55
62 public function getCategoryIterator( IDatabase $dbr, $fname ) {
63 $it = new BatchRowIterator(
64 $dbr,
65 [ 'page', 'page_props', 'category' ],
66 [ 'page_title' ],
67 $this->getBatchSize()
68 );
69 $it->addConditions( [
70 'page_namespace' => NS_CATEGORY,
71 ] );
72 $it->setFetchColumns( [
73 'page_title',
74 'page_id',
75 'pp_propname',
76 'cat_pages',
77 'cat_subcats',
78 'cat_files'
79 ] );
80 $it->addJoinConditions(
81 [
82 'page_props' => [
83 'LEFT JOIN', [ 'pp_propname' => 'hiddencat', 'pp_page = page_id' ]
84 ],
85 'category' => [
86 'LEFT JOIN', [ 'cat_title = page_title' ]
87 ]
88 ]
89
90 );
91 $it->setCaller( $fname );
92 return $it;
93 }
94
102 public function getCategoryLinksIterator( IDatabase $dbr, array $ids, $fname ) {
103 $it = new BatchRowIterator(
104 $dbr,
105 'categorylinks',
106 [ 'cl_from', 'cl_to' ],
107 $this->getBatchSize()
108 );
109 $it->addConditions( [
110 'cl_type' => 'subcat',
111 'cl_from' => $ids
112 ] );
113 $it->setFetchColumns( [ 'cl_from', 'cl_to' ] );
114 $it->setCaller( $fname );
115 return new RecursiveIteratorIterator( $it );
116 }
117
121 public function addDumpHeader( $timestamp ) {
122 $licenseUrl = $this->getConfig()->get( MainConfigNames::RightsUrl );
123 if ( substr( $licenseUrl, 0, 2 ) == '//' ) {
124 $licenseUrl = 'https:' . $licenseUrl;
125 }
126 $this->rdfWriter->about( $this->categoriesRdf->getDumpURI() )
127 ->a( 'schema', 'Dataset' )
128 ->a( 'owl', 'Ontology' )
129 ->say( 'cc', 'license' )->is( $licenseUrl )
130 ->say( 'schema', 'softwareVersion' )->value( CategoriesRdf::FORMAT_VERSION )
131 ->say( 'schema', 'dateModified' )
132 ->value( wfTimestamp( TS_ISO_8601, $timestamp ), 'xsd', 'dateTime' )
133 ->say( 'schema', 'isPartOf' )->is( wfExpandUrl( '/', PROTO_CANONICAL ) )
134 ->say( 'owl', 'imports' )->is( CategoriesRdf::OWL_URL );
135 }
136
137 public function execute() {
138 $outFile = $this->getOption( 'output', 'php://stdout' );
139
140 if ( $outFile === '-' ) {
141 $outFile = 'php://stdout';
142 }
143
144 $output = fopen( $outFile, 'w' );
145 $this->rdfWriter = $this->createRdfWriter( $this->getOption( 'format', 'ttl' ) );
146 $this->categoriesRdf = new CategoriesRdf( $this->rdfWriter );
147
148 $this->categoriesRdf->setupPrefixes();
149 $this->rdfWriter->start();
150
151 $this->addDumpHeader( time() );
152 fwrite( $output, $this->rdfWriter->drain() );
153
154 $dbr = $this->getDB( DB_REPLICA, [ 'vslow' ] );
155
156 foreach ( $this->getCategoryIterator( $dbr, __METHOD__ ) as $batch ) {
157 $pages = [];
158 foreach ( $batch as $row ) {
159 $this->categoriesRdf->writeCategoryData(
160 $row->page_title,
161 $row->pp_propname === 'hiddencat',
162 (int)$row->cat_pages - (int)$row->cat_subcats - (int)$row->cat_files,
163 (int)$row->cat_subcats
164 );
165 if ( $row->page_id ) {
166 $pages[$row->page_id] = $row->page_title;
167 }
168 }
169
170 foreach ( $this->getCategoryLinksIterator( $dbr, array_keys( $pages ), __METHOD__ ) as $row ) {
171 $this->categoriesRdf->writeCategoryLinkData( $pages[$row->cl_from], $row->cl_to );
172 }
173 fwrite( $output, $this->rdfWriter->drain() );
174 }
175 fflush( $output );
176 if ( $outFile !== '-' ) {
177 fclose( $output );
178 }
179 }
180
185 private function createRdfWriter( $format ) {
186 $factory = new RdfWriterFactory();
187 return $factory->getWriter( $factory->getFormatName( $format ) );
188 }
189}
190
191$maintClass = DumpCategoriesAsRdf::class;
192require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const PROTO_CANONICAL
Definition Defines.php:199
const NS_CATEGORY
Definition Defines.php:78
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
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(IDatabase $dbr, array $ids, $fname)
Get iterator for links for categories.
getCategoryIterator(IDatabase $dbr, $fname)
Produce row iterator for categories.
execute()
Do the actual work.
__construct()
Default constructor.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getBatchSize()
Returns batch size.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
setBatchSize( $s=0)
Helper class to produce RDF representation of categories.
A class containing constants representing the names of configuration variables.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:36
const DB_REPLICA
Definition defines.php:26