Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
50.79% covered (warning)
50.79%
32 / 63
36.36% covered (danger)
36.36%
4 / 11
CRAP
0.00% covered (danger)
0.00%
0 / 1
TextContentHandler
50.79% covered (warning)
50.79%
32 / 63
36.36% covered (danger)
36.36%
4 / 11
62.01
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 serializeContent
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 merge3
83.33% covered (warning)
83.33%
15 / 18
0.00% covered (danger)
0.00%
0 / 1
5.12
 getContentClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 unserializeContent
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 makeEmptyContent
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 supportsDirectEditing
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getFieldsForSearchIndex
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getDataForSearchIndex
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 preSaveTransform
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 fillParserOutput
39.13% covered (danger)
39.13%
9 / 23
0.00% covered (danger)
0.00%
0 / 1
7.61
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
26use MediaWiki\Content\Renderer\ContentParseParams;
27use MediaWiki\Content\Transform\PreSaveTransformParams;
28use MediaWiki\MainConfigNames;
29use MediaWiki\MediaWikiServices;
30use MediaWiki\Parser\ParserOutput;
31use MediaWiki\Revision\RevisionRecord;
32
33/**
34 * Base content handler implementation for flat text contents.
35 *
36 * @ingroup Content
37 */
38class TextContentHandler extends ContentHandler {
39
40    public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) {
41        parent::__construct( $modelId, $formats );
42    }
43
44    /**
45     * Returns the content's text as-is.
46     *
47     * @param Content $content
48     * @param string|null $format The serialization format to check
49     *
50     * @return mixed
51     */
52    public function serializeContent( Content $content, $format = null ) {
53        $this->checkFormat( $format );
54
55        // @phan-suppress-next-line PhanUndeclaredMethod
56        return $content->getText();
57    }
58
59    /**
60     * Attempts to merge differences between three versions. Returns a new
61     * Content object for a clean merge and false for failure or a conflict.
62     *
63     * All three Content objects passed as parameters must have the same
64     * content model.
65     *
66     * This text-based implementation uses wfMerge().
67     *
68     * @param Content $oldContent The page's previous content.
69     * @param Content $myContent One of the page's conflicting contents.
70     * @param Content $yourContent One of the page's conflicting contents.
71     *
72     * @return Content|false
73     */
74    public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
75        // Nothing to do when the unsaved edit is already identical to the latest revision
76        if ( $myContent->equals( $yourContent ) ) {
77            return $yourContent;
78        }
79        // Impossible to have a conflict when the user just edited the latest revision. This can
80        // happen e.g. when $wgDiff3 is badly configured.
81        if ( $oldContent->equals( $yourContent ) ) {
82            return $myContent;
83        }
84
85        $this->checkModelID( $oldContent->getModel() );
86        $this->checkModelID( $myContent->getModel() );
87        $this->checkModelID( $yourContent->getModel() );
88
89        $format = $this->getDefaultFormat();
90
91        $old = $this->serializeContent( $oldContent, $format );
92        $mine = $this->serializeContent( $myContent, $format );
93        $yours = $this->serializeContent( $yourContent, $format );
94
95        $ok = wfMerge( $old, $mine, $yours, $result );
96
97        if ( !$ok ) {
98            return false;
99        }
100
101        if ( !$result ) {
102            return $this->makeEmptyContent();
103        }
104
105        $mergedContent = $this->unserializeContent( $result, $format );
106
107        return $mergedContent;
108    }
109
110    /**
111     * Returns the name of the associated Content class, to
112     * be used when creating new objects. Override expected
113     * by subclasses.
114     *
115     * @since 1.24
116     *
117     * @return class-string<TextContent>
118     */
119    protected function getContentClass() {
120        return TextContent::class;
121    }
122
123    /**
124     * Unserializes a Content object of the type supported by this ContentHandler.
125     *
126     * @since 1.21
127     *
128     * @param string $text Serialized form of the content
129     * @param string|null $format The format used for serialization
130     *
131     * @return Content The TextContent object wrapping $text
132     */
133    public function unserializeContent( $text, $format = null ) {
134        $this->checkFormat( $format );
135
136        $class = $this->getContentClass();
137        return new $class( $text );
138    }
139
140    /**
141     * Creates an empty TextContent object.
142     *
143     * @since 1.21
144     *
145     * @return Content A new TextContent object with empty text.
146     */
147    public function makeEmptyContent() {
148        $class = $this->getContentClass();
149        return new $class( '' );
150    }
151
152    /**
153     * @see ContentHandler::supportsDirectEditing
154     *
155     * @return bool Should return true for TextContent and derivatives.
156     */
157    public function supportsDirectEditing() {
158        return true;
159    }
160
161    public function getFieldsForSearchIndex( SearchEngine $engine ) {
162        $fields = parent::getFieldsForSearchIndex( $engine );
163        $fields['language'] =
164            $engine->makeSearchFieldMapping( 'language', SearchIndexField::INDEX_TYPE_KEYWORD );
165
166        return $fields;
167    }
168
169    public function getDataForSearchIndex(
170        WikiPage $page,
171        ParserOutput $output,
172        SearchEngine $engine,
173        ?RevisionRecord $revision = null
174    ) {
175        $fields = parent::getDataForSearchIndex( $page, $output, $engine, $revision );
176        $fields['language'] =
177            $this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode();
178        return $fields;
179    }
180
181    public function preSaveTransform(
182        Content $content,
183        PreSaveTransformParams $pstParams
184    ): Content {
185        '@phan-var TextContent $content';
186
187        $text = $content->getText();
188
189        $pst = TextContent::normalizeLineEndings( $text );
190
191        $contentClass = $this->getContentClass();
192        return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() );
193    }
194
195    /**
196     * Fills the provided ParserOutput object with information derived from the content.
197     * Unless $generateHtml was false, this includes an HTML representation of the content
198     * provided by getHtml().
199     *
200     * For content models listed in $wgTextModelsToParse, this method will call the MediaWiki
201     * wikitext parser on the text to extract any (wikitext) links, magic words, etc.,
202     * but note that the Table of Contents will *not* be generated
203     * (feature added by T307691, but should be refactored: T313455).
204     *
205     * Subclasses may override this to provide custom content processing.
206     * For custom HTML generation alone, it is sufficient to override getHtml().
207     *
208     * @stable to override
209     *
210     * @since 1.38
211     * @param Content $content
212     * @param ContentParseParams $cpoParams
213     * @param ParserOutput &$output The output object to fill (reference).
214     */
215    protected function fillParserOutput(
216        Content $content,
217        ContentParseParams $cpoParams,
218        ParserOutput &$output
219    ) {
220        $textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get(
221            MainConfigNames::TextModelsToParse );
222        '@phan-var TextContent $content';
223        if ( in_array( $content->getModel(), $textModelsToParse ) ) {
224            // parse just to get links etc into the database, HTML is replaced below.
225            $output = MediaWikiServices::getInstance()->getParserFactory()->getInstance()
226                ->parse(
227                    $content->getText(),
228                    $cpoParams->getPage(),
229                    $cpoParams->getParserOptions(),
230                    true,
231                    true,
232                    $cpoParams->getRevId()
233                );
234        }
235
236        if ( $cpoParams->getGenerateHtml() ) {
237            // Temporary changes as getHtml() is deprecated, we are working on removing usage of it.
238            if ( method_exists( $content, 'getHtml' ) ) {
239                $method = new ReflectionMethod( $content, 'getHtml' );
240                $method->setAccessible( true );
241                $html = $method->invoke( $content );
242                $html = "<pre>$html</pre>";
243            } else {
244                // Return an HTML representation of the content
245                $html = htmlspecialchars( $content->getText(), ENT_COMPAT );
246                $html = "<pre>$html</pre>";
247            }
248        } else {
249            $html = null;
250        }
251
252        $output->clearWrapperDivClass();
253        $output->setRawText( $html );
254    }
255}