Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
41.18% covered (danger)
41.18%
7 / 17
71.43% covered (warning)
71.43%
5 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
FallbackContentHandler
41.18% covered (danger)
41.18%
7 / 17
71.43% covered (warning)
71.43%
5 / 7
16.97
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
2
 serializeContent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unserializeContent
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 makeEmptyContent
100.00% covered (success)
100.00%
1 / 1
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
 fillParserOutput
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 getSlotDiffRendererInternal
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
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.36 (As UnknownContentHandler in 1.34)
21 *
22 * @file
23 * @ingroup Content
24 */
25
26use MediaWiki\Content\Renderer\ContentParseParams;
27use MediaWiki\Context\IContextSource;
28use MediaWiki\Html\Html;
29use MediaWiki\Parser\ParserOutput;
30
31/**
32 * Content handler implementation for unknown content.
33 *
34 * This can be used to handle content for which no ContentHandler exists on the system,
35 * perhaps because the extension that provided it has been removed.
36 *
37 * @ingroup Content
38 */
39class FallbackContentHandler extends ContentHandler {
40
41    /**
42     * Constructs an FallbackContentHandler. Since FallbackContentHandler can be registered
43     * for multiple model IDs on a system, multiple instances of FallbackContentHandler may
44     * coexist.
45     *
46     * To preserve the serialization format of the original content model, it must be supplied
47     * to the constructor via the $formats parameter. If not given, the default format is
48     * reported as 'application/octet-stream'.
49     *
50     * @param string $modelId
51     * @param string[]|null $formats
52     */
53    public function __construct( $modelId, $formats = null ) {
54        parent::__construct(
55            $modelId,
56            $formats ?? [
57                'application/octet-stream',
58                'application/unknown',
59                'application/x-binary',
60                'text/unknown',
61                'unknown/unknown',
62            ]
63        );
64    }
65
66    /**
67     * Returns the content's data as-is.
68     *
69     * @param Content $content
70     * @param string|null $format The serialization format to check
71     *
72     * @return mixed
73     */
74    public function serializeContent( Content $content, $format = null ) {
75        /** @var FallbackContent $content */
76        '@phan-var FallbackContent $content';
77        return $content->getData();
78    }
79
80    /**
81     * Constructs an FallbackContent instance wrapping the given data.
82     *
83     * @since 1.21
84     *
85     * @param string $blob serialized content in an unknown format
86     * @param string|null $format ignored
87     *
88     * @return Content The FallbackContent object wrapping $data
89     */
90    public function unserializeContent( $blob, $format = null ) {
91        return new FallbackContent( $blob, $this->getModelID() );
92    }
93
94    /**
95     * Creates an empty FallbackContent object.
96     *
97     * @since 1.21
98     *
99     * @return Content A new FallbackContent object with empty text.
100     */
101    public function makeEmptyContent() {
102        return $this->unserializeContent( '' );
103    }
104
105    /**
106     * @return false
107     */
108    public function supportsDirectEditing() {
109        return false;
110    }
111
112    /**
113     * Fills the ParserOutput with an error message.
114     * @since 1.38
115     * @param Content $content
116     * @param ContentParseParams $cpoParams
117     * @param ParserOutput &$output The output object to fill (reference).
118     *
119     */
120    protected function fillParserOutput(
121        Content $content,
122        ContentParseParams $cpoParams,
123        ParserOutput &$output
124    ) {
125        '@phan-var FallbackContent $content';
126        $msg = wfMessage( 'unsupported-content-model', [ $content->getModel() ] );
127        $html = Html::rawElement( 'div', [ 'class' => 'error' ], $msg->inContentLanguage()->parse() );
128        $output->setRawText( $html );
129    }
130
131    /**
132     * @param IContextSource $context
133     *
134     * @return SlotDiffRenderer
135     */
136    protected function getSlotDiffRendererInternal( IContextSource $context ) {
137        return new UnsupportedSlotDiffRenderer( $context );
138    }
139}