Make warning about deprecated SlotDiffRenderer wrapper less noisy
[lhc/web/wiklou.git] / includes / content / ContentHandler.php
1 <?php
2
3 use MediaWiki\Logger\LoggerFactory;
4 use MediaWiki\MediaWikiServices;
5 use MediaWiki\Search\ParserOutputSearchDataExtractor;
6
7 /**
8 * Base class for content handling.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 *
25 * @since 1.21
26 *
27 * @file
28 * @ingroup Content
29 *
30 * @author Daniel Kinzler
31 */
32 /**
33 * A content handler knows how do deal with a specific type of content on a wiki
34 * page. Content is stored in the database in a serialized form (using a
35 * serialization format a.k.a. MIME type) and is unserialized into its native
36 * PHP representation (the content model), which is wrapped in an instance of
37 * the appropriate subclass of Content.
38 *
39 * ContentHandler instances are stateless singletons that serve, among other
40 * things, as a factory for Content objects. Generally, there is one subclass
41 * of ContentHandler and one subclass of Content for every type of content model.
42 *
43 * Some content types have a flat model, that is, their native representation
44 * is the same as their serialized form. Examples would be JavaScript and CSS
45 * code. As of now, this also applies to wikitext (MediaWiki's default content
46 * type), but wikitext content may be represented by a DOM or AST structure in
47 * the future.
48 *
49 * @ingroup Content
50 */
51 abstract class ContentHandler {
52 /**
53 * Convenience function for getting flat text from a Content object. This
54 * should only be used in the context of backwards compatibility with code
55 * that is not yet able to handle Content objects!
56 *
57 * If $content is null, this method returns the empty string.
58 *
59 * If $content is an instance of TextContent, this method returns the flat
60 * text as returned by $content->getNativeData().
61 *
62 * If $content is not a TextContent object, the behavior of this method
63 * depends on the global $wgContentHandlerTextFallback:
64 * - If $wgContentHandlerTextFallback is 'fail' and $content is not a
65 * TextContent object, an MWException is thrown.
66 * - If $wgContentHandlerTextFallback is 'serialize' and $content is not a
67 * TextContent object, $content->serialize() is called to get a string
68 * form of the content.
69 * - If $wgContentHandlerTextFallback is 'ignore' and $content is not a
70 * TextContent object, this method returns null.
71 * - otherwise, the behavior is undefined.
72 *
73 * @since 1.21
74 *
75 * @param Content|null $content
76 *
77 * @throws MWException If the content is not an instance of TextContent and
78 * wgContentHandlerTextFallback was set to 'fail'.
79 * @return string|null Textual form of the content, if available.
80 */
81 public static function getContentText( Content $content = null ) {
82 global $wgContentHandlerTextFallback;
83
84 if ( is_null( $content ) ) {
85 return '';
86 }
87
88 if ( $content instanceof TextContent ) {
89 return $content->getNativeData();
90 }
91
92 wfDebugLog( 'ContentHandler', 'Accessing ' . $content->getModel() . ' content as text!' );
93
94 if ( $wgContentHandlerTextFallback == 'fail' ) {
95 throw new MWException(
96 "Attempt to get text from Content with model " .
97 $content->getModel()
98 );
99 }
100
101 if ( $wgContentHandlerTextFallback == 'serialize' ) {
102 return $content->serialize();
103 }
104
105 return null;
106 }
107
108 /**
109 * Convenience function for creating a Content object from a given textual
110 * representation.
111 *
112 * $text will be deserialized into a Content object of the model specified
113 * by $modelId (or, if that is not given, $title->getContentModel()) using
114 * the given format.
115 *
116 * @since 1.21
117 *
118 * @param string $text The textual representation, will be
119 * unserialized to create the Content object
120 * @param Title|null $title The title of the page this text belongs to.
121 * Required if $modelId is not provided.
122 * @param string|null $modelId The model to deserialize to. If not provided,
123 * $title->getContentModel() is used.
124 * @param string|null $format The format to use for deserialization. If not
125 * given, the model's default format is used.
126 *
127 * @throws MWException If model ID or format is not supported or if the text can not be
128 * unserialized using the format.
129 * @return Content A Content object representing the text.
130 */
131 public static function makeContent( $text, Title $title = null,
132 $modelId = null, $format = null ) {
133 if ( is_null( $modelId ) ) {
134 if ( is_null( $title ) ) {
135 throw new MWException( "Must provide a Title object or a content model ID." );
136 }
137
138 $modelId = $title->getContentModel();
139 }
140
141 $handler = self::getForModelID( $modelId );
142
143 return $handler->unserializeContent( $text, $format );
144 }
145
146 /**
147 * Returns the name of the default content model to be used for the page
148 * with the given title.
149 *
150 * Note: There should rarely be need to call this method directly.
151 * To determine the actual content model for a given page, use
152 * Title::getContentModel().
153 *
154 * Which model is to be used by default for the page is determined based
155 * on several factors:
156 * - The global setting $wgNamespaceContentModels specifies a content model
157 * per namespace.
158 * - The hook ContentHandlerDefaultModelFor may be used to override the page's default
159 * model.
160 * - Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript
161 * model if they end in .js or .css, respectively.
162 * - Pages in NS_MEDIAWIKI default to the wikitext model otherwise.
163 * - The hook TitleIsCssOrJsPage may be used to force a page to use the CSS
164 * or JavaScript model. This is a compatibility feature. The ContentHandlerDefaultModelFor
165 * hook should be used instead if possible.
166 * - The hook TitleIsWikitextPage may be used to force a page to use the
167 * wikitext model. This is a compatibility feature. The ContentHandlerDefaultModelFor
168 * hook should be used instead if possible.
169 *
170 * If none of the above applies, the wikitext model is used.
171 *
172 * Note: this is used by, and may thus not use, Title::getContentModel()
173 *
174 * @since 1.21
175 *
176 * @param Title $title
177 *
178 * @return string Default model name for the page given by $title
179 */
180 public static function getDefaultModelFor( Title $title ) {
181 // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
182 // because it is used to initialize the mContentModel member.
183
184 $ns = $title->getNamespace();
185
186 $ext = false;
187 $m = null;
188 $model = MWNamespace::getNamespaceContentModel( $ns );
189
190 // Hook can determine default model
191 if ( !Hooks::run( 'ContentHandlerDefaultModelFor', [ $title, &$model ] ) ) {
192 if ( !is_null( $model ) ) {
193 return $model;
194 }
195 }
196
197 // Could this page contain code based on the title?
198 $isCodePage = NS_MEDIAWIKI == $ns && preg_match( '!\.(css|js|json)$!u', $title->getText(), $m );
199 if ( $isCodePage ) {
200 $ext = $m[1];
201 }
202
203 // Is this a user subpage containing code?
204 $isCodeSubpage = NS_USER == $ns
205 && !$isCodePage
206 && preg_match( "/\\/.*\\.(js|css|json)$/", $title->getText(), $m );
207 if ( $isCodeSubpage ) {
208 $ext = $m[1];
209 }
210
211 // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
212 $isWikitext = is_null( $model ) || $model == CONTENT_MODEL_WIKITEXT;
213 $isWikitext = $isWikitext && !$isCodePage && !$isCodeSubpage;
214
215 if ( !$isWikitext ) {
216 switch ( $ext ) {
217 case 'js':
218 return CONTENT_MODEL_JAVASCRIPT;
219 case 'css':
220 return CONTENT_MODEL_CSS;
221 case 'json':
222 return CONTENT_MODEL_JSON;
223 default:
224 return is_null( $model ) ? CONTENT_MODEL_TEXT : $model;
225 }
226 }
227
228 // We established that it must be wikitext
229
230 return CONTENT_MODEL_WIKITEXT;
231 }
232
233 /**
234 * Returns the appropriate ContentHandler singleton for the given title.
235 *
236 * @since 1.21
237 *
238 * @param Title $title
239 *
240 * @return ContentHandler
241 */
242 public static function getForTitle( Title $title ) {
243 $modelId = $title->getContentModel();
244
245 return self::getForModelID( $modelId );
246 }
247
248 /**
249 * Returns the appropriate ContentHandler singleton for the given Content
250 * object.
251 *
252 * @since 1.21
253 *
254 * @param Content $content
255 *
256 * @return ContentHandler
257 */
258 public static function getForContent( Content $content ) {
259 $modelId = $content->getModel();
260
261 return self::getForModelID( $modelId );
262 }
263
264 /**
265 * @var array A Cache of ContentHandler instances by model id
266 */
267 protected static $handlers;
268
269 /**
270 * Returns the ContentHandler singleton for the given model ID. Use the
271 * CONTENT_MODEL_XXX constants to identify the desired content model.
272 *
273 * ContentHandler singletons are taken from the global $wgContentHandlers
274 * array. Keys in that array are model names, the values are either
275 * ContentHandler singleton objects, or strings specifying the appropriate
276 * subclass of ContentHandler.
277 *
278 * If a class name is encountered when looking up the singleton for a given
279 * model name, the class is instantiated and the class name is replaced by
280 * the resulting singleton in $wgContentHandlers.
281 *
282 * If no ContentHandler is defined for the desired $modelId, the
283 * ContentHandler may be provided by the ContentHandlerForModelID hook.
284 * If no ContentHandler can be determined, an MWException is raised.
285 *
286 * @since 1.21
287 *
288 * @param string $modelId The ID of the content model for which to get a
289 * handler. Use CONTENT_MODEL_XXX constants.
290 *
291 * @throws MWException For internal errors and problems in the configuration.
292 * @throws MWUnknownContentModelException If no handler is known for the model ID.
293 * @return ContentHandler The ContentHandler singleton for handling the model given by the ID.
294 */
295 public static function getForModelID( $modelId ) {
296 global $wgContentHandlers;
297
298 if ( isset( self::$handlers[$modelId] ) ) {
299 return self::$handlers[$modelId];
300 }
301
302 if ( empty( $wgContentHandlers[$modelId] ) ) {
303 $handler = null;
304
305 Hooks::run( 'ContentHandlerForModelID', [ $modelId, &$handler ] );
306
307 if ( $handler === null ) {
308 throw new MWUnknownContentModelException( $modelId );
309 }
310
311 if ( !( $handler instanceof ContentHandler ) ) {
312 throw new MWException( "ContentHandlerForModelID must supply a ContentHandler instance" );
313 }
314 } else {
315 $classOrCallback = $wgContentHandlers[$modelId];
316
317 if ( is_callable( $classOrCallback ) ) {
318 $handler = call_user_func( $classOrCallback, $modelId );
319 } else {
320 $handler = new $classOrCallback( $modelId );
321 }
322
323 if ( !( $handler instanceof ContentHandler ) ) {
324 throw new MWException( "$classOrCallback from \$wgContentHandlers is not " .
325 "compatible with ContentHandler" );
326 }
327 }
328
329 wfDebugLog( 'ContentHandler', 'Created handler for ' . $modelId
330 . ': ' . get_class( $handler ) );
331
332 self::$handlers[$modelId] = $handler;
333
334 return self::$handlers[$modelId];
335 }
336
337 /**
338 * Clean up handlers cache.
339 */
340 public static function cleanupHandlersCache() {
341 self::$handlers = [];
342 }
343
344 /**
345 * Returns the localized name for a given content model.
346 *
347 * Model names are localized using system messages. Message keys
348 * have the form content-model-$name, where $name is getContentModelName( $id ).
349 *
350 * @param string $name The content model ID, as given by a CONTENT_MODEL_XXX
351 * constant or returned by Revision::getContentModel().
352 * @param Language|null $lang The language to parse the message in (since 1.26)
353 *
354 * @throws MWException If the model ID isn't known.
355 * @return string The content model's localized name.
356 */
357 public static function getLocalizedName( $name, Language $lang = null ) {
358 // Messages: content-model-wikitext, content-model-text,
359 // content-model-javascript, content-model-css
360 $key = "content-model-$name";
361
362 $msg = wfMessage( $key );
363 if ( $lang ) {
364 $msg->inLanguage( $lang );
365 }
366
367 return $msg->exists() ? $msg->plain() : $name;
368 }
369
370 public static function getContentModels() {
371 global $wgContentHandlers;
372
373 $models = array_keys( $wgContentHandlers );
374 Hooks::run( 'GetContentModels', [ &$models ] );
375 return $models;
376 }
377
378 public static function getAllContentFormats() {
379 global $wgContentHandlers;
380
381 $formats = [];
382
383 foreach ( $wgContentHandlers as $model => $class ) {
384 $handler = self::getForModelID( $model );
385 $formats = array_merge( $formats, $handler->getSupportedFormats() );
386 }
387
388 $formats = array_unique( $formats );
389
390 return $formats;
391 }
392
393 // ------------------------------------------------------------------------
394
395 /**
396 * @var string
397 */
398 protected $mModelID;
399
400 /**
401 * @var string[]
402 */
403 protected $mSupportedFormats;
404
405 /**
406 * Constructor, initializing the ContentHandler instance with its model ID
407 * and a list of supported formats. Values for the parameters are typically
408 * provided as literals by subclass's constructors.
409 *
410 * @param string $modelId (use CONTENT_MODEL_XXX constants).
411 * @param string[] $formats List for supported serialization formats
412 * (typically as MIME types)
413 */
414 public function __construct( $modelId, $formats ) {
415 $this->mModelID = $modelId;
416 $this->mSupportedFormats = $formats;
417 }
418
419 /**
420 * Serializes a Content object of the type supported by this ContentHandler.
421 *
422 * @since 1.21
423 *
424 * @param Content $content The Content object to serialize
425 * @param string|null $format The desired serialization format
426 *
427 * @return string Serialized form of the content
428 */
429 abstract public function serializeContent( Content $content, $format = null );
430
431 /**
432 * Applies transformations on export (returns the blob unchanged per default).
433 * Subclasses may override this to perform transformations such as conversion
434 * of legacy formats or filtering of internal meta-data.
435 *
436 * @param string $blob The blob to be exported
437 * @param string|null $format The blob's serialization format
438 *
439 * @return string
440 */
441 public function exportTransform( $blob, $format = null ) {
442 return $blob;
443 }
444
445 /**
446 * Unserializes a Content object of the type supported by this ContentHandler.
447 *
448 * @since 1.21
449 *
450 * @param string $blob Serialized form of the content
451 * @param string|null $format The format used for serialization
452 *
453 * @return Content The Content object created by deserializing $blob
454 */
455 abstract public function unserializeContent( $blob, $format = null );
456
457 /**
458 * Apply import transformation (per default, returns $blob unchanged).
459 * This gives subclasses an opportunity to transform data blobs on import.
460 *
461 * @since 1.24
462 *
463 * @param string $blob
464 * @param string|null $format
465 *
466 * @return string
467 */
468 public function importTransform( $blob, $format = null ) {
469 return $blob;
470 }
471
472 /**
473 * Creates an empty Content object of the type supported by this
474 * ContentHandler.
475 *
476 * @since 1.21
477 *
478 * @return Content
479 */
480 abstract public function makeEmptyContent();
481
482 /**
483 * Creates a new Content object that acts as a redirect to the given page,
484 * or null if redirects are not supported by this content model.
485 *
486 * This default implementation always returns null. Subclasses supporting redirects
487 * must override this method.
488 *
489 * Note that subclasses that override this method to return a Content object
490 * should also override supportsRedirects() to return true.
491 *
492 * @since 1.21
493 *
494 * @param Title $destination The page to redirect to.
495 * @param string $text Text to include in the redirect, if possible.
496 *
497 * @return Content Always null.
498 */
499 public function makeRedirectContent( Title $destination, $text = '' ) {
500 return null;
501 }
502
503 /**
504 * Returns the model id that identifies the content model this
505 * ContentHandler can handle. Use with the CONTENT_MODEL_XXX constants.
506 *
507 * @since 1.21
508 *
509 * @return string The model ID
510 */
511 public function getModelID() {
512 return $this->mModelID;
513 }
514
515 /**
516 * @since 1.21
517 *
518 * @param string $model_id The model to check
519 *
520 * @throws MWException If the model ID is not the ID of the content model supported by this
521 * ContentHandler.
522 */
523 protected function checkModelID( $model_id ) {
524 if ( $model_id !== $this->mModelID ) {
525 throw new MWException( "Bad content model: " .
526 "expected {$this->mModelID} " .
527 "but got $model_id." );
528 }
529 }
530
531 /**
532 * Returns a list of serialization formats supported by the
533 * serializeContent() and unserializeContent() methods of this
534 * ContentHandler.
535 *
536 * @since 1.21
537 *
538 * @return string[] List of serialization formats as MIME type like strings
539 */
540 public function getSupportedFormats() {
541 return $this->mSupportedFormats;
542 }
543
544 /**
545 * The format used for serialization/deserialization by default by this
546 * ContentHandler.
547 *
548 * This default implementation will return the first element of the array
549 * of formats that was passed to the constructor.
550 *
551 * @since 1.21
552 *
553 * @return string The name of the default serialization format as a MIME type
554 */
555 public function getDefaultFormat() {
556 return $this->mSupportedFormats[0];
557 }
558
559 /**
560 * Returns true if $format is a serialization format supported by this
561 * ContentHandler, and false otherwise.
562 *
563 * Note that if $format is null, this method always returns true, because
564 * null means "use the default format".
565 *
566 * @since 1.21
567 *
568 * @param string $format The serialization format to check
569 *
570 * @return bool
571 */
572 public function isSupportedFormat( $format ) {
573 if ( !$format ) {
574 return true; // this means "use the default"
575 }
576
577 return in_array( $format, $this->mSupportedFormats );
578 }
579
580 /**
581 * Convenient for checking whether a format provided as a parameter is actually supported.
582 *
583 * @param string $format The serialization format to check
584 *
585 * @throws MWException If the format is not supported by this content handler.
586 */
587 protected function checkFormat( $format ) {
588 if ( !$this->isSupportedFormat( $format ) ) {
589 throw new MWException(
590 "Format $format is not supported for content model "
591 . $this->getModelID()
592 );
593 }
594 }
595
596 /**
597 * Returns overrides for action handlers.
598 * Classes listed here will be used instead of the default one when
599 * (and only when) $wgActions[$action] === true. This allows subclasses
600 * to override the default action handlers.
601 *
602 * @since 1.21
603 *
604 * @return array An array mapping action names (typically "view", "edit", "history" etc.) to
605 * either the full qualified class name of an Action class, a callable taking ( Page $page,
606 * IContextSource $context = null ) as parameters and returning an Action object, or an actual
607 * Action object. An empty array in this default implementation.
608 *
609 * @see Action::factory
610 */
611 public function getActionOverrides() {
612 return [];
613 }
614
615 /**
616 * Factory for creating an appropriate DifferenceEngine for this content model.
617 * Since 1.32, this is only used for page-level diffs; to diff two content objects,
618 * use getSlotDiffRenderer.
619 *
620 * The DifferenceEngine subclass to use is selected in getDiffEngineClass(). The
621 * GetDifferenceEngine hook will receive the DifferenceEngine object and can replace or
622 * wrap it.
623 * (Note that in older versions of MediaWiki the hook documentation instructed extensions
624 * to return false from the hook; you should not rely on always being able to decorate
625 * the DifferenceEngine instance from the hook. If the owner of the content type wants to
626 * decorare the instance, overriding this method is a safer approach.)
627 *
628 * @todo This is page-level functionality so it should not belong to ContentHandler.
629 * Move it to a better place once one exists (e.g. PageTypeHandler).
630 *
631 * @since 1.21
632 *
633 * @param IContextSource $context Context to use, anything else will be ignored.
634 * @param int $old Revision ID we want to show and diff with.
635 * @param int|string $new Either a revision ID or one of the strings 'cur', 'prev' or 'next'.
636 * @param int $rcid FIXME: Deprecated, no longer used. Defaults to 0.
637 * @param bool $refreshCache If set, refreshes the diff cache. Defaults to false.
638 * @param bool $unhide If set, allow viewing deleted revs. Defaults to false.
639 *
640 * @return DifferenceEngine
641 */
642 public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0,
643 $rcid = 0, // FIXME: Deprecated, no longer used
644 $refreshCache = false, $unhide = false
645 ) {
646 $diffEngineClass = $this->getDiffEngineClass();
647 $differenceEngine = new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
648 Hooks::run( 'GetDifferenceEngine', [ $context, $old, $new, $refreshCache, $unhide,
649 &$differenceEngine ] );
650 return $differenceEngine;
651 }
652
653 /**
654 * Get an appropriate SlotDiffRenderer for this content model.
655 * @since 1.32
656 * @param IContextSource $context
657 * @return SlotDiffRenderer
658 */
659 final public function getSlotDiffRenderer( IContextSource $context ) {
660 $slotDiffRenderer = $this->getSlotDiffRendererInternal( $context );
661 if ( get_class( $slotDiffRenderer ) === TextSlotDiffRenderer::class ) {
662 // To keep B/C, when SlotDiffRenderer is not overridden for a given content type
663 // but DifferenceEngine is, use that instead.
664 $differenceEngine = $this->createDifferenceEngine( $context );
665 if ( get_class( $differenceEngine ) !== DifferenceEngine::class ) {
666 // TODO turn this into a deprecation warning in a later release
667 LoggerFactory::getInstance( 'diff' )->info(
668 'Falling back to DifferenceEngineSlotDiffRenderer', [
669 'modelID' => $this->getModelID(),
670 'DifferenceEngine' => get_class( $differenceEngine ),
671 ] );
672 $slotDiffRenderer = new DifferenceEngineSlotDiffRenderer( $differenceEngine );
673 }
674 }
675 Hooks::run( 'GetSlotDiffRenderer', [ $this, &$slotDiffRenderer, $context ] );
676 return $slotDiffRenderer;
677 }
678
679 /**
680 * Return the SlotDiffRenderer appropriate for this content handler.
681 * @param IContextSource $context
682 * @return SlotDiffRenderer
683 */
684 protected function getSlotDiffRendererInternal( IContextSource $context ) {
685 $contentLanguage = MediaWikiServices::getInstance()->getContentLanguage();
686 $statsdDataFactory = MediaWikiServices::getInstance()->getStatsdDataFactory();
687 $slotDiffRenderer = new TextSlotDiffRenderer();
688 $slotDiffRenderer->setStatsdDataFactory( $statsdDataFactory );
689 // XXX using the page language would be better, but it's unclear how that should be injected
690 $slotDiffRenderer->setLanguage( $contentLanguage );
691 $slotDiffRenderer->setWikiDiff2MovedParagraphDetectionCutoff(
692 $context->getConfig()->get( 'WikiDiff2MovedParagraphDetectionCutoff' )
693 );
694
695 $engine = DifferenceEngine::getEngine();
696 if ( $engine === false ) {
697 $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_PHP );
698 } elseif ( $engine === 'wikidiff2' ) {
699 $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_WIKIDIFF2 );
700 } else {
701 $slotDiffRenderer->setEngine( TextSlotDiffRenderer::ENGINE_EXTERNAL, $engine );
702 }
703
704 return $slotDiffRenderer;
705 }
706
707 /**
708 * Get the language in which the content of the given page is written.
709 *
710 * This default implementation just returns the content language (except for pages
711 * in the MediaWiki namespace)
712 *
713 * Note that the pages language is not cacheable, since it may in some
714 * cases depend on user settings.
715 *
716 * Also note that the page language may or may not depend on the actual content of the page,
717 * that is, this method may load the content in order to determine the language.
718 *
719 * @since 1.21
720 *
721 * @param Title $title The page to determine the language for.
722 * @param Content|null $content The page's content, if you have it handy, to avoid reloading it.
723 *
724 * @return Language The page's language
725 */
726 public function getPageLanguage( Title $title, Content $content = null ) {
727 global $wgLang;
728 $pageLang = MediaWikiServices::getInstance()->getContentLanguage();
729
730 if ( $title->getNamespace() == NS_MEDIAWIKI ) {
731 // Parse mediawiki messages with correct target language
732 list( /* $unused */, $lang ) = MessageCache::singleton()->figureMessage( $title->getText() );
733 $pageLang = Language::factory( $lang );
734 }
735
736 Hooks::run( 'PageContentLanguage', [ $title, &$pageLang, $wgLang ] );
737
738 return wfGetLangObj( $pageLang );
739 }
740
741 /**
742 * Get the language in which the content of this page is written when
743 * viewed by user. Defaults to $this->getPageLanguage(), but if the user
744 * specified a preferred variant, the variant will be used.
745 *
746 * This default implementation just returns $this->getPageLanguage( $title, $content ) unless
747 * the user specified a preferred variant.
748 *
749 * Note that the pages view language is not cacheable, since it depends on user settings.
750 *
751 * Also note that the page language may or may not depend on the actual content of the page,
752 * that is, this method may load the content in order to determine the language.
753 *
754 * @since 1.21
755 *
756 * @param Title $title The page to determine the language for.
757 * @param Content|null $content The page's content, if you have it handy, to avoid reloading it.
758 *
759 * @return Language The page's language for viewing
760 */
761 public function getPageViewLanguage( Title $title, Content $content = null ) {
762 $pageLang = $this->getPageLanguage( $title, $content );
763
764 if ( $title->getNamespace() !== NS_MEDIAWIKI ) {
765 // If the user chooses a variant, the content is actually
766 // in a language whose code is the variant code.
767 $variant = $pageLang->getPreferredVariant();
768 if ( $pageLang->getCode() !== $variant ) {
769 $pageLang = Language::factory( $variant );
770 }
771 }
772
773 return $pageLang;
774 }
775
776 /**
777 * Determines whether the content type handled by this ContentHandler
778 * can be used on the given page.
779 *
780 * This default implementation always returns true.
781 * Subclasses may override this to restrict the use of this content model to specific locations,
782 * typically based on the namespace or some other aspect of the title, such as a special suffix
783 * (e.g. ".svg" for SVG content).
784 *
785 * @note this calls the ContentHandlerCanBeUsedOn hook which may be used to override which
786 * content model can be used where.
787 *
788 * @param Title $title The page's title.
789 *
790 * @return bool True if content of this kind can be used on the given page, false otherwise.
791 */
792 public function canBeUsedOn( Title $title ) {
793 $ok = true;
794
795 Hooks::run( 'ContentModelCanBeUsedOn', [ $this->getModelID(), $title, &$ok ] );
796
797 return $ok;
798 }
799
800 /**
801 * Returns the name of the diff engine to use.
802 *
803 * @since 1.21
804 *
805 * @return string
806 */
807 protected function getDiffEngineClass() {
808 return DifferenceEngine::class;
809 }
810
811 /**
812 * Attempts to merge differences between three versions. Returns a new
813 * Content object for a clean merge and false for failure or a conflict.
814 *
815 * This default implementation always returns false.
816 *
817 * @since 1.21
818 *
819 * @param Content $oldContent The page's previous content.
820 * @param Content $myContent One of the page's conflicting contents.
821 * @param Content $yourContent One of the page's conflicting contents.
822 *
823 * @return Content|bool Always false.
824 */
825 public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
826 return false;
827 }
828
829 /**
830 * Return type of change if one exists for the given edit.
831 *
832 * @since 1.31
833 *
834 * @param Content|null $oldContent The previous text of the page.
835 * @param Content|null $newContent The submitted text of the page.
836 * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
837 *
838 * @return string|null String key representing type of change, or null.
839 */
840 private function getChangeType(
841 Content $oldContent = null,
842 Content $newContent = null,
843 $flags = 0
844 ) {
845 $oldTarget = $oldContent !== null ? $oldContent->getRedirectTarget() : null;
846 $newTarget = $newContent !== null ? $newContent->getRedirectTarget() : null;
847
848 // We check for the type of change in the given edit, and return string key accordingly
849
850 // Blanking of a page
851 if ( $oldContent && $oldContent->getSize() > 0 &&
852 $newContent && $newContent->getSize() === 0
853 ) {
854 return 'blank';
855 }
856
857 // Redirects
858 if ( $newTarget ) {
859 if ( !$oldTarget ) {
860 // New redirect page (by creating new page or by changing content page)
861 return 'new-redirect';
862 } elseif ( !$newTarget->equals( $oldTarget ) ||
863 $oldTarget->getFragment() !== $newTarget->getFragment()
864 ) {
865 // Redirect target changed
866 return 'changed-redirect-target';
867 }
868 } elseif ( $oldTarget ) {
869 // Changing an existing redirect into a non-redirect
870 return 'removed-redirect';
871 }
872
873 // New page created
874 if ( $flags & EDIT_NEW && $newContent ) {
875 if ( $newContent->getSize() === 0 ) {
876 // New blank page
877 return 'newblank';
878 } else {
879 return 'newpage';
880 }
881 }
882
883 // Removing more than 90% of the page
884 if ( $oldContent && $newContent && $oldContent->getSize() > 10 * $newContent->getSize() ) {
885 return 'replace';
886 }
887
888 // Content model changed
889 if ( $oldContent && $newContent && $oldContent->getModel() !== $newContent->getModel() ) {
890 return 'contentmodelchange';
891 }
892
893 return null;
894 }
895
896 /**
897 * Return an applicable auto-summary if one exists for the given edit.
898 *
899 * @since 1.21
900 *
901 * @param Content|null $oldContent The previous text of the page.
902 * @param Content|null $newContent The submitted text of the page.
903 * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
904 *
905 * @return string An appropriate auto-summary, or an empty string.
906 */
907 public function getAutosummary(
908 Content $oldContent = null,
909 Content $newContent = null,
910 $flags = 0
911 ) {
912 $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
913
914 // There's no applicable auto-summary for our case, so our auto-summary is empty.
915 if ( !$changeType ) {
916 return '';
917 }
918
919 // Decide what kind of auto-summary is needed.
920 switch ( $changeType ) {
921 case 'new-redirect':
922 $newTarget = $newContent->getRedirectTarget();
923 $truncatedtext = $newContent->getTextForSummary(
924 250
925 - strlen( wfMessage( 'autoredircomment' )->inContentLanguage()->text() )
926 - strlen( $newTarget->getFullText() )
927 );
928
929 return wfMessage( 'autoredircomment', $newTarget->getFullText() )
930 ->plaintextParams( $truncatedtext )->inContentLanguage()->text();
931 case 'changed-redirect-target':
932 $oldTarget = $oldContent->getRedirectTarget();
933 $newTarget = $newContent->getRedirectTarget();
934
935 $truncatedtext = $newContent->getTextForSummary(
936 250
937 - strlen( wfMessage( 'autosumm-changed-redirect-target' )
938 ->inContentLanguage()->text() )
939 - strlen( $oldTarget->getFullText() )
940 - strlen( $newTarget->getFullText() )
941 );
942
943 return wfMessage( 'autosumm-changed-redirect-target',
944 $oldTarget->getFullText(),
945 $newTarget->getFullText() )
946 ->rawParams( $truncatedtext )->inContentLanguage()->text();
947 case 'removed-redirect':
948 $oldTarget = $oldContent->getRedirectTarget();
949 $truncatedtext = $newContent->getTextForSummary(
950 250
951 - strlen( wfMessage( 'autosumm-removed-redirect' )
952 ->inContentLanguage()->text() )
953 - strlen( $oldTarget->getFullText() ) );
954
955 return wfMessage( 'autosumm-removed-redirect', $oldTarget->getFullText() )
956 ->rawParams( $truncatedtext )->inContentLanguage()->text();
957 case 'newpage':
958 // If they're making a new article, give its text, truncated, in the summary.
959 $truncatedtext = $newContent->getTextForSummary(
960 200 - strlen( wfMessage( 'autosumm-new' )->inContentLanguage()->text() ) );
961
962 return wfMessage( 'autosumm-new' )->rawParams( $truncatedtext )
963 ->inContentLanguage()->text();
964 case 'blank':
965 return wfMessage( 'autosumm-blank' )->inContentLanguage()->text();
966 case 'replace':
967 $truncatedtext = $newContent->getTextForSummary(
968 200 - strlen( wfMessage( 'autosumm-replace' )->inContentLanguage()->text() ) );
969
970 return wfMessage( 'autosumm-replace' )->rawParams( $truncatedtext )
971 ->inContentLanguage()->text();
972 case 'newblank':
973 return wfMessage( 'autosumm-newblank' )->inContentLanguage()->text();
974 default:
975 return '';
976 }
977 }
978
979 /**
980 * Return an applicable tag if one exists for the given edit or return null.
981 *
982 * @since 1.31
983 *
984 * @param Content|null $oldContent The previous text of the page.
985 * @param Content|null $newContent The submitted text of the page.
986 * @param int $flags Bit mask: a bit mask of flags submitted for the edit.
987 *
988 * @return string|null An appropriate tag, or null.
989 */
990 public function getChangeTag(
991 Content $oldContent = null,
992 Content $newContent = null,
993 $flags = 0
994 ) {
995 $changeType = $this->getChangeType( $oldContent, $newContent, $flags );
996
997 // There's no applicable tag for this change.
998 if ( !$changeType ) {
999 return null;
1000 }
1001
1002 // Core tags use the same keys as ones returned from $this->getChangeType()
1003 // but prefixed with pseudo namespace 'mw-', so we add the prefix before checking
1004 // if this type of change should be tagged
1005 $tag = 'mw-' . $changeType;
1006
1007 // Not all change types are tagged, so we check against the list of defined tags.
1008 if ( in_array( $tag, ChangeTags::getSoftwareTags() ) ) {
1009 return $tag;
1010 }
1011
1012 return null;
1013 }
1014
1015 /**
1016 * Auto-generates a deletion reason
1017 *
1018 * @since 1.21
1019 *
1020 * @param Title $title The page's title
1021 * @param bool &$hasHistory Whether the page has a history
1022 *
1023 * @return mixed String containing deletion reason or empty string, or
1024 * boolean false if no revision occurred
1025 *
1026 * @todo &$hasHistory is extremely ugly, it's here because
1027 * WikiPage::getAutoDeleteReason() and Article::generateReason()
1028 * have it / want it.
1029 */
1030 public function getAutoDeleteReason( Title $title, &$hasHistory ) {
1031 $dbr = wfGetDB( DB_REPLICA );
1032
1033 // Get the last revision
1034 $rev = Revision::newFromTitle( $title );
1035
1036 if ( is_null( $rev ) ) {
1037 return false;
1038 }
1039
1040 // Get the article's contents
1041 $content = $rev->getContent();
1042 $blank = false;
1043
1044 // If the page is blank, use the text from the previous revision,
1045 // which can only be blank if there's a move/import/protect dummy
1046 // revision involved
1047 if ( !$content || $content->isEmpty() ) {
1048 $prev = $rev->getPrevious();
1049
1050 if ( $prev ) {
1051 $rev = $prev;
1052 $content = $rev->getContent();
1053 $blank = true;
1054 }
1055 }
1056
1057 $this->checkModelID( $rev->getContentModel() );
1058
1059 // Find out if there was only one contributor
1060 // Only scan the last 20 revisions
1061 $revQuery = Revision::getQueryInfo();
1062 $res = $dbr->select(
1063 $revQuery['tables'],
1064 [ 'rev_user_text' => $revQuery['fields']['rev_user_text'] ],
1065 [
1066 'rev_page' => $title->getArticleID(),
1067 $dbr->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0'
1068 ],
1069 __METHOD__,
1070 [ 'LIMIT' => 20 ],
1071 $revQuery['joins']
1072 );
1073
1074 if ( $res === false ) {
1075 // This page has no revisions, which is very weird
1076 return false;
1077 }
1078
1079 $hasHistory = ( $res->numRows() > 1 );
1080 $row = $dbr->fetchObject( $res );
1081
1082 if ( $row ) { // $row is false if the only contributor is hidden
1083 $onlyAuthor = $row->rev_user_text;
1084 // Try to find a second contributor
1085 foreach ( $res as $row ) {
1086 if ( $row->rev_user_text != $onlyAuthor ) { // T24999
1087 $onlyAuthor = false;
1088 break;
1089 }
1090 }
1091 } else {
1092 $onlyAuthor = false;
1093 }
1094
1095 // Generate the summary with a '$1' placeholder
1096 if ( $blank ) {
1097 // The current revision is blank and the one before is also
1098 // blank. It's just not our lucky day
1099 $reason = wfMessage( 'exbeforeblank', '$1' )->inContentLanguage()->text();
1100 } else {
1101 if ( $onlyAuthor ) {
1102 $reason = wfMessage(
1103 'excontentauthor',
1104 '$1',
1105 $onlyAuthor
1106 )->inContentLanguage()->text();
1107 } else {
1108 $reason = wfMessage( 'excontent', '$1' )->inContentLanguage()->text();
1109 }
1110 }
1111
1112 if ( $reason == '-' ) {
1113 // Allow these UI messages to be blanked out cleanly
1114 return '';
1115 }
1116
1117 // Max content length = max comment length - length of the comment (excl. $1)
1118 $text = $content ? $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) ) : '';
1119
1120 // Now replace the '$1' placeholder
1121 $reason = str_replace( '$1', $text, $reason );
1122
1123 return $reason;
1124 }
1125
1126 /**
1127 * Get the Content object that needs to be saved in order to undo all revisions
1128 * between $undo and $undoafter. Revisions must belong to the same page,
1129 * must exist and must not be deleted.
1130 *
1131 * @since 1.21
1132 *
1133 * @param Revision $current The current text
1134 * @param Revision $undo The revision to undo
1135 * @param Revision $undoafter Must be an earlier revision than $undo
1136 *
1137 * @return mixed Content on success, false on failure
1138 */
1139 public function getUndoContent( Revision $current, Revision $undo, Revision $undoafter ) {
1140 $cur_content = $current->getContent();
1141
1142 if ( empty( $cur_content ) ) {
1143 return false; // no page
1144 }
1145
1146 $undo_content = $undo->getContent();
1147 $undoafter_content = $undoafter->getContent();
1148
1149 if ( !$undo_content || !$undoafter_content ) {
1150 return false; // no content to undo
1151 }
1152
1153 try {
1154 $this->checkModelID( $cur_content->getModel() );
1155 $this->checkModelID( $undo_content->getModel() );
1156 if ( $current->getId() !== $undo->getId() ) {
1157 // If we are undoing the most recent revision,
1158 // its ok to revert content model changes. However
1159 // if we are undoing a revision in the middle, then
1160 // doing that will be confusing.
1161 $this->checkModelID( $undoafter_content->getModel() );
1162 }
1163 } catch ( MWException $e ) {
1164 // If the revisions have different content models
1165 // just return false
1166 return false;
1167 }
1168
1169 if ( $cur_content->equals( $undo_content ) ) {
1170 // No use doing a merge if it's just a straight revert.
1171 return $undoafter_content;
1172 }
1173
1174 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
1175
1176 return $undone_content;
1177 }
1178
1179 /**
1180 * Get parser options suitable for rendering and caching the article
1181 *
1182 * @deprecated since 1.32, use WikiPage::makeParserOptions() or
1183 * ParserOptions::newCanonical() instead.
1184 * @param IContextSource|User|string $context One of the following:
1185 * - IContextSource: Use the User and the Language of the provided
1186 * context
1187 * - User: Use the provided User object and $wgLang for the language,
1188 * so use an IContextSource object if possible.
1189 * - 'canonical': Canonical options (anonymous user with default
1190 * preferences and content language).
1191 *
1192 * @throws MWException
1193 * @return ParserOptions
1194 */
1195 public function makeParserOptions( $context ) {
1196 wfDeprecated( __METHOD__, '1.32' );
1197 return ParserOptions::newCanonical( $context );
1198 }
1199
1200 /**
1201 * Returns true for content models that support caching using the
1202 * ParserCache mechanism. See WikiPage::shouldCheckParserCache().
1203 *
1204 * @since 1.21
1205 *
1206 * @return bool Always false.
1207 */
1208 public function isParserCacheSupported() {
1209 return false;
1210 }
1211
1212 /**
1213 * Returns true if this content model supports sections.
1214 * This default implementation returns false.
1215 *
1216 * Content models that return true here should also implement
1217 * Content::getSection, Content::replaceSection, etc. to handle sections..
1218 *
1219 * @return bool Always false.
1220 */
1221 public function supportsSections() {
1222 return false;
1223 }
1224
1225 /**
1226 * Returns true if this content model supports categories.
1227 * The default implementation returns true.
1228 *
1229 * @return bool Always true.
1230 */
1231 public function supportsCategories() {
1232 return true;
1233 }
1234
1235 /**
1236 * Returns true if this content model supports redirects.
1237 * This default implementation returns false.
1238 *
1239 * Content models that return true here should also implement
1240 * ContentHandler::makeRedirectContent to return a Content object.
1241 *
1242 * @return bool Always false.
1243 */
1244 public function supportsRedirects() {
1245 return false;
1246 }
1247
1248 /**
1249 * Return true if this content model supports direct editing, such as via EditPage.
1250 *
1251 * @return bool Default is false, and true for TextContent and it's derivatives.
1252 */
1253 public function supportsDirectEditing() {
1254 return false;
1255 }
1256
1257 /**
1258 * Whether or not this content model supports direct editing via ApiEditPage
1259 *
1260 * @return bool Default is false, and true for TextContent and derivatives.
1261 */
1262 public function supportsDirectApiEditing() {
1263 return $this->supportsDirectEditing();
1264 }
1265
1266 /**
1267 * Get fields definition for search index
1268 *
1269 * @todo Expose title, redirect, namespace, text, source_text, text_bytes
1270 * field mappings here. (see T142670 and T143409)
1271 *
1272 * @param SearchEngine $engine
1273 * @return SearchIndexField[] List of fields this content handler can provide.
1274 * @since 1.28
1275 */
1276 public function getFieldsForSearchIndex( SearchEngine $engine ) {
1277 $fields['category'] = $engine->makeSearchFieldMapping(
1278 'category',
1279 SearchIndexField::INDEX_TYPE_TEXT
1280 );
1281 $fields['category']->setFlag( SearchIndexField::FLAG_CASEFOLD );
1282
1283 $fields['external_link'] = $engine->makeSearchFieldMapping(
1284 'external_link',
1285 SearchIndexField::INDEX_TYPE_KEYWORD
1286 );
1287
1288 $fields['outgoing_link'] = $engine->makeSearchFieldMapping(
1289 'outgoing_link',
1290 SearchIndexField::INDEX_TYPE_KEYWORD
1291 );
1292
1293 $fields['template'] = $engine->makeSearchFieldMapping(
1294 'template',
1295 SearchIndexField::INDEX_TYPE_KEYWORD
1296 );
1297 $fields['template']->setFlag( SearchIndexField::FLAG_CASEFOLD );
1298
1299 $fields['content_model'] = $engine->makeSearchFieldMapping(
1300 'content_model',
1301 SearchIndexField::INDEX_TYPE_KEYWORD
1302 );
1303
1304 return $fields;
1305 }
1306
1307 /**
1308 * Add new field definition to array.
1309 * @param SearchIndexField[] &$fields
1310 * @param SearchEngine $engine
1311 * @param string $name
1312 * @param int $type
1313 * @return SearchIndexField[] new field defs
1314 * @since 1.28
1315 */
1316 protected function addSearchField( &$fields, SearchEngine $engine, $name, $type ) {
1317 $fields[$name] = $engine->makeSearchFieldMapping( $name, $type );
1318 return $fields;
1319 }
1320
1321 /**
1322 * Return fields to be indexed by search engine
1323 * as representation of this document.
1324 * Overriding class should call parent function or take care of calling
1325 * the SearchDataForIndex hook.
1326 * @param WikiPage $page Page to index
1327 * @param ParserOutput $output
1328 * @param SearchEngine $engine Search engine for which we are indexing
1329 * @return array Map of name=>value for fields
1330 * @since 1.28
1331 */
1332 public function getDataForSearchIndex(
1333 WikiPage $page,
1334 ParserOutput $output,
1335 SearchEngine $engine
1336 ) {
1337 $fieldData = [];
1338 $content = $page->getContent();
1339
1340 if ( $content ) {
1341 $searchDataExtractor = new ParserOutputSearchDataExtractor();
1342
1343 $fieldData['category'] = $searchDataExtractor->getCategories( $output );
1344 $fieldData['external_link'] = $searchDataExtractor->getExternalLinks( $output );
1345 $fieldData['outgoing_link'] = $searchDataExtractor->getOutgoingLinks( $output );
1346 $fieldData['template'] = $searchDataExtractor->getTemplates( $output );
1347
1348 $text = $content->getTextForSearchIndex();
1349
1350 $fieldData['text'] = $text;
1351 $fieldData['source_text'] = $text;
1352 $fieldData['text_bytes'] = $content->getSize();
1353 $fieldData['content_model'] = $content->getModel();
1354 }
1355
1356 Hooks::run( 'SearchDataForIndex', [ &$fieldData, $this, $page, $output, $engine ] );
1357 return $fieldData;
1358 }
1359
1360 /**
1361 * Produce page output suitable for indexing.
1362 *
1363 * Specific content handlers may override it if they need different content handling.
1364 *
1365 * @param WikiPage $page
1366 * @param ParserCache|null $cache
1367 * @return ParserOutput
1368 */
1369 public function getParserOutputForIndexing( WikiPage $page, ParserCache $cache = null ) {
1370 $parserOptions = $page->makeParserOptions( 'canonical' );
1371 $revId = $page->getRevision()->getId();
1372 if ( $cache ) {
1373 $parserOutput = $cache->get( $page, $parserOptions );
1374 }
1375 if ( empty( $parserOutput ) ) {
1376 $parserOutput =
1377 $page->getContent()->getParserOutput( $page->getTitle(), $revId, $parserOptions );
1378 if ( $cache ) {
1379 $cache->save( $parserOutput, $page, $parserOptions );
1380 }
1381 }
1382 return $parserOutput;
1383 }
1384
1385 }