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