4 * Exception representing a failure to serialize or unserialize a content object.
6 class MWContentSerializationException
extends MWException
{
11 * A content handler knows how do deal with a specific type of content on a wiki
12 * page. Content is stored in the database in a serialized form (using a
13 * serialization format a.k.a. MIME type) and is unserialized into its native
14 * PHP representation (the content model), which is wrapped in an instance of
15 * the appropriate subclass of Content.
17 * ContentHandler instances are stateless singletons that serve, among other
18 * things, as a factory for Content objects. Generally, there is one subclass
19 * of ContentHandler and one subclass of Content for every type of content model.
21 * Some content types have a flat model, that is, their native representation
22 * is the same as their serialized form. Examples would be JavaScript and CSS
23 * code. As of now, this also applies to wikitext (MediaWiki's default content
24 * type), but wikitext content may be represented by a DOM or AST structure in
27 * This program is free software; you can redistribute it and/or modify
28 * it under the terms of the GNU General Public License as published by
29 * the Free Software Foundation; either version 2 of the License, or
30 * (at your option) any later version.
32 * This program is distributed in the hope that it will be useful,
33 * but WITHOUT ANY WARRANTY; without even the implied warranty of
34 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
35 * GNU General Public License for more details.
37 * You should have received a copy of the GNU General Public License along
38 * with this program; if not, write to the Free Software Foundation, Inc.,
39 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
40 * http://www.gnu.org/copyleft/gpl.html
47 * @author Daniel Kinzler
49 abstract class ContentHandler
{
52 * Switch for enabling deprecation warnings. Used by ContentHandler::deprecated()
53 * and ContentHandler::runLegacyHooks().
55 * Once the ContentHandler code has settled in a bit, this should be set to true to
56 * make extensions etc. show warnings when using deprecated functions and hooks.
58 protected static $enableDeprecationWarnings = false;
61 * Convenience function for getting flat text from a Content object. This
62 * should only be used in the context of backwards compatibility with code
63 * that is not yet able to handle Content objects!
65 * If $content is null, this method returns the empty string.
67 * If $content is an instance of TextContent, this method returns the flat
68 * text as returned by $content->getNativeData().
70 * If $content is not a TextContent object, the behavior of this method
71 * depends on the global $wgContentHandlerTextFallback:
72 * - If $wgContentHandlerTextFallback is 'fail' and $content is not a
73 * TextContent object, an MWException is thrown.
74 * - If $wgContentHandlerTextFallback is 'serialize' and $content is not a
75 * TextContent object, $content->serialize() is called to get a string
76 * form of the content.
77 * - If $wgContentHandlerTextFallback is 'ignore' and $content is not a
78 * TextContent object, this method returns null.
79 * - otherwise, the behaviour is undefined.
83 * @param $content Content|null
84 * @return null|string the textual form of $content, if available
85 * @throws MWException if $content is not an instance of TextContent and
86 * $wgContentHandlerTextFallback was set to 'fail'.
88 public static function getContentText( Content
$content = null ) {
89 global $wgContentHandlerTextFallback;
91 if ( is_null( $content ) ) {
95 if ( $content instanceof TextContent
) {
96 return $content->getNativeData();
99 wfDebugLog( 'ContentHandler', 'Accessing ' . $content->getModel() . ' content as text!' );
101 if ( $wgContentHandlerTextFallback == 'fail' ) {
102 throw new MWException(
103 "Attempt to get text from Content with model " .
108 if ( $wgContentHandlerTextFallback == 'serialize' ) {
109 return $content->serialize();
116 * Convenience function for creating a Content object from a given textual
119 * $text will be deserialized into a Content object of the model specified
120 * by $modelId (or, if that is not given, $title->getContentModel()) using
125 * @param $text string the textual representation, will be
126 * unserialized to create the Content object
127 * @param $title null|Title the title of the page this text belongs to.
128 * Required if $modelId is not provided.
129 * @param $modelId null|string the model to deserialize to. If not provided,
130 * $title->getContentModel() is used.
131 * @param $format null|string the format to use for deserialization. If not
132 * given, the model's default format is used.
134 * @throws MWException
135 * @return Content a Content object representing $text
137 * @throw MWException if $model or $format is not supported or if $text can
138 * not be unserialized using $format.
140 public static function makeContent( $text, Title
$title = null,
141 $modelId = null, $format = null )
143 if ( is_null( $modelId ) ) {
144 if ( is_null( $title ) ) {
145 throw new MWException( "Must provide a Title object or a content model ID." );
148 $modelId = $title->getContentModel();
151 $handler = ContentHandler
::getForModelID( $modelId );
152 return $handler->unserializeContent( $text, $format );
156 * Returns the name of the default content model to be used for the page
157 * with the given title.
159 * Note: There should rarely be need to call this method directly.
160 * To determine the actual content model for a given page, use
161 * Title::getContentModel().
163 * Which model is to be used by default for the page is determined based
164 * on several factors:
165 * - The global setting $wgNamespaceContentModels specifies a content model
167 * - The hook ContentHandlerDefaultModelFor may be used to override the page's default
169 * - Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript
170 * model if they end in .js or .css, respectively.
171 * - Pages in NS_MEDIAWIKI default to the wikitext model otherwise.
172 * - The hook TitleIsCssOrJsPage may be used to force a page to use the CSS
173 * or JavaScript model. This is a compatibility feature. The ContentHandlerDefaultModelFor
174 * hook should be used instead if possible.
175 * - The hook TitleIsWikitextPage may be used to force a page to use the
176 * wikitext model. This is a compatibility feature. The ContentHandlerDefaultModelFor
177 * hook should be used instead if possible.
179 * If none of the above applies, the wikitext model is used.
181 * Note: this is used by, and may thus not use, Title::getContentModel()
185 * @param $title Title
186 * @return null|string default model name for the page given by $title
188 public static function getDefaultModelFor( Title
$title ) {
189 global $wgNamespaceContentModels;
191 // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
192 // because it is used to initialize the mContentModel member.
194 $ns = $title->getNamespace();
200 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
201 $model = $wgNamespaceContentModels[ $ns ];
204 // Hook can determine default model
205 if ( !wfRunHooks( 'ContentHandlerDefaultModelFor', array( $title, &$model ) ) ) {
206 if ( !is_null( $model ) ) {
211 // Could this page contain custom CSS or JavaScript, based on the title?
212 $isCssOrJsPage = NS_MEDIAWIKI
== $ns && preg_match( '!\.(css|js)$!u', $title->getText(), $m );
213 if ( $isCssOrJsPage ) {
217 // Hook can force JS/CSS
218 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage ) );
220 // Is this a .css subpage of a user page?
221 $isJsCssSubpage = NS_USER
== $ns
223 && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m );
224 if ( $isJsCssSubpage ) {
228 // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
229 $isWikitext = is_null( $model ) ||
$model == CONTENT_MODEL_WIKITEXT
;
230 $isWikitext = $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage;
232 // Hook can override $isWikitext
233 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
235 if ( !$isWikitext ) {
238 return CONTENT_MODEL_JAVASCRIPT
;
240 return CONTENT_MODEL_CSS
;
242 return is_null( $model ) ? CONTENT_MODEL_TEXT
: $model;
246 // We established that it must be wikitext
248 return CONTENT_MODEL_WIKITEXT
;
252 * Returns the appropriate ContentHandler singleton for the given title.
256 * @param $title Title
257 * @return ContentHandler
259 public static function getForTitle( Title
$title ) {
260 $modelId = $title->getContentModel();
261 return ContentHandler
::getForModelID( $modelId );
265 * Returns the appropriate ContentHandler singleton for the given Content
270 * @param $content Content
271 * @return ContentHandler
273 public static function getForContent( Content
$content ) {
274 $modelId = $content->getModel();
275 return ContentHandler
::getForModelID( $modelId );
279 * @var Array A Cache of ContentHandler instances by model id
284 * Returns the ContentHandler singleton for the given model ID. Use the
285 * CONTENT_MODEL_XXX constants to identify the desired content model.
287 * ContentHandler singletons are taken from the global $wgContentHandlers
288 * array. Keys in that array are model names, the values are either
289 * ContentHandler singleton objects, or strings specifying the appropriate
290 * subclass of ContentHandler.
292 * If a class name is encountered when looking up the singleton for a given
293 * model name, the class is instantiated and the class name is replaced by
294 * the resulting singleton in $wgContentHandlers.
296 * If no ContentHandler is defined for the desired $modelId, the
297 * ContentHandler may be provided by the ContentHandlerForModelID hook.
298 * If no ContentHandler can be determined, an MWException is raised.
302 * @param $modelId String The ID of the content model for which to get a
303 * handler. Use CONTENT_MODEL_XXX constants.
304 * @return ContentHandler The ContentHandler singleton for handling the
305 * model given by $modelId
306 * @throws MWException if no handler is known for $modelId.
308 public static function getForModelID( $modelId ) {
309 global $wgContentHandlers;
311 if ( isset( ContentHandler
::$handlers[$modelId] ) ) {
312 return ContentHandler
::$handlers[$modelId];
315 if ( empty( $wgContentHandlers[$modelId] ) ) {
318 wfRunHooks( 'ContentHandlerForModelID', array( $modelId, &$handler ) );
320 if ( $handler === null ) {
321 throw new MWException( "No handler for model '$modelId'' registered in \$wgContentHandlers" );
324 if ( !( $handler instanceof ContentHandler
) ) {
325 throw new MWException( "ContentHandlerForModelID must supply a ContentHandler instance" );
328 $class = $wgContentHandlers[$modelId];
329 $handler = new $class( $modelId );
331 if ( !( $handler instanceof ContentHandler
) ) {
332 throw new MWException( "$class from \$wgContentHandlers is not compatible with ContentHandler" );
336 wfDebugLog( 'ContentHandler', 'Created handler for ' . $modelId
337 . ': ' . get_class( $handler ) );
339 ContentHandler
::$handlers[$modelId] = $handler;
340 return ContentHandler
::$handlers[$modelId];
344 * Returns the localized name for a given content model.
346 * Model names are localized using system messages. Message keys
347 * have the form content-model-$name, where $name is getContentModelName( $id ).
349 * @param $name String The content model ID, as given by a CONTENT_MODEL_XXX
350 * constant or returned by Revision::getContentModel().
352 * @return string The content format's localized name.
353 * @throws MWException if the model id isn't known.
355 public static function getLocalizedName( $name ) {
356 $key = "content-model-$name";
358 $msg = wfMessage( $key );
360 return $msg->exists() ?
$msg->plain() : $name;
363 public static function getContentModels() {
364 global $wgContentHandlers;
366 return array_keys( $wgContentHandlers );
369 public static function getAllContentFormats() {
370 global $wgContentHandlers;
374 foreach ( $wgContentHandlers as $model => $class ) {
375 $handler = ContentHandler
::getForModelID( $model );
376 $formats = array_merge( $formats, $handler->getSupportedFormats() );
379 $formats = array_unique( $formats );
383 // ------------------------------------------------------------------------
386 protected $mSupportedFormats;
389 * Constructor, initializing the ContentHandler instance with its model ID
390 * and a list of supported formats. Values for the parameters are typically
391 * provided as literals by subclass's constructors.
393 * @param $modelId String (use CONTENT_MODEL_XXX constants).
394 * @param $formats array List for supported serialization formats
395 * (typically as MIME types)
397 public function __construct( $modelId, $formats ) {
398 $this->mModelID
= $modelId;
399 $this->mSupportedFormats
= $formats;
401 $this->mModelName
= preg_replace( '/(Content)?Handler$/', '', get_class( $this ) );
402 $this->mModelName
= preg_replace( '/[_\\\\]/', '', $this->mModelName
);
403 $this->mModelName
= strtolower( $this->mModelName
);
407 * Serializes a Content object of the type supported by this ContentHandler.
411 * @param $content Content The Content object to serialize
412 * @param $format null|String The desired serialization format
413 * @return string Serialized form of the content
415 public abstract function serializeContent( Content
$content, $format = null );
418 * Unserializes a Content object of the type supported by this ContentHandler.
422 * @param $blob string serialized form of the content
423 * @param $format null|String the format used for serialization
424 * @return Content the Content object created by deserializing $blob
426 public abstract function unserializeContent( $blob, $format = null );
429 * Creates an empty Content object of the type supported by this
436 public abstract function makeEmptyContent();
439 * Creates a new Content object that acts as a redirect to the given page,
440 * or null of redirects are not supported by this content model.
442 * This default implementation always returns null. Subclasses supporting redirects
443 * must override this method.
445 * Note that subclasses that override this method to return a Content object
446 * should also override supportsRedirects() to return true.
450 * @param Title $destination the page to redirect to.
454 public function makeRedirectContent( Title
$destination ) {
459 * Returns the model id that identifies the content model this
460 * ContentHandler can handle. Use with the CONTENT_MODEL_XXX constants.
464 * @return String The model ID
466 public function getModelID() {
467 return $this->mModelID
;
471 * Throws an MWException if $model_id is not the ID of the content model
472 * supported by this ContentHandler.
476 * @param String $model_id The model to check
478 * @throws MWException
480 protected function checkModelID( $model_id ) {
481 if ( $model_id !== $this->mModelID
) {
482 throw new MWException( "Bad content model: " .
483 "expected {$this->mModelID} " .
484 "but got $model_id." );
489 * Returns a list of serialization formats supported by the
490 * serializeContent() and unserializeContent() methods of this
495 * @return array of serialization formats as MIME type like strings
497 public function getSupportedFormats() {
498 return $this->mSupportedFormats
;
502 * The format used for serialization/deserialization by default by this
505 * This default implementation will return the first element of the array
506 * of formats that was passed to the constructor.
510 * @return string the name of the default serialization format as a MIME type
512 public function getDefaultFormat() {
513 return $this->mSupportedFormats
[0];
517 * Returns true if $format is a serialization format supported by this
518 * ContentHandler, and false otherwise.
520 * Note that if $format is null, this method always returns true, because
521 * null means "use the default format".
525 * @param $format string the serialization format to check
528 public function isSupportedFormat( $format ) {
531 return true; // this means "use the default"
534 return in_array( $format, $this->mSupportedFormats
);
538 * Throws an MWException if isSupportedFormat( $format ) is not true.
539 * Convenient for checking whether a format provided as a parameter is
540 * actually supported.
542 * @param $format string the serialization format to check
544 * @throws MWException
546 protected function checkFormat( $format ) {
547 if ( !$this->isSupportedFormat( $format ) ) {
548 throw new MWException(
549 "Format $format is not supported for content model "
550 . $this->getModelID()
556 * Returns overrides for action handlers.
557 * Classes listed here will be used instead of the default one when
558 * (and only when) $wgActions[$action] === true. This allows subclasses
559 * to override the default action handlers.
565 public function getActionOverrides() {
570 * Factory for creating an appropriate DifferenceEngine for this content model.
574 * @param $context IContextSource context to use, anything else will be
576 * @param $old Integer Old ID we want to show and diff with.
577 * @param $new int|string String either 'prev' or 'next'.
578 * @param $rcid Integer ??? FIXME (default 0)
579 * @param $refreshCache boolean If set, refreshes the diff cache
580 * @param $unhide boolean If set, allow viewing deleted revs
582 * @return DifferenceEngine
584 public function createDifferenceEngine( IContextSource
$context,
586 $rcid = 0, # FIXME: use everywhere!
587 $refreshCache = false, $unhide = false
589 $diffEngineClass = $this->getDiffEngineClass();
591 return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
595 * Get the language in which the content of the given page is written.
597 * This default implementation just returns $wgContLang (except for pages in the MediaWiki namespace)
599 * Note that the pages language is not cacheable, since it may in some cases depend on user settings.
601 * Also note that the page language may or may not depend on the actual content of the page,
602 * that is, this method may load the content in order to determine the language.
606 * @param Title $title the page to determine the language for.
607 * @param Content|null $content the page's content, if you have it handy, to avoid reloading it.
609 * @return Language the page's language
611 public function getPageLanguage( Title
$title, Content
$content = null ) {
612 global $wgContLang, $wgLang;
613 $pageLang = $wgContLang;
615 if ( $title->getNamespace() == NS_MEDIAWIKI
) {
616 // Parse mediawiki messages with correct target language
617 list( /* $unused */, $lang ) = MessageCache
::singleton()->figureMessage( $title->getText() );
618 $pageLang = wfGetLangObj( $lang );
621 wfRunHooks( 'PageContentLanguage', array( $title, &$pageLang, $wgLang ) );
622 return wfGetLangObj( $pageLang );
626 * Get the language in which the content of this page is written when
627 * viewed by user. Defaults to $this->getPageLanguage(), but if the user
628 * specified a preferred variant, the variant will be used.
630 * This default implementation just returns $this->getPageLanguage( $title, $content ) unless
631 * the user specified a preferred variant.
633 * Note that the pages view language is not cacheable, since it depends on user settings.
635 * Also note that the page language may or may not depend on the actual content of the page,
636 * that is, this method may load the content in order to determine the language.
640 * @param Title $title the page to determine the language for.
641 * @param Content|null $content the page's content, if you have it handy, to avoid reloading it.
643 * @return Language the page's language for viewing
645 public function getPageViewLanguage( Title
$title, Content
$content = null ) {
646 $pageLang = $this->getPageLanguage( $title, $content );
648 if ( $title->getNamespace() !== NS_MEDIAWIKI
) {
649 // If the user chooses a variant, the content is actually
650 // in a language whose code is the variant code.
651 $variant = $pageLang->getPreferredVariant();
652 if ( $pageLang->getCode() !== $variant ) {
653 $pageLang = Language
::factory( $variant );
661 * Determines whether the content type handled by this ContentHandler
662 * can be used on the given page.
664 * This default implementation always returns true.
665 * Subclasses may override this to restrict the use of this content model to specific locations,
666 * typically based on the namespace or some other aspect of the title, such as a special suffix
667 * (e.g. ".svg" for SVG content).
669 * @param Title $title the page's title.
671 * @return bool true if content of this kind can be used on the given page, false otherwise.
673 public function canBeUsedOn( Title
$title ) {
678 * Returns the name of the diff engine to use.
684 protected function getDiffEngineClass() {
685 return 'DifferenceEngine';
689 * Attempts to merge differences between three versions.
690 * Returns a new Content object for a clean merge and false for failure or
693 * This default implementation always returns false.
697 * @param $oldContent Content|string String
698 * @param $myContent Content|string String
699 * @param $yourContent Content|string String
701 * @return Content|Bool
703 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
708 * Return an applicable auto-summary if one exists for the given edit.
712 * @param $oldContent Content|null: the previous text of the page.
713 * @param $newContent Content|null: The submitted text of the page.
714 * @param $flags int Bit mask: a bit mask of flags submitted for the edit.
716 * @return string An appropriate auto-summary, or an empty string.
718 public function getAutosummary( Content
$oldContent = null, Content
$newContent = null, $flags ) {
719 // Decide what kind of auto-summary is needed.
721 // Redirect auto-summaries
728 $ot = !is_null( $oldContent ) ?
$oldContent->getRedirectTarget() : null;
729 $rt = !is_null( $newContent ) ?
$newContent->getRedirectTarget() : null;
731 if ( is_object( $rt ) ) {
732 if ( !is_object( $ot )
733 ||
!$rt->equals( $ot )
734 ||
$ot->getFragment() != $rt->getFragment() )
736 $truncatedtext = $newContent->getTextForSummary(
738 - strlen( wfMessage( 'autoredircomment' )->inContentLanguage()->text() )
739 - strlen( $rt->getFullText() ) );
741 return wfMessage( 'autoredircomment', $rt->getFullText() )
742 ->rawParams( $truncatedtext )->inContentLanguage()->text();
746 // New page auto-summaries
747 if ( $flags & EDIT_NEW
&& $newContent->getSize() > 0 ) {
748 // If they're making a new article, give its text, truncated, in
751 $truncatedtext = $newContent->getTextForSummary(
752 200 - strlen( wfMessage( 'autosumm-new' )->inContentLanguage()->text() ) );
754 return wfMessage( 'autosumm-new' )->rawParams( $truncatedtext )
755 ->inContentLanguage()->text();
758 // Blanking auto-summaries
759 if ( !empty( $oldContent ) && $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
760 return wfMessage( 'autosumm-blank' )->inContentLanguage()->text();
761 } elseif ( !empty( $oldContent )
762 && $oldContent->getSize() > 10 * $newContent->getSize()
763 && $newContent->getSize() < 500 )
765 // Removing more than 90% of the article
767 $truncatedtext = $newContent->getTextForSummary(
768 200 - strlen( wfMessage( 'autosumm-replace' )->inContentLanguage()->text() ) );
770 return wfMessage( 'autosumm-replace' )->rawParams( $truncatedtext )
771 ->inContentLanguage()->text();
774 // If we reach this point, there's no applicable auto-summary for our
775 // case, so our auto-summary is empty.
780 * Auto-generates a deletion reason
784 * @param $title Title: the page's title
785 * @param &$hasHistory Boolean: whether the page has a history
786 * @return mixed String containing deletion reason or empty string, or
787 * boolean false if no revision occurred
789 * @XXX &$hasHistory is extremely ugly, it's here because
790 * WikiPage::getAutoDeleteReason() and Article::getReason()
793 public function getAutoDeleteReason( Title
$title, &$hasHistory ) {
794 $dbw = wfGetDB( DB_MASTER
);
796 // Get the last revision
797 $rev = Revision
::newFromTitle( $title );
799 if ( is_null( $rev ) ) {
803 // Get the article's contents
804 $content = $rev->getContent();
807 // If the page is blank, use the text from the previous revision,
808 // which can only be blank if there's a move/import/protect dummy
810 if ( !$content ||
$content->isEmpty() ) {
811 $prev = $rev->getPrevious();
815 $content = $rev->getContent();
820 $this->checkModelID( $rev->getContentModel() );
822 // Find out if there was only one contributor
823 // Only scan the last 20 revisions
824 $res = $dbw->select( 'revision', 'rev_user_text',
826 'rev_page' => $title->getArticleID(),
827 $dbw->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0'
830 array( 'LIMIT' => 20 )
833 if ( $res === false ) {
834 // This page has no revisions, which is very weird
838 $hasHistory = ( $res->numRows() > 1 );
839 $row = $dbw->fetchObject( $res );
841 if ( $row ) { // $row is false if the only contributor is hidden
842 $onlyAuthor = $row->rev_user_text
;
843 // Try to find a second contributor
844 foreach ( $res as $row ) {
845 if ( $row->rev_user_text
!= $onlyAuthor ) { // Bug 22999
854 // Generate the summary with a '$1' placeholder
856 // The current revision is blank and the one before is also
857 // blank. It's just not our lucky day
858 $reason = wfMessage( 'exbeforeblank', '$1' )->inContentLanguage()->text();
865 )->inContentLanguage()->text();
867 $reason = wfMessage( 'excontent', '$1' )->inContentLanguage()->text();
871 if ( $reason == '-' ) {
872 // Allow these UI messages to be blanked out cleanly
876 // Max content length = max comment length - length of the comment (excl. $1)
877 $text = $content ?
$content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) ) : '';
879 // Now replace the '$1' placeholder
880 $reason = str_replace( '$1', $text, $reason );
886 * Get the Content object that needs to be saved in order to undo all revisions
887 * between $undo and $undoafter. Revisions must belong to the same page,
888 * must exist and must not be deleted.
892 * @param $current Revision The current text
893 * @param $undo Revision The revision to undo
894 * @param $undoafter Revision Must be an earlier revision than $undo
896 * @return mixed String on success, false on failure
898 public function getUndoContent( Revision
$current, Revision
$undo, Revision
$undoafter ) {
899 $cur_content = $current->getContent();
901 if ( empty( $cur_content ) ) {
902 return false; // no page
905 $undo_content = $undo->getContent();
906 $undoafter_content = $undoafter->getContent();
908 $this->checkModelID( $cur_content->getModel() );
909 $this->checkModelID( $undo_content->getModel() );
910 $this->checkModelID( $undoafter_content->getModel() );
912 if ( $cur_content->equals( $undo_content ) ) {
913 // No use doing a merge if it's just a straight revert.
914 return $undoafter_content;
917 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
919 return $undone_content;
923 * Get parser options suitable for rendering the primary article wikitext
925 * @param IContextSource|User|string $context One of the following:
926 * - IContextSource: Use the User and the Language of the provided
928 * - User: Use the provided User object and $wgLang for the language,
929 * so use an IContextSource object if possible.
930 * - 'canonical': Canonical options (anonymous user with default
931 * preferences and content language).
933 * @param IContextSource|User|string $context
935 * @throws MWException
936 * @return ParserOptions
938 public function makeParserOptions( $context ) {
941 if ( $context instanceof IContextSource
) {
942 $options = ParserOptions
::newFromContext( $context );
943 } elseif ( $context instanceof User
) { // settings per user (even anons)
944 $options = ParserOptions
::newFromUser( $context );
945 } elseif ( $context === 'canonical' ) { // canonical settings
946 $options = ParserOptions
::newFromUserAndLang( new User
, $wgContLang );
948 throw new MWException( "Bad context for parser options: $context" );
951 $options->enableLimitReport(); // show inclusion/loop reports
952 $options->setTidy( true ); // fix bad HTML
958 * Returns true for content models that support caching using the
959 * ParserCache mechanism. See WikiPage::isParserCacheUsed().
965 public function isParserCacheSupported() {
970 * Returns true if this content model supports sections.
971 * This default implementation returns false.
973 * Content models that return true here should also implement
974 * Content::getSection, Content::replaceSection, etc. to handle sections..
976 * @return boolean whether sections are supported.
978 public function supportsSections() {
983 * Returns true if this content model supports redirects.
984 * This default implementation returns false.
986 * Content models that return true here should also implement
987 * ContentHandler::makeRedirectContent to return a Content object.
989 * @return boolean whether redirects are supported.
991 public function supportsRedirects() {
996 * Logs a deprecation warning, visible if $wgDevelopmentWarnings, but only if
997 * self::$enableDeprecationWarnings is set to true.
999 * @param String $func The name of the deprecated function
1000 * @param string $version The version since the method is deprecated. Usually 1.21
1001 * for ContentHandler related stuff.
1002 * @param String|bool $component: Component to which the function belongs.
1003 * If false, it is assumed the function is in MediaWiki core.
1005 * @see ContentHandler::$enableDeprecationWarnings
1008 public static function deprecated( $func, $version, $component = false ) {
1009 if ( self
::$enableDeprecationWarnings ) {
1010 wfDeprecated( $func, $version, $component, 3 );
1015 * Call a legacy hook that uses text instead of Content objects.
1016 * Will log a warning when a matching hook function is registered.
1017 * If the textual representation of the content is changed by the
1018 * hook function, a new Content object is constructed from the new
1021 * @param $event String: event name
1022 * @param $args Array: parameters passed to hook functions
1023 * @param $warn bool: whether to log a warning.
1024 * Default to self::$enableDeprecationWarnings.
1025 * May be set to false for testing.
1027 * @return Boolean True if no handler aborted the hook
1029 * @see ContentHandler::$enableDeprecationWarnings
1031 public static function runLegacyHooks( $event, $args = array(),
1034 if ( $warn === null ) {
1035 $warn = self
::$enableDeprecationWarnings;
1038 if ( !Hooks
::isRegistered( $event ) ) {
1039 return true; // nothing to do here
1043 // Log information about which handlers are registered for the legacy hook,
1044 // so we can find and fix them.
1046 $handlers = Hooks
::getHandlers( $event );
1047 $handlerInfo = array();
1049 wfSuppressWarnings();
1051 foreach ( $handlers as $handler ) {
1054 if ( is_array( $handler ) ) {
1055 if ( is_object( $handler[0] ) ) {
1056 $info = get_class( $handler[0] );
1058 $info = $handler[0];
1061 if ( isset( $handler[1] ) ) {
1062 $info .= '::' . $handler[1];
1064 } else if ( is_object( $handler ) ) {
1065 $info = get_class( $handler[0] );
1066 $info .= '::on' . $event;
1071 $handlerInfo[] = $info;
1074 wfRestoreWarnings();
1076 wfWarn( "Using obsolete hook $event via ContentHandler::runLegacyHooks()! Handlers: " . implode(', ', $handlerInfo), 2 );
1079 // convert Content objects to text
1080 $contentObjects = array();
1081 $contentTexts = array();
1083 foreach ( $args as $k => $v ) {
1084 if ( $v instanceof Content
) {
1085 /* @var Content $v */
1087 $contentObjects[$k] = $v;
1089 $v = $v->serialize();
1090 $contentTexts[ $k ] = $v;
1095 // call the hook functions
1096 $ok = wfRunHooks( $event, $args );
1098 // see if the hook changed the text
1099 foreach ( $contentTexts as $k => $orig ) {
1100 /* @var Content $content */
1102 $modified = $args[ $k ];
1103 $content = $contentObjects[$k];
1105 if ( $modified !== $orig ) {
1106 // text was changed, create updated Content object
1107 $content = $content->getContentHandler()->unserializeContent( $modified );
1110 $args[ $k ] = $content;