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 page.
12 * Content is stored in the database in a serialized form (using a serialization format aka mime type)
13 * and is be unserialized into it's native PHP representation (the content model), which is wrapped in
14 * an instance of the appropriate subclass of Content.
16 * ContentHandler instances are stateless singletons that serve, among other things, as a factory for
17 * Content objects. Generally, there is one subclass of ContentHandler and one subclass of Content
18 * for every type of content model.
20 * Some content types have a flat model, that is, their native representation is the
21 * same as their serialized form. Examples would be JavaScript and CSS code. As of now,
22 * this also applies to wikitext (mediawiki's default content type), but wikitext
23 * content may be represented by a DOM or AST structure in the future.
27 abstract class ContentHandler
{
30 * Convenience function for getting flat text from a Content object. This should only
31 * be used in the context of backwards compatibility with code that is not yet able
32 * to handle Content objects!
34 * If $content is null, this method returns the empty string.
36 * If $content is an instance of TextContent, this method returns the flat text as returned by $content->getNativeData().
38 * If $content is not a TextContent object, the behavior of this method depends on the global $wgContentHandlerTextFallback:
39 * * If $wgContentHandlerTextFallback is 'fail' and $content is not a TextContent object, an MWException is thrown.
40 * * If $wgContentHandlerTextFallback is 'serialize' and $content is not a TextContent object, $content->serialize()
41 * is called to get a string form of the content.
42 * * If $wgContentHandlerTextFallback is 'ignore' and $content is not a TextContent object, this method returns null.
43 * * otherwise, the behaviour is undefined.
48 * @param Content|null $content
49 * @return null|string the textual form of $content, if available
50 * @throws MWException if $content is not an instance of TextContent and $wgContentHandlerTextFallback was set to 'fail'.
52 public static function getContentText( Content
$content = null ) {
53 global $wgContentHandlerTextFallback;
55 if ( is_null( $content ) ) {
59 if ( $content instanceof TextContent
) {
60 return $content->getNativeData();
63 if ( $wgContentHandlerTextFallback == 'fail' ) {
64 throw new MWException( "Attempt to get text from Content with model " . $content->getModel() );
67 if ( $wgContentHandlerTextFallback == 'serialize' ) {
68 return $content->serialize();
75 * Convenience function for creating a Content object from a given textual representation.
77 * $text will be deserialized into a Content object of the model specified by $modelId (or,
78 * if that is not given, $title->getContentModel()) using the given format.
84 * @param string $text the textual representation, will be unserialized to create the Content object
85 * @param null|Title $title the title of the page this text belongs to. Required if $modelId is not provided.
86 * @param null|String $modelId the model to deserialize to. If not provided, $title->getContentModel() is used.
87 * @param null|String $format the format to use for deserialization. If not given, the model's default format is used.
89 * @return Content a Content object representing $text
91 * @throw MWException if $model or $format is not supported or if $text can not be unserialized using $format.
93 public static function makeContent( $text, Title
$title = null, $modelId = null, $format = null ) {
95 if ( is_null( $modelId ) ) {
96 if ( is_null( $title ) ) {
97 throw new MWException( "Must provide a Title object or a content model ID." );
100 $modelId = $title->getContentModel();
103 $handler = ContentHandler
::getForModelID( $modelId );
104 return $handler->unserializeContent( $text, $format );
108 * Returns the name of the default content model to be used for the page with the given title.
110 * Note: There should rarely be need to call this method directly.
111 * To determine the actual content model for a given page, use Title::getContentModel().
113 * Which model is to be used per default for the page is determined based on several factors:
114 * * The global setting $wgNamespaceContentModels specifies a content model per namespace.
115 * * The hook DefaultModelFor may be used to override the page's default model.
116 * * Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript model if they end in .js or .css, respectively.
117 * * Pages in NS_MEDIAWIKI default to the wikitext model otherwise.
118 * * The hook TitleIsCssOrJsPage may be used to force a page to use the CSS or JavaScript model if they end in .js or .css, respectively.
119 * * The hook TitleIsWikitextPage may be used to force a page to use the wikitext model.
121 * If none of the above applies, the wikitext model is used.
123 * Note: this is used by, and may thus not use, Title::getContentModel()
128 * @param Title $title
129 * @return null|string default model name for the page given by $title
131 public static function getDefaultModelFor( Title
$title ) {
132 global $wgNamespaceContentModels;
134 // NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
135 // because it is used to initialized the mContentModel member.
137 $ns = $title->getNamespace();
143 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
144 $model = $wgNamespaceContentModels[ $ns ];
147 // hook can determin default model
148 if ( !wfRunHooks( 'ContentHandlerDefaultModelFor', array( $title, &$model ) ) ) {
149 if ( !is_null( $model ) ) {
154 // Could this page contain custom CSS or JavaScript, based on the title?
155 $isCssOrJsPage = NS_MEDIAWIKI
== $ns && preg_match( '!\.(css|js)$!u', $title->getText(), $m );
156 if ( $isCssOrJsPage ) {
160 // hook can force js/css
161 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage ) );
163 // Is this a .css subpage of a user page?
164 $isJsCssSubpage = NS_USER
== $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m );
165 if ( $isJsCssSubpage ) {
169 // is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
170 $isWikitext = is_null( $model ) ||
$model == CONTENT_MODEL_WIKITEXT
;
171 $isWikitext = $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage;
173 // hook can override $isWikitext
174 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
176 if ( !$isWikitext ) {
179 return CONTENT_MODEL_JAVASCRIPT
;
181 return CONTENT_MODEL_CSS
;
183 return is_null( $model ) ? CONTENT_MODEL_TEXT
: $model;
187 // we established that is must be wikitext
189 return CONTENT_MODEL_WIKITEXT
;
193 * returns the appropriate ContentHandler singleton for the given title
198 * @param Title $title
199 * @return ContentHandler
201 public static function getForTitle( Title
$title ) {
202 $modelId = $title->getContentModel();
203 return ContentHandler
::getForModelID( $modelId );
207 * returns the appropriate ContentHandler singleton for the given Content object
212 * @param Content $content
213 * @return ContentHandler
215 public static function getForContent( Content
$content ) {
216 $modelId = $content->getModel();
217 return ContentHandler
::getForModelID( $modelId );
221 * returns the ContentHandler singleton for the given model id. Use the CONTENT_MODEL_XXX constants to
222 * identify the desired content model.
224 * ContentHandler singletons are take from the global $wgContentHandlers array. Keys in that array are
225 * model names, the values are either ContentHandler singleton objects, or strings specifying the appropriate
226 * subclass of ContentHandler.
228 * If a class name in encountered when looking up the singleton for a given model name, the class is
229 * instantiated and the class name is replaced by te resulting singleton in $wgContentHandlers.
231 * If no ContentHandler is defined for the desired $modelId, the ContentHandler may be provided by the
232 * a ContentHandlerForModelID hook. if no ContentHandler can be determined, an MWException is raised.
237 * @param $modelId int the id of the content model for which to get a handler. Use CONTENT_MODEL_XXX constants.
238 * @return ContentHandler the ContentHandler singleton for handling the model given by $modelId
239 * @throws MWException if no handler is known for $modelId.
241 public static function getForModelID( $modelId ) {
242 global $wgContentHandlers;
244 if ( empty( $wgContentHandlers[$modelId] ) ) {
247 wfRunHooks( 'ContentHandlerForModelID', array( $modelId, &$handler ) );
249 if ( $handler ) { // NOTE: may be a string or an object, either is fine!
250 $wgContentHandlers[$modelId] = $handler;
252 throw new MWException( "No handler for model #$modelId registered in \$wgContentHandlers" );
256 if ( is_string( $wgContentHandlers[$modelId] ) ) {
257 $class = $wgContentHandlers[$modelId];
258 $wgContentHandlers[$modelId] = new $class( $modelId );
261 return $wgContentHandlers[$modelId];
265 * Returns the appropriate mime type for a given content format,
266 * or null if no mime type is known for this format.
268 * Mime types can be registered in the global array $wgContentFormatMimeTypes.
271 * @param int $id the content format id, as given by a CONTENT_FORMAT_XXX constant
272 * or returned by Revision::getContentFormat().
274 * @return String|null the content format's mime type.
276 public static function getContentFormatMimeType( $id ) {
277 global $wgContentFormatMimeTypes;
279 if ( !isset( $wgContentFormatMimeTypes[ $id ] ) ) {
283 return $wgContentFormatMimeTypes[ $id ];
287 * Returns the content format if for a given mime type,
288 * or null if no format id if known for this mime type.
290 * Mime types can be registered in the global array $wgContentFormatMimeTypes.
293 * @param String $mime the mime type
295 * @return int|null the format id, as defined by a CONTENT_FORMAT_XXX constant
297 public static function getContentFormatID( $mime ) {
298 global $wgContentFormatMimeTypes;
300 static $format_ids = null;
302 if ( $format_ids === null ) {
303 $format_ids = array_flip( $wgContentFormatMimeTypes );
306 if ( !isset( $format_ids[ $mime ] ) ) {
310 return $format_ids[ $mime ];
314 * Returns the localized name for a given content model,
315 * or null of no mime type is known.
317 * Model names are localized using system messages. Message keys
318 * have the form content-model-$id.
321 * @param int $id the content model id, as given by a CONTENT_MODEL_XXX constant
322 * or returned by Revision::getContentModel().
324 * @return String|null the content format's mime type.
326 public static function getContentModelName( $id ) {
327 $key = "content-model-$id";
329 if ( wfEmptyMsg( $key ) ) return null;
330 else return wfMsg( $key );
333 // ----------------------------------------------------------------------------------------------------------
336 protected $mSupportedFormats;
339 * Constructor, initializing the ContentHandler instance with it's model id and a list of supported formats.
340 * Values for the parameters are typically provided as literals by subclasses' constructors.
342 * @param int $modelId (use CONTENT_MODEL_XXX constants).
343 * @param array $formats list for supported serialization formats (typically as MIME types)
345 public function __construct( $modelId, $formats ) {
346 $this->mModelID
= $modelId;
347 $this->mSupportedFormats
= $formats;
352 * Serializes Content object of the type supported by this ContentHandler.
357 * @param Content $content the Content object to serialize
358 * @param null $format the desired serialization format
359 * @return String serialized form of the content
361 public abstract function serializeContent( Content
$content, $format = null );
364 * Unserializes a Content object of the type supported by this ContentHandler.
369 * @param $blob String serialized form of the content
370 * @param null $format the format used for serialization
371 * @return Content the Content object created by deserializing $blob
373 public abstract function unserializeContent( $blob, $format = null );
376 * Creates an empty Content object of the type supported by this ContentHandler.
382 public abstract function makeEmptyContent();
385 * Returns the model id that identifies the content model this ContentHandler can handle.
386 * Use with the CONTENT_MODEL_XXX constants.
390 * @return int the model id
392 public function getModelID() {
393 return $this->mModelID
;
397 * Throws an MWException if $model_id is not the id of the content model
398 * supported by this ContentHandler.
402 * @param int $model_id the model to check
404 * @throws MWException
406 protected function checkModelID( $model_id ) {
407 if ( $model_id !== $this->mModelID
) {
408 $model_name = ContentHandler
::getContentModelName( $model_id );
409 $own_model_name = ContentHandler
::getContentModelName( $this->mModelID
);
411 throw new MWException( "Bad content model: expected {$this->mModelID} ($own_model_name) but got found $model_id ($model_name)." );
416 * Returns a list of serialization formats supported by the serializeContent() and unserializeContent() methods of
417 * this ContentHandler.
421 * @return array of serialization formats as MIME type like strings
423 public function getSupportedFormats() {
424 return $this->mSupportedFormats
;
428 * The format used for serialization/deserialization per default by this ContentHandler.
430 * This default implementation will return the first element of the array of formats
431 * that was passed to the constructor.
435 * @return String the name of the default serialization format as a MIME type
437 public function getDefaultFormat() {
438 return $this->mSupportedFormats
[0];
442 * Returns true if $format is a serialization format supported by this ContentHandler,
443 * and false otherwise.
445 * Note that if $format is null, this method always returns true, because null
446 * means "use the default format".
450 * @param String $format the serialization format to check
453 public function isSupportedFormat( $format ) {
456 return true; // this means "use the default"
459 return in_array( $format, $this->mSupportedFormats
);
463 * Throws an MWException if isSupportedFormat( $format ) is not true. Convenient
464 * for checking whether a format provided as a parameter is actually supported.
466 * @param String $format the serialization format to check
468 * @throws MWException
470 protected function checkFormat( $format ) {
471 if ( !$this->isSupportedFormat( $format ) ) {
472 throw new MWException( "Format $format is not supported for content model " . $this->getModelID() );
477 * Returns if the content is consistent with the database, that is if saving it to the database would not violate any
478 * global constraints.
480 * Content needs to be valid using this method before it can be saved.
482 * This default implementation always returns true.
486 * @param \Content $content
490 public function isConsistentWithDatabase( Content
$content ) {
495 * Returns overrides for action handlers.
496 * Classes listed here will be used instead of the default one when
497 * (and only when) $wgActions[$action] === true. This allows subclasses
498 * to override the default action handlers.
504 public function getActionOverrides() {
509 * Factory creating an appropriate DifferenceEngine for this content model.
513 * @param $context IContextSource context to use, anything else will be ignored
514 * @param $old Integer old ID we want to show and diff with.
515 * @param int|String $new String either 'prev' or 'next'.
516 * @param $rcid Integer ??? FIXME (default 0)
517 * @param $refreshCache boolean If set, refreshes the diff cache
518 * @param $unhide boolean If set, allow viewing deleted revs
520 * @return DifferenceEngine
522 public function createDifferenceEngine( IContextSource
$context, $old = 0, $new = 0, $rcid = 0, #FIMXE: use everywhere!
523 $refreshCache = false, $unhide = false ) {
525 $this->checkModelID( $context->getTitle()->getContentModel() );
527 $diffEngineClass = $this->getDiffEngineClass();
529 return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
533 * Returns the name of the diff engine to use.
539 protected function getDiffEngineClass() {
540 return 'DifferenceEngine';
544 * attempts to merge differences between three versions.
545 * Returns a new Content object for a clean merge and false for failure or a conflict.
547 * This default implementation always returns false.
551 * @param Content|String $oldContent String
552 * @param Content|String $myContent String
553 * @param Content|String $yourContent String
555 * @return Content|Bool
557 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
562 * Return an applicable auto-summary if one exists for the given edit.
566 * @param $oldContent Content|null: the previous text of the page.
567 * @param $newContent Content|null: The submitted text of the page.
568 * @param $flags Int bit mask: a bit mask of flags submitted for the edit.
570 * @return string An appropriate auto-summary, or an empty string.
572 public function getAutosummary( Content
$oldContent = null, Content
$newContent = null, $flags ) {
575 // Decide what kind of auto-summary is needed.
577 // Redirect auto-summaries
584 $ot = !is_null( $oldContent ) ?
$oldContent->getRedirectTarget() : null;
585 $rt = !is_null( $newContent ) ?
$newContent->getRedirectTarget() : null;
587 if ( is_object( $rt ) && ( !is_object( $ot ) ||
!$rt->equals( $ot ) ||
$ot->getFragment() != $rt->getFragment() ) ) {
589 $truncatedtext = $newContent->getTextForSummary(
591 - strlen( wfMsgForContent( 'autoredircomment' ) )
592 - strlen( $rt->getFullText() ) );
594 return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext );
597 // New page auto-summaries
598 if ( $flags & EDIT_NEW
&& $newContent->getSize() > 0 ) {
599 // If they're making a new article, give its text, truncated, in the summary.
601 $truncatedtext = $newContent->getTextForSummary(
602 200 - strlen( wfMsgForContent( 'autosumm-new' ) ) );
604 return wfMsgForContent( 'autosumm-new', $truncatedtext );
607 // Blanking auto-summaries
608 if ( !empty( $oldContent ) && $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
609 return wfMsgForContent( 'autosumm-blank' );
610 } elseif ( !empty( $oldContent ) && $oldContent->getSize() > 10 * $newContent->getSize() && $newContent->getSize() < 500 ) {
611 // Removing more than 90% of the article
613 $truncatedtext = $newContent->getTextForSummary(
614 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) );
616 return wfMsgForContent( 'autosumm-replace', $truncatedtext );
619 // If we reach this point, there's no applicable auto-summary for our case, so our
620 // auto-summary is empty.
626 * Auto-generates a deletion reason
630 * @param $title Title: the page's title
631 * @param &$hasHistory Boolean: whether the page has a history
632 * @return mixed String containing deletion reason or empty string, or boolean false
633 * if no revision occurred
635 * @XXX &$hasHistory is extremely ugly, it's here because WikiPage::getAutoDeleteReason() and Article::getReason() have it / want it.
637 public function getAutoDeleteReason( Title
$title, &$hasHistory ) {
638 $dbw = wfGetDB( DB_MASTER
);
640 // Get the last revision
641 $rev = Revision
::newFromTitle( $title );
643 if ( is_null( $rev ) ) {
647 // Get the article's contents
648 $content = $rev->getContent();
651 $this->checkModelID( $content->getModel() );
653 // If the page is blank, use the text from the previous revision,
654 // which can only be blank if there's a move/import/protect dummy revision involved
655 if ( $content->getSize() == 0 ) {
656 $prev = $rev->getPrevious();
659 $content = $prev->getContent();
664 // Find out if there was only one contributor
665 // Only scan the last 20 revisions
666 $res = $dbw->select( 'revision', 'rev_user_text',
667 array( 'rev_page' => $title->getArticleID(), $dbw->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0' ),
669 array( 'LIMIT' => 20 )
672 if ( $res === false ) {
673 // This page has no revisions, which is very weird
677 $hasHistory = ( $res->numRows() > 1 );
678 $row = $dbw->fetchObject( $res );
680 if ( $row ) { // $row is false if the only contributor is hidden
681 $onlyAuthor = $row->rev_user_text
;
682 // Try to find a second contributor
683 foreach ( $res as $row ) {
684 if ( $row->rev_user_text
!= $onlyAuthor ) { // Bug 22999
693 // Generate the summary with a '$1' placeholder
695 // The current revision is blank and the one before is also
696 // blank. It's just not our lucky day
697 $reason = wfMsgForContent( 'exbeforeblank', '$1' );
700 $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
702 $reason = wfMsgForContent( 'excontent', '$1' );
706 if ( $reason == '-' ) {
707 // Allow these UI messages to be blanked out cleanly
711 // Max content length = max comment length - length of the comment (excl. $1)
712 $text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
714 // Now replace the '$1' placeholder
715 $reason = str_replace( '$1', $text, $reason );
721 * Parse the Content object and generate a ParserObject from the result. $result->getText() can
722 * be used to obtain the generated HTML. If no HTML is needed, $generateHtml can be set to false;
723 * in that case, $result->getText() may return null.
725 * @param Content $content the content to render
726 * @param Title $title the page title to use as a context for rendering
727 * @param null|int $revId the revision being rendered (optional)
728 * @param null|ParserOptions $options any parser options
729 * @param Boolean $generateHtml whether to generate Html (default: true). If false,
730 * the result of calling getText() on the ParserOutput object returned by
731 * this method is undefined.
735 * @return ParserOutput
737 public abstract function getParserOutput( Content
$content, Title
$title, $revId = null, ParserOptions
$options = null, $generateHtml = true );
738 #TODO: make RenderOutput and RenderOptions base classes
741 * Returns a list of DataUpdate objects for recording information about this Content in some secondary
742 * data store. If the optional second argument, $old, is given, the updates may model only the changes that
743 * need to be made to replace information about the old content with information about the new content.
745 * This default implementation calls $this->getParserOutput( $content, $title, null, null, false ), and then
746 * calls getSecondaryDataUpdates( $title, $recursive ) on the resulting ParserOutput object.
748 * Subclasses may implement this to determine the necessary updates more efficiently, or make use of information
749 * about the old content.
751 * @param Content $content the content for determining the necessary updates
752 * @param Title $title the context for determining the necessary updates
753 * @param Content|null $old an optional Content object representing the previous content, i.e. the content being
754 * replaced by this Content object.
755 * @param boolean $recursive whether to include recursive updates (default: false).
756 * @param ParserOutput|null $parserOutput optional ParserOutput object. Provide if you have one handy, to avoid re-parsing
759 * @return Array. A list of DataUpdate objects for putting information about this content object somewhere.
763 public function getSecondaryDataUpdates( Content
$content, Title
$title, Content
$old = null,
764 $recursive = true, ParserOutput
$parserOutput = null ) {
766 if ( !$parserOutput ) {
767 $parserOutput = $this->getParserOutput( $content, $title, null, null, false );
770 return $parserOutput->getSecondaryDataUpdates( $title, $recursive );
775 * Get the Content object that needs to be saved in order to undo all revisions
776 * between $undo and $undoafter. Revisions must belong to the same page,
777 * must exist and must not be deleted
781 * @param $current Revision the current text
782 * @param $undo Revision the revision to undo
783 * @param $undoafter Revision Must be an earlier revision than $undo
785 * @return mixed string on success, false on failure
787 public function getUndoContent( Revision
$current, Revision
$undo, Revision
$undoafter ) {
788 $cur_content = $current->getContent();
790 if ( empty( $cur_content ) ) {
791 return false; // no page
794 $undo_content = $undo->getContent();
795 $undoafter_content = $undoafter->getContent();
797 $this->checkModelID( $cur_content->getModel() );
798 $this->checkModelID( $undo_content->getModel() );
799 $this->checkModelID( $undoafter_content->getModel() );
801 if ( $cur_content->equals( $undo_content ) ) {
802 // No use doing a merge if it's just a straight revert.
803 return $undoafter_content;
806 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
808 return $undone_content;
812 * Returns true for content models that support caching using the ParserCache mechanism.
813 * See WikiPage::isParserCacheUser().
819 public function isParserCacheSupported() {
824 * Returns a lost of updates to perform when the given content is deleted.
825 * The necessary updates may be taken from the Content object, or depend on the current state of the database.
829 * @param \Content $content the Content object for deletion
830 * @param \Title $title the title of the deleted page
831 * @param null|\ParserOutput $parserOutput optional parser output object for efficient access to meta-information
832 * about the content object. Provide if you have one handy.
834 * @return array a list of DataUpdate instances that will clean up the database ofter deletion.
836 public function getDeletionUpdates( Content
$content, Title
$title, ParserOutput
$parserOutput = null ) {
838 new LinksDeletionUpdate( $title ),
843 * Returns true iff this content model supports sections.
845 * This default implementation returns false,
847 * @return boolean whether sections are supported.
849 public function supportsSections() {
857 abstract class TextContentHandler
extends ContentHandler
{
859 public function __construct( $modelId, $formats ) {
860 parent
::__construct( $modelId, $formats );
864 * Returns the content's text as-is.
866 * @param Content $content
867 * @param String|null $format
870 public function serializeContent( Content
$content, $format = null ) {
871 $this->checkFormat( $format );
872 return $content->getNativeData();
876 * attempts to merge differences between three versions.
877 * Returns a new Content object for a clean merge and false for failure or a conflict.
879 * All three Content objects passed as parameters must have the same content model.
881 * This text-based implementation uses wfMerge().
883 * @param \Content|String $oldContent String
884 * @param \Content|String $myContent String
885 * @param \Content|String $yourContent String
887 * @return Content|Bool
889 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
890 $this->checkModelID( $oldContent->getModel() );
891 $this->checkModelID( $myContent->getModel() );
892 $this->checkModelID( $yourContent->getModel() );
894 $format = $this->getDefaultFormat();
896 $old = $this->serializeContent( $oldContent, $format );
897 $mine = $this->serializeContent( $myContent, $format );
898 $yours = $this->serializeContent( $yourContent, $format );
900 $ok = wfMerge( $old, $mine, $yours, $result );
907 return $this->makeEmptyContent();
910 $mergedContent = $this->unserializeContent( $result, $format );
911 return $mergedContent;
915 * Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
917 * @param Content $content the content to render
918 * @param Title $title context title for parsing
919 * @param int|null $revId revision id (the parser wants that for some reason)
920 * @param ParserOptions|null $options parser options
921 * @param bool $generateHtml whether or not to generate HTML
923 * @return ParserOutput representing the HTML form of the text
925 public function getParserOutput( Content
$content, Title
$title, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
926 $this->checkModelID( $content->getModel() );
928 # generic implementation, relying on $this->getHtml()
930 if ( $generateHtml ) $html = $this->getHtml( $content );
933 $po = new ParserOutput( $html );
938 * Generates an HTML version of the content, for display.
939 * Used by getParserOutput() to construct a ParserOutput object.
941 * This default implementation just calls getHighlightHtml(). Content models that
942 * have another mapping to HTML (as is the case for markup languages like wikitext)
943 * should override this method to generate the appropriate html.
945 * @param Content $content the content to render
947 * @return String an HTML representation of the content
949 protected function getHtml( Content
$content ) {
950 $this->checkModelID( $content->getModel() );
953 return $this->getHighlighteHtml( $content );
957 * Generates a syntax-highlighted version the content, as HTML.
958 * Used by the default implementation if getHtml().
960 * @param Content $content the content to render
962 * @return String an HTML representation of the content's markup
964 protected function getHighlightHtml( Content
$content ) {
965 $this->checkModelID( $content->getModel() );
967 #TODO: use highlighter, if available
969 return htmlspecialchars( $content->getNativeData() );
978 class WikitextContentHandler
extends TextContentHandler
{
980 public function __construct( $modelId = CONTENT_MODEL_WIKITEXT
) {
981 parent
::__construct( $modelId, array( CONTENT_FORMAT_WIKITEXT
) );
984 public function unserializeContent( $text, $format = null ) {
985 $this->checkFormat( $format );
987 return new WikitextContent( $text );
990 public function makeEmptyContent() {
991 return new WikitextContent( '' );
995 * Returns a ParserOutput object resulting from parsing the content's text using $wgParser.
999 * @param Content $content the content to render
1000 * @param \Title $title
1001 * @param null $revId
1002 * @param null|ParserOptions $options
1003 * @param bool $generateHtml
1005 * @internal param \IContextSource|null $context
1006 * @return ParserOutput representing the HTML form of the text
1008 public function getParserOutput( Content
$content, Title
$title, $revId = null, ParserOptions
$options = null, $generateHtml = true ) {
1011 $this->checkModelID( $content->getModel() );
1014 $options = new ParserOptions();
1017 $po = $wgParser->parse( $content->getNativeData(), $title, $options, true, true, $revId );
1021 protected function getHtml( Content
$content ) {
1022 throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
1026 * Returns true because wikitext supports sections.
1028 * @return boolean whether sections are supported.
1030 public function supportsSections() {
1035 #XXX: make ScriptContentHandler base class with plugin interface for syntax highlighting?
1040 class JavaScriptContentHandler
extends TextContentHandler
{
1042 public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT
) {
1043 parent
::__construct( $modelId, array( CONTENT_FORMAT_JAVASCRIPT
) );
1046 public function unserializeContent( $text, $format = null ) {
1047 $this->checkFormat( $format );
1049 return new JavaScriptContent( $text );
1052 public function makeEmptyContent() {
1053 return new JavaScriptContent( '' );
1056 protected function getHtml( Content
$content ) {
1058 $html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
1059 $html .= $this->getHighlightHtml( $content );
1060 $html .= "\n</pre>\n";
1069 class CssContentHandler
extends TextContentHandler
{
1071 public function __construct( $modelId = CONTENT_MODEL_CSS
) {
1072 parent
::__construct( $modelId, array( CONTENT_FORMAT_CSS
) );
1075 public function unserializeContent( $text, $format = null ) {
1076 $this->checkFormat( $format );
1078 return new CssContent( $text );
1081 public function makeEmptyContent() {
1082 return new CssContent( '' );
1086 protected function getHtml( Content
$content ) {
1088 $html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
1089 $html .= $this->getHighlightHtml( $content );
1090 $html .= "\n</pre>\n";