WikitextContentHandler.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <?php
  2. /**
  3. * Content handler for wiki text pages.
  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. * @since 1.21
  21. *
  22. * @file
  23. * @ingroup Content
  24. */
  25. use MediaWiki\MediaWikiServices;
  26. /**
  27. * Content handler for wiki text pages.
  28. *
  29. * @ingroup Content
  30. */
  31. class WikitextContentHandler extends TextContentHandler {
  32. public function __construct( $modelId = CONTENT_MODEL_WIKITEXT ) {
  33. parent::__construct( $modelId, [ CONTENT_FORMAT_WIKITEXT ] );
  34. }
  35. protected function getContentClass() {
  36. return WikitextContent::class;
  37. }
  38. /**
  39. * Returns a WikitextContent object representing a redirect to the given destination page.
  40. *
  41. * @param Title $destination The page to redirect to.
  42. * @param string $text Text to include in the redirect, if possible.
  43. *
  44. * @return Content
  45. *
  46. * @see ContentHandler::makeRedirectContent
  47. */
  48. public function makeRedirectContent( Title $destination, $text = '' ) {
  49. $optionalColon = '';
  50. if ( $destination->getNamespace() == NS_CATEGORY ) {
  51. $optionalColon = ':';
  52. } else {
  53. $iw = $destination->getInterwiki();
  54. if ( $iw && Language::fetchLanguageName( $iw, null, 'mw' ) ) {
  55. $optionalColon = ':';
  56. }
  57. }
  58. $mwRedir = MediaWikiServices::getInstance()->getMagicWordFactory()->get( 'redirect' );
  59. $redirectText = $mwRedir->getSynonym( 0 ) .
  60. ' [[' . $optionalColon . $destination->getFullText() . ']]';
  61. if ( $text != '' ) {
  62. $redirectText .= "\n" . $text;
  63. }
  64. $class = $this->getContentClass();
  65. return new $class( $redirectText );
  66. }
  67. /**
  68. * Returns true because wikitext supports redirects.
  69. *
  70. * @return bool Always true.
  71. *
  72. * @see ContentHandler::supportsRedirects
  73. */
  74. public function supportsRedirects() {
  75. return true;
  76. }
  77. /**
  78. * Returns true because wikitext supports sections.
  79. *
  80. * @return bool Always true.
  81. *
  82. * @see ContentHandler::supportsSections
  83. */
  84. public function supportsSections() {
  85. return true;
  86. }
  87. /**
  88. * Returns true, because wikitext supports caching using the
  89. * ParserCache mechanism.
  90. *
  91. * @since 1.21
  92. *
  93. * @return bool Always true.
  94. *
  95. * @see ContentHandler::isParserCacheSupported
  96. */
  97. public function isParserCacheSupported() {
  98. return true;
  99. }
  100. /**
  101. * Get file handler
  102. * @return FileContentHandler
  103. */
  104. protected function getFileHandler() {
  105. return new FileContentHandler();
  106. }
  107. public function getFieldsForSearchIndex( SearchEngine $engine ) {
  108. $fields = parent::getFieldsForSearchIndex( $engine );
  109. $fields['heading'] =
  110. $engine->makeSearchFieldMapping( 'heading', SearchIndexField::INDEX_TYPE_TEXT );
  111. $fields['heading']->setFlag( SearchIndexField::FLAG_SCORING );
  112. $fields['auxiliary_text'] =
  113. $engine->makeSearchFieldMapping( 'auxiliary_text', SearchIndexField::INDEX_TYPE_TEXT );
  114. $fields['opening_text'] =
  115. $engine->makeSearchFieldMapping( 'opening_text', SearchIndexField::INDEX_TYPE_TEXT );
  116. $fields['opening_text']->setFlag(
  117. SearchIndexField::FLAG_SCORING | SearchIndexField::FLAG_NO_HIGHLIGHT
  118. );
  119. // Until we have full first-class content handler for files, we invoke it explicitly here
  120. $fields = array_merge( $fields, $this->getFileHandler()->getFieldsForSearchIndex( $engine ) );
  121. return $fields;
  122. }
  123. public function getDataForSearchIndex(
  124. WikiPage $page,
  125. ParserOutput $parserOutput,
  126. SearchEngine $engine
  127. ) {
  128. $fields = parent::getDataForSearchIndex( $page, $parserOutput, $engine );
  129. $structure = new WikiTextStructure( $parserOutput );
  130. $fields['heading'] = $structure->headings();
  131. // text fields
  132. $fields['opening_text'] = $structure->getOpeningText();
  133. $fields['text'] = $structure->getMainText(); // overwrites one from ContentHandler
  134. $fields['auxiliary_text'] = $structure->getAuxiliaryText();
  135. $fields['defaultsort'] = $structure->getDefaultSort();
  136. // Until we have full first-class content handler for files, we invoke it explicitly here
  137. if ( NS_FILE == $page->getTitle()->getNamespace() ) {
  138. $fields = array_merge( $fields,
  139. $this->getFileHandler()->getDataForSearchIndex( $page, $parserOutput, $engine ) );
  140. }
  141. return $fields;
  142. }
  143. }