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
29 abstract class ContentHandler
{
32 * Convenience function for getting flat text from a Content object. This
33 * should only be used in the context of backwards compatibility with code
34 * that is not yet able to handle Content objects!
36 * If $content is null, this method returns the empty string.
38 * If $content is an instance of TextContent, this method returns the flat
39 * text as returned by $content->getNativeData().
41 * If $content is not a TextContent object, the behavior of this method
42 * depends on the global $wgContentHandlerTextFallback:
43 * - If $wgContentHandlerTextFallback is 'fail' and $content is not a
44 * TextContent object, an MWException is thrown.
45 * - If $wgContentHandlerTextFallback is 'serialize' and $content is not a
46 * TextContent object, $content->serialize() is called to get a string
47 * form of the content.
48 * - If $wgContentHandlerTextFallback is 'ignore' and $content is not a
49 * TextContent object, this method returns null.
50 * - otherwise, the behaviour is undefined.
55 * @param $content Content|null
56 * @return null|string the textual form of $content, if available
57 * @throws MWException if $content is not an instance of TextContent and
58 * $wgContentHandlerTextFallback was set to 'fail'.
60 public static function getContentText( Content
$content = null ) {
61 global $wgContentHandlerTextFallback;
63 if ( is_null( $content ) ) {
67 if ( $content instanceof TextContent
) {
68 return $content->getNativeData();
71 if ( $wgContentHandlerTextFallback == 'fail' ) {
72 throw new MWException(
73 "Attempt to get text from Content with model " .
78 if ( $wgContentHandlerTextFallback == 'serialize' ) {
79 return $content->serialize();
86 * Convenience function for creating a Content object from a given textual
89 * $text will be deserialized into a Content object of the model specified
90 * by $modelId (or, if that is not given, $title->getContentModel()) using
97 * @param $text string the textual representation, will be
98 * unserialized to create the Content object
99 * @param $title null|Title the title of the page this text belongs to.
100 * Required if $modelId is not provided.
101 * @param $modelId null|string the model to deserialize to. If not provided,
102 * $title->getContentModel() is used.
103 * @param $format null|string the format to use for deserialization. If not
104 * given, the model's default format is used.
106 * @return Content a Content object representing $text
108 * @throw MWException if $model or $format is not supported or if $text can
109 * not be unserialized using $format.
111 public static function makeContent( $text, Title
$title = null,
112 $modelId = null, $format = null )
114 if ( is_null( $modelId ) ) {
115 if ( is_null( $title ) ) {
116 throw new MWException( "Must provide a Title object or a content model ID." );
119 $modelId = $title->getContentModel();
122 $handler = ContentHandler
::getForModelID( $modelId );
123 return $handler->unserializeContent( $text, $format );
127 * Returns the name of the default content model to be used for the page
128 * with the given title.
130 * Note: There should rarely be need to call this method directly.
131 * To determine the actual content model for a given page, use
132 * Title::getContentModel().
134 * Which model is to be used by default for the page is determined based
135 * on several factors:
136 * - The global setting $wgNamespaceContentModels specifies a content model
138 * - The hook DefaultModelFor may be used to override the page's default
140 * - Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript
141 * model if they end in .js or .css, respectively.
142 * - Pages in NS_MEDIAWIKI default to the wikitext model otherwise.
143 * - The hook TitleIsCssOrJsPage may be used to force a page to use the CSS
144 * or JavaScript model if they end in .js or .css, respectively.
145 * - The hook TitleIsWikitextPage may be used to force a page to use the
148 * If none of the above applies, the wikitext model is used.
150 * Note: this is used by, and may thus not use, Title::getContentModel()
155 * @param $title Title
156 * @return null|string default model name for the page given by $title
158 public static function getDefaultModelFor( Title
$title ) {
159 global $wgNamespaceContentModels;
161 // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
162 // because it is used to initialize the mContentModel member.
164 $ns = $title->getNamespace();
170 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
171 $model = $wgNamespaceContentModels[ $ns ];
174 // Hook can determine default model
175 if ( !wfRunHooks( 'ContentHandlerDefaultModelFor', array( $title, &$model ) ) ) {
176 if ( !is_null( $model ) ) {
181 // Could this page contain custom CSS or JavaScript, based on the title?
182 $isCssOrJsPage = NS_MEDIAWIKI
== $ns && preg_match( '!\.(css|js)$!u', $title->getText(), $m );
183 if ( $isCssOrJsPage ) {
187 // Hook can force JS/CSS
188 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage ) );
190 // Is this a .css subpage of a user page?
191 $isJsCssSubpage = NS_USER
== $ns
193 && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m );
194 if ( $isJsCssSubpage ) {
198 // Is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
199 $isWikitext = is_null( $model ) ||
$model == CONTENT_MODEL_WIKITEXT
;
200 $isWikitext = $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage;
202 // Hook can override $isWikitext
203 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
205 if ( !$isWikitext ) {
208 return CONTENT_MODEL_JAVASCRIPT
;
210 return CONTENT_MODEL_CSS
;
212 return is_null( $model ) ? CONTENT_MODEL_TEXT
: $model;
216 // We established that it must be wikitext
218 return CONTENT_MODEL_WIKITEXT
;
222 * Returns the appropriate ContentHandler singleton for the given title.
227 * @param $title Title
228 * @return ContentHandler
230 public static function getForTitle( Title
$title ) {
231 $modelId = $title->getContentModel();
232 return ContentHandler
::getForModelID( $modelId );
236 * Returns the appropriate ContentHandler singleton for the given Content
242 * @param $content Content
243 * @return ContentHandler
245 public static function getForContent( Content
$content ) {
246 $modelId = $content->getModel();
247 return ContentHandler
::getForModelID( $modelId );
251 * @var Array A Cache of ContentHandler instances by model id
256 * Returns the ContentHandler singleton for the given model ID. Use the
257 * CONTENT_MODEL_XXX constants to identify the desired content model.
259 * ContentHandler singletons are taken from the global $wgContentHandlers
260 * array. Keys in that array are model names, the values are either
261 * ContentHandler singleton objects, or strings specifying the appropriate
262 * subclass of ContentHandler.
264 * If a class name is encountered when looking up the singleton for a given
265 * model name, the class is instantiated and the class name is replaced by
266 * the resulting singleton in $wgContentHandlers.
268 * If no ContentHandler is defined for the desired $modelId, the
269 * ContentHandler may be provided by the ContentHandlerForModelID hook.
270 * If no ContentHandler can be determined, an MWException is raised.
275 * @param $modelId String The ID of the content model for which to get a
276 * handler. Use CONTENT_MODEL_XXX constants.
277 * @return ContentHandler The ContentHandler singleton for handling the
278 * model given by $modelId
279 * @throws MWException if no handler is known for $modelId.
281 public static function getForModelID( $modelId ) {
282 global $wgContentHandlers;
284 if ( isset( ContentHandler
::$handlers[$modelId] ) ) {
285 return ContentHandler
::$handlers[$modelId];
288 if ( empty( $wgContentHandlers[$modelId] ) ) {
291 wfRunHooks( 'ContentHandlerForModelID', array( $modelId, &$handler ) );
293 if ( $handler === null ) {
294 throw new MWException( "No handler for model #$modelId registered in \$wgContentHandlers" );
297 if ( !( $handler instanceof ContentHandler
) ) {
298 throw new MWException( "ContentHandlerForModelID must supply a ContentHandler instance" );
301 $class = $wgContentHandlers[$modelId];
302 $handler = new $class( $modelId );
304 if ( !( $handler instanceof ContentHandler
) ) {
305 throw new MWException( "$class from \$wgContentHandlers is not compatible with ContentHandler" );
309 ContentHandler
::$handlers[$modelId] = $handler;
310 return ContentHandler
::$handlers[$modelId];
314 * Returns the localized name for a given content model.
316 * Model names are localized using system messages. Message keys
317 * have the form content-model-$name, where $name is getContentModelName( $id ).
320 * @param $name String The content model ID, as given by a CONTENT_MODEL_XXX
321 * constant or returned by Revision::getContentModel().
323 * @return string The content format's localized name.
324 * @throws MWException if the model id isn't known.
326 public static function getLocalizedName( $name ) {
327 $key = "content-model-$name";
329 if ( wfEmptyMsg( $key ) ) return $name;
330 else return wfMsg( $key );
333 public static function getAllContentFormats() {
334 global $wgContentHandlers;
338 foreach ( $wgContentHandlers as $model => $class ) {
339 $handler = ContentHandler
::getForModelID( $model );
340 $formats = array_merge( $formats, $handler->getSupportedFormats() );
343 $formats = array_unique( $formats );
347 // ------------------------------------------------------------------------
350 protected $mSupportedFormats;
353 * Constructor, initializing the ContentHandler instance with its model ID
354 * and a list of supported formats. Values for the parameters are typically
355 * provided as literals by subclass's constructors.
357 * @param $modelId String (use CONTENT_MODEL_XXX constants).
358 * @param $formats array List for supported serialization formats
359 * (typically as MIME types)
361 public function __construct( $modelId, $formats ) {
362 $this->mModelID
= $modelId;
363 $this->mSupportedFormats
= $formats;
365 $this->mModelName
= preg_replace( '/(Content)?Handler$/', '', get_class( $this ) );
366 $this->mModelName
= preg_replace( '/[_\\\\]/', '', $this->mModelName
);
367 $this->mModelName
= strtolower( $this->mModelName
);
371 * Serializes a Content object of the type supported by this ContentHandler.
376 * @param $content Content The Content object to serialize
377 * @param $format null|String The desired serialization format
378 * @return string Serialized form of the content
380 public abstract function serializeContent( Content
$content, $format = null );
383 * Unserializes a Content object of the type supported by this ContentHandler.
388 * @param $blob string serialized form of the content
389 * @param $format null|String the format used for serialization
390 * @return Content the Content object created by deserializing $blob
392 public abstract function unserializeContent( $blob, $format = null );
395 * Creates an empty Content object of the type supported by this
402 public abstract function makeEmptyContent();
405 * Returns the model id that identifies the content model this
406 * ContentHandler can handle. Use with the CONTENT_MODEL_XXX constants.
410 * @return String The model ID
412 public function getModelID() {
413 return $this->mModelID
;
417 * Throws an MWException if $model_id is not the ID of the content model
418 * supported by this ContentHandler.
422 * @param String $model_id The model to check
424 * @throws MWException
426 protected function checkModelID( $model_id ) {
427 if ( $model_id !== $this->mModelID
) {
428 throw new MWException( "Bad content model: " .
429 "expected {$this->mModelID} " .
430 "but got $model_id." );
435 * Returns a list of serialization formats supported by the
436 * serializeContent() and unserializeContent() methods of this
441 * @return array of serialization formats as MIME type like strings
443 public function getSupportedFormats() {
444 return $this->mSupportedFormats
;
448 * The format used for serialization/deserialization by default by this
451 * This default implementation will return the first element of the array
452 * of formats that was passed to the constructor.
456 * @return string the name of the default serialization format as a MIME type
458 public function getDefaultFormat() {
459 return $this->mSupportedFormats
[0];
463 * Returns true if $format is a serialization format supported by this
464 * ContentHandler, and false otherwise.
466 * Note that if $format is null, this method always returns true, because
467 * null means "use the default format".
471 * @param $format string the serialization format to check
474 public function isSupportedFormat( $format ) {
477 return true; // this means "use the default"
480 return in_array( $format, $this->mSupportedFormats
);
484 * Throws an MWException if isSupportedFormat( $format ) is not true.
485 * Convenient for checking whether a format provided as a parameter is
486 * actually supported.
488 * @param $format string the serialization format to check
490 * @throws MWException
492 protected function checkFormat( $format ) {
493 if ( !$this->isSupportedFormat( $format ) ) {
494 throw new MWException(
495 "Format $format is not supported for content model "
496 . $this->getModelID()
502 * Returns true if the content is consistent with the database, that is if
503 * saving it to the database would not violate any global constraints.
505 * Content needs to be valid using this method before it can be saved.
507 * This default implementation always returns true.
511 * @param $content \Content
515 public function isConsistentWithDatabase( Content
$content ) {
520 * Returns overrides for action handlers.
521 * Classes listed here will be used instead of the default one when
522 * (and only when) $wgActions[$action] === true. This allows subclasses
523 * to override the default action handlers.
529 public function getActionOverrides() {
534 * Factory for creating an appropriate DifferenceEngine for this content model.
538 * @param $context IContextSource context to use, anything else will be
540 * @param $old Integer Old ID we want to show and diff with.
541 * @param $new int|string String either 'prev' or 'next'.
542 * @param $rcid Integer ??? FIXME (default 0)
543 * @param $refreshCache boolean If set, refreshes the diff cache
544 * @param $unhide boolean If set, allow viewing deleted revs
546 * @return DifferenceEngine
548 public function createDifferenceEngine( IContextSource
$context,
550 $rcid = 0, # FIXME: use everywhere!
551 $refreshCache = false, $unhide = false
553 $this->checkModelID( $context->getTitle()->getContentModel() );
555 $diffEngineClass = $this->getDiffEngineClass();
557 return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
561 * Get the language in which the content of the given page is written.
563 * This default implementation returns $wgContLang->getCode().
565 * Note that a page's language must be permanent and cacheable, that is, it must not depend
566 * on user preferences, request parameters or session state.
568 * Also note that the page language may or may not depend on the actual content of the page,
569 * that is, this method may load the content in order to determine the language.
573 * @param Title $title the page to determine the language for.
574 * @param Content|null $content the page's content, if you have it handy, to avoid reloading it.
576 * @return Language the page's language code
578 public function getPageLanguage( Title
$title, Content
$content = null ) {
584 * Returns the name of the diff engine to use.
590 protected function getDiffEngineClass() {
591 return 'DifferenceEngine';
595 * Attempts to merge differences between three versions.
596 * Returns a new Content object for a clean merge and false for failure or
599 * This default implementation always returns false.
603 * @param $oldContent Content|string String
604 * @param $myContent Content|string String
605 * @param $yourContent Content|string String
607 * @return Content|Bool
609 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
614 * Return an applicable auto-summary if one exists for the given edit.
618 * @param $oldContent Content|null: the previous text of the page.
619 * @param $newContent Content|null: The submitted text of the page.
620 * @param $flags int Bit mask: a bit mask of flags submitted for the edit.
622 * @return string An appropriate auto-summary, or an empty string.
624 public function getAutosummary( Content
$oldContent = null, Content
$newContent = null, $flags ) {
627 // Decide what kind of auto-summary is needed.
629 // Redirect auto-summaries
636 $ot = !is_null( $oldContent ) ?
$oldContent->getRedirectTarget() : null;
637 $rt = !is_null( $newContent ) ?
$newContent->getRedirectTarget() : null;
639 if ( is_object( $rt ) ) {
640 if ( !is_object( $ot )
641 ||
!$rt->equals( $ot )
642 ||
$ot->getFragment() != $rt->getFragment() )
644 $truncatedtext = $newContent->getTextForSummary(
646 - strlen( wfMsgForContent( 'autoredircomment' ) )
647 - strlen( $rt->getFullText() ) );
649 return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext );
653 // New page auto-summaries
654 if ( $flags & EDIT_NEW
&& $newContent->getSize() > 0 ) {
655 // If they're making a new article, give its text, truncated, in
658 $truncatedtext = $newContent->getTextForSummary(
659 200 - strlen( wfMsgForContent( 'autosumm-new' ) ) );
661 return wfMsgForContent( 'autosumm-new', $truncatedtext );
664 // Blanking auto-summaries
665 if ( !empty( $oldContent ) && $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
666 return wfMsgForContent( 'autosumm-blank' );
667 } elseif ( !empty( $oldContent )
668 && $oldContent->getSize() > 10 * $newContent->getSize()
669 && $newContent->getSize() < 500 )
671 // Removing more than 90% of the article
673 $truncatedtext = $newContent->getTextForSummary(
674 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) );
676 return wfMsgForContent( 'autosumm-replace', $truncatedtext );
679 // If we reach this point, there's no applicable auto-summary for our
680 // case, so our auto-summary is empty.
686 * Auto-generates a deletion reason
690 * @param $title Title: the page's title
691 * @param &$hasHistory Boolean: whether the page has a history
692 * @return mixed String containing deletion reason or empty string, or
693 * boolean false if no revision occurred
695 * @XXX &$hasHistory is extremely ugly, it's here because
696 * WikiPage::getAutoDeleteReason() and Article::getReason()
699 public function getAutoDeleteReason( Title
$title, &$hasHistory ) {
700 $dbw = wfGetDB( DB_MASTER
);
702 // Get the last revision
703 $rev = Revision
::newFromTitle( $title );
705 if ( is_null( $rev ) ) {
709 // Get the article's contents
710 $content = $rev->getContent();
713 $this->checkModelID( $content->getModel() );
715 // If the page is blank, use the text from the previous revision,
716 // which can only be blank if there's a move/import/protect dummy
718 if ( $content->getSize() == 0 ) {
719 $prev = $rev->getPrevious();
722 $content = $prev->getContent();
727 // Find out if there was only one contributor
728 // Only scan the last 20 revisions
729 $res = $dbw->select( 'revision', 'rev_user_text',
731 'rev_page' => $title->getArticleID(),
732 $dbw->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0'
735 array( 'LIMIT' => 20 )
738 if ( $res === false ) {
739 // This page has no revisions, which is very weird
743 $hasHistory = ( $res->numRows() > 1 );
744 $row = $dbw->fetchObject( $res );
746 if ( $row ) { // $row is false if the only contributor is hidden
747 $onlyAuthor = $row->rev_user_text
;
748 // Try to find a second contributor
749 foreach ( $res as $row ) {
750 if ( $row->rev_user_text
!= $onlyAuthor ) { // Bug 22999
759 // Generate the summary with a '$1' placeholder
761 // The current revision is blank and the one before is also
762 // blank. It's just not our lucky day
763 $reason = wfMsgForContent( 'exbeforeblank', '$1' );
766 $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
768 $reason = wfMsgForContent( 'excontent', '$1' );
772 if ( $reason == '-' ) {
773 // Allow these UI messages to be blanked out cleanly
777 // Max content length = max comment length - length of the comment (excl. $1)
778 $text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
780 // Now replace the '$1' placeholder
781 $reason = str_replace( '$1', $text, $reason );
787 * Parse the Content object and generate a ParserOutput from the result.
788 * $result->getText() can be used to obtain the generated HTML. If no HTML
789 * is needed, $generateHtml can be set to false; in that case,
790 * $result->getText() may return null.
792 * @param $content Content the content to render
793 * @param $title Title The page title to use as a context for rendering
794 * @param $revId null|int The revision being rendered (optional)
795 * @param $options null|ParserOptions Any parser options
796 * @param $generateHtml Boolean Whether to generate HTML (default: true). If false,
797 * the result of calling getText() on the ParserOutput object returned by
798 * this method is undefined.
802 * @return ParserOutput
804 public abstract function getParserOutput( Content
$content, Title
$title,
806 ParserOptions
$options = null, $generateHtml = true );
807 # TODO: make RenderOutput and RenderOptions base classes
810 * Returns a list of DataUpdate objects for recording information about this
811 * Content in some secondary data store. If the optional second argument,
812 * $old, is given, the updates may model only the changes that need to be
813 * made to replace information about the old content with information about
816 * This default implementation calls
817 * $this->getParserOutput( $content, $title, null, null, false ),
818 * and then calls getSecondaryDataUpdates( $title, $recursive ) on the
819 * resulting ParserOutput object.
821 * Subclasses may implement this to determine the necessary updates more
822 * efficiently, or make use of information about the old content.
824 * @param $content Content The content for determining the necessary updates
825 * @param $title Title The context for determining the necessary updates
826 * @param $old Content|null An optional Content object representing the
827 * previous content, i.e. the content being replaced by this Content
829 * @param $recursive boolean Whether to include recursive updates (default:
831 * @param $parserOutput ParserOutput|null Optional ParserOutput object.
832 * Provide if you have one handy, to avoid re-parsing of the content.
834 * @return Array. A list of DataUpdate objects for putting information
835 * about this content object somewhere.
839 public function getSecondaryDataUpdates( Content
$content, Title
$title,
841 $recursive = true, ParserOutput
$parserOutput = null
843 if ( !$parserOutput ) {
844 $parserOutput = $this->getParserOutput( $content, $title, null, null, false );
847 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
852 * Get the Content object that needs to be saved in order to undo all revisions
853 * between $undo and $undoafter. Revisions must belong to the same page,
854 * must exist and must not be deleted.
858 * @param $current Revision The current text
859 * @param $undo Revision The revision to undo
860 * @param $undoafter Revision Must be an earlier revision than $undo
862 * @return mixed String on success, false on failure
864 public function getUndoContent( Revision
$current, Revision
$undo, Revision
$undoafter ) {
865 $cur_content = $current->getContent();
867 if ( empty( $cur_content ) ) {
868 return false; // no page
871 $undo_content = $undo->getContent();
872 $undoafter_content = $undoafter->getContent();
874 $this->checkModelID( $cur_content->getModel() );
875 $this->checkModelID( $undo_content->getModel() );
876 $this->checkModelID( $undoafter_content->getModel() );
878 if ( $cur_content->equals( $undo_content ) ) {
879 // No use doing a merge if it's just a straight revert.
880 return $undoafter_content;
883 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
885 return $undone_content;
889 * Returns true for content models that support caching using the
890 * ParserCache mechanism. See WikiPage::isParserCacheUser().
896 public function isParserCacheSupported() {
901 * Returns a list of updates to perform when the given content is deleted.
902 * The necessary updates may be taken from the Content object, or depend on
903 * the current state of the database.
907 * @param $content \Content the Content object for deletion
908 * @param $title \Title the title of the deleted page
909 * @param $parserOutput null|\ParserOutput optional parser output object
910 * for efficient access to meta-information about the content object.
911 * Provide if you have one handy.
913 * @return array A list of DataUpdate instances that will clean up the
914 * database after deletion.
916 public function getDeletionUpdates( Content
$content, Title
$title,
917 ParserOutput
$parserOutput = null )
920 new LinksDeletionUpdate( $title ),
925 * Returns true if this content model supports sections.
927 * This default implementation returns false.
929 * @return boolean whether sections are supported.
931 public function supportsSections() {
939 abstract class TextContentHandler
extends ContentHandler
{
941 public function __construct( $modelId, $formats ) {
942 parent
::__construct( $modelId, $formats );
946 * Returns the content's text as-is.
948 * @param $content Content
949 * @param $format string|null
952 public function serializeContent( Content
$content, $format = null ) {
953 $this->checkFormat( $format );
954 return $content->getNativeData();
958 * Attempts to merge differences between three versions. Returns a new
959 * Content object for a clean merge and false for failure or a conflict.
961 * All three Content objects passed as parameters must have the same
964 * This text-based implementation uses wfMerge().
966 * @param $oldContent \Content|string String
967 * @param $myContent \Content|string String
968 * @param $yourContent \Content|string String
970 * @return Content|Bool
972 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
973 $this->checkModelID( $oldContent->getModel() );
974 $this->checkModelID( $myContent->getModel() );
975 $this->checkModelID( $yourContent->getModel() );
977 $format = $this->getDefaultFormat();
979 $old = $this->serializeContent( $oldContent, $format );
980 $mine = $this->serializeContent( $myContent, $format );
981 $yours = $this->serializeContent( $yourContent, $format );
983 $ok = wfMerge( $old, $mine, $yours, $result );
990 return $this->makeEmptyContent();
993 $mergedContent = $this->unserializeContent( $result, $format );
994 return $mergedContent;
998 * Returns a generic ParserOutput object, wrapping the HTML returned by
1001 * @param $content Content The content to render
1002 * @param $title Title Context title for parsing
1003 * @param $revId int|null Revision ID (for {{REVISIONID}})
1004 * @param $options ParserOptions|null Parser options
1005 * @param $generateHtml bool Whether or not to generate HTML
1007 * @return ParserOutput representing the HTML form of the text
1009 public function getParserOutput( Content
$content, Title
$title,
1011 ParserOptions
$options = null, $generateHtml = true
1013 $this->checkModelID( $content->getModel() );
1015 # Generic implementation, relying on $this->getHtml()
1017 if ( $generateHtml ) {
1018 $html = $this->getHtml( $content );
1023 $po = new ParserOutput( $html );
1028 * Generates an HTML version of the content, for display. Used by
1029 * getParserOutput() to construct a ParserOutput object.
1031 * This default implementation just calls getHighlightHtml(). Content
1032 * models that have another mapping to HTML (as is the case for markup
1033 * languages like wikitext) should override this method to generate the
1036 * @param $content Content The content to render
1038 * @return string An HTML representation of the content
1040 protected function getHtml( Content
$content ) {
1041 $this->checkModelID( $content->getModel() );
1043 return $this->getHighlightHtml( $content );
1047 * Generates a syntax-highlighted version the content, as HTML.
1048 * Used by the default implementation of getHtml().
1050 * @param $content Content the content to render
1052 * @return string an HTML representation of the content's markup
1054 protected function getHighlightHtml( Content
$content ) {
1055 $this->checkModelID( $content->getModel() );
1057 # TODO: make Highlighter interface, use highlighter here, if available
1058 return htmlspecialchars( $content->getNativeData() );
1067 class WikitextContentHandler
extends TextContentHandler
{
1069 public function __construct( $modelId = CONTENT_MODEL_WIKITEXT
) {
1070 parent
::__construct( $modelId, array( CONTENT_FORMAT_WIKITEXT
) );
1073 public function unserializeContent( $text, $format = null ) {
1074 $this->checkFormat( $format );
1076 return new WikitextContent( $text );
1079 public function makeEmptyContent() {
1080 return new WikitextContent( '' );
1084 * Returns a ParserOutput object resulting from parsing the content's text
1089 * @param $content Content the content to render
1090 * @param $title \Title
1091 * @param $revId null
1092 * @param $options null|ParserOptions
1093 * @param $generateHtml bool
1095 * @internal param \IContextSource|null $context
1096 * @return ParserOutput representing the HTML form of the text
1098 public function getParserOutput( Content
$content, Title
$title,
1100 ParserOptions
$options = null, $generateHtml = true
1104 $this->checkModelID( $content->getModel() );
1107 $options = new ParserOptions();
1110 $po = $wgParser->parse( $content->getNativeData(), $title, $options, true, true, $revId );
1114 protected function getHtml( Content
$content ) {
1115 throw new MWException(
1116 "getHtml() not implemented for wikitext. "
1117 . "Use getParserOutput()->getText()."
1122 * Returns true because wikitext supports sections.
1124 * @return boolean whether sections are supported.
1126 public function supportsSections() {
1131 # XXX: make ScriptContentHandler base class, do highlighting stuff there?
1136 class JavaScriptContentHandler
extends TextContentHandler
{
1138 public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT
) {
1139 parent
::__construct( $modelId, array( CONTENT_FORMAT_JAVASCRIPT
) );
1142 public function unserializeContent( $text, $format = null ) {
1143 $this->checkFormat( $format );
1145 return new JavaScriptContent( $text );
1148 public function makeEmptyContent() {
1149 return new JavaScriptContent( '' );
1153 * Returns the english language, because JS is english, and should be handled as such.
1155 * @return Language wfGetLangObj( 'en' )
1157 * @see ContentHandler::getPageLanguage()
1159 public function getPageLanguage( Title
$title, Content
$content = null ) {
1160 return wfGetLangObj( 'en' );
1163 protected function getHtml( Content
$content ) {
1165 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
1166 $html .= $this->getHighlightHtml( $content );
1167 $html .= "\n</pre>\n";
1176 class CssContentHandler
extends TextContentHandler
{
1178 public function __construct( $modelId = CONTENT_MODEL_CSS
) {
1179 parent
::__construct( $modelId, array( CONTENT_FORMAT_CSS
) );
1182 public function unserializeContent( $text, $format = null ) {
1183 $this->checkFormat( $format );
1185 return new CssContent( $text );
1188 public function makeEmptyContent() {
1189 return new CssContent( '' );
1193 * Returns the english language, because CSS is english, and should be handled as such.
1195 * @return Language wfGetLangObj( 'en' )
1197 * @see ContentHandler::getPageLanguage()
1199 public function getPageLanguage( Title
$title, Content
$content = null ) {
1200 return wfGetLangObj( 'en' );
1203 protected function getHtml( Content
$content ) {
1205 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
1206 $html .= $this->getHighlightHtml( $content );
1207 $html .= "\n</pre>\n";