TextContentHandler.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. <?php
  2. /**
  3. * Base content handler class for flat text contents.
  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. /**
  26. * Base content handler implementation for flat text contents.
  27. *
  28. * @ingroup Content
  29. */
  30. class TextContentHandler extends ContentHandler {
  31. public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
  32. parent::__construct( $modelId, $formats );
  33. }
  34. /**
  35. * Returns the content's text as-is.
  36. *
  37. * @param Content $content
  38. * @param string|null $format The serialization format to check
  39. *
  40. * @return mixed
  41. */
  42. public function serializeContent( Content $content, $format = null ) {
  43. $this->checkFormat( $format );
  44. // @phan-suppress-next-line PhanUndeclaredMethod
  45. return $content->getText();
  46. }
  47. /**
  48. * Attempts to merge differences between three versions. Returns a new
  49. * Content object for a clean merge and false for failure or a conflict.
  50. *
  51. * All three Content objects passed as parameters must have the same
  52. * content model.
  53. *
  54. * This text-based implementation uses wfMerge().
  55. *
  56. * @param Content $oldContent The page's previous content.
  57. * @param Content $myContent One of the page's conflicting contents.
  58. * @param Content $yourContent One of the page's conflicting contents.
  59. *
  60. * @return Content|bool
  61. */
  62. public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
  63. $this->checkModelID( $oldContent->getModel() );
  64. $this->checkModelID( $myContent->getModel() );
  65. $this->checkModelID( $yourContent->getModel() );
  66. $format = $this->getDefaultFormat();
  67. $old = $this->serializeContent( $oldContent, $format );
  68. $mine = $this->serializeContent( $myContent, $format );
  69. $yours = $this->serializeContent( $yourContent, $format );
  70. $ok = wfMerge( $old, $mine, $yours, $result );
  71. if ( !$ok ) {
  72. return false;
  73. }
  74. if ( !$result ) {
  75. return $this->makeEmptyContent();
  76. }
  77. $mergedContent = $this->unserializeContent( $result, $format );
  78. return $mergedContent;
  79. }
  80. /**
  81. * Returns the name of the associated Content class, to
  82. * be used when creating new objects. Override expected
  83. * by subclasses.
  84. *
  85. * @since 1.24
  86. *
  87. * @return string
  88. */
  89. protected function getContentClass() {
  90. return TextContent::class;
  91. }
  92. /**
  93. * Unserializes a Content object of the type supported by this ContentHandler.
  94. *
  95. * @since 1.21
  96. *
  97. * @param string $text Serialized form of the content
  98. * @param string|null $format The format used for serialization
  99. *
  100. * @return Content The TextContent object wrapping $text
  101. */
  102. public function unserializeContent( $text, $format = null ) {
  103. $this->checkFormat( $format );
  104. $class = $this->getContentClass();
  105. return new $class( $text );
  106. }
  107. /**
  108. * Creates an empty TextContent object.
  109. *
  110. * @since 1.21
  111. *
  112. * @return Content A new TextContent object with empty text.
  113. */
  114. public function makeEmptyContent() {
  115. $class = $this->getContentClass();
  116. return new $class( '' );
  117. }
  118. /**
  119. * @see ContentHandler::supportsDirectEditing
  120. *
  121. * @return bool Default is true for TextContent and derivatives.
  122. */
  123. public function supportsDirectEditing() {
  124. return true;
  125. }
  126. public function getFieldsForSearchIndex( SearchEngine $engine ) {
  127. $fields = parent::getFieldsForSearchIndex( $engine );
  128. $fields['language'] =
  129. $engine->makeSearchFieldMapping( 'language', SearchIndexField::INDEX_TYPE_KEYWORD );
  130. return $fields;
  131. }
  132. public function getDataForSearchIndex(
  133. WikiPage $page,
  134. ParserOutput $output,
  135. SearchEngine $engine
  136. ) {
  137. $fields = parent::getDataForSearchIndex( $page, $output, $engine );
  138. $fields['language'] =
  139. $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
  140. return $fields;
  141. }
  142. }