ForeignTitle.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * A structure to hold the title of a page on a foreign MediaWiki installation
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @author This, that and the other
  22. */
  23. /**
  24. * A simple, immutable structure to hold the title of a page on a foreign
  25. * MediaWiki installation.
  26. */
  27. class ForeignTitle {
  28. /**
  29. * @var int|null
  30. * Null if we don't know the namespace ID (e.g. interwiki links)
  31. */
  32. private $namespaceId;
  33. /** @var string */
  34. private $namespaceName;
  35. /** @var string */
  36. private $pageName;
  37. /**
  38. * Creates a new ForeignTitle object.
  39. *
  40. * @param int|null $namespaceId Null if the namespace ID is unknown (e.g.
  41. * interwiki links)
  42. * @param string $namespaceName
  43. * @param string $pageName
  44. */
  45. public function __construct( $namespaceId, $namespaceName, $pageName ) {
  46. if ( is_null( $namespaceId ) ) {
  47. $this->namespaceId = null;
  48. } else {
  49. $this->namespaceId = intval( $namespaceId );
  50. }
  51. $this->namespaceName = str_replace( ' ', '_', $namespaceName );
  52. $this->pageName = str_replace( ' ', '_', $pageName );
  53. }
  54. /**
  55. * Do we know the namespace ID of the page on the foreign wiki?
  56. * @return bool
  57. */
  58. public function isNamespaceIdKnown() {
  59. return !is_null( $this->namespaceId );
  60. }
  61. /**
  62. * @return int
  63. * @throws MWException If isNamespaceIdKnown() is false, it does not make
  64. * sense to call this function.
  65. */
  66. public function getNamespaceId() {
  67. if ( is_null( $this->namespaceId ) ) {
  68. throw new MWException(
  69. "Attempted to call getNamespaceId when the namespace ID is not known" );
  70. }
  71. return $this->namespaceId;
  72. }
  73. /** @return string */
  74. public function getNamespaceName() {
  75. return $this->namespaceName;
  76. }
  77. /** @return string */
  78. public function getText() {
  79. return $this->pageName;
  80. }
  81. /** @return string */
  82. public function getFullText() {
  83. $result = '';
  84. if ( $this->namespaceName ) {
  85. $result .= $this->namespaceName . ':';
  86. }
  87. $result .= $this->pageName;
  88. return $result;
  89. }
  90. /**
  91. * Returns a string representation of the title, for logging. This is purely
  92. * informative and must not be used programmatically. Use the appropriate
  93. * ImportTitleFactory to generate the correct string representation for a
  94. * given use.
  95. *
  96. * @return string
  97. */
  98. public function __toString() {
  99. $name = '';
  100. if ( $this->isNamespaceIdKnown() ) {
  101. $name .= '{ns' . $this->namespaceId . '}';
  102. } else {
  103. $name .= '{ns??}';
  104. }
  105. $name .= $this->namespaceName . ':' . $this->pageName;
  106. return $name;
  107. }
  108. }