]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/CategoriesRdf.php
MediaWiki 1.30.2
[autoinstalls/mediawiki.git] / includes / CategoriesRdf.php
1 <?php
2 /**
3  * This program is free software; you can redistribute it and/or modify
4  * it under the terms of the GNU General Public License as published by
5  * the Free Software Foundation; either version 2 of the License, or
6  * (at your option) any later version.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16  * http://www.gnu.org/copyleft/gpl.html
17  *
18  */
19 use Wikimedia\Purtle\RdfWriter;
20
21 /**
22  * Helper class to produce RDF representation of categories.
23  */
24 class CategoriesRdf {
25         /**
26          * Prefix used for Mediawiki ontology in the dump.
27          */
28         const ONTOLOGY_PREFIX = 'mediawiki';
29         /**
30          * Base URL for Mediawiki ontology.
31          */
32         const ONTOLOGY_URL = 'https://www.mediawiki.org/ontology#';
33         /**
34          * OWL description of the ontology.
35          */
36         const OWL_URL = 'https://www.mediawiki.org/ontology/ontology.owl';
37         /**
38          * Current version of the dump format.
39          */
40         const FORMAT_VERSION = "1.0";
41         /**
42          * @var RdfWriter
43          */
44         private $rdfWriter;
45
46         public function __construct( RdfWriter $writer ) {
47                 $this->rdfWriter = $writer;
48         }
49
50         /**
51          * Setup prefixes relevant for the dump
52          */
53         public function setupPrefixes() {
54                 $this->rdfWriter->prefix( self::ONTOLOGY_PREFIX, self::ONTOLOGY_URL );
55                 $this->rdfWriter->prefix( 'rdfs', 'http://www.w3.org/2000/01/rdf-schema#' );
56                 $this->rdfWriter->prefix( 'owl', 'http://www.w3.org/2002/07/owl#' );
57                 $this->rdfWriter->prefix( 'schema', 'http://schema.org/' );
58                 $this->rdfWriter->prefix( 'cc', 'http://creativecommons.org/ns#' );
59         }
60
61         /**
62          * Write RDF data for link between categories.
63          * @param string $fromName Child category name
64          * @param string $toName Parent category name
65          */
66         public function writeCategoryLinkData( $fromName, $toName ) {
67                 $titleFrom = Title::makeTitle( NS_CATEGORY, $fromName );
68                 $titleTo = Title::makeTitle( NS_CATEGORY, $toName );
69                 $this->rdfWriter->about( $this->titleToUrl( $titleFrom ) )
70                         ->say( self::ONTOLOGY_PREFIX, 'isInCategory' )
71                         ->is( $this->titleToUrl( $titleTo ) );
72         }
73
74         /**
75          * Write out the data for single category.
76          * @param string $categoryName Category name
77          */
78         public function writeCategoryData( $categoryName ) {
79                 $title = Title::makeTitle( NS_CATEGORY, $categoryName );
80                 $this->rdfWriter->about( $this->titleToUrl( $title ) )
81                         ->say( 'a' )
82                         ->is( self::ONTOLOGY_PREFIX, 'Category' );
83                 $titletext = $title->getText();
84                 $this->rdfWriter->say( 'rdfs', 'label' )->value( $titletext );
85         }
86
87         /**
88          * Convert Title to link to target page.
89          * @param Title $title
90          * @return string
91          */
92         private function titleToUrl( Title $title ) {
93                 return $title->getFullURL( '', false, PROTO_CANONICAL );
94         }
95 }