Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like ContentHandler often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use ContentHandler, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 84 | abstract class ContentHandler { |
||
| 85 | /** |
||
| 86 | * Switch for enabling deprecation warnings. Used by ContentHandler::deprecated() |
||
| 87 | * and ContentHandler::runLegacyHooks(). |
||
| 88 | * |
||
| 89 | * Once the ContentHandler code has settled in a bit, this should be set to true to |
||
| 90 | * make extensions etc. show warnings when using deprecated functions and hooks. |
||
| 91 | */ |
||
| 92 | protected static $enableDeprecationWarnings = false; |
||
| 93 | |||
| 94 | /** |
||
| 95 | * Convenience function for getting flat text from a Content object. This |
||
| 96 | * should only be used in the context of backwards compatibility with code |
||
| 97 | * that is not yet able to handle Content objects! |
||
| 98 | * |
||
| 99 | * If $content is null, this method returns the empty string. |
||
| 100 | * |
||
| 101 | * If $content is an instance of TextContent, this method returns the flat |
||
| 102 | * text as returned by $content->getNativeData(). |
||
| 103 | * |
||
| 104 | * If $content is not a TextContent object, the behavior of this method |
||
| 105 | * depends on the global $wgContentHandlerTextFallback: |
||
| 106 | * - If $wgContentHandlerTextFallback is 'fail' and $content is not a |
||
| 107 | * TextContent object, an MWException is thrown. |
||
| 108 | * - If $wgContentHandlerTextFallback is 'serialize' and $content is not a |
||
| 109 | * TextContent object, $content->serialize() is called to get a string |
||
| 110 | * form of the content. |
||
| 111 | * - If $wgContentHandlerTextFallback is 'ignore' and $content is not a |
||
| 112 | * TextContent object, this method returns null. |
||
| 113 | * - otherwise, the behavior is undefined. |
||
| 114 | * |
||
| 115 | * @since 1.21 |
||
| 116 | * |
||
| 117 | * @param Content $content |
||
| 118 | * |
||
| 119 | * @throws MWException If the content is not an instance of TextContent and |
||
| 120 | * wgContentHandlerTextFallback was set to 'fail'. |
||
| 121 | * @return string|null Textual form of the content, if available. |
||
| 122 | */ |
||
| 123 | public static function getContentText( Content $content = null ) { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Convenience function for creating a Content object from a given textual |
||
| 152 | * representation. |
||
| 153 | * |
||
| 154 | * $text will be deserialized into a Content object of the model specified |
||
| 155 | * by $modelId (or, if that is not given, $title->getContentModel()) using |
||
| 156 | * the given format. |
||
| 157 | * |
||
| 158 | * @since 1.21 |
||
| 159 | * |
||
| 160 | * @param string $text The textual representation, will be |
||
| 161 | * unserialized to create the Content object |
||
| 162 | * @param Title $title The title of the page this text belongs to. |
||
| 163 | * Required if $modelId is not provided. |
||
| 164 | * @param string $modelId The model to deserialize to. If not provided, |
||
| 165 | * $title->getContentModel() is used. |
||
| 166 | * @param string $format The format to use for deserialization. If not |
||
| 167 | * given, the model's default format is used. |
||
| 168 | * |
||
| 169 | * @throws MWException If model ID or format is not supported or if the text can not be |
||
| 170 | * unserialized using the format. |
||
| 171 | * @return Content A Content object representing the text. |
||
| 172 | */ |
||
| 173 | public static function makeContent( $text, Title $title = null, |
||
| 187 | |||
| 188 | /** |
||
| 189 | * Returns the name of the default content model to be used for the page |
||
| 190 | * with the given title. |
||
| 191 | * |
||
| 192 | * Note: There should rarely be need to call this method directly. |
||
| 193 | * To determine the actual content model for a given page, use |
||
| 194 | * Title::getContentModel(). |
||
| 195 | * |
||
| 196 | * Which model is to be used by default for the page is determined based |
||
| 197 | * on several factors: |
||
| 198 | * - The global setting $wgNamespaceContentModels specifies a content model |
||
| 199 | * per namespace. |
||
| 200 | * - The hook ContentHandlerDefaultModelFor may be used to override the page's default |
||
| 201 | * model. |
||
| 202 | * - Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript |
||
| 203 | * model if they end in .js or .css, respectively. |
||
| 204 | * - Pages in NS_MEDIAWIKI default to the wikitext model otherwise. |
||
| 205 | * - The hook TitleIsCssOrJsPage may be used to force a page to use the CSS |
||
| 206 | * or JavaScript model. This is a compatibility feature. The ContentHandlerDefaultModelFor |
||
| 207 | * hook should be used instead if possible. |
||
| 208 | * - The hook TitleIsWikitextPage may be used to force a page to use the |
||
| 209 | * wikitext model. This is a compatibility feature. The ContentHandlerDefaultModelFor |
||
| 210 | * hook should be used instead if possible. |
||
| 211 | * |
||
| 212 | * If none of the above applies, the wikitext model is used. |
||
| 213 | * |
||
| 214 | * Note: this is used by, and may thus not use, Title::getContentModel() |
||
| 215 | * |
||
| 216 | * @since 1.21 |
||
| 217 | * |
||
| 218 | * @param Title $title |
||
| 219 | * |
||
| 220 | * @return string Default model name for the page given by $title |
||
| 221 | */ |
||
| 222 | public static function getDefaultModelFor( Title $title ) { |
||
| 280 | |||
| 281 | /** |
||
| 282 | * Returns the appropriate ContentHandler singleton for the given title. |
||
| 283 | * |
||
| 284 | * @since 1.21 |
||
| 285 | * |
||
| 286 | * @param Title $title |
||
| 287 | * |
||
| 288 | * @return ContentHandler |
||
| 289 | */ |
||
| 290 | public static function getForTitle( Title $title ) { |
||
| 295 | |||
| 296 | /** |
||
| 297 | * Returns the appropriate ContentHandler singleton for the given Content |
||
| 298 | * object. |
||
| 299 | * |
||
| 300 | * @since 1.21 |
||
| 301 | * |
||
| 302 | * @param Content $content |
||
| 303 | * |
||
| 304 | * @return ContentHandler |
||
| 305 | */ |
||
| 306 | public static function getForContent( Content $content ) { |
||
| 311 | |||
| 312 | /** |
||
| 313 | * @var array A Cache of ContentHandler instances by model id |
||
| 314 | */ |
||
| 315 | protected static $handlers; |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns the ContentHandler singleton for the given model ID. Use the |
||
| 319 | * CONTENT_MODEL_XXX constants to identify the desired content model. |
||
| 320 | * |
||
| 321 | * ContentHandler singletons are taken from the global $wgContentHandlers |
||
| 322 | * array. Keys in that array are model names, the values are either |
||
| 323 | * ContentHandler singleton objects, or strings specifying the appropriate |
||
| 324 | * subclass of ContentHandler. |
||
| 325 | * |
||
| 326 | * If a class name is encountered when looking up the singleton for a given |
||
| 327 | * model name, the class is instantiated and the class name is replaced by |
||
| 328 | * the resulting singleton in $wgContentHandlers. |
||
| 329 | * |
||
| 330 | * If no ContentHandler is defined for the desired $modelId, the |
||
| 331 | * ContentHandler may be provided by the ContentHandlerForModelID hook. |
||
| 332 | * If no ContentHandler can be determined, an MWException is raised. |
||
| 333 | * |
||
| 334 | * @since 1.21 |
||
| 335 | * |
||
| 336 | * @param string $modelId The ID of the content model for which to get a |
||
| 337 | * handler. Use CONTENT_MODEL_XXX constants. |
||
| 338 | * |
||
| 339 | * @throws MWException For internal errors and problems in the configuration. |
||
| 340 | * @throws MWUnknownContentModelException If no handler is known for the model ID. |
||
| 341 | * @return ContentHandler The ContentHandler singleton for handling the model given by the ID. |
||
| 342 | */ |
||
| 343 | public static function getForModelID( $modelId ) { |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Returns the localized name for a given content model. |
||
| 387 | * |
||
| 388 | * Model names are localized using system messages. Message keys |
||
| 389 | * have the form content-model-$name, where $name is getContentModelName( $id ). |
||
| 390 | * |
||
| 391 | * @param string $name The content model ID, as given by a CONTENT_MODEL_XXX |
||
| 392 | * constant or returned by Revision::getContentModel(). |
||
| 393 | * @param Language|null $lang The language to parse the message in (since 1.26) |
||
| 394 | * |
||
| 395 | * @throws MWException If the model ID isn't known. |
||
| 396 | * @return string The content model's localized name. |
||
| 397 | */ |
||
| 398 | public static function getLocalizedName( $name, Language $lang = null ) { |
||
| 410 | |||
| 411 | public static function getContentModels() { |
||
| 416 | |||
| 417 | public static function getAllContentFormats() { |
||
| 431 | |||
| 432 | // ------------------------------------------------------------------------ |
||
| 433 | |||
| 434 | /** |
||
| 435 | * @var string |
||
| 436 | */ |
||
| 437 | protected $mModelID; |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @var string[] |
||
| 441 | */ |
||
| 442 | protected $mSupportedFormats; |
||
| 443 | |||
| 444 | /** |
||
| 445 | * Constructor, initializing the ContentHandler instance with its model ID |
||
| 446 | * and a list of supported formats. Values for the parameters are typically |
||
| 447 | * provided as literals by subclass's constructors. |
||
| 448 | * |
||
| 449 | * @param string $modelId (use CONTENT_MODEL_XXX constants). |
||
| 450 | * @param string[] $formats List for supported serialization formats |
||
| 451 | * (typically as MIME types) |
||
| 452 | */ |
||
| 453 | public function __construct( $modelId, $formats ) { |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Serializes a Content object of the type supported by this ContentHandler. |
||
| 464 | * |
||
| 465 | * @since 1.21 |
||
| 466 | * |
||
| 467 | * @param Content $content The Content object to serialize |
||
| 468 | * @param string $format The desired serialization format |
||
| 469 | * |
||
| 470 | * @return string Serialized form of the content |
||
| 471 | */ |
||
| 472 | abstract public function serializeContent( Content $content, $format = null ); |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Applies transformations on export (returns the blob unchanged per default). |
||
| 476 | * Subclasses may override this to perform transformations such as conversion |
||
| 477 | * of legacy formats or filtering of internal meta-data. |
||
| 478 | * |
||
| 479 | * @param string $blob The blob to be exported |
||
| 480 | * @param string|null $format The blob's serialization format |
||
| 481 | * |
||
| 482 | * @return string |
||
| 483 | */ |
||
| 484 | public function exportTransform( $blob, $format = null ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Unserializes a Content object of the type supported by this ContentHandler. |
||
| 490 | * |
||
| 491 | * @since 1.21 |
||
| 492 | * |
||
| 493 | * @param string $blob Serialized form of the content |
||
| 494 | * @param string $format The format used for serialization |
||
| 495 | * |
||
| 496 | * @return Content The Content object created by deserializing $blob |
||
| 497 | */ |
||
| 498 | abstract public function unserializeContent( $blob, $format = null ); |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Apply import transformation (per default, returns $blob unchanged). |
||
| 502 | * This gives subclasses an opportunity to transform data blobs on import. |
||
| 503 | * |
||
| 504 | * @since 1.24 |
||
| 505 | * |
||
| 506 | * @param string $blob |
||
| 507 | * @param string|null $format |
||
| 508 | * |
||
| 509 | * @return string |
||
| 510 | */ |
||
| 511 | public function importTransform( $blob, $format = null ) { |
||
| 514 | |||
| 515 | /** |
||
| 516 | * Creates an empty Content object of the type supported by this |
||
| 517 | * ContentHandler. |
||
| 518 | * |
||
| 519 | * @since 1.21 |
||
| 520 | * |
||
| 521 | * @return Content |
||
| 522 | */ |
||
| 523 | abstract public function makeEmptyContent(); |
||
| 524 | |||
| 525 | /** |
||
| 526 | * Creates a new Content object that acts as a redirect to the given page, |
||
| 527 | * or null if redirects are not supported by this content model. |
||
| 528 | * |
||
| 529 | * This default implementation always returns null. Subclasses supporting redirects |
||
| 530 | * must override this method. |
||
| 531 | * |
||
| 532 | * Note that subclasses that override this method to return a Content object |
||
| 533 | * should also override supportsRedirects() to return true. |
||
| 534 | * |
||
| 535 | * @since 1.21 |
||
| 536 | * |
||
| 537 | * @param Title $destination The page to redirect to. |
||
| 538 | * @param string $text Text to include in the redirect, if possible. |
||
| 539 | * |
||
| 540 | * @return Content Always null. |
||
| 541 | */ |
||
| 542 | public function makeRedirectContent( Title $destination, $text = '' ) { |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Returns the model id that identifies the content model this |
||
| 548 | * ContentHandler can handle. Use with the CONTENT_MODEL_XXX constants. |
||
| 549 | * |
||
| 550 | * @since 1.21 |
||
| 551 | * |
||
| 552 | * @return string The model ID |
||
| 553 | */ |
||
| 554 | public function getModelID() { |
||
| 557 | |||
| 558 | /** |
||
| 559 | * @since 1.21 |
||
| 560 | * |
||
| 561 | * @param string $model_id The model to check |
||
| 562 | * |
||
| 563 | * @throws MWException If the model ID is not the ID of the content model supported by this |
||
| 564 | * ContentHandler. |
||
| 565 | */ |
||
| 566 | protected function checkModelID( $model_id ) { |
||
| 573 | |||
| 574 | /** |
||
| 575 | * Returns a list of serialization formats supported by the |
||
| 576 | * serializeContent() and unserializeContent() methods of this |
||
| 577 | * ContentHandler. |
||
| 578 | * |
||
| 579 | * @since 1.21 |
||
| 580 | * |
||
| 581 | * @return string[] List of serialization formats as MIME type like strings |
||
| 582 | */ |
||
| 583 | public function getSupportedFormats() { |
||
| 586 | |||
| 587 | /** |
||
| 588 | * The format used for serialization/deserialization by default by this |
||
| 589 | * ContentHandler. |
||
| 590 | * |
||
| 591 | * This default implementation will return the first element of the array |
||
| 592 | * of formats that was passed to the constructor. |
||
| 593 | * |
||
| 594 | * @since 1.21 |
||
| 595 | * |
||
| 596 | * @return string The name of the default serialization format as a MIME type |
||
| 597 | */ |
||
| 598 | public function getDefaultFormat() { |
||
| 601 | |||
| 602 | /** |
||
| 603 | * Returns true if $format is a serialization format supported by this |
||
| 604 | * ContentHandler, and false otherwise. |
||
| 605 | * |
||
| 606 | * Note that if $format is null, this method always returns true, because |
||
| 607 | * null means "use the default format". |
||
| 608 | * |
||
| 609 | * @since 1.21 |
||
| 610 | * |
||
| 611 | * @param string $format The serialization format to check |
||
| 612 | * |
||
| 613 | * @return bool |
||
| 614 | */ |
||
| 615 | public function isSupportedFormat( $format ) { |
||
| 622 | |||
| 623 | /** |
||
| 624 | * Convenient for checking whether a format provided as a parameter is actually supported. |
||
| 625 | * |
||
| 626 | * @param string $format The serialization format to check |
||
| 627 | * |
||
| 628 | * @throws MWException If the format is not supported by this content handler. |
||
| 629 | */ |
||
| 630 | protected function checkFormat( $format ) { |
||
| 638 | |||
| 639 | /** |
||
| 640 | * Returns overrides for action handlers. |
||
| 641 | * Classes listed here will be used instead of the default one when |
||
| 642 | * (and only when) $wgActions[$action] === true. This allows subclasses |
||
| 643 | * to override the default action handlers. |
||
| 644 | * |
||
| 645 | * @since 1.21 |
||
| 646 | * |
||
| 647 | * @return array An array mapping action names (typically "view", "edit", "history" etc.) to |
||
| 648 | * either the full qualified class name of an Action class, a callable taking ( Page $page, |
||
| 649 | * IContextSource $context = null ) as parameters and returning an Action object, or an actual |
||
| 650 | * Action object. An empty array in this default implementation. |
||
| 651 | * |
||
| 652 | * @see Action::factory |
||
| 653 | */ |
||
| 654 | public function getActionOverrides() { |
||
| 657 | |||
| 658 | /** |
||
| 659 | * Factory for creating an appropriate DifferenceEngine for this content model. |
||
| 660 | * |
||
| 661 | * @since 1.21 |
||
| 662 | * |
||
| 663 | * @param IContextSource $context Context to use, anything else will be ignored. |
||
| 664 | * @param int $old Revision ID we want to show and diff with. |
||
| 665 | * @param int|string $new Either a revision ID or one of the strings 'cur', 'prev' or 'next'. |
||
| 666 | * @param int $rcid FIXME: Deprecated, no longer used. Defaults to 0. |
||
| 667 | * @param bool $refreshCache If set, refreshes the diff cache. Defaults to false. |
||
| 668 | * @param bool $unhide If set, allow viewing deleted revs. Defaults to false. |
||
| 669 | * |
||
| 670 | * @return DifferenceEngine |
||
| 671 | */ |
||
| 672 | public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0, |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Get the language in which the content of the given page is written. |
||
| 689 | * |
||
| 690 | * This default implementation just returns $wgContLang (except for pages |
||
| 691 | * in the MediaWiki namespace) |
||
| 692 | * |
||
| 693 | * Note that the pages language is not cacheable, since it may in some |
||
| 694 | * cases depend on user settings. |
||
| 695 | * |
||
| 696 | * Also note that the page language may or may not depend on the actual content of the page, |
||
| 697 | * that is, this method may load the content in order to determine the language. |
||
| 698 | * |
||
| 699 | * @since 1.21 |
||
| 700 | * |
||
| 701 | * @param Title $title The page to determine the language for. |
||
| 702 | * @param Content $content The page's content, if you have it handy, to avoid reloading it. |
||
| 703 | * |
||
| 704 | * @return Language The page's language |
||
| 705 | */ |
||
| 706 | public function getPageLanguage( Title $title, Content $content = null ) { |
||
| 720 | |||
| 721 | /** |
||
| 722 | * Get the language in which the content of this page is written when |
||
| 723 | * viewed by user. Defaults to $this->getPageLanguage(), but if the user |
||
| 724 | * specified a preferred variant, the variant will be used. |
||
| 725 | * |
||
| 726 | * This default implementation just returns $this->getPageLanguage( $title, $content ) unless |
||
| 727 | * the user specified a preferred variant. |
||
| 728 | * |
||
| 729 | * Note that the pages view language is not cacheable, since it depends on user settings. |
||
| 730 | * |
||
| 731 | * Also note that the page language may or may not depend on the actual content of the page, |
||
| 732 | * that is, this method may load the content in order to determine the language. |
||
| 733 | * |
||
| 734 | * @since 1.21 |
||
| 735 | * |
||
| 736 | * @param Title $title The page to determine the language for. |
||
| 737 | * @param Content $content The page's content, if you have it handy, to avoid reloading it. |
||
| 738 | * |
||
| 739 | * @return Language The page's language for viewing |
||
| 740 | */ |
||
| 741 | public function getPageViewLanguage( Title $title, Content $content = null ) { |
||
| 755 | |||
| 756 | /** |
||
| 757 | * Determines whether the content type handled by this ContentHandler |
||
| 758 | * can be used on the given page. |
||
| 759 | * |
||
| 760 | * This default implementation always returns true. |
||
| 761 | * Subclasses may override this to restrict the use of this content model to specific locations, |
||
| 762 | * typically based on the namespace or some other aspect of the title, such as a special suffix |
||
| 763 | * (e.g. ".svg" for SVG content). |
||
| 764 | * |
||
| 765 | * @note this calls the ContentHandlerCanBeUsedOn hook which may be used to override which |
||
| 766 | * content model can be used where. |
||
| 767 | * |
||
| 768 | * @param Title $title The page's title. |
||
| 769 | * |
||
| 770 | * @return bool True if content of this kind can be used on the given page, false otherwise. |
||
| 771 | */ |
||
| 772 | public function canBeUsedOn( Title $title ) { |
||
| 779 | |||
| 780 | /** |
||
| 781 | * Returns the name of the diff engine to use. |
||
| 782 | * |
||
| 783 | * @since 1.21 |
||
| 784 | * |
||
| 785 | * @return string |
||
| 786 | */ |
||
| 787 | protected function getDiffEngineClass() { |
||
| 790 | |||
| 791 | /** |
||
| 792 | * Attempts to merge differences between three versions. Returns a new |
||
| 793 | * Content object for a clean merge and false for failure or a conflict. |
||
| 794 | * |
||
| 795 | * This default implementation always returns false. |
||
| 796 | * |
||
| 797 | * @since 1.21 |
||
| 798 | * |
||
| 799 | * @param Content $oldContent The page's previous content. |
||
| 800 | * @param Content $myContent One of the page's conflicting contents. |
||
| 801 | * @param Content $yourContent One of the page's conflicting contents. |
||
| 802 | * |
||
| 803 | * @return Content|bool Always false. |
||
| 804 | */ |
||
| 805 | public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) { |
||
| 808 | |||
| 809 | /** |
||
| 810 | * Return an applicable auto-summary if one exists for the given edit. |
||
| 811 | * |
||
| 812 | * @since 1.21 |
||
| 813 | * |
||
| 814 | * @param Content $oldContent The previous text of the page. |
||
| 815 | * @param Content $newContent The submitted text of the page. |
||
| 816 | * @param int $flags Bit mask: a bit mask of flags submitted for the edit. |
||
| 817 | * |
||
| 818 | * @return string An appropriate auto-summary, or an empty string. |
||
| 819 | */ |
||
| 820 | public function getAutosummary( Content $oldContent = null, Content $newContent = null, |
||
| 886 | |||
| 887 | /** |
||
| 888 | * Auto-generates a deletion reason |
||
| 889 | * |
||
| 890 | * @since 1.21 |
||
| 891 | * |
||
| 892 | * @param Title $title The page's title |
||
| 893 | * @param bool &$hasHistory Whether the page has a history |
||
| 894 | * |
||
| 895 | * @return mixed String containing deletion reason or empty string, or |
||
| 896 | * boolean false if no revision occurred |
||
| 897 | * |
||
| 898 | * @todo &$hasHistory is extremely ugly, it's here because |
||
| 899 | * WikiPage::getAutoDeleteReason() and Article::generateReason() |
||
| 900 | * have it / want it. |
||
| 901 | */ |
||
| 902 | public function getAutoDeleteReason( Title $title, &$hasHistory ) { |
||
| 993 | |||
| 994 | /** |
||
| 995 | * Get the Content object that needs to be saved in order to undo all revisions |
||
| 996 | * between $undo and $undoafter. Revisions must belong to the same page, |
||
| 997 | * must exist and must not be deleted. |
||
| 998 | * |
||
| 999 | * @since 1.21 |
||
| 1000 | * |
||
| 1001 | * @param Revision $current The current text |
||
| 1002 | * @param Revision $undo The revision to undo |
||
| 1003 | * @param Revision $undoafter Must be an earlier revision than $undo |
||
| 1004 | * |
||
| 1005 | * @return mixed String on success, false on failure |
||
| 1006 | */ |
||
| 1007 | public function getUndoContent( Revision $current, Revision $undo, Revision $undoafter ) { |
||
| 1034 | |||
| 1035 | /** |
||
| 1036 | * Get parser options suitable for rendering and caching the article |
||
| 1037 | * |
||
| 1038 | * @param IContextSource|User|string $context One of the following: |
||
| 1039 | * - IContextSource: Use the User and the Language of the provided |
||
| 1040 | * context |
||
| 1041 | * - User: Use the provided User object and $wgLang for the language, |
||
| 1042 | * so use an IContextSource object if possible. |
||
| 1043 | * - 'canonical': Canonical options (anonymous user with default |
||
| 1044 | * preferences and content language). |
||
| 1045 | * |
||
| 1046 | * @throws MWException |
||
| 1047 | * @return ParserOptions |
||
| 1048 | */ |
||
| 1049 | public function makeParserOptions( $context ) { |
||
| 1067 | |||
| 1068 | /** |
||
| 1069 | * Returns true for content models that support caching using the |
||
| 1070 | * ParserCache mechanism. See WikiPage::shouldCheckParserCache(). |
||
| 1071 | * |
||
| 1072 | * @since 1.21 |
||
| 1073 | * |
||
| 1074 | * @return bool Always false. |
||
| 1075 | */ |
||
| 1076 | public function isParserCacheSupported() { |
||
| 1079 | |||
| 1080 | /** |
||
| 1081 | * Returns true if this content model supports sections. |
||
| 1082 | * This default implementation returns false. |
||
| 1083 | * |
||
| 1084 | * Content models that return true here should also implement |
||
| 1085 | * Content::getSection, Content::replaceSection, etc. to handle sections.. |
||
| 1086 | * |
||
| 1087 | * @return bool Always false. |
||
| 1088 | */ |
||
| 1089 | public function supportsSections() { |
||
| 1092 | |||
| 1093 | /** |
||
| 1094 | * Returns true if this content model supports categories. |
||
| 1095 | * The default implementation returns true. |
||
| 1096 | * |
||
| 1097 | * @return bool Always true. |
||
| 1098 | */ |
||
| 1099 | public function supportsCategories() { |
||
| 1102 | |||
| 1103 | /** |
||
| 1104 | * Returns true if this content model supports redirects. |
||
| 1105 | * This default implementation returns false. |
||
| 1106 | * |
||
| 1107 | * Content models that return true here should also implement |
||
| 1108 | * ContentHandler::makeRedirectContent to return a Content object. |
||
| 1109 | * |
||
| 1110 | * @return bool Always false. |
||
| 1111 | */ |
||
| 1112 | public function supportsRedirects() { |
||
| 1115 | |||
| 1116 | /** |
||
| 1117 | * Return true if this content model supports direct editing, such as via EditPage. |
||
| 1118 | * |
||
| 1119 | * @return bool Default is false, and true for TextContent and it's derivatives. |
||
| 1120 | */ |
||
| 1121 | public function supportsDirectEditing() { |
||
| 1124 | |||
| 1125 | /** |
||
| 1126 | * Whether or not this content model supports direct editing via ApiEditPage |
||
| 1127 | * |
||
| 1128 | * @return bool Default is false, and true for TextContent and derivatives. |
||
| 1129 | */ |
||
| 1130 | public function supportsDirectApiEditing() { |
||
| 1133 | |||
| 1134 | /** |
||
| 1135 | * Logs a deprecation warning, visible if $wgDevelopmentWarnings, but only if |
||
| 1136 | * self::$enableDeprecationWarnings is set to true. |
||
| 1137 | * |
||
| 1138 | * @param string $func The name of the deprecated function |
||
| 1139 | * @param string $version The version since the method is deprecated. Usually 1.21 |
||
| 1140 | * for ContentHandler related stuff. |
||
| 1141 | * @param string|bool $component : Component to which the function belongs. |
||
| 1142 | * If false, it is assumed the function is in MediaWiki core. |
||
| 1143 | * |
||
| 1144 | * @see ContentHandler::$enableDeprecationWarnings |
||
| 1145 | * @see wfDeprecated |
||
| 1146 | */ |
||
| 1147 | public static function deprecated( $func, $version, $component = false ) { |
||
| 1152 | |||
| 1153 | /** |
||
| 1154 | * Call a legacy hook that uses text instead of Content objects. |
||
| 1155 | * Will log a warning when a matching hook function is registered. |
||
| 1156 | * If the textual representation of the content is changed by the |
||
| 1157 | * hook function, a new Content object is constructed from the new |
||
| 1158 | * text. |
||
| 1159 | * |
||
| 1160 | * @param string $event Event name |
||
| 1161 | * @param array $args Parameters passed to hook functions |
||
| 1162 | * @param string|null $deprecatedVersion Emit a deprecation notice |
||
| 1163 | * when the hook is run for the provided version |
||
| 1164 | * |
||
| 1165 | * @return bool True if no handler aborted the hook |
||
| 1166 | */ |
||
| 1167 | public static function runLegacyHooks( $event, $args = [], |
||
| 1211 | |||
| 1212 | /** |
||
| 1213 | * Get fields definition for search index |
||
| 1214 | * |
||
| 1215 | * @todo Expose title, redirect, namespace, text, source_text, text_bytes |
||
| 1216 | * field mappings here. (see T142670 and T143409) |
||
| 1217 | * |
||
| 1218 | * @param SearchEngine $engine |
||
| 1219 | * @return SearchIndexField[] List of fields this content handler can provide. |
||
| 1220 | * @since 1.28 |
||
| 1221 | */ |
||
| 1222 | public function getFieldsForSearchIndex( SearchEngine $engine ) { |
||
| 1249 | |||
| 1250 | /** |
||
| 1251 | * Add new field definition to array. |
||
| 1252 | * @param SearchIndexField[] $fields |
||
| 1253 | * @param SearchEngine $engine |
||
| 1254 | * @param string $name |
||
| 1255 | * @param int $type |
||
| 1256 | * @return SearchIndexField[] new field defs |
||
| 1257 | * @since 1.28 |
||
| 1258 | */ |
||
| 1259 | protected function addSearchField( &$fields, SearchEngine $engine, $name, $type ) { |
||
| 1263 | |||
| 1264 | /** |
||
| 1265 | * Return fields to be indexed by search engine |
||
| 1266 | * as representation of this document. |
||
| 1267 | * Overriding class should call parent function or take care of calling |
||
| 1268 | * the SearchDataForIndex hook. |
||
| 1269 | * @param WikiPage $page Page to index |
||
| 1270 | * @param ParserOutput $output |
||
| 1271 | * @param SearchEngine $engine Search engine for which we are indexing |
||
| 1272 | * @return array Map of name=>value for fields |
||
| 1273 | * @since 1.28 |
||
| 1274 | */ |
||
| 1275 | public function getDataForSearchIndex( WikiPage $page, ParserOutput $output, |
||
| 1298 | |||
| 1299 | /** |
||
| 1300 | * Produce page output suitable for indexing. |
||
| 1301 | * |
||
| 1302 | * Specific content handlers may override it if they need different content handling. |
||
| 1303 | * |
||
| 1304 | * @param WikiPage $page |
||
| 1305 | * @param ParserCache $cache |
||
| 1306 | * @return ParserOutput |
||
| 1307 | */ |
||
| 1308 | public function getParserOutputForIndexing( WikiPage $page, ParserCache $cache = null ) { |
||
| 1323 | |||
| 1324 | } |
||
| 1325 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: