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