MediaWiki REL1_37
dumpCategoriesAsRdf.php
Go to the documentation of this file.
1<?php
19use Wikimedia\Purtle\RdfWriter;
20use Wikimedia\Purtle\RdfWriterFactory;
22
23require_once __DIR__ . '/Maintenance.php';
24
35 private $rdfWriter;
41
42 public function __construct() {
43 parent::__construct();
44
45 $this->addDescription( "Generate RDF dump of categories in a wiki." );
46
47 $this->setBatchSize( 200 );
48 $this->addOption( 'output', "Output file (default is stdout). Will be overwritten.",
49 false, true );
50 $this->addOption( 'format', "Set the dump format.", false, true );
51 }
52
59 public function getCategoryIterator( IDatabase $dbr, $fname ) {
60 $it = new BatchRowIterator(
61 $dbr,
62 [ 'page', 'page_props', 'category' ],
63 [ 'page_title' ],
64 $this->getBatchSize()
65 );
66 $it->addConditions( [
67 'page_namespace' => NS_CATEGORY,
68 ] );
69 $it->setFetchColumns( [
70 'page_title',
71 'page_id',
72 'pp_propname',
73 'cat_pages',
74 'cat_subcats',
75 'cat_files'
76 ] );
77 $it->addJoinConditions(
78 [
79 'page_props' => [
80 'LEFT JOIN', [ 'pp_propname' => 'hiddencat', 'pp_page = page_id' ]
81 ],
82 'category' => [
83 'LEFT JOIN', [ 'cat_title = page_title' ]
84 ]
85 ]
86
87 );
88 $it->setCaller( $fname );
89 return $it;
90 }
91
99 public function getCategoryLinksIterator( IDatabase $dbr, array $ids, $fname ) {
100 $it = new BatchRowIterator(
101 $dbr,
102 'categorylinks',
103 [ 'cl_from', 'cl_to' ],
104 $this->getBatchSize()
105 );
106 $it->addConditions( [
107 'cl_type' => 'subcat',
108 'cl_from' => $ids
109 ] );
110 $it->setFetchColumns( [ 'cl_from', 'cl_to' ] );
111 $it->setCaller( $fname );
112 return new RecursiveIteratorIterator( $it );
113 }
114
118 public function addDumpHeader( $timestamp ) {
119 global $wgRightsUrl;
120 $licenseUrl = $wgRightsUrl;
121 if ( substr( $licenseUrl, 0, 2 ) == '//' ) {
122 $licenseUrl = 'https:' . $licenseUrl;
123 }
124 $this->rdfWriter->about( $this->categoriesRdf->getDumpURI() )
125 ->a( 'schema', 'Dataset' )
126 ->a( 'owl', 'Ontology' )
127 ->say( 'cc', 'license' )->is( $licenseUrl )
128 ->say( 'schema', 'softwareVersion' )->value( CategoriesRdf::FORMAT_VERSION )
129 ->say( 'schema', 'dateModified' )
130 ->value( wfTimestamp( TS_ISO_8601, $timestamp ), 'xsd', 'dateTime' )
131 ->say( 'schema', 'isPartOf' )->is( wfExpandUrl( '/', PROTO_CANONICAL ) )
132 ->say( 'owl', 'imports' )->is( CategoriesRdf::OWL_URL );
133 }
134
135 public function execute() {
136 $outFile = $this->getOption( 'output', 'php://stdout' );
137
138 if ( $outFile === '-' ) {
139 $outFile = 'php://stdout';
140 }
141
142 $output = fopen( $outFile, 'w' );
143 $this->rdfWriter = $this->createRdfWriter( $this->getOption( 'format', 'ttl' ) );
144 $this->categoriesRdf = new CategoriesRdf( $this->rdfWriter );
145
146 $this->categoriesRdf->setupPrefixes();
147 $this->rdfWriter->start();
148
149 $this->addDumpHeader( time() );
150 fwrite( $output, $this->rdfWriter->drain() );
151
152 $dbr = $this->getDB( DB_REPLICA, [ 'vslow' ] );
153
154 foreach ( $this->getCategoryIterator( $dbr, __METHOD__ ) as $batch ) {
155 $pages = [];
156 foreach ( $batch as $row ) {
157 $this->categoriesRdf->writeCategoryData(
158 $row->page_title,
159 $row->pp_propname === 'hiddencat',
160 (int)$row->cat_pages - (int)$row->cat_subcats - (int)$row->cat_files,
161 (int)$row->cat_subcats
162 );
163 if ( $row->page_id ) {
164 $pages[$row->page_id] = $row->page_title;
165 }
166 }
167
168 foreach ( $this->getCategoryLinksIterator( $dbr, array_keys( $pages ), __METHOD__ ) as $row ) {
169 $this->categoriesRdf->writeCategoryLinkData( $pages[$row->cl_from], $row->cl_to );
170 }
171 fwrite( $output, $this->rdfWriter->drain() );
172 }
173 fflush( $output );
174 if ( $outFile !== '-' ) {
175 fclose( $output );
176 }
177 }
178
183 private function createRdfWriter( $format ) {
184 $factory = new RdfWriterFactory();
185 return $factory->getWriter( $factory->getFormatName( $format ) );
186 }
187}
188
189$maintClass = DumpCategoriesAsRdf::class;
190require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
$wgRightsUrl
Set this to specify an external URL containing details about the content license used on your wiki.
const PROTO_CANONICAL
Definition Defines.php:196
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.
Helper class to produce RDF representation of categories.
const FORMAT_VERSION
Current version of the dump format.
const OWL_URL
OWL description of the ontology.
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.
CategoriesRdf $categoriesRdf
Categories RDF helper.
__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)
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:38
const DB_REPLICA
Definition defines.php:25