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 $format The serialization format to check
45 public function serializeContent( Content
$content, $format = null ) {
46 $this->checkFormat( $format );
48 return $content->getNativeData();
52 * Attempts to merge differences between three versions. Returns a new
53 * Content object for a clean merge and false for failure or a conflict.
55 * All three Content objects passed as parameters must have the same
58 * This text-based implementation uses wfMerge().
60 * @param Content $oldContent The page's previous content.
61 * @param Content $myContent One of the page's conflicting contents.
62 * @param Content $yourContent One of the page's conflicting contents.
64 * @return Content|bool
66 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
67 $this->checkModelID( $oldContent->getModel() );
68 $this->checkModelID( $myContent->getModel() );
69 $this->checkModelID( $yourContent->getModel() );
71 $format = $this->getDefaultFormat();
73 $old = $this->serializeContent( $oldContent, $format );
74 $mine = $this->serializeContent( $myContent, $format );
75 $yours = $this->serializeContent( $yourContent, $format );
77 $ok = wfMerge( $old, $mine, $yours, $result );
84 return $this->makeEmptyContent();
87 $mergedContent = $this->unserializeContent( $result, $format );
89 return $mergedContent;
93 * Returns the name of the associated Content class, to
94 * be used when creating new objects. Override expected
101 protected function getContentClass() {
102 return TextContent
::class;
106 * Unserializes a Content object of the type supported by this ContentHandler.
110 * @param string $text Serialized form of the content
111 * @param string $format The format used for serialization
113 * @return Content The TextContent object wrapping $text
115 public function unserializeContent( $text, $format = null ) {
116 $this->checkFormat( $format );
118 $class = $this->getContentClass();
119 return new $class( $text );
123 * Creates an empty TextContent object.
127 * @return Content A new TextContent object with empty text.
129 public function makeEmptyContent() {
130 $class = $this->getContentClass();
131 return new $class( '' );
135 * @see ContentHandler::supportsDirectEditing
137 * @return bool Default is true for TextContent and derivatives.
139 public function supportsDirectEditing() {
143 public function getFieldsForSearchIndex( SearchEngine
$engine ) {
144 $fields = parent
::getFieldsForSearchIndex( $engine );
145 $fields['language'] =
146 $engine->makeSearchFieldMapping( 'language', SearchIndexField
::INDEX_TYPE_KEYWORD
);
151 public function getDataForSearchIndex(
153 ParserOutput
$output,
156 $fields = parent
::getDataForSearchIndex( $page, $output, $engine );
157 $fields['language'] =
158 $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();