CategoriesRdf.php 4.3 KB

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