MediaWiki  1.30.0
dumpCategoriesAsRdf.php
Go to the documentation of this file.
1 <?php
19 use Wikimedia\Purtle\RdfWriter;
20 use Wikimedia\Purtle\RdfWriterFactory;
22 
23 require_once __DIR__ . '/Maintenance.php';
24 
35  private $rdfWriter;
40  private $categoriesRdf;
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 
58  public function getCategoryIterator( IDatabase $dbr ) {
59  $it = new BatchRowIterator(
60  $dbr,
61  'page',
62  [ 'page_title' ],
63  $this->mBatchSize
64  );
65  $it->addConditions( [
66  'page_namespace' => NS_CATEGORY,
67  ] );
68  $it->setFetchColumns( [ 'page_title', 'page_id' ] );
69  return $it;
70  }
71 
78  public function getCategoryLinksIterator( IDatabase $dbr, array $ids ) {
79  $it = new BatchRowIterator(
80  $dbr,
81  'categorylinks',
82  [ 'cl_from', 'cl_to' ],
83  $this->mBatchSize
84  );
85  $it->addConditions( [
86  'cl_type' => 'subcat',
87  'cl_from' => $ids
88  ] );
89  $it->setFetchColumns( [ 'cl_from', 'cl_to' ] );
90  return new RecursiveIteratorIterator( $it );
91  }
92 
93  public function addDumpHeader( $timestamp ) {
95  $licenseUrl = $wgRightsUrl;
96  if ( substr( $licenseUrl, 0, 2 ) == '//' ) {
97  $licenseUrl = 'https:' . $licenseUrl;
98  }
99  $this->rdfWriter->about( wfExpandUrl( '/categoriesDump', PROTO_CANONICAL ) )
100  ->a( 'schema', 'Dataset' )
101  ->a( 'owl', 'Ontology' )
102  ->say( 'cc', 'license' )->is( $licenseUrl )
103  ->say( 'schema', 'softwareVersion' )->value( CategoriesRdf::FORMAT_VERSION )
104  ->say( 'schema', 'dateModified' )
105  ->value( wfTimestamp( TS_ISO_8601, $timestamp ), 'xsd', 'dateTime' )
106  ->say( 'schema', 'isPartOf' )->is( wfExpandUrl( '/', PROTO_CANONICAL ) )
107  ->say( 'owl', 'imports' )->is( CategoriesRdf::OWL_URL );
108  }
109 
110  public function execute() {
111  $outFile = $this->getOption( 'output', 'php://stdout' );
112 
113  if ( $outFile === '-' ) {
114  $outFile = 'php://stdout';
115  }
116 
117  $output = fopen( $outFile, 'w' );
118  $this->rdfWriter = $this->createRdfWriter( $this->getOption( 'format', 'ttl' ) );
119  $this->categoriesRdf = new CategoriesRdf( $this->rdfWriter );
120 
121  $this->categoriesRdf->setupPrefixes();
122  $this->rdfWriter->start();
123 
124  $this->addDumpHeader( time() );
125  fwrite( $output, $this->rdfWriter->drain() );
126 
127  $dbr = $this->getDB( DB_REPLICA, [ 'vslow' ] );
128 
129  foreach ( $this->getCategoryIterator( $dbr ) as $batch ) {
130  $pages = [];
131  foreach ( $batch as $row ) {
132  $this->categoriesRdf->writeCategoryData( $row->page_title );
133  $pages[$row->page_id] = $row->page_title;
134  }
135 
136  foreach ( $this->getCategoryLinksIterator( $dbr, array_keys( $pages ) ) as $row ) {
137  $this->categoriesRdf->writeCategoryLinkData( $pages[$row->cl_from], $row->cl_to );
138  }
139  fwrite( $output, $this->rdfWriter->drain() );
140  }
141  fflush( $output );
142  if ( $outFile !== '-' ) {
143  fclose( $output );
144  }
145  }
146 
151  private function createRdfWriter( $format ) {
152  $factory = new RdfWriterFactory();
153  return $factory->getWriter( $factory->getFormatName( $format ) );
154  }
155 }
156 
157 $maintClass = "DumpCategoriesAsRdf";
158 require_once RUN_MAINTENANCE_IF_MAIN;
PROTO_CANONICAL
const PROTO_CANONICAL
Definition: Defines.php:224
CategoriesRdf
Helper class to produce RDF representation of categories.
Definition: CategoriesRdf.php:24
CategoriesRdf\OWL_URL
const OWL_URL
OWL description of the ontology.
Definition: CategoriesRdf.php:36
DumpCategoriesAsRdf\$categoriesRdf
CategoriesRdf $categoriesRdf
Categories RDF helper.
Definition: dumpCategoriesAsRdf.php:40
DumpCategoriesAsRdf\getCategoryIterator
getCategoryIterator(IDatabase $dbr)
Produce row iterator for categories.
Definition: dumpCategoriesAsRdf.php:58
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:287
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2040
BatchRowIterator
Definition: BatchRowIterator.php:29
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$maintClass
$maintClass
Definition: dumpCategoriesAsRdf.php:157
DumpCategoriesAsRdf\getCategoryLinksIterator
getCategoryLinksIterator(IDatabase $dbr, array $ids)
Get iterator for links for categories.
Definition: dumpCategoriesAsRdf.php:78
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
DumpCategoriesAsRdf\__construct
__construct()
Default constructor.
Definition: dumpCategoriesAsRdf.php:42
$output
static configuration should be added through ResourceLoaderGetConfigVars instead can be used to get the real title after the basic globals have been set but before ordinary actions take place $output
Definition: hooks.txt:2198
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Wikimedia\Rdbms\IDatabase
Basic database interface for live and lazy-loaded relation database handles.
Definition: IDatabase.php:40
DumpCategoriesAsRdf\$rdfWriter
RdfWriter $rdfWriter
Definition: dumpCategoriesAsRdf.php:35
DumpCategoriesAsRdf\createRdfWriter
createRdfWriter( $format)
Definition: dumpCategoriesAsRdf.php:151
CategoriesRdf\FORMAT_VERSION
const FORMAT_VERSION
Current version of the dump format.
Definition: CategoriesRdf.php:40
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:215
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
DB_REPLICA
const DB_REPLICA
Definition: defines.php:25
NS_CATEGORY
const NS_CATEGORY
Definition: Defines.php:79
DumpCategoriesAsRdf\addDumpHeader
addDumpHeader( $timestamp)
Definition: dumpCategoriesAsRdf.php:93
DumpCategoriesAsRdf\execute
execute()
Do the actual work.
Definition: dumpCategoriesAsRdf.php:110
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:250
$dbr
if(! $regexes) $dbr
Definition: cleanup.php:94
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1251
$batch
$batch
Definition: linkcache.txt:23
DumpCategoriesAsRdf
Maintenance script to provide RDF representation of the category tree.
Definition: dumpCategoriesAsRdf.php:31
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:586
array
the array() calling protocol came about after MediaWiki 1.4rc1.
Maintenance\setBatchSize
setBatchSize( $s=0)
Set the batch size.
Definition: Maintenance.php:314
$wgRightsUrl
$wgRightsUrl
Set this to specify an external URL containing details about the content license used on your wiki.
Definition: DefaultSettings.php:7027