| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
namespace MediaWiki\Content; |
| 27 |
|
| 28 |
use MediaWiki\Content\Renderer\ContentParseParams; |
| 29 |
use MediaWiki\Content\Transform\PreSaveTransformParams; |
| 30 |
use MediaWiki\MainConfigNames; |
| 31 |
use MediaWiki\MediaWikiServices; |
| 32 |
use MediaWiki\Parser\ParserOutput; |
| 33 |
use MediaWiki\Revision\RevisionRecord; |
| 34 |
use SearchEngine; |
| 35 |
use SearchIndexField; |
| 36 |
use WikiPage; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class TextContentHandler extends ContentHandler { |
| 44 |
|
| 45 |
public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = [ CONTENT_FORMAT_TEXT ] ) { |
| 46 |
parent::__construct( $modelId, $formats ); |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public function serializeContent( Content $content, $format = null ) { |
| 58 |
$this->checkFormat( $format ); |
| 59 |
|
| 60 |
|
| 61 |
return $content->getText(); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) { |
| 80 |
|
| 81 |
if ( $myContent->equals( $yourContent ) ) { |
| 82 |
return $yourContent; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
if ( $oldContent->equals( $yourContent ) ) { |
| 87 |
return $myContent; |
| 88 |
} |
| 89 |
|
| 90 |
$this->checkModelID( $oldContent->getModel() ); |
| 91 |
$this->checkModelID( $myContent->getModel() ); |
| 92 |
$this->checkModelID( $yourContent->getModel() ); |
| 93 |
|
| 94 |
$format = $this->getDefaultFormat(); |
| 95 |
|
| 96 |
$old = $this->serializeContent( $oldContent, $format ); |
| 97 |
$mine = $this->serializeContent( $myContent, $format ); |
| 98 |
$yours = $this->serializeContent( $yourContent, $format ); |
| 99 |
|
| 100 |
$ok = wfMerge( $old, $mine, $yours, $result ); |
| 101 |
|
| 102 |
if ( !$ok ) { |
| 103 |
return false; |
| 104 |
} |
| 105 |
|
| 106 |
if ( !$result ) { |
| 107 |
return $this->makeEmptyContent(); |
| 108 |
} |
| 109 |
|
| 110 |
$mergedContent = $this->unserializeContent( $result, $format ); |
| 111 |
|
| 112 |
return $mergedContent; |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
protected function getContentClass() { |
| 125 |
return TextContent::class; |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
public function unserializeContent( $text, $format = null ) { |
| 139 |
$this->checkFormat( $format ); |
| 140 |
|
| 141 |
$class = $this->getContentClass(); |
| 142 |
return new $class( $text ); |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
public function makeEmptyContent() { |
| 153 |
$class = $this->getContentClass(); |
| 154 |
return new $class( '' ); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
public function supportsDirectEditing() { |
| 163 |
return true; |
| 164 |
} |
| 165 |
|
| 166 |
public function getFieldsForSearchIndex( SearchEngine $engine ) { |
| 167 |
$fields = parent::getFieldsForSearchIndex( $engine ); |
| 168 |
$fields['language'] = |
| 169 |
$engine->makeSearchFieldMapping( 'language', SearchIndexField::INDEX_TYPE_KEYWORD ); |
| 170 |
|
| 171 |
return $fields; |
| 172 |
} |
| 173 |
|
| 174 |
public function getDataForSearchIndex( |
| 175 |
WikiPage $page, |
| 176 |
ParserOutput $output, |
| 177 |
SearchEngine $engine, |
| 178 |
?RevisionRecord $revision = null |
| 179 |
) { |
| 180 |
$fields = parent::getDataForSearchIndex( $page, $output, $engine, $revision ); |
| 181 |
$fields['language'] = |
| 182 |
$this->getPageLanguage( $page->getTitle(), $page->getContent() )->getCode(); |
| 183 |
return $fields; |
| 184 |
} |
| 185 |
|
| 186 |
public function preSaveTransform( |
| 187 |
Content $content, |
| 188 |
PreSaveTransformParams $pstParams |
| 189 |
): Content { |
| 190 |
'@phan-var TextContent $content'; |
| 191 |
|
| 192 |
$text = $content->getText(); |
| 193 |
|
| 194 |
$pst = TextContent::normalizeLineEndings( $text ); |
| 195 |
|
| 196 |
$contentClass = $this->getContentClass(); |
| 197 |
return ( $text === $pst ) ? $content : new $contentClass( $pst, $content->getModel() ); |
| 198 |
} |
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
protected function fillParserOutput( |
| 221 |
Content $content, |
| 222 |
ContentParseParams $cpoParams, |
| 223 |
ParserOutput &$output |
| 224 |
) { |
| 225 |
$textModelsToParse = MediaWikiServices::getInstance()->getMainConfig()->get( |
| 226 |
MainConfigNames::TextModelsToParse ); |
| 227 |
'@phan-var TextContent $content'; |
| 228 |
if ( in_array( $content->getModel(), $textModelsToParse ) ) { |
| 229 |
|
| 230 |
$output = MediaWikiServices::getInstance()->getParserFactory()->getInstance() |
| 231 |
->parse( |
| 232 |
$content->getText(), |
| 233 |
$cpoParams->getPage(), |
| 234 |
$cpoParams->getParserOptions(), |
| 235 |
true, |
| 236 |
true, |
| 237 |
$cpoParams->getRevId() |
| 238 |
); |
| 239 |
} |
| 240 |
|
| 241 |
if ( $cpoParams->getGenerateHtml() ) { |
| 242 |
|
| 243 |
$html = htmlspecialchars( $content->getText(), ENT_COMPAT ); |
| 244 |
$html = "<pre>$html</pre>"; |
| 245 |
} else { |
| 246 |
$html = null; |
| 247 |
} |
| 248 |
|
| 249 |
$output->clearWrapperDivClass(); |
| 250 |
$output->setRawText( $html ); |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
class_alias( TextContentHandler::class, 'TextContentHandler' ); |