Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.89% covered (warning)
88.89%
24 / 27
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
CategoriesRdf
92.31% covered (success)
92.31%
24 / 26
71.43% covered (warning)
71.43%
5 / 7
9.04
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 setupPrefixes
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 writeCategoryLinkData
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 writeCategoryData
91.67% covered (success)
91.67%
11 / 12
0.00% covered (danger)
0.00%
0 / 1
3.01
 labelToUrl
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 titleToUrl
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDumpURI
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
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
20namespace MediaWiki\Category;
21
22use MediaWiki\Title\Title;
23use Wikimedia\Purtle\RdfWriter;
24
25/**
26 * Helper class to produce RDF representation of categories.
27 */
28class CategoriesRdf {
29    /**
30     * Prefix used for MediaWiki ontology in the dump.
31     */
32    private const ONTOLOGY_PREFIX = 'mediawiki';
33    /**
34     * Base URL for MediaWiki ontology.
35     */
36    private const ONTOLOGY_URL = 'https://www.mediawiki.org/ontology#';
37    /**
38     * OWL description of the ontology.
39     */
40    public const OWL_URL = 'https://www.mediawiki.org/ontology/ontology.owl';
41    /**
42     * Current version of the dump format.
43     */
44    public const FORMAT_VERSION = "1.1";
45    /**
46     * Special page for Dump identification.
47     * Used as head URI for each wiki's category dump, e.g.:
48     * https://en.wikipedia.org/wiki/Special:CategoryDump
49     */
50    private const SPECIAL_DUMP = 'Special:CategoryDump';
51    /**
52     * @var RdfWriter
53     */
54    private $rdfWriter;
55
56    public function __construct( RdfWriter $writer ) {
57        $this->rdfWriter = $writer;
58    }
59
60    /**
61     * Setup prefixes relevant for the dump
62     */
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
71    /**
72     * Write RDF data for link between categories.
73     * @param string $fromName Child category name
74     * @param string $toName Parent category name
75     */
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
84    /**
85     * Write out the data for single category.
86     * @param string $categoryName
87     * @param bool $isHidden Hidden category?
88     * @param int $pages Page count (note this includes only Wiki articles, not subcats or files)
89     * @param int $subcategories Subcategory count
90     */
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
112    /**
113     * Make URL from title label
114     * @param string $titleLabel Short label (without namespace) of the category
115     * @return string URL for the category
116     */
117    public function labelToUrl( $titleLabel ) {
118        return $this->titleToUrl( Title::makeTitle( NS_CATEGORY, $titleLabel ) );
119    }
120
121    /**
122     * Convert Title to link to target page.
123     * @param Title $title
124     * @return string URL for the category
125     */
126    private function titleToUrl( Title $title ) {
127        return $title->getFullURL( '', false, PROTO_CANONICAL );
128    }
129
130    /**
131     * Get URI of the dump for this particular wiki.
132     * @return string
133     */
134    public function getDumpURI() {
135        return $this->titleToUrl( Title::makeTitle( NS_MAIN, self::SPECIAL_DUMP ) );
136    }
137
138}
139
140/** @deprecated class alias since 1.40 */
141class_alias( CategoriesRdf::class, 'CategoriesRdf' );