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';
40 private $rdfWriter;
41
42 public function __construct( RdfWriter $writer ) {
43 $this->rdfWriter = $writer;
44 }
45
49 public function setupPrefixes() {
50 $this->rdfWriter->prefix( self::ONTOLOGY_PREFIX, self::ONTOLOGY_URL );
51 $this->rdfWriter->prefix( 'rdfs', 'http://www.w3.org/2000/01/rdf-schema#' );
52 $this->rdfWriter->prefix( 'owl', 'http://www.w3.org/2002/07/owl#' );
53 $this->rdfWriter->prefix( 'schema', 'http://schema.org/' );
54 $this->rdfWriter->prefix( 'cc', 'http://creativecommons.org/ns#' );
55 }
56
62 public function writeCategoryLinkData( $fromName, $toName ) {
63 $titleFrom = Title::makeTitle( NS_CATEGORY, $fromName );
64 $titleTo = Title::makeTitle( NS_CATEGORY, $toName );
65 $this->rdfWriter->about( $this->titleToUrl( $titleFrom ) )
66 ->say( self::ONTOLOGY_PREFIX, 'isInCategory' )
67 ->is( $this->titleToUrl( $titleTo ) );
68 }
69
77 public function writeCategoryData( $categoryName, $isHidden, $pages, $subcategories ) {
78 if ( $pages < 0 ) {
79 // Bugfix for T201119
80 $pages = 0;
81 }
82 $title = Title::makeTitle( NS_CATEGORY, $categoryName );
83 $this->rdfWriter->about( $this->titleToUrl( $title ) )
84 ->say( 'a' )
85 ->is( self::ONTOLOGY_PREFIX, 'Category' );
86 if ( $isHidden ) {
87 $this->rdfWriter->is( self::ONTOLOGY_PREFIX, 'HiddenCategory' );
88 }
89 $titletext = $title->getText();
90 $this->rdfWriter->say( 'rdfs', 'label' )->value( $titletext );
91 // @phan-suppress-next-line PhanTypeMismatchArgument T302667
92 $this->rdfWriter->say( self::ONTOLOGY_PREFIX, 'pages' )->value( $pages );
93 // @phan-suppress-next-line PhanTypeMismatchArgument T302667
94 $this->rdfWriter->say( self::ONTOLOGY_PREFIX, 'subcategories' )->value( $subcategories );
95 // TODO: do we want files too here? Easy to add, but don't have use case so far.
96 }
97
103 public function labelToUrl( $titleLabel ) {
104 return $this->titleToUrl( Title::makeTitle( NS_CATEGORY, $titleLabel ) );
105 }
106
112 private function titleToUrl( Title $title ) {
113 return $title->getFullURL( '', false, PROTO_CANONICAL );
114 }
115
120 public function getDumpURI() {
121 return $this->titleToUrl( Title::makeTitle( NS_MAIN, self::SPECIAL_DUMP ) );
122 }
123
124}
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.
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:70
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2153