PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/extensions/WikiCitation/includes/segments/WCTitleSegment.php

https://github.com/ChuguluGames/mediawiki-svn
PHP | 139 lines | 95 code | 19 blank | 25 comment | 19 complexity | 6ad107053fb19f3717cdc42a794fc932 MD5 | raw file
  1. <?php
  2. /**
  3. * Part of WikiCitation extension for Mediawiki.
  4. *
  5. * @ingroup WikiCitation
  6. * @file
  7. */
  8. class WCTitleSegment extends WCDataSegment {
  9. protected $titleObject;
  10. protected $titleFormat;
  11. protected $scope;
  12. protected $propertyType;
  13. public function __construct(
  14. WCCitation $citation,
  15. WCSegmentImportance $importance,
  16. WCScopeEnum $scope,
  17. WCPropertyEnum $propertyType,
  18. WCTitleFormat $titleFormat,
  19. $prefix = '',
  20. $suffix = ''
  21. ) {
  22. parent::__construct( $citation, $prefix, $suffix );
  23. $this->titleFormat = $titleFormat;
  24. switch( $importance->key ) {
  25. case WCSegmentImportance::mandatory:
  26. $this->titleObject = $citation->reference->inferProperty( $scope, $propertyType );
  27. $this->exists = True;
  28. return;
  29. case WCSegmentImportance::important:
  30. $this->titleObject = $citation->reference->inferProperty( $scope, $propertyType );
  31. $this->exists = (bool) $this->titleObject;
  32. break;
  33. case WCSegmentImportance::optional:
  34. $titleObject = &$citation->properties[ $scope->key ][ $propertyType->key ];
  35. if ( isset( $titleObject ) ) {
  36. $this->titleObject = $titleObject;
  37. $this->exists = True;
  38. } else {
  39. $this->exists = False;
  40. }
  41. }
  42. # $scope and $propertyType may have changed by now.
  43. $this->scope = $scope;
  44. $this->propertyType = $propertyType;
  45. }
  46. public function getLabel( WCStyle $style, WCLabelFormEnum $form, WCPluralEnum $plural ) {
  47. return $style->propertyLabels[ $this->propertyType->key ][ $form->key ][ $plural->key ];
  48. }
  49. /**
  50. * Format title with quotes and other optional styling.
  51. *
  52. * This will wrap the title in an HTML <cite> element. The title will also
  53. * be quoted if the reference style is designated as a quoted-style
  54. * title in WCTypeEnum::$titleFormat.
  55. * @param WCStyle $style
  56. * @param string $endSeparator
  57. * @return string
  58. */
  59. public function render( WCStyle $style, $endSeparator = '' ) {
  60. $endSeparator = $this->extendSuffix( $endSeparator );
  61. if ( $this->titleObject ) {
  62. $title = $this->titleObject->getTitle();
  63. # "quoted"-type title
  64. if ( $this->titleFormat->key == WCTitleFormat::quoted ) {
  65. if ( $style->punctuationInQuotes ) { # Punctuation is inside quotes:
  66. # Check for final quotes at the end of the title:
  67. $p = preg_split( '/(<\/q>+)$/uS', $title, 2, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE );
  68. if ( count( $p ) >= 2 ) {
  69. $title = $p[0] . mb_substr( $endSeparator, 0, 1 ) . $p[1];
  70. $title = $style->convertSemanticToCharacterQuotes( $title, True );
  71. $title = $style->transformTitle( $title );
  72. $title = '<cite class="' . WCStyle::quotedTitleHTML . '">' . $title . '</cite>';
  73. $title = $style->quote( $title );
  74. } else {
  75. $title = $style->convertSemanticToCharacterQuotes( $title, True );
  76. $title = $style->transformTitle( $title );
  77. $chrL = mb_substr( $title, -1, 1 );
  78. $chrR = mb_substr( $endSeparator, 0, 1 );
  79. if ( $chrR && ( $chrL === $chrR ) ) {
  80. # Wrap <cite> tag inside the quotes.
  81. $title = '<cite class="' . WCStyle::quotedTitleHTML . '">' . $title . '</cite>';
  82. $title = $style->quote( $title );
  83. } else {
  84. $title = $style->quote( $title . mb_substr( $endSeparator, 0, 1 ) );
  85. # Wrap <cite> tag outside the quotes.
  86. $title = '<cite class="' . WCStyle::quotedTitleHTML . '">' . $title . '</cite>';
  87. }
  88. }
  89. return $this->prefix . $title . mb_substr( $endSeparator, 1 );
  90. } else {
  91. # Punctuation follows quotes.
  92. $title = $style->convertSemanticToCharacterQuotes( $title, True );
  93. $title = $style->transformTitle( $title );
  94. $title = '<cite class="' . WCStyle::quotedTitleHTML . '">' . $title . '</cite>';
  95. return $this->prefix . $style->quote( $title ) . $endSeparator;
  96. }
  97. }
  98. # "italic"-type title
  99. $title = $style->convertSemanticToCharacterQuotes( $title );
  100. $title = $style->transformTitle( $title );
  101. $chrL = mb_substr( $title, -1, 1 );
  102. $chrR = mb_substr( $endSeparator, 0, 1 );
  103. if ( $chrR && $chrL == $chrR ) {
  104. $endSeparator = ltrim( $endSeparator, $chrR );
  105. }
  106. if ( $this->titleFormat->key == WCTitleFormat::italic ) {
  107. # "Italic"-type title
  108. return $this->prefix . '<cite class="' . WCStyle::italicTitleHTML . '">' . $title . '</cite>' . $endSeparator;
  109. } else {
  110. # WCTitleFormat::normal:
  111. return $this->prefix . '<cite>' . $title . '</cite>' . $endSeparator;
  112. }
  113. } else {
  114. return $this->prefix . $style->segmentMissing . $endSeparator;
  115. }
  116. }
  117. public function getSortingParts() {
  118. return array( strip_tags( $this->titleObject->getTitle() ) );
  119. }
  120. }