Merge "Upgrade wikimedia/remex-html to 2.0.1" into REL1_31
[lhc/web/wiklou.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.1";
41 /**
42 * Special page for Dump identification.
43 * Used as head URI for each wiki's category dump, e.g.:
44 * https://en.wikipedia.org/wiki/Special:CategoryDump
45 */
46 const SPECIAL_DUMP = 'Special:CategoryDump';
47 /**
48 * @var RdfWriter
49 */
50 private $rdfWriter;
51
52 public function __construct( RdfWriter $writer ) {
53 $this->rdfWriter = $writer;
54 }
55
56 /**
57 * Setup prefixes relevant for the dump
58 */
59 public function setupPrefixes() {
60 $this->rdfWriter->prefix( self::ONTOLOGY_PREFIX, self::ONTOLOGY_URL );
61 $this->rdfWriter->prefix( 'rdfs', 'http://www.w3.org/2000/01/rdf-schema#' );
62 $this->rdfWriter->prefix( 'owl', 'http://www.w3.org/2002/07/owl#' );
63 $this->rdfWriter->prefix( 'schema', 'http://schema.org/' );
64 $this->rdfWriter->prefix( 'cc', 'http://creativecommons.org/ns#' );
65 }
66
67 /**
68 * Write RDF data for link between categories.
69 * @param string $fromName Child category name
70 * @param string $toName Parent category name
71 */
72 public function writeCategoryLinkData( $fromName, $toName ) {
73 $titleFrom = Title::makeTitle( NS_CATEGORY, $fromName );
74 $titleTo = Title::makeTitle( NS_CATEGORY, $toName );
75 $this->rdfWriter->about( $this->titleToUrl( $titleFrom ) )
76 ->say( self::ONTOLOGY_PREFIX, 'isInCategory' )
77 ->is( $this->titleToUrl( $titleTo ) );
78 }
79
80 /**
81 * Write out the data for single category.
82 * @param string $categoryName Category name
83 * @param bool $isHidden Hidden category?
84 * @param int $pages Page count (note this includes only Wiki articles, not subcats or files)
85 * @param int $subcategories Subcategory count
86 */
87 public function writeCategoryData( $categoryName, $isHidden, $pages, $subcategories ) {
88 $title = Title::makeTitle( NS_CATEGORY, $categoryName );
89 $this->rdfWriter->about( $this->titleToUrl( $title ) )
90 ->say( 'a' )
91 ->is( self::ONTOLOGY_PREFIX, 'Category' );
92 if ( $isHidden ) {
93 $this->rdfWriter->is( self::ONTOLOGY_PREFIX, 'HiddenCategory' );
94 }
95 $titletext = $title->getText();
96 $this->rdfWriter->say( 'rdfs', 'label' )->value( $titletext );
97 $this->rdfWriter->say( self::ONTOLOGY_PREFIX, 'pages' )->value( $pages );
98 $this->rdfWriter->say( self::ONTOLOGY_PREFIX, 'subcategories' )->value( $subcategories );
99 // TODO: do we want files too here? Easy to add, but don't have use case so far.
100 }
101
102 /**
103 * Make URL from title label
104 * @param string $titleLabel Short label (without namespace) of the category
105 * @return string URL for the category
106 */
107 public function labelToUrl( $titleLabel ) {
108 return $this->titleToUrl( Title::makeTitle( NS_CATEGORY, $titleLabel ) );
109 }
110
111 /**
112 * Convert Title to link to target page.
113 * @param Title $title
114 * @return string URL for the category
115 */
116 private function titleToUrl( Title $title ) {
117 return $title->getFullURL( '', false, PROTO_CANONICAL );
118 }
119
120 /**
121 * Get URI of the dump for this particular wiki.
122 * @return false|string
123 */
124 public function getDumpURI() {
125 return $this->titleToUrl( Title::makeTitle( NS_MAIN, self::SPECIAL_DUMP ) );
126 }
127
128 }