Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / content / TextContentHandler.php
blob09cdcee143813fb6e648a790fbac9a7a2eb36844
1 <?php
2 /**
3 * Base content handler class for flat text contents.
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.
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.
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
20 * @since 1.21
22 * @file
23 * @ingroup Content
26 /**
27 * Base content handler implementation for flat text contents.
29 * @ingroup Content
31 class TextContentHandler extends ContentHandler {
33 // @codingStandardsIgnoreStart bug 57585
34 public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
35 parent::__construct( $modelId, $formats );
37 // @codingStandardsIgnoreEnd
39 /**
40 * Returns the content's text as-is.
42 * @param Content $content
43 * @param string $format The serialization format to check
45 * @return mixed
47 public function serializeContent( Content $content, $format = null ) {
48 $this->checkFormat( $format );
50 return $content->getNativeData();
53 /**
54 * Attempts to merge differences between three versions. Returns a new
55 * Content object for a clean merge and false for failure or a conflict.
57 * All three Content objects passed as parameters must have the same
58 * content model.
60 * This text-based implementation uses wfMerge().
62 * @param Content $oldContent The page's previous content.
63 * @param Content $myContent One of the page's conflicting contents.
64 * @param Content $yourContent One of the page's conflicting contents.
66 * @return Content|bool
68 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
69 $this->checkModelID( $oldContent->getModel() );
70 $this->checkModelID( $myContent->getModel() );
71 $this->checkModelID( $yourContent->getModel() );
73 $format = $this->getDefaultFormat();
75 $old = $this->serializeContent( $oldContent, $format );
76 $mine = $this->serializeContent( $myContent, $format );
77 $yours = $this->serializeContent( $yourContent, $format );
79 $ok = wfMerge( $old, $mine, $yours, $result );
81 if ( !$ok ) {
82 return false;
85 if ( !$result ) {
86 return $this->makeEmptyContent();
89 $mergedContent = $this->unserializeContent( $result, $format );
91 return $mergedContent;
94 /**
95 * Returns the name of the associated Content class, to
96 * be used when creating new objects. Override expected
97 * by subclasses.
99 * @since 1.24
101 * @return string
103 protected function getContentClass() {
104 return TextContent::class;
108 * Unserializes a Content object of the type supported by this ContentHandler.
110 * @since 1.21
112 * @param string $text Serialized form of the content
113 * @param string $format The format used for serialization
115 * @return Content The TextContent object wrapping $text
117 public function unserializeContent( $text, $format = null ) {
118 $this->checkFormat( $format );
120 $class = $this->getContentClass();
121 return new $class( $text );
125 * Creates an empty TextContent object.
127 * @since 1.21
129 * @return Content A new TextContent object with empty text.
131 public function makeEmptyContent() {
132 $class = $this->getContentClass();
133 return new $class( '' );
137 * @see ContentHandler::supportsDirectEditing
139 * @return bool Default is true for TextContent and derivatives.
141 public function supportsDirectEditing() {
142 return true;
145 public function getFieldsForSearchIndex( SearchEngine $engine ) {
146 $fields = parent::getFieldsForSearchIndex( $engine );
147 $fields['language'] =
148 $engine->makeSearchFieldMapping( 'language', SearchIndexField::INDEX_TYPE_KEYWORD );
150 return $fields;
153 public function getDataForSearchIndex( WikiPage $page, ParserOutput $output,
154 SearchEngine $engine ) {
155 $fields = parent::getDataForSearchIndex( $page, $output, $engine );
156 $fields['language'] =
157 $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
158 return $fields;