MediaWiki master
CategoriesRdf.php
Go to the documentation of this file.
1<?php
21
23use Wikimedia\Purtle\RdfWriter;
24
32 private const ONTOLOGY_PREFIX = 'mediawiki';
36 private const ONTOLOGY_URL = 'https://www.mediawiki.org/ontology#';
40 public const OWL_URL = 'https://www.mediawiki.org/ontology/ontology.owl';
44 public const FORMAT_VERSION = "1.1";
50 private const SPECIAL_DUMP = 'Special:CategoryDump';
54 private $rdfWriter;
55
56 public function __construct( RdfWriter $writer ) {
57 $this->rdfWriter = $writer;
58 }
59
63 public function setupPrefixes() {
64 $this->rdfWriter->prefix( self::ONTOLOGY_PREFIX, self::ONTOLOGY_URL );
65 $this->rdfWriter->prefix( 'rdfs', 'http://www.w3.org/2000/01/rdf-schema#' );
66 $this->rdfWriter->prefix( 'owl', 'http://www.w3.org/2002/07/owl#' );
67 $this->rdfWriter->prefix( 'schema', 'http://schema.org/' );
68 $this->rdfWriter->prefix( 'cc', 'http://creativecommons.org/ns#' );
69 }
70
76 public function writeCategoryLinkData( $fromName, $toName ) {
77 $titleFrom = Title::makeTitle( NS_CATEGORY, $fromName );
78 $titleTo = Title::makeTitle( NS_CATEGORY, $toName );
79 $this->rdfWriter->about( $this->titleToUrl( $titleFrom ) )
80 ->say( self::ONTOLOGY_PREFIX, 'isInCategory' )
81 ->is( $this->titleToUrl( $titleTo ) );
82 }
83
91 public function writeCategoryData( $categoryName, $isHidden, $pages, $subcategories ) {
92 if ( $pages < 0 ) {
93 // Bugfix for T201119
94 $pages = 0;
95 }
96 $title = Title::makeTitle( NS_CATEGORY, $categoryName );
97 $this->rdfWriter->about( $this->titleToUrl( $title ) )
98 ->say( 'a' )
99 ->is( self::ONTOLOGY_PREFIX, 'Category' );
100 if ( $isHidden ) {
101 $this->rdfWriter->is( self::ONTOLOGY_PREFIX, 'HiddenCategory' );
102 }
103 $titletext = $title->getText();
104 $this->rdfWriter->say( 'rdfs', 'label' )->value( $titletext );
105 // @phan-suppress-next-line PhanTypeMismatchArgument T302667
106 $this->rdfWriter->say( self::ONTOLOGY_PREFIX, 'pages' )->value( $pages );
107 // @phan-suppress-next-line PhanTypeMismatchArgument T302667
108 $this->rdfWriter->say( self::ONTOLOGY_PREFIX, 'subcategories' )->value( $subcategories );
109 // TODO: do we want files too here? Easy to add, but don't have use case so far.
110 }
111
117 public function labelToUrl( $titleLabel ) {
118 return $this->titleToUrl( Title::makeTitle( NS_CATEGORY, $titleLabel ) );
119 }
120
126 private function titleToUrl( Title $title ) {
127 return $title->getFullURL( '', false, PROTO_CANONICAL );
128 }
129
134 public function getDumpURI() {
135 return $this->titleToUrl( Title::makeTitle( NS_MAIN, self::SPECIAL_DUMP ) );
136 }
137
138}
139
141class_alias( CategoriesRdf::class, 'CategoriesRdf' );
const PROTO_CANONICAL
Definition Defines.php:209
const NS_MAIN
Definition Defines.php:65
const NS_CATEGORY
Definition Defines.php:79
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.
Represents a title within MediaWiki.
Definition Title.php:78
getFullURL( $query='', $query2=false, $proto=PROTO_RELATIVE)
Get a real URL referring to this title, with interwiki link and fragment.
Definition Title.php:2133
This program is free software; you can redistribute it and/or modify it under the terms of the GNU Ge...