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