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
27 * Base content handler implementation for flat text contents.
31 class TextContentHandler
extends ContentHandler
{
33 public function __construct( $modelId = CONTENT_MODEL_TEXT
, $formats = [ CONTENT_FORMAT_TEXT
] ) {
34 parent
::__construct( $modelId, $formats );
38 * Returns the content's text as-is.
40 * @param Content $content
41 * @param string|null $format The serialization format to check
45 public function serializeContent( Content
$content, $format = null ) {
46 $this->checkFormat( $format );
48 // @phan-suppress-next-line PhanUndeclaredMethod
49 return $content->getText();
53 * Attempts to merge differences between three versions. Returns a new
54 * Content object for a clean merge and false for failure or a conflict.
56 * All three Content objects passed as parameters must have the same
59 * This text-based implementation uses wfMerge().
61 * @param Content $oldContent The page's previous content.
62 * @param Content $myContent One of the page's conflicting contents.
63 * @param Content $yourContent One of the page's conflicting contents.
65 * @return Content|bool
67 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
68 $this->checkModelID( $oldContent->getModel() );
69 $this->checkModelID( $myContent->getModel() );
70 $this->checkModelID( $yourContent->getModel() );
72 $format = $this->getDefaultFormat();
74 $old = $this->serializeContent( $oldContent, $format );
75 $mine = $this->serializeContent( $myContent, $format );
76 $yours = $this->serializeContent( $yourContent, $format );
78 $ok = wfMerge( $old, $mine, $yours, $result );
85 return $this->makeEmptyContent();
88 $mergedContent = $this->unserializeContent( $result, $format );
90 return $mergedContent;
94 * Returns the name of the associated Content class, to
95 * be used when creating new objects. Override expected
102 protected function getContentClass() {
103 return TextContent
::class;
107 * Unserializes a Content object of the type supported by this ContentHandler.
111 * @param string $text Serialized form of the content
112 * @param string|null $format The format used for serialization
114 * @return Content The TextContent object wrapping $text
116 public function unserializeContent( $text, $format = null ) {
117 $this->checkFormat( $format );
119 $class = $this->getContentClass();
120 return new $class( $text );
124 * Creates an empty TextContent object.
128 * @return Content A new TextContent object with empty text.
130 public function makeEmptyContent() {
131 $class = $this->getContentClass();
132 return new $class( '' );
136 * @see ContentHandler::supportsDirectEditing
138 * @return bool Default is true for TextContent and derivatives.
140 public function supportsDirectEditing() {
144 public function getFieldsForSearchIndex( SearchEngine
$engine ) {
145 $fields = parent
::getFieldsForSearchIndex( $engine );
146 $fields['language'] =
147 $engine->makeSearchFieldMapping( 'language', SearchIndexField
::INDEX_TYPE_KEYWORD
);
152 public function getDataForSearchIndex(
154 ParserOutput
$output,
157 $fields = parent
::getDataForSearchIndex( $page, $output, $engine );
158 $fields['language'] =
159 $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();