Merge "Add .pipeline/ with dev image variant"
[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|null $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 // @phan-suppress-next-line PhanUndeclaredMethod
49 return $content->getText();
50 }
51
52 /**
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.
55 *
56 * All three Content objects passed as parameters must have the same
57 * content model.
58 *
59 * This text-based implementation uses wfMerge().
60 *
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.
64 *
65 * @return Content|bool
66 */
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() );
71
72 $format = $this->getDefaultFormat();
73
74 $old = $this->serializeContent( $oldContent, $format );
75 $mine = $this->serializeContent( $myContent, $format );
76 $yours = $this->serializeContent( $yourContent, $format );
77
78 $ok = wfMerge( $old, $mine, $yours, $result );
79
80 if ( !$ok ) {
81 return false;
82 }
83
84 if ( !$result ) {
85 return $this->makeEmptyContent();
86 }
87
88 $mergedContent = $this->unserializeContent( $result, $format );
89
90 return $mergedContent;
91 }
92
93 /**
94 * Returns the name of the associated Content class, to
95 * be used when creating new objects. Override expected
96 * by subclasses.
97 *
98 * @since 1.24
99 *
100 * @return string
101 */
102 protected function getContentClass() {
103 return TextContent::class;
104 }
105
106 /**
107 * Unserializes a Content object of the type supported by this ContentHandler.
108 *
109 * @since 1.21
110 *
111 * @param string $text Serialized form of the content
112 * @param string|null $format The format used for serialization
113 *
114 * @return Content The TextContent object wrapping $text
115 */
116 public function unserializeContent( $text, $format = null ) {
117 $this->checkFormat( $format );
118
119 $class = $this->getContentClass();
120 return new $class( $text );
121 }
122
123 /**
124 * Creates an empty TextContent object.
125 *
126 * @since 1.21
127 *
128 * @return Content A new TextContent object with empty text.
129 */
130 public function makeEmptyContent() {
131 $class = $this->getContentClass();
132 return new $class( '' );
133 }
134
135 /**
136 * @see ContentHandler::supportsDirectEditing
137 *
138 * @return bool Default is true for TextContent and derivatives.
139 */
140 public function supportsDirectEditing() {
141 return true;
142 }
143
144 public function getFieldsForSearchIndex( SearchEngine $engine ) {
145 $fields = parent::getFieldsForSearchIndex( $engine );
146 $fields['language'] =
147 $engine->makeSearchFieldMapping( 'language', SearchIndexField::INDEX_TYPE_KEYWORD );
148
149 return $fields;
150 }
151
152 public function getDataForSearchIndex(
153 WikiPage $page,
154 ParserOutput $output,
155 SearchEngine $engine
156 ) {
157 $fields = parent::getDataForSearchIndex( $page, $output, $engine );
158 $fields['language'] =
159 $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
160 return $fields;
161 }
162
163 }