Merge "Warn if stateful ParserOutput transforms are used"
[lhc/web/wiklou.git] / includes / content / TextContentHandler.php
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 /**
27 * Base content handler implementation for flat text contents.
28 *
29 * @ingroup Content
30 */
31 class TextContentHandler extends ContentHandler {
32
33 public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
34 parent::__construct( $modelId, $formats );
35 }
36
37 /**
38 * Returns the content's text as-is.
39 *
40 * @param Content $content
41 * @param string $format The serialization format to check
42 *
43 * @return mixed
44 */
45 public function serializeContent( Content $content, $format = null ) {
46 $this->checkFormat( $format );
47
48 return $content->getNativeData();
49 }
50
51 /**
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.
54 *
55 * All three Content objects passed as parameters must have the same
56 * content model.
57 *
58 * This text-based implementation uses wfMerge().
59 *
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.
63 *
64 * @return Content|bool
65 */
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() );
70
71 $format = $this->getDefaultFormat();
72
73 $old = $this->serializeContent( $oldContent, $format );
74 $mine = $this->serializeContent( $myContent, $format );
75 $yours = $this->serializeContent( $yourContent, $format );
76
77 $ok = wfMerge( $old, $mine, $yours, $result );
78
79 if ( !$ok ) {
80 return false;
81 }
82
83 if ( !$result ) {
84 return $this->makeEmptyContent();
85 }
86
87 $mergedContent = $this->unserializeContent( $result, $format );
88
89 return $mergedContent;
90 }
91
92 /**
93 * Returns the name of the associated Content class, to
94 * be used when creating new objects. Override expected
95 * by subclasses.
96 *
97 * @since 1.24
98 *
99 * @return string
100 */
101 protected function getContentClass() {
102 return TextContent::class;
103 }
104
105 /**
106 * Unserializes a Content object of the type supported by this ContentHandler.
107 *
108 * @since 1.21
109 *
110 * @param string $text Serialized form of the content
111 * @param string $format The format used for serialization
112 *
113 * @return Content The TextContent object wrapping $text
114 */
115 public function unserializeContent( $text, $format = null ) {
116 $this->checkFormat( $format );
117
118 $class = $this->getContentClass();
119 return new $class( $text );
120 }
121
122 /**
123 * Creates an empty TextContent object.
124 *
125 * @since 1.21
126 *
127 * @return Content A new TextContent object with empty text.
128 */
129 public function makeEmptyContent() {
130 $class = $this->getContentClass();
131 return new $class( '' );
132 }
133
134 /**
135 * @see ContentHandler::supportsDirectEditing
136 *
137 * @return bool Default is true for TextContent and derivatives.
138 */
139 public function supportsDirectEditing() {
140 return true;
141 }
142
143 public function getFieldsForSearchIndex( SearchEngine $engine ) {
144 $fields = parent::getFieldsForSearchIndex( $engine );
145 $fields['language'] =
146 $engine->makeSearchFieldMapping( 'language', SearchIndexField::INDEX_TYPE_KEYWORD );
147
148 return $fields;
149 }
150
151 public function getDataForSearchIndex(
152 WikiPage $page,
153 ParserOutput $output,
154 SearchEngine $engine
155 ) {
156 $fields = parent::getDataForSearchIndex( $page, $output, $engine );
157 $fields['language'] =
158 $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
159 return $fields;
160 }
161
162 }