3 class MWContentSerializationException
extends MWException
{
9 * A content handler knows how do deal with a specific type of content on a wiki page.
10 * Content is stored in the database in a serialized form (using a serialization format aka mime type)
11 * and is be unserialized into it's native PHP represenation (the content model), which is wrappe in
12 * an instance of the appropriate subclass of Content.
14 * ContentHandler instances are stateless singletons that serve, among other things, as a factory for
15 * Content objects. Generally, there is one subclass of ContentHandler and one subclass of Content
16 * for every type of content model.
18 * Some content types have a flat model, that is, their native represenation is the
19 * same as their serialized form. Examples would be JavaScript and CSS code. As of now,
20 * this also applies to wikitext (mediawiki's default content type), but wikitext
21 * content may be represented by a DOM or AST structure in the future.
25 abstract class ContentHandler
{
28 * Conveniance function for getting flat text from a Content object. This should only
29 * be used in the context of backwards compatibility with code that is not yet able
30 * to handle Content objects!
32 * If $content is null, this method returns the empty string.
34 * If $content is an instance of TextContent, this method returns the flat text as returned by $content->getNativeData().
36 * If $content is not a TextContent object, the bahaviour of this method depends on the global $wgContentHandlerTextFallback:
37 * * If $wgContentHandlerTextFallback is 'fail' and $content is not a TextContent object, an MWException is thrown.
38 * * If $wgContentHandlerTextFallback is 'serialize' and $content is not a TextContent object, $content->serialize()
39 * is called to get a string form of the content.
40 * * If $wgContentHandlerTextFallback is 'ignore' and $content is not a TextContent object, this method returns null.
41 * * otherwise, the behaviour is undefined.
46 * @param Content|null $content
47 * @return null|string the textual form of $content, if available
48 * @throws MWException if $content is not an instance of TextContent and $wgContentHandlerTextFallback was set to 'fail'.
50 public static function getContentText( Content
$content = null ) {
51 global $wgContentHandlerTextFallback;
53 if ( is_null( $content ) ) {
57 if ( $content instanceof TextContent
) {
58 return $content->getNativeData();
61 if ( $wgContentHandlerTextFallback == 'fail' ) {
62 throw new MWException( "Attempt to get text from Content with model " . $content->getModelName() );
65 if ( $wgContentHandlerTextFallback == 'serialize' ) {
66 return $content->serialize();
73 * Conveniance function for creating a Content object from a given textual representation.
75 * $text will be deserialized into a Content object of the model specified by $modelName (or,
76 * if that is not given, $title->getContentModelName()) using the given format.
81 * @param string $text the textual represenation, will be unserialized to create the Content object
82 * @param Title $title the title of the page this text belongs to, required as a context for deserialization
83 * @param null|String $modelName the model to deserialize to. If not provided, $title->getContentModelName() is used.
84 * @param null|String $format the format to use for deserialization. If not given, the model's default format is used.
86 * @return Content a Content object representing $text
87 * @throw MWException if $model or $format is not supported or if $text can not be unserialized using $format.
89 public static function makeContent( $text, Title
$title, $modelName = null, $format = null ) {
91 if ( is_null( $modelName ) ) {
92 $modelName = $title->getContentModelName();
95 $handler = ContentHandler
::getForModelName( $modelName );
96 return $handler->unserializeContent( $text, $format );
100 * Returns the name of the default content model to be used for the page with the given title.
102 * Note: There should rarely be need to call this method directly.
103 * To determine the actual content model for a given page, use Title::getContentModelName().
105 * Which model is to be used per default for the page is determined based on several factors:
106 * * The global setting $wgNamespaceContentModels specifies a content model per namespace.
107 * * The hook DefaultModelFor may be used to override the page's default model.
108 * * Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript model if they end in .js or .css, respectively.
109 * * Pages in NS_MEDIAWIKI default to the wikitext model otherwise.
110 * * 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.
111 * * The hook TitleIsWikitextPage may be used to force a page to use the wikitext model.
113 * If none of the above applies, the wikitext model is used.
115 * Note: this is used by, and may thus not use, Title::getContentModelName()
120 * @param Title $title
121 * @return null|string default model name for the page given by $title
123 public static function getDefaultModelFor( Title
$title ) {
124 global $wgNamespaceContentModels;
126 // NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
127 // because it is used to initialized the mContentModelName memebr.
129 $ns = $title->getNamespace();
135 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
136 $model = $wgNamespaceContentModels[ $ns ];
139 // hook can determin default model
140 if ( !wfRunHooks( 'ContentHandlerDefaultModelFor', array( $title, &$model ) ) ) {
141 if ( !is_null( $model ) ) {
146 // Could this page contain custom CSS or JavaScript, based on the title?
147 $isCssOrJsPage = NS_MEDIAWIKI
== $ns && preg_match( '!\.(css|js)$!u', $title->getText(), $m );
148 if ( $isCssOrJsPage ) {
152 // hook can force js/css
153 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage ) );
155 // Is this a .css subpage of a user page?
156 $isJsCssSubpage = NS_USER
== $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m );
157 if ( $isJsCssSubpage ) {
161 // is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
162 $isWikitext = is_null( $model ) ||
$model == CONTENT_MODEL_WIKITEXT
;
163 $isWikitext = $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage;
165 // hook can override $isWikitext
166 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
168 if ( !$isWikitext ) {
171 return CONTENT_MODEL_JAVASCRIPT
;
173 return CONTENT_MODEL_CSS
;
175 return is_null( $model ) ? CONTENT_MODEL_TEXT
: $model;
179 // we established that is must be wikitext
181 return CONTENT_MODEL_WIKITEXT
;
185 * returns the appropriate ContentHandler singleton for the given title
190 * @param Title $title
191 * @return ContentHandler
193 public static function getForTitle( Title
$title ) {
194 $modelName = $title->getContentModelName();
195 return ContentHandler
::getForModelName( $modelName );
199 * returns the appropriate ContentHandler singleton for the given Content object
204 * @param Content $content
205 * @return ContentHandler
207 public static function getForContent( Content
$content ) {
208 $modelName = $content->getModelName();
209 return ContentHandler
::getForModelName( $modelName );
213 * returns the ContentHandler singleton for the given model name. Use the CONTENT_MODEL_XXX constants to
214 * identify the desired content model.
216 * ContentHandler singletons are take from the global $wgContentHandlers array. Keys in that array are
217 * model names, the values are either ContentHandler singleton objects, or strings specifying the appropriate
218 * subclass of ContentHandler.
220 * If a class name in encountered when looking up the singleton for a given model name, the class is
221 * instantiated and the class name is replaced by te resulting singleton in $wgContentHandlers.
223 * If no ContentHandler is defined for the desired $modelName, the ContentHandler may be provided by the
224 * a ContentHandlerForModelName hook. if no Contenthandler can be determined, an MWException is raised.
229 * @param $modelName String the name of the content model for which to get a handler. Use CONTENT_MODEL_XXX constants.
230 * @return ContentHandler the ContentHandler singleton for handling the model given by $modelName
231 * @throws MWException if no handler is known for $modelName.
233 public static function getForModelName( $modelName ) {
234 global $wgContentHandlers;
236 if ( empty( $wgContentHandlers[$modelName] ) ) {
239 wfRunHooks( 'ContentHandlerForModelName', array( $modelName, &$handler ) );
241 if ( $handler ) { // NOTE: may be a string or an object, either is fine!
242 $wgContentHandlers[$modelName] = $handler;
244 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
248 if ( is_string( $wgContentHandlers[$modelName] ) ) {
249 $class = $wgContentHandlers[$modelName];
250 $wgContentHandlers[$modelName] = new $class( $modelName );
253 return $wgContentHandlers[$modelName];
256 // ----------------------------------------------------------------------------------------------------------
259 * Constructor, initializing the ContentHandler instance with it's model name and a list of supported formats.
260 * Values for the parameters are typically provided as literals by subclasses' constructors.
262 * @param String $modelName (use CONTENT_MODEL_XXX constants).
263 * @param array $formats list for supported serialization formats (typically as MIME types)
265 public function __construct( $modelName, $formats ) {
266 $this->mModelName
= $modelName;
267 $this->mSupportedFormats
= $formats;
272 * Serializes Content object of the type supported by this ContentHandler.
277 * @param Content $content the Content object to serialize
278 * @param null $format the desired serialization format
279 * @return String serialized form of the content
281 public abstract function serializeContent( Content
$content, $format = null );
284 * Unserializes a Content object of the type supported by this ContentHandler.
289 * @param $blob String serialized form of the content
290 * @param null $format the format used for serialization
291 * @return Content the Content object created by deserializing $blob
293 public abstract function unserializeContent( $blob, $format = null );
296 * Creates an empty Content object of the type supported by this ContentHandler.
302 public abstract function makeEmptyContent();
305 * Returns the model name that identifies the content model this ContentHandler can handle.
306 * Use with the CONTENT_MODEL_XXX constants.
310 * @return String the model name
312 public function getModelName() {
313 return $this->mModelName
;
317 * Throws an MWException if $modelName is not the content model handeled by this ContentHandler.
321 * @param String $modelName the model name to check
323 protected function checkModelName( $modelName ) {
324 if ( $modelName !== $this->mModelName
) {
325 throw new MWException( "Bad content model: expected " . $this->mModelName
. " but got found " . $modelName );
330 * Returns a list of serialization formats supported by the serializeContent() and unserializeContent() methods of
331 * this ContentHandler.
335 * @return array of serialization formats as MIME type like strings
337 public function getSupportedFormats() {
338 return $this->mSupportedFormats
;
342 * The format used for serialization/deserialization per default by this ContentHandler.
344 * This default implementation will return the first element of the array of formats
345 * that was passed to the constructor.
349 * @return String the name of the default serialiozation format as a MIME type
351 public function getDefaultFormat() {
352 return $this->mSupportedFormats
[0];
356 * Returns true if $format is a serialization format supported by this ContentHandler,
357 * and false otherwise.
359 * Note that if $format is null, this method always returns true, because null
360 * means "use the default format".
364 * @param String $format the serialization format to check
367 public function isSupportedFormat( $format ) {
370 return true; // this means "use the default"
373 return in_array( $format, $this->mSupportedFormats
);
377 * Throws an MWException if isSupportedFormat( $format ) is not true. Convenient
378 * for checking whether a format provided as a parameter is actually supported.
380 * @param String $format the serialization format to check
382 protected function checkFormat( $format ) {
383 if ( !$this->isSupportedFormat( $format ) ) {
384 throw new MWException( "Format $format is not supported for content model " . $this->getModelName() );
389 * Returns overrides for action handlers.
390 * Classes listed here will be used instead of the default one when
391 * (and only when) $wgActions[$action] === true. This allows subclasses
392 * to override the default action handlers.
398 public function getActionOverrides() {
403 * Return an Article object suitable for viewing the given object
405 * NOTE: does *not* do special handling for Image and Category pages!
406 * Use Article::newFromTitle() for that!
410 * @param Title $title
412 * @todo Article is being refactored into an action class, keep track of that
413 * @todo Article really defines the view of the content... rename this method to createViewPage ?
415 public function createArticle( Title
$title ) {
416 $this->checkModelName( $title->getContentModelName() );
418 $article = new Article($title);
423 * Return an EditPage object suitable for editing the given object
427 * @param Article $article
430 public function createEditPage( Article
$article ) {
431 $this->checkModelName( $article->getContentModelName() );
433 $editPage = new EditPage( $article );
438 * Return an ExternalEdit object suitable for editing the given object
442 * @param IContextSource $context
443 * @return ExternalEdit
444 * @todo does anyone or anythign actually use the external edit facility? Can we just deprecate and ignore it?
446 public function createExternalEdit( IContextSource
$context ) {
447 $this->checkModelName( $context->getTitle()->getContentModelName() );
449 $externalEdit = new ExternalEdit( $context );
450 return $externalEdit;
457 * @param $context IContextSource context to use, anything else will be ignored
458 * @param $old Integer old ID we want to show and diff with.
459 * @param $new String either 'prev' or 'next'.
460 * @param $rcid Integer ??? FIXME (default 0)
461 * @param $refreshCache boolean If set, refreshes the diff cache
462 * @param $unhide boolean If set, allow viewing deleted revs
464 * @return DifferenceEngine
466 public function createDifferenceEngine( IContextSource
$context, $old = 0, $new = 0, $rcid = 0, #FIMXE: use everywhere!
467 $refreshCache = false, $unhide = false ) {
469 $this->checkModelName( $context->getTitle()->getContentModelName() );
471 $diffEngineClass = $this->getDiffEngineClass();
473 return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
477 * Returns the name of the diff engine to use.
483 protected function getDiffEngineClass() {
484 return 'DifferenceEngine';
488 * attempts to merge differences between three versions.
489 * Returns a new Content object for a clean merge and false for failure or a conflict.
491 * This default implementation always returns false.
495 * @param $oldContent String
496 * @param $myContent String
497 * @param $yourContent String
498 * @return Content|Bool
500 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
505 * Return an applicable autosummary if one exists for the given edit.
509 * @param $oldContent Content|null: the previous text of the page.
510 * @param $newContent Content|null: The submitted text of the page.
511 * @param $flags Int bitmask: a bitmask of flags submitted for the edit.
513 * @return string An appropriate autosummary, or an empty string.
515 public function getAutosummary( Content
$oldContent = null, Content
$newContent = null, $flags ) {
518 // Decide what kind of autosummary is needed.
520 // Redirect autosummaries
527 $ot = !is_null( $oldContent ) ?
$oldContent->getRedirectTarget() : null;
528 $rt = !is_null( $newContent ) ?
$newContent->getRedirectTarget() : null;
530 if ( is_object( $rt ) && ( !is_object( $ot ) ||
!$rt->equals( $ot ) ||
$ot->getFragment() != $rt->getFragment() ) ) {
532 $truncatedtext = $newContent->getTextForSummary(
534 - strlen( wfMsgForContent( 'autoredircomment' ) )
535 - strlen( $rt->getFullText() ) );
537 return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext );
540 // New page autosummaries
541 if ( $flags & EDIT_NEW
&& $newContent->getSize() > 0 ) {
542 // If they're making a new article, give its text, truncated, in the summary.
544 $truncatedtext = $newContent->getTextForSummary(
545 200 - strlen( wfMsgForContent( 'autosumm-new' ) ) );
547 return wfMsgForContent( 'autosumm-new', $truncatedtext );
550 // Blanking autosummaries
551 if ( !empty( $oldContent ) && $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
552 return wfMsgForContent( 'autosumm-blank' );
553 } elseif ( !empty( $oldContent ) && $oldContent->getSize() > 10 * $newContent->getSize() && $newContent->getSize() < 500 ) {
554 // Removing more than 90% of the article
556 $truncatedtext = $newContent->getTextForSummary(
557 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) );
559 return wfMsgForContent( 'autosumm-replace', $truncatedtext );
562 // If we reach this point, there's no applicable autosummary for our case, so our
563 // autosummary is empty.
569 * Auto-generates a deletion reason
573 * @param $title Title: the page's title
574 * @param &$hasHistory Boolean: whether the page has a history
575 * @return mixed String containing deletion reason or empty string, or boolean false
576 * if no revision occurred
578 * @XXX &$hasHistory is extremely ugly, it's here because WikiPage::getAutoDeleteReason() and Article::getReason() have it / want it.
580 public function getAutoDeleteReason( Title
$title, &$hasHistory ) {
581 $dbw = wfGetDB( DB_MASTER
);
583 // Get the last revision
584 $rev = Revision
::newFromTitle( $title );
586 if ( is_null( $rev ) ) {
590 // Get the article's contents
591 $content = $rev->getContent();
594 // If the page is blank, use the text from the previous revision,
595 // which can only be blank if there's a move/import/protect dummy revision involved
596 if ( $content->getSize() == 0 ) {
597 $prev = $rev->getPrevious();
600 $content = $rev->getContent();
605 // Find out if there was only one contributor
606 // Only scan the last 20 revisions
607 $res = $dbw->select( 'revision', 'rev_user_text',
608 array( 'rev_page' => $title->getArticleID(), $dbw->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0' ),
610 array( 'LIMIT' => 20 )
613 if ( $res === false ) {
614 // This page has no revisions, which is very weird
618 $hasHistory = ( $res->numRows() > 1 );
619 $row = $dbw->fetchObject( $res );
621 if ( $row ) { // $row is false if the only contributor is hidden
622 $onlyAuthor = $row->rev_user_text
;
623 // Try to find a second contributor
624 foreach ( $res as $row ) {
625 if ( $row->rev_user_text
!= $onlyAuthor ) { // Bug 22999
634 // Generate the summary with a '$1' placeholder
636 // The current revision is blank and the one before is also
637 // blank. It's just not our lucky day
638 $reason = wfMsgForContent( 'exbeforeblank', '$1' );
641 $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
643 $reason = wfMsgForContent( 'excontent', '$1' );
647 if ( $reason == '-' ) {
648 // Allow these UI messages to be blanked out cleanly
652 // Max content length = max comment length - length of the comment (excl. $1)
653 $text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
655 // Now replace the '$1' placeholder
656 $reason = str_replace( '$1', $text, $reason );
661 #@TODO: getSecondaryUpdatesForDeletion( Content ) returns an array of SecondaryDataUpdate objects
662 #... or do that in the Content class?
665 * Get the Content object that needs to be saved in order to undo all revisions
666 * between $undo and $undoafter. Revisions must belong to the same page,
667 * must exist and must not be deleted
671 * @param $current Revision the current text
672 * @param $undo Revision the revision to undo
673 * @param $undoafter Revision Must be an earlier revision than $undo
675 * @return mixed string on success, false on failure
677 public function getUndoContent( Revision
$current, Revision
$undo, Revision
$undoafter ) {
678 $cur_content = $current->getContent();
680 if ( empty( $cur_content ) ) {
681 return false; // no page
684 $undo_content = $undo->getContent();
685 $undoafter_content = $undoafter->getContent();
687 if ( $cur_content->equals( $undo_content ) ) {
688 // No use doing a merge if it's just a straight revert.
689 return $undoafter_content;
692 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
694 return $undone_content;
698 * Returns true for content models that support caching using the ParserCache mechanism.
699 * See WikiPage::isParserCacheUser().
705 public function isParserCacheSupported() {
712 * @param $page WikiPage the page that was deleted (note: $page->getId() must still return the old page ID!)
714 * @return array a list of SecondaryDataUpdate instances that will clean up the database ofter deletion.
716 public function getDeletionUpdates( WikiPage
$page ) {
718 new LinksDeletionUpdate( $page ),
726 abstract class TextContentHandler
extends ContentHandler
{
728 public function __construct( $modelName, $formats ) {
729 parent
::__construct( $modelName, $formats );
732 public function serializeContent( Content
$content, $format = null ) {
733 $this->checkFormat( $format );
734 return $content->getNativeData();
738 * attempts to merge differences between three versions.
739 * Returns a new Content object for a clean merge and false for failure or a conflict.
741 * All three Content objects passed as parameters must have the same content model.
743 * This text-based implementation uses wfMerge().
745 * @param $oldContent String
746 * @param $myContent String
747 * @param $yourContent String
748 * @return Content|Bool
750 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
751 $this->checkModelName( $oldContent->getModelName() );
752 $this->checkModelName( $myContent->getModelName() );
753 $this->checkModelName( $yourContent->getModelName() );
755 $format = $this->getDefaultFormat();
757 $old = $this->serializeContent( $oldContent, $format );
758 $mine = $this->serializeContent( $myContent, $format );
759 $yours = $this->serializeContent( $yourContent, $format );
761 $ok = wfMerge( $old, $mine, $yours, $result );
768 return $this->makeEmptyContent();
771 $mergedContent = $this->unserializeContent( $result, $format );
772 return $mergedContent;
781 class WikitextContentHandler
extends TextContentHandler
{
783 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
784 parent
::__construct( $modelName, array( 'text/x-wiki' ) );
787 public function unserializeContent( $text, $format = null ) {
788 $this->checkFormat( $format );
790 return new WikitextContent( $text );
793 public function makeEmptyContent() {
794 return new WikitextContent( '' );
800 #XXX: make ScriptContentHandler base class with plugin interface for syntax highlighting?
805 class JavaScriptContentHandler
extends TextContentHandler
{
807 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
808 parent
::__construct( $modelName, array( 'text/javascript' ) ); #XXX: or use $wgJsMimeType? this is for internal storage, not HTTP...
811 public function unserializeContent( $text, $format = null ) {
812 $this->checkFormat( $format );
814 return new JavaScriptContent( $text );
817 public function makeEmptyContent() {
818 return new JavaScriptContent( '' );
825 class CssContentHandler
extends TextContentHandler
{
827 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
828 parent
::__construct( $modelName, array( 'text/css' ) );
831 public function unserializeContent( $text, $format = null ) {
832 $this->checkFormat( $format );
834 return new CssContent( $text );
837 public function makeEmptyContent() {
838 return new CssContent( '' );