Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
63.54% |
244 / 384 |
|
43.40% |
23 / 53 |
CRAP | |
0.00% |
0 / 1 |
| ContentHandler | |
63.71% |
244 / 383 |
|
43.40% |
23 / 53 |
964.91 | |
0.00% |
0 / 1 |
| makeContent | |
83.33% |
5 / 6 |
|
0.00% |
0 / 1 |
3.04 | |||
| getLocalizedName | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
3.07 | |||
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| serializeContent | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| serializeContentToJsonArray | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
| exportTransform | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| unserializeContent | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| deserializeContentFromJsonArray | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| importTransform | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| makeEmptyContent | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| makeRedirectContent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getModelID | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| checkModelID | |
25.00% |
1 / 4 |
|
0.00% |
0 / 1 |
3.69 | |||
| getSupportedFormats | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getDefaultFormat | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getJsonFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| isSupportedFormat | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| checkFormat | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| getActionOverrides | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| createDifferenceEngine | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| getSlotDiffRenderer | |
100.00% |
12 / 12 |
|
100.00% |
1 / 1 |
3 | |||
| getSlotDiffRendererInternal | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getSlotDiffRendererWithOptions | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
2 | |||
| createTextSlotDiffRenderer | |
84.62% |
22 / 26 |
|
0.00% |
0 / 1 |
5.09 | |||
| getPageLanguage | |
90.00% |
9 / 10 |
|
0.00% |
0 / 1 |
3.01 | |||
| getPageViewLanguage | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| canBeUsedOn | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| getDiffEngineClass | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| merge3 | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getLanguageConverter | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getChangeType | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
21 | |||
| getAutosummary | |
56.86% |
29 / 51 |
|
0.00% |
0 / 1 |
18.03 | |||
| getChangeTag | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
3 | |||
| getAutoDeleteReason | |
0.00% |
0 / 53 |
|
0.00% |
0 / 1 |
342 | |||
| getUndoContent | |
0.00% |
0 / 10 |
|
0.00% |
0 / 1 |
20 | |||
| isParserCacheSupported | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| supportsSections | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| supportsCategories | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supportsRedirects | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| supportsDirectEditing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supportsPreloadContent | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| generateHTMLOnEdit | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| supportsDirectApiEditing | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| getFieldsForSearchIndex | |
100.00% |
24 / 24 |
|
100.00% |
1 / 1 |
1 | |||
| addSearchField | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| getDataForSearchIndex | |
100.00% |
26 / 26 |
|
100.00% |
1 / 1 |
3 | |||
| getParserOutputForIndexing | |
100.00% |
9 / 9 |
|
100.00% |
1 / 1 |
1 | |||
| latestRevision | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
12 | |||
| getSecondaryDataUpdates | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDeletionUpdates | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| preSaveTransform | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| preloadTransform | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| validateSave | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
2 | |||
| getParserOutput | |
81.58% |
31 / 38 |
|
0.00% |
0 / 1 |
5.16 | |||
| fillParserOutputInternal | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| fillParserOutput | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * @license GPL-2.0-or-later |
| 4 | * @file |
| 5 | */ |
| 6 | |
| 7 | namespace MediaWiki\Content; |
| 8 | |
| 9 | use DifferenceEngine; |
| 10 | use DifferenceEngineSlotDiffRenderer; |
| 11 | use InvalidArgumentException; |
| 12 | use JsonException; |
| 13 | use LogicException; |
| 14 | use MediaWiki\Actions\Action; |
| 15 | use MediaWiki\CommentStore\CommentStore; |
| 16 | use MediaWiki\Content\Renderer\ContentParseParams; |
| 17 | use MediaWiki\Content\Transform\PreloadTransformParams; |
| 18 | use MediaWiki\Content\Transform\PreSaveTransformParams; |
| 19 | use MediaWiki\Context\IContextSource; |
| 20 | use MediaWiki\Context\RequestContext; |
| 21 | use MediaWiki\Deferred\DeferrableUpdate; |
| 22 | use MediaWiki\Diff\TextDiffer\ManifoldTextDiffer; |
| 23 | use MediaWiki\Exception\MWContentSerializationException; |
| 24 | use MediaWiki\Exception\MWException; |
| 25 | use MediaWiki\Exception\MWUnknownContentModelException; |
| 26 | use MediaWiki\HookContainer\HookRunner; |
| 27 | use MediaWiki\HookContainer\ProtectedHookAccessorTrait; |
| 28 | use MediaWiki\Language\ILanguageConverter; |
| 29 | use MediaWiki\Language\Language; |
| 30 | use MediaWiki\Logger\LoggerFactory; |
| 31 | use MediaWiki\MainConfigNames; |
| 32 | use MediaWiki\MediaWikiServices; |
| 33 | use MediaWiki\Page\ParserOutputAccess; |
| 34 | use MediaWiki\Page\WikiPage; |
| 35 | use MediaWiki\Parser\ParserCache; |
| 36 | use MediaWiki\Parser\ParserOutput; |
| 37 | use MediaWiki\Revision\RevisionRecord; |
| 38 | use MediaWiki\Revision\SlotRecord; |
| 39 | use MediaWiki\Revision\SlotRenderingProvider; |
| 40 | use MediaWiki\Search\ParserOutputSearchDataExtractor; |
| 41 | use MediaWiki\Title\Title; |
| 42 | use SearchEngine; |
| 43 | use SearchIndexField; |
| 44 | use SlotDiffRenderer; |
| 45 | use StatusValue; |
| 46 | use TextSlotDiffRenderer; |
| 47 | use UnexpectedValueException; |
| 48 | use Wikimedia\Assert\Assert; |
| 49 | use Wikimedia\Rdbms\IDBAccessObject; |
| 50 | use Wikimedia\ScopedCallback; |
| 51 | |
| 52 | /** |
| 53 | * Base class for content handling. |
| 54 | * |
| 55 | * A content handler knows how do deal with a specific type of content on a wiki |
| 56 | * page. Content is stored in the database in a serialized form (using a |
| 57 | * serialization format a.k.a. MIME type) and is unserialized into its native |
| 58 | * PHP representation (the content model), which is wrapped in an instance of |
| 59 | * the appropriate subclass of Content. |
| 60 | * |
| 61 | * ContentHandler instances are stateless singletons that serve, among other |
| 62 | * things, as a factory for Content objects. Generally, there is one subclass |
| 63 | * of ContentHandler and one subclass of Content for every type of content model. |
| 64 | * |
| 65 | * Some content types have a flat model, that is, their native representation |
| 66 | * is the same as their serialized form. Examples would be JavaScript and CSS |
| 67 | * code. As of now, this also applies to wikitext (MediaWiki's default content |
| 68 | * type), but wikitext content may be represented by a DOM or AST structure in |
| 69 | * the future. |
| 70 | * |
| 71 | * @stable to extend |
| 72 | * @since 1.21 |
| 73 | * @ingroup Content |
| 74 | * @author Daniel Kinzler |
| 75 | */ |
| 76 | abstract class ContentHandler { |
| 77 | use ProtectedHookAccessorTrait; |
| 78 | |
| 79 | /** |
| 80 | * Convenience function for creating a Content object from a given textual |
| 81 | * representation. |
| 82 | * |
| 83 | * $text will be deserialized into a Content object of the model specified |
| 84 | * by $modelId (or, if that is not given, $title->getContentModel()) using |
| 85 | * the given format. |
| 86 | * |
| 87 | * @since 1.21 |
| 88 | * |
| 89 | * @param string $text The textual representation, will be |
| 90 | * unserialized to create the Content object |
| 91 | * @param Title|null $title The title of the page this text belongs to. |
| 92 | * Required if $modelId is not provided. |
| 93 | * @param string|null $modelId The model to deserialize to. If not provided, |
| 94 | * $title->getContentModel() is used. |
| 95 | * @param string|null $format The format to use for deserialization. If not |
| 96 | * given, the model's default format is used. |
| 97 | * |
| 98 | * @throws MWContentSerializationException |
| 99 | * @throws MWUnknownContentModelException |
| 100 | * @return Content A Content object representing the text. |
| 101 | */ |
| 102 | public static function makeContent( $text, ?Title $title = null, |
| 103 | $modelId = null, $format = null ) { |
| 104 | if ( !$title && !$modelId ) { |
| 105 | throw new InvalidArgumentException( "Must provide a Title object or a content model ID." ); |
| 106 | } |
| 107 | |
| 108 | return MediaWikiServices::getInstance() |
| 109 | ->getContentHandlerFactory() |
| 110 | ->getContentHandler( $modelId ?? $title->getContentModel() ) |
| 111 | ->unserializeContent( $text, $format ); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Returns the localized name for a given content model. |
| 116 | * |
| 117 | * Model names are localized using system messages. Message keys |
| 118 | * have the form content-model-$name, where $name is getContentModelName( $id ). |
| 119 | * |
| 120 | * @param string $name The content model ID, as given by a CONTENT_MODEL_XXX |
| 121 | * constant or returned by Content::getModel() or SlotRecord::getModel(). |
| 122 | * @param Language|null $lang The language to parse the message in (since 1.26) |
| 123 | * |
| 124 | * @return string The content model's localized name. |
| 125 | */ |
| 126 | public static function getLocalizedName( $name, ?Language $lang = null ) { |
| 127 | // Messages: content-model-wikitext, content-model-text, |
| 128 | // content-model-javascript, content-model-css |
| 129 | // Lowercase the name as message keys need to be in lowercase, T358341 |
| 130 | $key = "content-model-" . strtolower( $name ?? '' ); |
| 131 | |
| 132 | $msg = wfMessage( $key ); |
| 133 | if ( $lang ) { |
| 134 | $msg->inLanguage( $lang ); |
| 135 | } |
| 136 | |
| 137 | return $msg->exists() ? $msg->plain() : $name; |
| 138 | } |
| 139 | |
| 140 | // ------------------------------------------------------------------------ |
| 141 | |
| 142 | /** |
| 143 | * @var string |
| 144 | */ |
| 145 | protected $mModelID; |
| 146 | |
| 147 | /** |
| 148 | * @var string[] |
| 149 | */ |
| 150 | protected $mSupportedFormats; |
| 151 | |
| 152 | /** |
| 153 | * Constructor, initializing the ContentHandler instance with its model ID |
| 154 | * and a list of supported formats. Values for the parameters are typically |
| 155 | * provided as literals by subclass's constructors. |
| 156 | * |
| 157 | * @stable to call |
| 158 | * |
| 159 | * @param string $modelId (use CONTENT_MODEL_XXX constants). |
| 160 | * @param string[] $formats List for supported serialization formats |
| 161 | * (typically as MIME types) |
| 162 | */ |
| 163 | public function __construct( $modelId, $formats ) { |
| 164 | $this->mModelID = $modelId; |
| 165 | $this->mSupportedFormats = $formats; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * Serializes a Content object of the type supported by this ContentHandler. |
| 170 | * |
| 171 | * @stable to override |
| 172 | * |
| 173 | * @since 1.21 |
| 174 | * |
| 175 | * @param Content $content The Content object to serialize |
| 176 | * @param string|null $format The desired serialization format |
| 177 | * |
| 178 | * @return string Serialized form of the content |
| 179 | */ |
| 180 | abstract public function serializeContent( Content $content, $format = null ); |
| 181 | |
| 182 | /** |
| 183 | * Serializes a Content object of the type supported by this |
| 184 | * ContentHandler to an array which is JsonCodecable. |
| 185 | * |
| 186 | * @since 1.45 |
| 187 | * |
| 188 | * @param Content $content The Content object to serialize |
| 189 | * |
| 190 | * @return array An array of JsonCodecable content |
| 191 | */ |
| 192 | public function serializeContentToJsonArray( Content $content ): array { |
| 193 | $format = $this->getJsonFormat(); |
| 194 | $blob = $this->serializeContent( $content, $format ); |
| 195 | return [ |
| 196 | 'format' => $format, |
| 197 | 'blob' => $blob, |
| 198 | ]; |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * Applies transformations on export (returns the blob unchanged by default). |
| 203 | * Subclasses may override this to perform transformations such as conversion |
| 204 | * of legacy formats or filtering of internal meta-data. |
| 205 | * |
| 206 | * @stable to override |
| 207 | * |
| 208 | * @param string $blob The blob to be exported |
| 209 | * @param string|null $format The blob's serialization format |
| 210 | * |
| 211 | * @return string |
| 212 | */ |
| 213 | public function exportTransform( $blob, $format = null ) { |
| 214 | return $blob; |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Unserializes a Content object of the type supported by this ContentHandler. |
| 219 | * |
| 220 | * @stable to override |
| 221 | * @since 1.21 |
| 222 | * |
| 223 | * @param string $blob Serialized form of the content |
| 224 | * @param string|null $format The format used for serialization |
| 225 | * |
| 226 | * @return Content The Content object created by deserializing $blob |
| 227 | * @throws MWContentSerializationException |
| 228 | * @see ContentJsonCodec |
| 229 | */ |
| 230 | abstract public function unserializeContent( $blob, $format = null ); |
| 231 | |
| 232 | /** |
| 233 | * Deserializes a Content object of the type supported by this |
| 234 | * ContentHandler from a JsonCodecable array. |
| 235 | * |
| 236 | * @since 1.45 |
| 237 | * |
| 238 | * @param array $json Serialized form of the content |
| 239 | * |
| 240 | * @return Content The Content object created by deserializing $blob |
| 241 | * @throws JsonException |
| 242 | * @see ContentJsonCodec |
| 243 | */ |
| 244 | public function deserializeContentFromJsonArray( array $json ): Content { |
| 245 | try { |
| 246 | return $this->unserializeContent( $json['blob'], $json['format'] ); |
| 247 | } catch ( MWContentSerializationException $e ) { |
| 248 | throw new JsonException( $e->getMessage() ); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | /** |
| 253 | * Apply import transformation (by default, returns $blob unchanged). |
| 254 | * This gives subclasses an opportunity to transform data blobs on import. |
| 255 | * |
| 256 | * @stable to override |
| 257 | * @since 1.24 |
| 258 | * |
| 259 | * @param string $blob |
| 260 | * @param string|null $format |
| 261 | * |
| 262 | * @return string |
| 263 | */ |
| 264 | public function importTransform( $blob, $format = null ) { |
| 265 | return $blob; |
| 266 | } |
| 267 | |
| 268 | /** |
| 269 | * Creates an empty Content object of the type supported by this |
| 270 | * ContentHandler. |
| 271 | * |
| 272 | * @stable to override |
| 273 | * @since 1.21 |
| 274 | * @return Content |
| 275 | */ |
| 276 | abstract public function makeEmptyContent(); |
| 277 | |
| 278 | /** |
| 279 | * Creates a new Content object that acts as a redirect to the given page, |
| 280 | * or null if redirects are not supported by this content model. |
| 281 | * |
| 282 | * This default implementation always returns null. Subclasses supporting redirects |
| 283 | * must override this method. |
| 284 | * |
| 285 | * Note that subclasses that override this method to return a Content object |
| 286 | * should also override supportsRedirects() to return true. |
| 287 | * |
| 288 | * @stable to override |
| 289 | * @since 1.21 |
| 290 | * |
| 291 | * @param Title $destination The page to redirect to. |
| 292 | * @param string $text Text to include in the redirect, if possible. |
| 293 | * |
| 294 | * @return Content|null Always null. |
| 295 | */ |
| 296 | public function makeRedirectContent( Title $destination, $text = '' ) { |
| 297 | return null; |
| 298 | } |
| 299 | |
| 300 | /** |
| 301 | * Returns the model id that identifies the content model this |
| 302 | * ContentHandler can handle. Use with the CONTENT_MODEL_XXX constants. |
| 303 | * |
| 304 | * @since 1.21 |
| 305 | * @return string The model ID |
| 306 | */ |
| 307 | public function getModelID() { |
| 308 | return $this->mModelID; |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * @since 1.21 |
| 313 | * @param string $model_id The model to check |
| 314 | * @throws MWException If the provided model ID differs from this ContentHandler |
| 315 | */ |
| 316 | protected function checkModelID( $model_id ) { |
| 317 | if ( $model_id !== $this->mModelID ) { |
| 318 | throw new MWException( "Bad content model: " . |
| 319 | "expected {$this->mModelID} " . |
| 320 | "but got $model_id." ); |
| 321 | } |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * Returns a list of serialization formats supported by the |
| 326 | * serializeContent() and unserializeContent() methods of this |
| 327 | * ContentHandler. |
| 328 | * |
| 329 | * @stable to override |
| 330 | * @since 1.21 |
| 331 | * @return string[] List of serialization formats as MIME type like strings |
| 332 | */ |
| 333 | public function getSupportedFormats() { |
| 334 | return $this->mSupportedFormats; |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * The format used for serialization/deserialization by default by this |
| 339 | * ContentHandler. |
| 340 | * |
| 341 | * This default implementation will return the first element of the array |
| 342 | * of formats that was passed to the constructor. |
| 343 | * |
| 344 | * @stable to override |
| 345 | * @since 1.21 |
| 346 | * @return string The name of the default serialization format as a MIME type |
| 347 | */ |
| 348 | public function getDefaultFormat() { |
| 349 | return $this->mSupportedFormats[0]; |
| 350 | } |
| 351 | |
| 352 | /** |
| 353 | * Allow ContentHandler to chose a non-default format for JSON |
| 354 | * serialization. |
| 355 | * |
| 356 | * In most cases will return the same as `::getDefaultFormat()`. |
| 357 | */ |
| 358 | public function getJsonFormat(): string { |
| 359 | return $this->getDefaultFormat(); |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * Returns true if $format is a serialization format supported by this |
| 364 | * ContentHandler, and false otherwise. |
| 365 | * |
| 366 | * Note that if $format is null, this method always returns true, because |
| 367 | * null means "use the default format". |
| 368 | * |
| 369 | * @stable to override |
| 370 | * @since 1.21 |
| 371 | * |
| 372 | * @param string $format The serialization format to check |
| 373 | * |
| 374 | * @return bool |
| 375 | */ |
| 376 | public function isSupportedFormat( $format ) { |
| 377 | if ( !$format ) { |
| 378 | return true; // this means "use the default" |
| 379 | } |
| 380 | |
| 381 | return in_array( $format, $this->mSupportedFormats ); |
| 382 | } |
| 383 | |
| 384 | /** |
| 385 | * Convenient for checking whether a format provided as a parameter is actually supported. |
| 386 | * |
| 387 | * @param string $format The serialization format to check |
| 388 | * |
| 389 | * @throws MWException If the format is not supported by this content handler. |
| 390 | */ |
| 391 | protected function checkFormat( $format ) { |
| 392 | if ( !$this->isSupportedFormat( $format ) ) { |
| 393 | throw new MWException( |
| 394 | "Format $format is not supported for content model " |
| 395 | . $this->getModelID() |
| 396 | ); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /** |
| 401 | * Returns overrides for action handlers. |
| 402 | * Classes listed here will be used instead of the default one when |
| 403 | * (and only when) $wgActions[$action] === true. This allows subclasses |
| 404 | * to override the default action handlers. |
| 405 | * |
| 406 | * @stable to override |
| 407 | * @since 1.21 |
| 408 | * |
| 409 | * @return array<string,class-string|callable|false|Action|array> An array mapping action names |
| 410 | * (typically "view", "edit", "history" etc.) to a specification according to |
| 411 | * {@see ActionFactory::getActionSpec}. Can be the full qualified class name of an Action |
| 412 | * class, a callable taking ( Article $article, IContextSource $context ) as parameters and |
| 413 | * returning an Action object, false to disable an action, an actual Action object, |
| 414 | * or an ObjectFactory specification array (can have 'class', 'services', etc.). |
| 415 | * An empty array in this default implementation. |
| 416 | * |
| 417 | * @see Action::factory |
| 418 | */ |
| 419 | public function getActionOverrides() { |
| 420 | return []; |
| 421 | } |
| 422 | |
| 423 | /** |
| 424 | * Factory for creating an appropriate DifferenceEngine for this content model. |
| 425 | * Since 1.32, this is only used for page-level diffs; to diff two content objects, |
| 426 | * use getSlotDiffRenderer. |
| 427 | * |
| 428 | * The DifferenceEngine subclass to use is selected in getDiffEngineClass(). The |
| 429 | * GetDifferenceEngine hook will receive the DifferenceEngine object and can replace or |
| 430 | * wrap it. |
| 431 | * (Note that in older versions of MediaWiki the hook documentation instructed extensions |
| 432 | * to return false from the hook; you should not rely on always being able to decorate |
| 433 | * the DifferenceEngine instance from the hook. If the owner of the content type wants to |
| 434 | * decorate the instance, overriding this method is a safer approach.) |
| 435 | * |
| 436 | * @todo This is page-level functionality so it should not belong to ContentHandler. |
| 437 | * Move it to a better place once one exists (e.g. PageTypeHandler). |
| 438 | * |
| 439 | * @since 1.21 |
| 440 | * |
| 441 | * @param IContextSource $context Context to use, anything else will be ignored. |
| 442 | * @param int $old Revision ID we want to show and diff with. |
| 443 | * @param int|string $new Either a revision ID or one of the strings 'cur', 'prev' or 'next'. |
| 444 | * @param int $rcid FIXME: Deprecated, no longer used. Defaults to 0. |
| 445 | * @param bool $refreshCache If set, refreshes the diff cache. Defaults to false. |
| 446 | * @param bool $unhide If set, allow viewing deleted revs. Defaults to false. |
| 447 | * |
| 448 | * @return DifferenceEngine |
| 449 | */ |
| 450 | public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0, |
| 451 | $rcid = 0, // FIXME: Deprecated, no longer used |
| 452 | $refreshCache = false, $unhide = false |
| 453 | ) { |
| 454 | $diffEngineClass = $this->getDiffEngineClass(); |
| 455 | $differenceEngine = new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide ); |
| 456 | $this->getHookRunner()->onGetDifferenceEngine( |
| 457 | $context, $old, $new, $refreshCache, $unhide, $differenceEngine ); |
| 458 | return $differenceEngine; |
| 459 | } |
| 460 | |
| 461 | /** |
| 462 | * Get an appropriate SlotDiffRenderer for this content model. |
| 463 | * |
| 464 | * @stable to override |
| 465 | * @since 1.32 |
| 466 | * |
| 467 | * @param IContextSource $context |
| 468 | * @param array $options An associative array of options passed to the SlotDiffRenderer: |
| 469 | * - diff-type: (string) The text diff format |
| 470 | * - contentLanguage: (string) The language code of the content language, |
| 471 | * to be passed to the TextDiffer constructor. This is ignored if a |
| 472 | * TextDiffer object is provided. |
| 473 | * - textDiffer: (TextDiffer) A TextDiffer object to use for text |
| 474 | * comparison. |
| 475 | * @return SlotDiffRenderer |
| 476 | */ |
| 477 | final public function getSlotDiffRenderer( IContextSource $context, array $options = [] ) { |
| 478 | $slotDiffRenderer = $this->getSlotDiffRendererWithOptions( $context, $options ); |
| 479 | if ( get_class( $slotDiffRenderer ) === TextSlotDiffRenderer::class ) { |
| 480 | // To keep B/C, when SlotDiffRenderer is not overridden for a given content type |
| 481 | // but DifferenceEngine is, use that instead. |
| 482 | $differenceEngine = $this->createDifferenceEngine( $context ); |
| 483 | if ( get_class( $differenceEngine ) !== DifferenceEngine::class ) { |
| 484 | // TODO turn this into a deprecation warning in a later release |
| 485 | LoggerFactory::getInstance( 'diff' )->info( |
| 486 | 'Falling back to DifferenceEngineSlotDiffRenderer', [ |
| 487 | 'modelID' => $this->getModelID(), |
| 488 | 'DifferenceEngine' => get_class( $differenceEngine ), |
| 489 | ] ); |
| 490 | $slotDiffRenderer = new DifferenceEngineSlotDiffRenderer( $differenceEngine ); |
| 491 | } |
| 492 | } |
| 493 | $this->getHookRunner()->onGetSlotDiffRenderer( $this, $slotDiffRenderer, $context ); |
| 494 | return $slotDiffRenderer; |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * Return the SlotDiffRenderer appropriate for this content handler. |
| 499 | * @deprecated since 1.35; use getSlotDiffRendererWithOptions instead |
| 500 | * Emitting deprecation warnings since 1.41. |
| 501 | * @param IContextSource $context |
| 502 | * @return SlotDiffRenderer|null |
| 503 | */ |
| 504 | protected function getSlotDiffRendererInternal( IContextSource $context ) { |
| 505 | return null; |
| 506 | } |
| 507 | |
| 508 | /** |
| 509 | * Return the SlotDiffRenderer appropriate for this content handler. |
| 510 | * @stable to override |
| 511 | * |
| 512 | * @param IContextSource $context |
| 513 | * @param array $options See getSlotDiffRenderer() |
| 514 | * |
| 515 | * @return SlotDiffRenderer |
| 516 | */ |
| 517 | protected function getSlotDiffRendererWithOptions( IContextSource $context, $options = [] ) { |
| 518 | $internalRenderer = $this->getSlotDiffRendererInternal( $context ); |
| 519 | // `getSlotDiffRendererInternal` has been overridden by a class using the deprecated method. |
| 520 | // Options will not work so exit early! |
| 521 | if ( $internalRenderer !== null ) { |
| 522 | wfDeprecated( 'ContentHandler::getSlotDiffRendererInternal', '1.35' ); |
| 523 | return $internalRenderer; |
| 524 | } |
| 525 | return $this->createTextSlotDiffRenderer( $options ); |
| 526 | } |
| 527 | |
| 528 | /** |
| 529 | * Create a TextSlotDiffRenderer and inject dependencies |
| 530 | * |
| 531 | * @since 1.41 |
| 532 | * @param array $options See getSlotDiffRenderer() |
| 533 | * @return TextSlotDiffRenderer |
| 534 | */ |
| 535 | final protected function createTextSlotDiffRenderer( array $options = [] ): TextSlotDiffRenderer { |
| 536 | $slotDiffRenderer = new TextSlotDiffRenderer(); |
| 537 | |
| 538 | $services = MediaWikiServices::getInstance(); |
| 539 | $slotDiffRenderer->setStatsFactory( $services->getStatsFactory() ); |
| 540 | $slotDiffRenderer->setHookContainer( $services->getHookContainer() ); |
| 541 | $slotDiffRenderer->setContentModel( $this->getModelID() ); |
| 542 | |
| 543 | if ( isset( $options['textDiffer'] ) ) { |
| 544 | $textDiffer = $options['textDiffer']; |
| 545 | } else { |
| 546 | if ( isset( $options['contentLanguage'] ) ) { |
| 547 | $language = $services->getLanguageFactory()->getLanguage( $options['contentLanguage'] ); |
| 548 | } else { |
| 549 | $language = $services->getContentLanguage(); |
| 550 | } |
| 551 | $config = $services->getMainConfig(); |
| 552 | $textDiffer = new ManifoldTextDiffer( |
| 553 | RequestContext::getMain(), |
| 554 | $language, |
| 555 | $config->get( MainConfigNames::DiffEngine ), |
| 556 | $config->get( MainConfigNames::ExternalDiffEngine ), |
| 557 | $config->get( MainConfigNames::Wikidiff2Options ) |
| 558 | ); |
| 559 | } |
| 560 | $format = $options['diff-type'] ?? 'table'; |
| 561 | if ( !$textDiffer->hasFormat( $format ) ) { |
| 562 | // Maybe it would be better to throw an exception here, but at |
| 563 | // present, the value comes straight from user input without |
| 564 | // validation, so we have to fall back. |
| 565 | $format = 'table'; |
| 566 | } |
| 567 | $slotDiffRenderer->setFormat( $format ); |
| 568 | $slotDiffRenderer->setTextDiffer( $textDiffer ); |
| 569 | if ( $options['inline-toggle'] ?? false ) { |
| 570 | $slotDiffRenderer->setInlineToggleEnabled(); |
| 571 | } |
| 572 | |
| 573 | return $slotDiffRenderer; |
| 574 | } |
| 575 | |
| 576 | /** |
| 577 | * Get the language in which the content of the given page is written. |
| 578 | * |
| 579 | * This default implementation just returns the content language (except for pages |
| 580 | * in the MediaWiki namespace) |
| 581 | * |
| 582 | * Note that the page's language is not cacheable, since it may in some |
| 583 | * cases depend on user settings. |
| 584 | * |
| 585 | * Also note that the page language may or may not depend on the actual content of the page, |
| 586 | * that is, this method may load the content in order to determine the language. |
| 587 | * |
| 588 | * @stable to override |
| 589 | * @since 1.21 |
| 590 | * |
| 591 | * @param Title $title The page to determine the language for. |
| 592 | * @param Content|null $content The page's content, if you have it handy, to avoid reloading it. |
| 593 | * |
| 594 | * @return Language |
| 595 | */ |
| 596 | public function getPageLanguage( Title $title, ?Content $content = null ) { |
| 597 | $services = MediaWikiServices::getInstance(); |
| 598 | $pageLang = $services->getContentLanguage(); |
| 599 | |
| 600 | if ( $title->inNamespace( NS_MEDIAWIKI ) ) { |
| 601 | // Parse mediawiki messages with correct target language |
| 602 | [ /* $unused */, $lang ] = $services->getMessageCache()->figureMessage( $title->getText() ); |
| 603 | $pageLang = $services->getLanguageFactory()->getLanguage( $lang ); |
| 604 | } |
| 605 | |
| 606 | // Unused, T299369 |
| 607 | $userLang = null; |
| 608 | $this->getHookRunner()->onPageContentLanguage( $title, $pageLang, $userLang ); |
| 609 | |
| 610 | if ( !$pageLang instanceof Language ) { |
| 611 | throw new UnexpectedValueException( 'onPageContentLanguage() hook provided an invalid $pageLang object.' ); |
| 612 | } |
| 613 | |
| 614 | return $pageLang; |
| 615 | } |
| 616 | |
| 617 | /** |
| 618 | * Get the language in which the content of this page is written when |
| 619 | * viewed by user. Defaults to $this->getPageLanguage(), but if the user |
| 620 | * specified a preferred variant, the variant will be used. |
| 621 | * |
| 622 | * This default implementation just returns $this->getPageLanguage( $title, $content ) unless |
| 623 | * the user specified a preferred variant. |
| 624 | * |
| 625 | * Note that the pages view language is not cacheable, since it depends on user settings. |
| 626 | * |
| 627 | * Also note that the page language may or may not depend on the actual content of the page, |
| 628 | * that is, this method may load the content in order to determine the language. |
| 629 | * |
| 630 | * @stable to override |
| 631 | * @deprecated since 1.42 Use ParserOutput::getLanguage instead. See also OutputPage::getContLangForJS. |
| 632 | * @since 1.21 |
| 633 | * @param Title $title The page to determine the language for. |
| 634 | * @param Content|null $content The page's content, if you have it handy, to avoid reloading it. |
| 635 | * @return Language The page's language for viewing |
| 636 | */ |
| 637 | public function getPageViewLanguage( Title $title, ?Content $content = null ) { |
| 638 | $pageLang = $this->getPageLanguage( $title, $content ); |
| 639 | |
| 640 | if ( $title->getNamespace() !== NS_MEDIAWIKI ) { |
| 641 | // If the user chooses a variant, the content is actually |
| 642 | // in a language whose code is the variant code. |
| 643 | $variant = $this->getLanguageConverter( $pageLang )->getPreferredVariant(); |
| 644 | if ( $pageLang->getCode() !== $variant ) { |
| 645 | $pageLang = MediaWikiServices::getInstance()->getLanguageFactory() |
| 646 | ->getLanguage( $variant ); |
| 647 | } |
| 648 | } |
| 649 | |
| 650 | return $pageLang; |
| 651 | } |
| 652 | |
| 653 | /** |
| 654 | * Determines whether the content type handled by this ContentHandler |
| 655 | * can be used for the main slot of the given page. |
| 656 | * |
| 657 | * This default implementation always returns true. |
| 658 | * Subclasses may override this to restrict the use of this content model to specific locations, |
| 659 | * typically based on the namespace or some other aspect of the title, such as a special suffix |
| 660 | * (e.g. ".svg" for SVG content). |
| 661 | * |
| 662 | * @note this calls the ContentHandlerCanBeUsedOn hook which may be used to override which |
| 663 | * content model can be used where. |
| 664 | * |
| 665 | * @stable to override |
| 666 | * |
| 667 | * @see SlotRoleHandler::isAllowedModel |
| 668 | * |
| 669 | * @param Title $title The page's title. |
| 670 | * |
| 671 | * @return bool True if content of this kind can be used on the given page, false otherwise. |
| 672 | */ |
| 673 | public function canBeUsedOn( Title $title ) { |
| 674 | $ok = true; |
| 675 | |
| 676 | $this->getHookRunner()->onContentModelCanBeUsedOn( $this->getModelID(), $title, $ok ); |
| 677 | |
| 678 | return $ok; |
| 679 | } |
| 680 | |
| 681 | /** |
| 682 | * Returns the name of the diff engine to use. |
| 683 | * |
| 684 | * @stable to override |
| 685 | * @since 1.21 |
| 686 | * |
| 687 | * @return class-string<DifferenceEngine> |
| 688 | */ |
| 689 | protected function getDiffEngineClass() { |
| 690 | return DifferenceEngine::class; |
| 691 | } |
| 692 | |
| 693 | /** |
| 694 | * Attempts to merge differences between three versions. Returns a new |
| 695 | * Content object for a clean merge and false for failure or a conflict. |
| 696 | * |
| 697 | * This default implementation always returns false. |
| 698 | * |
| 699 | * @stable to override |
| 700 | * @since 1.21 |
| 701 | * |
| 702 | * @param Content $oldContent The page's previous content. |
| 703 | * @param Content $myContent One of the page's conflicting contents. |
| 704 | * @param Content $yourContent One of the page's conflicting contents. |
| 705 | * |
| 706 | * @return Content|false Always false. |
| 707 | */ |
| 708 | public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) { |
| 709 | return false; |
| 710 | } |
| 711 | |
| 712 | /** |
| 713 | * Shorthand for getting a Language Converter for specific language |
| 714 | * @param Language $language Language of converter |
| 715 | * @return ILanguageConverter |
| 716 | */ |
| 717 | private function getLanguageConverter( $language ): ILanguageConverter { |
| 718 | return MediaWikiServices::getInstance()->getLanguageConverterFactory() |
| 719 | ->getLanguageConverter( $language ); |
| 720 | } |
| 721 | |
| 722 | /** |
| 723 | * Return type of change if one exists for the given edit. |
| 724 | * |
| 725 | * @stable to override |
| 726 | * @since 1.31 |
| 727 | * |
| 728 | * @param Content|null $oldContent The previous text of the page. |
| 729 | * @param Content|null $newContent The submitted text of the page. |
| 730 | * @param int $flags Bit mask: a bit mask of flags submitted for the edit. |
| 731 | * |
| 732 | * @return string|null String key representing type of change, or null. |
| 733 | */ |
| 734 | private function getChangeType( |
| 735 | ?Content $oldContent = null, |
| 736 | ?Content $newContent = null, |
| 737 | $flags = 0 |
| 738 | ) { |
| 739 | $oldTarget = $oldContent !== null ? $oldContent->getRedirectTarget() : null; |
| 740 | $newTarget = $newContent !== null ? $newContent->getRedirectTarget() : null; |
| 741 | |
| 742 | // We check for the type of change in the given edit, and return string key accordingly |
| 743 | |
| 744 | // Blanking of a page |
| 745 | if ( $oldContent && $oldContent->getSize() > 0 && |
| 746 | $newContent && $newContent->getSize() === 0 |
| 747 | ) { |
| 748 | return 'blank'; |
| 749 | } |
| 750 | |
| 751 | // Redirects |
| 752 | if ( $newTarget ) { |
| 753 | if ( !$oldTarget ) { |
| 754 | // New redirect page (by creating new page or by changing content page) |
| 755 | return 'new-redirect'; |
| 756 | } elseif ( !$newTarget->equals( $oldTarget ) || |
| 757 | $oldTarget->getFragment() !== $newTarget->getFragment() |
| 758 | ) { |
| 759 | // Redirect target changed |
| 760 | return 'changed-redirect-target'; |
| 761 | } |
| 762 | } elseif ( $oldTarget ) { |
| 763 | // Changing an existing redirect into a non-redirect |
| 764 | return 'removed-redirect'; |
| 765 | } |
| 766 | |
| 767 | // New page created |
| 768 | if ( $flags & EDIT_NEW && $newContent ) { |
| 769 | if ( $newContent->getSize() === 0 ) { |
| 770 | // New blank page |
| 771 | return 'newblank'; |
| 772 | } else { |
| 773 | return 'newpage'; |
| 774 | } |
| 775 | } |
| 776 | |
| 777 | // Removing more than 90% of the page |
| 778 | if ( $oldContent && $newContent && $oldContent->getSize() > 10 * $newContent->getSize() ) { |
| 779 | return 'replace'; |
| 780 | } |
| 781 | |
| 782 | // Content model changed |
| 783 | if ( $oldContent && $newContent && $oldContent->getModel() !== $newContent->getModel() ) { |
| 784 | return 'contentmodelchange'; |
| 785 | } |
| 786 | |
| 787 | return null; |
| 788 | } |
| 789 | |
| 790 | /** |
| 791 | * Return an applicable auto-summary if one exists for the given edit. |
| 792 | * |
| 793 | * @stable to override |
| 794 | * @since 1.21 |
| 795 | * |
| 796 | * @param Content|null $oldContent The previous text of the page. |
| 797 | * @param Content|null $newContent The submitted text of the page. |
| 798 | * @param int $flags Bit mask: a bit mask of flags submitted for the edit. |
| 799 | * |
| 800 | * @return string An appropriate auto-summary, or an empty string. |
| 801 | */ |
| 802 | public function getAutosummary( |
| 803 | ?Content $oldContent = null, |
| 804 | ?Content $newContent = null, |
| 805 | $flags = 0 |
| 806 | ) { |
| 807 | $changeType = $this->getChangeType( $oldContent, $newContent, $flags ); |
| 808 | |
| 809 | // There's no applicable auto-summary for our case, so our auto-summary is empty. |
| 810 | if ( !$changeType ) { |
| 811 | return ''; |
| 812 | } |
| 813 | |
| 814 | // Set the maximum auto-summary length to the general maximum summary length |
| 815 | // T221617 |
| 816 | $summaryLimit = CommentStore::COMMENT_CHARACTER_LIMIT; |
| 817 | |
| 818 | // Decide what kind of auto-summary is needed. |
| 819 | switch ( $changeType ) { |
| 820 | case 'new-redirect': |
| 821 | $newTarget = $newContent->getRedirectTarget(); |
| 822 | $truncatedtext = $newContent->getTextForSummary( |
| 823 | $summaryLimit |
| 824 | - strlen( wfMessage( 'autoredircomment' )->inContentLanguage()->text() ) |
| 825 | - strlen( $newTarget->getFullText() ) |
| 826 | ); |
| 827 | |
| 828 | return wfMessage( 'autoredircomment', $newTarget->getFullText() ) |
| 829 | ->plaintextParams( $truncatedtext )->inContentLanguage()->text(); |
| 830 | case 'changed-redirect-target': |
| 831 | $oldTarget = $oldContent->getRedirectTarget(); |
| 832 | $newTarget = $newContent->getRedirectTarget(); |
| 833 | |
| 834 | $truncatedtext = $newContent->getTextForSummary( |
| 835 | $summaryLimit |
| 836 | - strlen( wfMessage( 'autosumm-changed-redirect-target' ) |
| 837 | ->inContentLanguage()->text() ) |
| 838 | - strlen( $oldTarget->getFullText() ) |
| 839 | - strlen( $newTarget->getFullText() ) |
| 840 | ); |
| 841 | |
| 842 | return wfMessage( 'autosumm-changed-redirect-target', |
| 843 | $oldTarget->getFullText(), |
| 844 | $newTarget->getFullText() ) |
| 845 | ->rawParams( $truncatedtext )->inContentLanguage()->text(); |
| 846 | case 'removed-redirect': |
| 847 | $oldTarget = $oldContent->getRedirectTarget(); |
| 848 | $truncatedtext = $newContent->getTextForSummary( |
| 849 | $summaryLimit |
| 850 | - strlen( wfMessage( 'autosumm-removed-redirect' ) |
| 851 | ->inContentLanguage()->text() ) |
| 852 | - strlen( $oldTarget->getFullText() ) ); |
| 853 | |
| 854 | return wfMessage( 'autosumm-removed-redirect', $oldTarget->getFullText() ) |
| 855 | ->rawParams( $truncatedtext )->inContentLanguage()->text(); |
| 856 | case 'newpage': |
| 857 | // If they're making a new article, give its text, truncated, in the summary. |
| 858 | $truncatedtext = $newContent->getTextForSummary( |
| 859 | $summaryLimit - strlen( wfMessage( 'autosumm-new' )->inContentLanguage()->text() ) ); |
| 860 | |
| 861 | return wfMessage( 'autosumm-new' )->rawParams( $truncatedtext ) |
| 862 | ->inContentLanguage()->text(); |
| 863 | case 'blank': |
| 864 | return wfMessage( 'autosumm-blank' )->inContentLanguage()->text(); |
| 865 | case 'replace': |
| 866 | $truncatedtext = $newContent->getTextForSummary( |
| 867 | $summaryLimit - strlen( wfMessage( 'autosumm-replace' )->inContentLanguage()->text() ) ); |
| 868 | |
| 869 | return wfMessage( 'autosumm-replace' )->rawParams( $truncatedtext ) |
| 870 | ->inContentLanguage()->text(); |
| 871 | case 'newblank': |
| 872 | return wfMessage( 'autosumm-newblank' )->inContentLanguage()->text(); |
| 873 | default: |
| 874 | return ''; |
| 875 | } |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * Return an applicable tag if one exists for the given edit or return null. |
| 880 | * |
| 881 | * @stable to override |
| 882 | * @since 1.31 |
| 883 | * |
| 884 | * @param Content|null $oldContent The previous text of the page. |
| 885 | * @param Content|null $newContent The submitted text of the page. |
| 886 | * @param int $flags Bit mask: a bit mask of flags submitted for the edit. |
| 887 | * |
| 888 | * @return string|null An appropriate tag, or null. |
| 889 | */ |
| 890 | public function getChangeTag( |
| 891 | ?Content $oldContent = null, |
| 892 | ?Content $newContent = null, |
| 893 | $flags = 0 |
| 894 | ) { |
| 895 | $changeType = $this->getChangeType( $oldContent, $newContent, $flags ); |
| 896 | |
| 897 | // There's no applicable tag for this change. |
| 898 | if ( !$changeType ) { |
| 899 | return null; |
| 900 | } |
| 901 | |
| 902 | // Core tags use the same keys as ones returned from $this->getChangeType() |
| 903 | // but prefixed with pseudo namespace 'mw-', so we add the prefix before checking |
| 904 | // if this type of change should be tagged |
| 905 | $tag = 'mw-' . $changeType; |
| 906 | |
| 907 | // Not all change types are tagged, so we check against the list of defined tags. |
| 908 | if ( in_array( $tag, MediaWikiServices::getInstance()->getChangeTagsStore()->getSoftwareTags() ) ) { |
| 909 | return $tag; |
| 910 | } |
| 911 | |
| 912 | return null; |
| 913 | } |
| 914 | |
| 915 | /** |
| 916 | * Auto-generates a deletion reason |
| 917 | * |
| 918 | * @stable to override |
| 919 | * @since 1.21 |
| 920 | * |
| 921 | * @param Title $title The page's title |
| 922 | * @param bool &$hasHistory Whether the page has a history |
| 923 | * |
| 924 | * @return string|false String containing deletion reason or empty string, or |
| 925 | * boolean false if no revision occurred |
| 926 | */ |
| 927 | public function getAutoDeleteReason( Title $title, &$hasHistory = false ) { |
| 928 | if ( func_num_args() === 2 ) { |
| 929 | wfDeprecated( __METHOD__ . ': $hasHistory parameter', '1.38' ); |
| 930 | } |
| 931 | $dbr = MediaWikiServices::getInstance()->getConnectionProvider()->getReplicaDatabase(); |
| 932 | $revStore = MediaWikiServices::getInstance()->getRevisionStore(); |
| 933 | |
| 934 | // Get the last revision |
| 935 | $revRecord = $revStore->getRevisionByTitle( $title ); |
| 936 | |
| 937 | if ( $revRecord === null ) { |
| 938 | return false; |
| 939 | } |
| 940 | |
| 941 | // Get the article's contents |
| 942 | $content = $revRecord->getContent( SlotRecord::MAIN ); |
| 943 | $blank = false; |
| 944 | |
| 945 | // If the page is blank, use the text from the previous revision, |
| 946 | if ( !$content || $content->isEmpty() ) { |
| 947 | $prev = $revStore->getPreviousRevision( $revRecord ); |
| 948 | |
| 949 | if ( $prev ) { |
| 950 | $prevContent = $prev->getContent( SlotRecord::MAIN ); |
| 951 | if ( $prevContent && !$prevContent->isEmpty() ) { |
| 952 | $revRecord = $prev; |
| 953 | $content = $prevContent; |
| 954 | $blank = true; |
| 955 | } |
| 956 | // Else since the previous revision is also blank or revdelled |
| 957 | // (the blank case only happen due to a move/import/protect dummy revision) |
| 958 | // skip the "before blanking" logic and fall back to just `content was ""` |
| 959 | } |
| 960 | } |
| 961 | |
| 962 | $this->checkModelID( $revRecord->getSlot( SlotRecord::MAIN )->getModel() ); |
| 963 | |
| 964 | // Find out if there was only one contributor |
| 965 | // Only scan the last 20 revisions |
| 966 | $queryBuilder = $revStore->newSelectQueryBuilder( $dbr ) |
| 967 | ->where( [ |
| 968 | 'rev_page' => $title->getArticleID(), |
| 969 | $dbr->bitAnd( 'rev_deleted', RevisionRecord::DELETED_USER ) . ' = 0' |
| 970 | ] ) |
| 971 | ->limit( 20 ); |
| 972 | $res = $queryBuilder->caller( __METHOD__ )->fetchResultSet(); |
| 973 | |
| 974 | if ( !$res->numRows() ) { |
| 975 | // This page has no revisions, which is very weird |
| 976 | return false; |
| 977 | } |
| 978 | |
| 979 | $hasHistory = ( $res->numRows() > 1 ); |
| 980 | $row = $res->fetchObject(); |
| 981 | |
| 982 | if ( $row ) { // $row is false if the only contributor is hidden |
| 983 | $onlyAuthor = $row->rev_user_text; |
| 984 | // Try to find a second contributor |
| 985 | foreach ( $res as $row ) { |
| 986 | if ( $row->rev_user_text != $onlyAuthor ) { // T24999 |
| 987 | $onlyAuthor = false; |
| 988 | break; |
| 989 | } |
| 990 | } |
| 991 | } else { |
| 992 | $onlyAuthor = false; |
| 993 | } |
| 994 | |
| 995 | // Generate the summary with a '$1' placeholder |
| 996 | if ( $blank ) { |
| 997 | $reason = wfMessage( 'exbeforeblank', '$1' )->inContentLanguage()->text(); |
| 998 | } else { |
| 999 | if ( $onlyAuthor ) { |
| 1000 | $reason = wfMessage( |
| 1001 | 'excontentauthor', |
| 1002 | '$1', |
| 1003 | $onlyAuthor |
| 1004 | )->inContentLanguage()->text(); |
| 1005 | } else { |
| 1006 | $reason = wfMessage( 'excontent', '$1' )->inContentLanguage()->text(); |
| 1007 | } |
| 1008 | } |
| 1009 | |
| 1010 | if ( $reason == '-' ) { |
| 1011 | // Allow these UI messages to be blanked out cleanly |
| 1012 | return ''; |
| 1013 | } |
| 1014 | |
| 1015 | // Max content length = max comment length - length of the comment (excl. $1) |
| 1016 | $maxLength = CommentStore::COMMENT_CHARACTER_LIMIT - ( strlen( $reason ) - 2 ); |
| 1017 | $text = $content ? $content->getTextForSummary( $maxLength ) : ''; |
| 1018 | if ( $blank && !$text ) { |
| 1019 | // Don't display "content before blanking was ''" as misleading |
| 1020 | // This can happen if the content before blanking was two unclosed square brackets, for example |
| 1021 | // Do display `content was ""` if the page was always blank, though |
| 1022 | return false; |
| 1023 | } |
| 1024 | |
| 1025 | // Now replace the '$1' placeholder |
| 1026 | $reason = str_replace( '$1', $text, $reason ); |
| 1027 | |
| 1028 | return $reason; |
| 1029 | } |
| 1030 | |
| 1031 | /** |
| 1032 | * Get the Content object that needs to be saved in order to undo all changes |
| 1033 | * between $undo and $undoafter. |
| 1034 | * |
| 1035 | * @stable to override |
| 1036 | * @since 1.21 |
| 1037 | * @since 1.32 accepts Content objects for all parameters instead of Revision objects. |
| 1038 | * Passing Revision objects is deprecated. |
| 1039 | * @since 1.37 only accepts Content objects |
| 1040 | * |
| 1041 | * @param Content $currentContent The current text |
| 1042 | * @param Content $undoContent The content of the revision to undo |
| 1043 | * @param Content $undoAfterContent Must be from an earlier revision than $undo |
| 1044 | * @param bool $undoIsLatest Set true if $undo is from the current revision (since 1.32) |
| 1045 | * |
| 1046 | * @return Content|false Content on success, false on failure |
| 1047 | */ |
| 1048 | public function getUndoContent( |
| 1049 | Content $currentContent, |
| 1050 | Content $undoContent, |
| 1051 | Content $undoAfterContent, |
| 1052 | $undoIsLatest = false |
| 1053 | ) { |
| 1054 | try { |
| 1055 | $this->checkModelID( $currentContent->getModel() ); |
| 1056 | $this->checkModelID( $undoContent->getModel() ); |
| 1057 | if ( !$undoIsLatest ) { |
| 1058 | // If we are undoing the most recent revision, |
| 1059 | // its ok to revert content model changes. However |
| 1060 | // if we are undoing a revision in the middle, then |
| 1061 | // doing that will be confusing. |
| 1062 | $this->checkModelID( $undoAfterContent->getModel() ); |
| 1063 | } |
| 1064 | } catch ( MWException ) { |
| 1065 | // If the revisions have different content models |
| 1066 | // just return false |
| 1067 | return false; |
| 1068 | } |
| 1069 | |
| 1070 | if ( $currentContent->equals( $undoContent ) ) { |
| 1071 | // No use doing a merge if it's just a straight revert. |
| 1072 | return $undoAfterContent; |
| 1073 | } |
| 1074 | |
| 1075 | $undone_content = $this->merge3( $undoContent, $undoAfterContent, $currentContent ); |
| 1076 | |
| 1077 | return $undone_content; |
| 1078 | } |
| 1079 | |
| 1080 | /** |
| 1081 | * Returns true for content models that support caching using the |
| 1082 | * ParserCache mechanism. See WikiPage::shouldCheckParserCache(). |
| 1083 | * |
| 1084 | * @stable to override |
| 1085 | * @since 1.21 |
| 1086 | * |
| 1087 | * @return bool Always false. |
| 1088 | */ |
| 1089 | public function isParserCacheSupported() { |
| 1090 | return false; |
| 1091 | } |
| 1092 | |
| 1093 | /** |
| 1094 | * Returns true if this content model supports sections. |
| 1095 | * This default implementation returns false. |
| 1096 | * |
| 1097 | * Content models that return true here should also implement |
| 1098 | * Content::getSection, Content::replaceSection, etc. to handle sections. |
| 1099 | * |
| 1100 | * @stable to override |
| 1101 | * |
| 1102 | * @return bool Always false. |
| 1103 | */ |
| 1104 | public function supportsSections() { |
| 1105 | return false; |
| 1106 | } |
| 1107 | |
| 1108 | /** |
| 1109 | * Returns true if this content model supports categories. |
| 1110 | * The default implementation returns true. |
| 1111 | * |
| 1112 | * @stable to override |
| 1113 | * |
| 1114 | * @return bool Always true. |
| 1115 | */ |
| 1116 | public function supportsCategories() { |
| 1117 | return true; |
| 1118 | } |
| 1119 | |
| 1120 | /** |
| 1121 | * Returns true if this content model supports redirects. |
| 1122 | * This default implementation returns false. |
| 1123 | * |
| 1124 | * Content models that return true here should also implement |
| 1125 | * ContentHandler::makeRedirectContent to return a Content object. |
| 1126 | * |
| 1127 | * @stable to override |
| 1128 | * |
| 1129 | * @return bool Always false. |
| 1130 | */ |
| 1131 | public function supportsRedirects() { |
| 1132 | return false; |
| 1133 | } |
| 1134 | |
| 1135 | /** |
| 1136 | * Return true if this content model supports direct editing, such as via EditPage. |
| 1137 | * This should return true for TextContent and its derivatives, and return false |
| 1138 | * for structured data content. |
| 1139 | * |
| 1140 | * @stable to override |
| 1141 | * |
| 1142 | * @return bool Default is false. |
| 1143 | */ |
| 1144 | public function supportsDirectEditing() { |
| 1145 | return false; |
| 1146 | } |
| 1147 | |
| 1148 | /** |
| 1149 | * If a non-existing page can be created with the contents from another (arbitrary) page being |
| 1150 | * preloaded in the editor, see {@see EditPage::getContentObject}. Only makes sense together |
| 1151 | * with {@see supportsDirectEditing}. |
| 1152 | * |
| 1153 | * @stable to override |
| 1154 | * @since 1.39 |
| 1155 | * |
| 1156 | * @return bool |
| 1157 | */ |
| 1158 | public function supportsPreloadContent(): bool { |
| 1159 | return false; |
| 1160 | } |
| 1161 | |
| 1162 | /** |
| 1163 | * Whether an edit on the content should trigger an HTML render and ParserCache entry. |
| 1164 | * |
| 1165 | * @stable to override |
| 1166 | * @since 1.37 |
| 1167 | * |
| 1168 | * @return bool true if edit should trigger an HTML render false otherwise |
| 1169 | */ |
| 1170 | public function generateHTMLOnEdit(): bool { |
| 1171 | return true; |
| 1172 | } |
| 1173 | |
| 1174 | /** |
| 1175 | * Whether or not this content model supports direct editing via ApiEditPage |
| 1176 | * |
| 1177 | * @stable to override |
| 1178 | * |
| 1179 | * @return bool Default is false, and true for TextContent and derivatives. |
| 1180 | */ |
| 1181 | public function supportsDirectApiEditing() { |
| 1182 | return $this->supportsDirectEditing(); |
| 1183 | } |
| 1184 | |
| 1185 | /** |
| 1186 | * Get fields definition for search index |
| 1187 | * |
| 1188 | * @todo Expose title, redirect, namespace, text, source_text, text_bytes |
| 1189 | * field mappings here. (see T142670 and T143409) |
| 1190 | * |
| 1191 | * @stable to override |
| 1192 | * |
| 1193 | * @param SearchEngine $engine |
| 1194 | * @return SearchIndexField[] List of fields this content handler can provide. |
| 1195 | * @since 1.28 |
| 1196 | */ |
| 1197 | public function getFieldsForSearchIndex( SearchEngine $engine ) { |
| 1198 | $fields = []; |
| 1199 | $fields['category'] = $engine->makeSearchFieldMapping( |
| 1200 | 'category', |
| 1201 | SearchIndexField::INDEX_TYPE_TEXT |
| 1202 | ); |
| 1203 | $fields['category']->setFlag( SearchIndexField::FLAG_CASEFOLD ); |
| 1204 | |
| 1205 | $fields['external_link'] = $engine->makeSearchFieldMapping( |
| 1206 | 'external_link', |
| 1207 | SearchIndexField::INDEX_TYPE_KEYWORD |
| 1208 | ); |
| 1209 | |
| 1210 | $fields['outgoing_link'] = $engine->makeSearchFieldMapping( |
| 1211 | 'outgoing_link', |
| 1212 | SearchIndexField::INDEX_TYPE_KEYWORD |
| 1213 | ); |
| 1214 | |
| 1215 | $fields['template'] = $engine->makeSearchFieldMapping( |
| 1216 | 'template', |
| 1217 | SearchIndexField::INDEX_TYPE_KEYWORD |
| 1218 | ); |
| 1219 | $fields['template']->setFlag( SearchIndexField::FLAG_CASEFOLD ); |
| 1220 | |
| 1221 | $fields['content_model'] = $engine->makeSearchFieldMapping( |
| 1222 | 'content_model', |
| 1223 | SearchIndexField::INDEX_TYPE_KEYWORD |
| 1224 | ); |
| 1225 | |
| 1226 | return $fields; |
| 1227 | } |
| 1228 | |
| 1229 | /** |
| 1230 | * Add new field definition to array. |
| 1231 | * @param SearchIndexField[] &$fields |
| 1232 | * @param SearchEngine $engine |
| 1233 | * @param string $name |
| 1234 | * @param string $type |
| 1235 | * @return SearchIndexField[] new field defs |
| 1236 | * @since 1.28 |
| 1237 | */ |
| 1238 | protected function addSearchField( &$fields, SearchEngine $engine, $name, $type ) { |
| 1239 | $fields[$name] = $engine->makeSearchFieldMapping( $name, $type ); |
| 1240 | return $fields; |
| 1241 | } |
| 1242 | |
| 1243 | /** |
| 1244 | * Return fields to be indexed by search engine |
| 1245 | * as representation of this document. |
| 1246 | * Overriding class should call parent function or take care of calling |
| 1247 | * the SearchDataForIndex hook. |
| 1248 | * |
| 1249 | * The $output must be the result of a call to {@link getParserOutputForIndexing()} |
| 1250 | * on the same content handler. That method may return ParserOutput |
| 1251 | * {@link ParserOutput::hasText() without HTML}; this base implementation |
| 1252 | * does not rely on the HTML being present, so it is safe to call |
| 1253 | * even by subclasses that override {@link getParserOutputForIndexing()} |
| 1254 | * to skip HTML generation. On the other hand, |
| 1255 | * since the default implementation of {@link getParserOutputForIndexing()} |
| 1256 | * does generate HTML, subclasses are free to rely on the HTML here |
| 1257 | * if they do not override {@link getParserOutputForIndexing()}. |
| 1258 | * |
| 1259 | * @stable to override |
| 1260 | * @param WikiPage $page Page to index |
| 1261 | * @param ParserOutput $output |
| 1262 | * @param SearchEngine $engine Search engine for which we are indexing |
| 1263 | * @param RevisionRecord|null $revision Revision content to fetch if provided or use the latest revision |
| 1264 | * from WikiPage::getRevisionRecord() if not |
| 1265 | * @return array Map of name=>value for fields, an empty array is returned if the latest |
| 1266 | * revision cannot be retrieved. |
| 1267 | * @since 1.28 |
| 1268 | */ |
| 1269 | public function getDataForSearchIndex( |
| 1270 | WikiPage $page, |
| 1271 | ParserOutput $output, |
| 1272 | SearchEngine $engine, |
| 1273 | ?RevisionRecord $revision = null |
| 1274 | ) { |
| 1275 | $revision ??= $page->getRevisionRecord(); |
| 1276 | if ( $revision === null ) { |
| 1277 | LoggerFactory::getInstance( 'search' )->warning( |
| 1278 | "Called getDataForSearchIndex on the page {page_id} for which the " . |
| 1279 | "latest revision cannot be loaded.", |
| 1280 | [ "page_id" => $page->getId() ] |
| 1281 | ); |
| 1282 | return []; |
| 1283 | } |
| 1284 | Assert::invariant( $revision->getPageId() === $page->getId(), |
| 1285 | '$revision and $page must target the same page_id' ); |
| 1286 | |
| 1287 | $fieldData = []; |
| 1288 | $content = $revision->getContent( SlotRecord::MAIN ); |
| 1289 | |
| 1290 | if ( $content ) { |
| 1291 | $searchDataExtractor = new ParserOutputSearchDataExtractor(); |
| 1292 | |
| 1293 | $fieldData['category'] = $searchDataExtractor->getCategories( $output ); |
| 1294 | $fieldData['external_link'] = $searchDataExtractor->getExternalLinks( $output ); |
| 1295 | $fieldData['outgoing_link'] = $searchDataExtractor->getOutgoingLinks( $output ); |
| 1296 | $fieldData['template'] = $searchDataExtractor->getTemplates( $output ); |
| 1297 | |
| 1298 | $text = $content->getTextForSearchIndex(); |
| 1299 | |
| 1300 | $fieldData['text'] = $text; |
| 1301 | $fieldData['source_text'] = $text; |
| 1302 | $fieldData['text_bytes'] = $content->getSize(); |
| 1303 | $fieldData['content_model'] = $content->getModel(); |
| 1304 | } |
| 1305 | |
| 1306 | $this->getHookRunner()->onSearchDataForIndex( $fieldData, $this, $page, $output, $engine ); |
| 1307 | $this->getHookRunner()->onSearchDataForIndex2( $fieldData, $this, $page, $output, $engine, $revision ); |
| 1308 | |
| 1309 | return $fieldData; |
| 1310 | } |
| 1311 | |
| 1312 | /** |
| 1313 | * Produce page output suitable for indexing. |
| 1314 | * Typically used with {@link getDataForSearchIndex()}. |
| 1315 | * |
| 1316 | * Specific content handlers may override it if they need different content handling. |
| 1317 | * |
| 1318 | * The default implementation returns output {@link ParserOutput::hasText() with HTML}, |
| 1319 | * but callers should not rely on this, and subclasses may override this method |
| 1320 | * and skip HTML generation if it is not needed for indexing. |
| 1321 | * (In that case, they should not attempt to store the output in the $cache.) |
| 1322 | * |
| 1323 | * @stable to override |
| 1324 | * |
| 1325 | * @param WikiPage $page |
| 1326 | * @param ParserCache|null $cache deprecated since 1.38 and won't have any effect |
| 1327 | * @param RevisionRecord|null $revision |
| 1328 | * @return ParserOutput|null null when the ParserOutput cannot be obtained |
| 1329 | * @see ParserOutputAccess::getParserOutput() for failure modes |
| 1330 | */ |
| 1331 | public function getParserOutputForIndexing( |
| 1332 | WikiPage $page, |
| 1333 | ?ParserCache $cache = null, |
| 1334 | ?RevisionRecord $revision = null |
| 1335 | ) { |
| 1336 | // TODO: MCR: ContentHandler should be called per slot, not for the whole page. |
| 1337 | // See T190066. |
| 1338 | $parserOptions = $page->makeParserOptions( 'canonical' ); |
| 1339 | $parserOptions->setRenderReason( 'ParserOutputForIndexing' ); |
| 1340 | $parserOutputAccess = MediaWikiServices::getInstance()->getParserOutputAccess(); |
| 1341 | return $parserOutputAccess->getParserOutput( |
| 1342 | $page, |
| 1343 | $parserOptions, |
| 1344 | $revision, |
| 1345 | [ ParserOutputAccess::OPT_NO_UPDATE_CACHE => true ], |
| 1346 | )->getValue(); |
| 1347 | } |
| 1348 | |
| 1349 | /** |
| 1350 | * Get the latest revision of the given $page, |
| 1351 | * fetching it from the primary if necessary. |
| 1352 | * |
| 1353 | * @param WikiPage $page |
| 1354 | * @return RevisionRecord |
| 1355 | * @since 1.36 (previously private) |
| 1356 | */ |
| 1357 | protected function latestRevision( WikiPage $page ): RevisionRecord { |
| 1358 | $revRecord = $page->getRevisionRecord(); |
| 1359 | if ( $revRecord == null ) { |
| 1360 | // If the content represents a brand new page it's possible |
| 1361 | // we need to fetch it from the primary. |
| 1362 | $page->loadPageData( IDBAccessObject::READ_LATEST ); |
| 1363 | $revRecord = $page->getRevisionRecord(); |
| 1364 | if ( $revRecord == null ) { |
| 1365 | $text = $page->getTitle()->getPrefixedText(); |
| 1366 | throw new MWException( |
| 1367 | "No revision could be loaded for page: $text" ); |
| 1368 | } |
| 1369 | } |
| 1370 | |
| 1371 | return $revRecord; |
| 1372 | } |
| 1373 | |
| 1374 | /** |
| 1375 | * Returns a list of DeferrableUpdate objects for recording information about the |
| 1376 | * given Content in some secondary data store. |
| 1377 | * |
| 1378 | * Application logic should not call this method directly. Instead, it should call |
| 1379 | * DerivedPageDataUpdater::getSecondaryDataUpdates(). |
| 1380 | * |
| 1381 | * @note Implementations must not return a LinksUpdate instance. Instead, a LinksUpdate |
| 1382 | * is created by the calling code in DerivedPageDataUpdater, on the combined ParserOutput |
| 1383 | * of all slots, not for each slot individually. This is in contrast to the old |
| 1384 | * getSecondaryDataUpdates method defined by AbstractContent, which returned a LinksUpdate. |
| 1385 | * |
| 1386 | * @note Implementations should not call $content->getParserOutput, they should call |
| 1387 | * $slotOutput->getSlotRendering( $role, false ) instead if they need to access a ParserOutput |
| 1388 | * of $content. This allows existing ParserOutput objects to be re-used, while avoiding |
| 1389 | * creating a ParserOutput when none is needed. |
| 1390 | * |
| 1391 | * @stable to override |
| 1392 | * |
| 1393 | * @param Title $title The title of the page to supply the updates for |
| 1394 | * @param Content $content The content to generate data updates for. |
| 1395 | * @param string $role The role (slot) in which the content is being used. Which updates |
| 1396 | * are performed should generally not depend on the role the content has, but the |
| 1397 | * DeferrableUpdates themselves may need to know the role, to track to which slot the |
| 1398 | * data refers, and to avoid overwriting data of the same kind from another slot. |
| 1399 | * @param SlotRenderingProvider $slotOutput A provider that can be used to gain access to |
| 1400 | * a ParserOutput of $content by calling $slotOutput->getSlotParserOutput( $role, false ). |
| 1401 | * @return DeferrableUpdate[] A list of DeferrableUpdate objects for putting information |
| 1402 | * about this content object somewhere. The default implementation returns an empty |
| 1403 | * array. |
| 1404 | * @since 1.32 |
| 1405 | */ |
| 1406 | public function getSecondaryDataUpdates( |
| 1407 | Title $title, |
| 1408 | Content $content, |
| 1409 | $role, |
| 1410 | SlotRenderingProvider $slotOutput |
| 1411 | ) { |
| 1412 | return []; |
| 1413 | } |
| 1414 | |
| 1415 | /** |
| 1416 | * Returns a list of DeferrableUpdate objects for removing information about content |
| 1417 | * in some secondary data store. This is used when a page is deleted, and also when |
| 1418 | * a slot is removed from a page. |
| 1419 | * |
| 1420 | * Application logic should not call this method directly. Instead, it should call |
| 1421 | * WikiPage::getSecondaryDataUpdates(). |
| 1422 | * |
| 1423 | * @note Implementations must not return a LinksDeletionUpdate instance. Instead, a |
| 1424 | * LinksDeletionUpdate is created by the calling code in WikiPage. |
| 1425 | * This is in contrast to the old getDeletionUpdates method defined by AbstractContent, |
| 1426 | * which returned a LinksUpdate. |
| 1427 | * |
| 1428 | * @note Implementations should not rely on the page's current content, but rather the current |
| 1429 | * state of the secondary data store. |
| 1430 | * |
| 1431 | * @stable to override |
| 1432 | * |
| 1433 | * @param Title $title The title of the page to supply the updates for |
| 1434 | * @param string $role The role (slot) in which the content is being used. Which updates |
| 1435 | * are performed should generally not depend on the role the content has, but the |
| 1436 | * DeferrableUpdates themselves may need to know the role, to track to which slot the |
| 1437 | * data refers, and to avoid overwriting data of the same kind from another slot. |
| 1438 | * |
| 1439 | * @return DeferrableUpdate[] A list of DeferrableUpdate objects for putting information |
| 1440 | * about this content object somewhere. The default implementation returns an empty |
| 1441 | * array. |
| 1442 | * |
| 1443 | * @since 1.32 |
| 1444 | */ |
| 1445 | public function getDeletionUpdates( Title $title, $role ) { |
| 1446 | return []; |
| 1447 | } |
| 1448 | |
| 1449 | /** |
| 1450 | * Returns a $content object with pre-save transformations applied (or the same |
| 1451 | * object if no transformations apply). |
| 1452 | * |
| 1453 | * @note Not stable to call other then from ContentHandler hierarchy. |
| 1454 | * Callers need to use ContentTransformer::preSaveTransform. |
| 1455 | * @stable to override |
| 1456 | * @since 1.37 |
| 1457 | * |
| 1458 | * @param Content $content |
| 1459 | * @param PreSaveTransformParams $pstParams |
| 1460 | * |
| 1461 | * @return Content |
| 1462 | */ |
| 1463 | public function preSaveTransform( |
| 1464 | Content $content, |
| 1465 | PreSaveTransformParams $pstParams |
| 1466 | ): Content { |
| 1467 | return $content; |
| 1468 | } |
| 1469 | |
| 1470 | /** |
| 1471 | * Returns a $content object with preload transformations applied (or the same |
| 1472 | * object if no transformations apply). |
| 1473 | * |
| 1474 | * @note Not stable to call other then from ContentHandler hierarchy. |
| 1475 | * Callers need to use ContentTransformer::preLoadTransform. |
| 1476 | * @stable to override |
| 1477 | * @since 1.37 |
| 1478 | * |
| 1479 | * @param Content $content |
| 1480 | * @param PreloadTransformParams $pltParams |
| 1481 | * |
| 1482 | * @return Content |
| 1483 | */ |
| 1484 | public function preloadTransform( |
| 1485 | Content $content, |
| 1486 | PreloadTransformParams $pltParams |
| 1487 | ): Content { |
| 1488 | return $content; |
| 1489 | } |
| 1490 | |
| 1491 | /** |
| 1492 | * Validate content for saving it. |
| 1493 | * |
| 1494 | * This may be used to check the content's consistency with global state. This function should |
| 1495 | * NOT write any information to the database. |
| 1496 | * |
| 1497 | * Note that this method will usually be called inside the same transaction |
| 1498 | * bracket that will be used to save the new revision, so the revision passed |
| 1499 | * in is probably unsaved (has no id) and might belong to unsaved page. |
| 1500 | * |
| 1501 | * @since 1.38 |
| 1502 | * @stable to override |
| 1503 | * |
| 1504 | * @param Content $content |
| 1505 | * @param ValidationParams $validationParams |
| 1506 | * |
| 1507 | * @return StatusValue A status object indicating if content can be saved in the given revision. |
| 1508 | */ |
| 1509 | public function validateSave( |
| 1510 | Content $content, |
| 1511 | ValidationParams $validationParams |
| 1512 | ) { |
| 1513 | if ( $content->isValid() ) { |
| 1514 | return StatusValue::newGood(); |
| 1515 | } else { |
| 1516 | return StatusValue::newFatal( "invalid-content-data" ); |
| 1517 | } |
| 1518 | } |
| 1519 | |
| 1520 | /** |
| 1521 | * Returns a ParserOutput object containing information derived from this content. |
| 1522 | * Most importantly, unless $cpoParams->getGenerateHtml was false, the return value contains an |
| 1523 | * HTML representation of the content. |
| 1524 | * |
| 1525 | * Subclasses that want to control the parser output may override |
| 1526 | * fillParserOutput() instead. |
| 1527 | * |
| 1528 | * |
| 1529 | * |
| 1530 | * @since 1.38 |
| 1531 | * |
| 1532 | * @param Content $content |
| 1533 | * @param ContentParseParams $cpoParams |
| 1534 | * @return ParserOutput Containing information derived from this content. |
| 1535 | */ |
| 1536 | public function getParserOutput( |
| 1537 | Content $content, |
| 1538 | ContentParseParams $cpoParams |
| 1539 | ) { |
| 1540 | $services = MediaWikiServices::getInstance(); |
| 1541 | $title = $services->getTitleFactory()->newFromPageReference( $cpoParams->getPage() ); |
| 1542 | $parserOptions = $cpoParams->getParserOptions(); |
| 1543 | |
| 1544 | if ( $parserOptions->getIsPreview() ) { |
| 1545 | $scopedCallback = $parserOptions->setupFakeRevision( |
| 1546 | $title, |
| 1547 | $content, |
| 1548 | $parserOptions->getUserIdentity(), |
| 1549 | $cpoParams->getRevId() ?: 0 |
| 1550 | ); |
| 1551 | } |
| 1552 | |
| 1553 | $hookRunner = new HookRunner( $services->getHookContainer() ); |
| 1554 | |
| 1555 | $po = new ParserOutput(); |
| 1556 | |
| 1557 | // Initialize to the page language |
| 1558 | $po->setLanguage( $title->getPageLanguage() ); |
| 1559 | |
| 1560 | // Necessary use of a reference, because the fillParserOutput() call below also uses |
| 1561 | // pass-by-reference and may reassign $po (5c9322ae06384a8845962ba7e3c499731110e7f0). |
| 1562 | $parserOptions->registerWatcher( [ &$po, 'recordOption' ] ); |
| 1563 | if ( $hookRunner->onContentGetParserOutput( |
| 1564 | // FIXME $cpoParams->getRevId() may be null here? |
| 1565 | // @phan-suppress-next-line PhanTypeMismatchArgumentNullable |
| 1566 | $content, $title, $cpoParams->getRevId(), $parserOptions, $cpoParams->getGenerateHtml(), $po ) |
| 1567 | ) { |
| 1568 | // Save and restore the old value, just in case something is reusing |
| 1569 | // the ParserOptions object in some weird way. |
| 1570 | $oldRedir = $parserOptions->getRedirectTarget(); |
| 1571 | $parserOptions->setRedirectTarget( $content->getRedirectTarget() ); |
| 1572 | |
| 1573 | $po->resetParseStartTime(); |
| 1574 | $this->fillParserOutput( |
| 1575 | $content, |
| 1576 | $cpoParams, |
| 1577 | $po |
| 1578 | ); |
| 1579 | $po->recordTimeProfile(); |
| 1580 | |
| 1581 | MediaWikiServices::getInstance()->get( '_ParserObserver' )->notifyParse( |
| 1582 | $title, |
| 1583 | $cpoParams->getRevId(), |
| 1584 | $parserOptions, |
| 1585 | $content, |
| 1586 | $po |
| 1587 | ); |
| 1588 | $parserOptions->setRedirectTarget( $oldRedir ); |
| 1589 | } |
| 1590 | |
| 1591 | $hookRunner->onContentAlterParserOutput( $content, $title, $po ); |
| 1592 | $parserOptions->registerWatcher( null ); |
| 1593 | if ( isset( $scopedCallback ) ) { |
| 1594 | ScopedCallback::consume( $scopedCallback ); |
| 1595 | } |
| 1596 | |
| 1597 | return $po; |
| 1598 | } |
| 1599 | |
| 1600 | /** |
| 1601 | * A temporary layer to move AbstractContent::fillParserOutput to ContentHandler::fillParserOutput |
| 1602 | * |
| 1603 | * @internal only core AbstractContent::fillParserOutput implementations need to call this. |
| 1604 | * @since 1.38 |
| 1605 | * @param Content $content |
| 1606 | * @param ContentParseParams $cpoParams |
| 1607 | * @param ParserOutput &$output The output object to fill (reference). |
| 1608 | */ |
| 1609 | public function fillParserOutputInternal( |
| 1610 | Content $content, |
| 1611 | ContentParseParams $cpoParams, |
| 1612 | ParserOutput &$output |
| 1613 | ) { |
| 1614 | $this->fillParserOutput( $content, $cpoParams, $output ); |
| 1615 | } |
| 1616 | |
| 1617 | /** |
| 1618 | * Fills the provided ParserOutput with information derived from the content. |
| 1619 | * Unless $cpoParams->getGenerateHtml() was false, |
| 1620 | * this includes an HTML representation of the content. |
| 1621 | * |
| 1622 | * If $cpoParams->getGenerateHtml() is false, and you chose not to generate |
| 1623 | * html, the ParserOutput must have a text of null. If the |
| 1624 | * text of the ParserOutput object is anything other than null (even if ''), |
| 1625 | * it is assumed that you don't support not generating html, and that it is |
| 1626 | * safe to reuse the parser output for calls expecting that html was generated. |
| 1627 | * |
| 1628 | * Subclasses are expected to override this method. |
| 1629 | * |
| 1630 | * This placeholder implementation always throws an exception. |
| 1631 | * |
| 1632 | * @stable to override |
| 1633 | * |
| 1634 | * @since 1.38 |
| 1635 | * @param Content $content |
| 1636 | * @param ContentParseParams $cpoParams |
| 1637 | * @param ParserOutput &$output The output object to fill (reference). |
| 1638 | * Most implementations should modify the output object passed in here; |
| 1639 | * if you choose to replace it with a fresh object instead, |
| 1640 | * make sure you call {@link ParserOutput::resetParseStartTime()} on it. |
| 1641 | */ |
| 1642 | protected function fillParserOutput( |
| 1643 | Content $content, |
| 1644 | ContentParseParams $cpoParams, |
| 1645 | ParserOutput &$output |
| 1646 | ) { |
| 1647 | // Subclasses must override fillParserOutput() to directly don't fail. |
| 1648 | throw new LogicException( 'Subclasses of ContentHandler must override fillParserOutput!' ); |
| 1649 | } |
| 1650 | |
| 1651 | } |
| 1652 | |
| 1653 | /** @deprecated class alias since 1.43 */ |
| 1654 | class_alias( ContentHandler::class, 'ContentHandler' ); |