MediaWiki master
CategoriesRdf.php
Go to the documentation of this file.
1<?php
7
9use Wikimedia\Purtle\RdfWriter;
10
18 private const ONTOLOGY_PREFIX = 'mediawiki';
22 private const ONTOLOGY_URL = 'https://www.mediawiki.org/ontology#';
26 public const OWL_URL = 'https://www.mediawiki.org/ontology/ontology.owl';
30 public const FORMAT_VERSION = "1.1";
36 private const SPECIAL_DUMP = 'Special:CategoryDump';
37
38 public function __construct(
39 private readonly RdfWriter $rdfWriter,
40 ) {
41 }
42
46 public function setupPrefixes() {
47 $this->rdfWriter->prefix( self::ONTOLOGY_PREFIX, self::ONTOLOGY_URL );
48 $this->rdfWriter->prefix( 'rdfs', 'http://www.w3.org/2000/01/rdf-schema#' );
49 $this->rdfWriter->prefix( 'owl', 'http://www.w3.org/2002/07/owl#' );
50 $this->rdfWriter->prefix( 'schema', 'http://schema.org/' );
51 $this->rdfWriter->prefix( 'cc', 'http://creativecommons.org/ns#' );
52 }
53
59 public function writeCategoryLinkData( $fromName, $toName ) {
60 $titleFrom = Title::makeTitle( NS_CATEGORY, $fromName );
61 $titleTo = Title::makeTitle( NS_CATEGORY, $toName );
62 $this->rdfWriter->about( $this->titleToUrl( $titleFrom ) )
63 ->say( self::ONTOLOGY_PREFIX, 'isInCategory' )
64 ->is( $this->titleToUrl( $titleTo ) );
65 }
66
74 public function writeCategoryData( $categoryName, $isHidden, $pages, $subcategories ) {
75 if ( $pages < 0 ) {
76 // Bugfix for T201119
77 $pages = 0;
78 }
79 $title = Title::makeTitle( NS_CATEGORY, $categoryName );
80 $this->rdfWriter->about( $this->titleToUrl( $title ) )
81 ->say( 'a' )
82 ->is( self::ONTOLOGY_PREFIX, 'Category' );
83 if ( $isHidden ) {
84 $this->rdfWriter->is( self::ONTOLOGY_PREFIX, 'HiddenCategory' );
85 }
86 $titletext = $title->getText();
87 $this->rdfWriter->say( 'rdfs', 'label' )->value( $titletext );
88 // @phan-suppress-next-line PhanTypeMismatchArgument T302667
89 $this->rdfWriter->say( self::ONTOLOGY_PREFIX, 'pages' )->value( $pages );
90 // @phan-suppress-next-line PhanTypeMismatchArgument T302667
91 $this->rdfWriter->say( self::ONTOLOGY_PREFIX, 'subcategories' )->value( $subcategories );
92 // TODO: do we want files too here? Easy to add, but don't have use case so far.
93 }
94
100 public function labelToUrl( $titleLabel ) {
101 return $this->titleToUrl( Title::makeTitle( NS_CATEGORY, $titleLabel ) );
102 }
103
109 private function titleToUrl( Title $title ) {
110 return $title->getFullURL( '', false, PROTO_CANONICAL );
111 }
112
117 public function getDumpURI() {
118 return $this->titleToUrl( Title::makeTitle( NS_MAIN, self::SPECIAL_DUMP ) );
119 }
120
121}
const PROTO_CANONICAL
Definition Defines.php:223
const NS_MAIN
Definition Defines.php:51
const NS_CATEGORY
Definition Defines.php:65
Helper class to produce RDF representation of categories.
const OWL_URL
OWL description of the ontology.
setupPrefixes()
Setup prefixes relevant for the dump.
getDumpURI()
Get URI of the dump for this particular wiki.
__construct(private readonly RdfWriter $rdfWriter,)
labelToUrl( $titleLabel)
Make URL from title label.
writeCategoryLinkData( $fromName, $toName)
Write RDF data for link between categories.
const FORMAT_VERSION
Current version of the dump format.
writeCategoryData( $categoryName, $isHidden, $pages, $subcategories)
Write out the data for single category.
makeTitle( $linkId)
Convert a link ID to a Title.to override Title
Represents a title within MediaWiki.
Definition Title.php:69
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2152