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).
13 * Some content types have a flat model, that is, their native represenation is the
14 * same as their serialized form. Examples would be JavaScript and CSS code. As of now,
15 * this also applies to wikitext (mediawiki's default content type), but wikitext
16 * content may be represented by a DOM or AST structure in the future.
18 * TODO: add documentation
20 abstract class ContentHandler
{
24 * @param Content $content
28 public abstract function serialize( Content
$content, $format = null );
31 * TODO: calling unserialize on a ContentHandler returns a Content?!! Something looks wrong here...
38 public abstract function unserialize( $blob, $format = null );
41 * FIXME: bad method name: suggests it empties the content of an instance rather then creating a new empty one
43 public abstract function emptyContent();
45 public static function getContentText( Content
$content = null ) {
46 global $wgContentHandlerTextFallback;
48 if ( is_null( $content ) ) {
52 if ( $content instanceof TextContent
) {
53 return $content->getNativeData();
56 if ( $wgContentHandlerTextFallback == 'fail' ) {
57 throw new MWException( "Attempt to get text from Content with model " . $content->getModelName() );
60 if ( $wgContentHandlerTextFallback == 'serialize' ) {
61 return $content->serialize();
67 public static function makeContent( $text, Title
$title, $modelName = null, $format = null ) {
69 if ( is_null( $modelName ) ) {
70 $modelName = $title->getContentModelName();
73 $handler = ContentHandler
::getForModelName( $modelName );
74 return $handler->unserialize( $text, $format );
77 public static function getDefaultModelFor( Title
$title ) {
78 global $wgNamespaceContentModels;
80 // NOTE: this method must not rely on $title->getContentModelName() directly or indirectly,
81 // because it is used to initialized the mContentModelName memebr.
83 $ns = $title->getNamespace();
89 if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
90 $model = $wgNamespaceContentModels[ $ns ];
93 // hook can determin default model
94 if ( !wfRunHooks( 'DefaultModelFor', array( $title, &$model ) ) ) { #FIXME: document new hook!
95 if ( !is_null( $model ) ) {
100 // Could this page contain custom CSS or JavaScript, based on the title?
101 $isCssOrJsPage = NS_MEDIAWIKI
== $ns && preg_match( '!\.(css|js)$!u', $title->getText(), $m );
102 if ( $isCssOrJsPage ) {
106 // hook can force js/css
107 wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage ) );
109 // Is this a .css subpage of a user page?
110 $isJsCssSubpage = NS_USER
== $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m );
111 if ( $isJsCssSubpage ) {
115 // is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
116 $isWikitext = is_null( $model ) ||
$model == CONTENT_MODEL_WIKITEXT
;
117 $isWikitext = $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage;
119 // hook can override $isWikitext
120 wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
122 if ( !$isWikitext ) {
125 return CONTENT_MODEL_JAVASCRIPT
;
127 return CONTENT_MODEL_CSS
;
129 return is_null( $model ) ? CONTENT_MODEL_TEXT
: $model;
133 // we established that is must be wikitext
135 return CONTENT_MODEL_WIKITEXT
;
138 public static function getForTitle( Title
$title ) {
139 $modelName = $title->getContentModelName();
140 return ContentHandler
::getForModelName( $modelName );
143 public static function getForContent( Content
$content ) {
144 $modelName = $content->getModelName();
145 return ContentHandler
::getForModelName( $modelName );
150 * @param $modelName String the name of the content model for which to get a handler. Use CONTENT_MODEL_XXX constants.
151 * @return ContentHandler
152 * @throws MWException
154 public static function getForModelName( $modelName ) {
155 global $wgContentHandlers;
157 if ( empty( $wgContentHandlers[$modelName] ) ) {
160 // TODO: document new hook
161 wfRunHooks( 'ContentHandlerForModelName', array( $modelName, &$handler ) );
163 if ( $handler ) { // NOTE: may be a string or an object, either is fine!
164 $wgContentHandlers[$modelName] = $handler;
166 throw new MWException( "No handler for model $modelName registered in \$wgContentHandlers" );
170 if ( is_string( $wgContentHandlers[$modelName] ) ) {
171 $class = $wgContentHandlers[$modelName];
172 $wgContentHandlers[$modelName] = new $class( $modelName );
175 return $wgContentHandlers[$modelName];
178 // ----------------------------------------------------------------------------------------------------------
179 public function __construct( $modelName, $formats ) {
180 $this->mModelName
= $modelName;
181 $this->mSupportedFormats
= $formats;
184 public function getModelName() {
185 // for wikitext: wikitext; in the future: wikiast, wikidom?
186 // for wikidata: wikidata
187 return $this->mModelName
;
190 protected function checkModelName( $modelName ) {
191 if ( $modelName !== $this->mModelName
) {
192 throw new MWException( "Bad content model: expected " . $this->mModelName
. " but got found " . $modelName );
196 public function getSupportedFormats() {
197 // for wikitext: "text/x-mediawiki-1", "text/x-mediawiki-2", etc
198 // for wikidata: "application/json", "application/x-php", etc
199 return $this->mSupportedFormats
;
202 public function getDefaultFormat() {
203 return $this->mSupportedFormats
[0];
206 public function isSupportedFormat( $format ) {
209 return true; // this means "use the default"
212 return in_array( $format, $this->mSupportedFormats
);
215 protected function checkFormat( $format ) {
216 if ( !$this->isSupportedFormat( $format ) ) {
217 throw new MWException( "Format $format is not supported for content model " . $this->getModelName() );
222 * Return an Article object suitable for viewing the given object
224 * NOTE: does *not* do special handling for Image and Category pages!
225 * Use Article::newFromTitle() for that!
227 * @param Title $title
229 * @todo Article is being refactored into an action class, keep track of that
231 public function createArticle( Title
$title ) {
232 $this->checkModelName( $title->getContentModelName() );
234 $article = new Article($title);
239 * Return an EditPage object suitable for editing the given object
241 * @param Article $article
244 public function createEditPage( Article
$article ) {
245 $this->checkModelName( $article->getContentModelName() );
247 $editPage = new EditPage( $article );
252 * Return an ExternalEdit object suitable for editing the given object
254 * @param IContextSource $context
255 * @return ExternalEdit
257 public function createExternalEdit( IContextSource
$context ) {
258 $this->checkModelName( $context->getTitle()->getModelName() );
260 $externalEdit = new ExternalEdit( $context );
261 return $externalEdit;
266 * @param $context IContextSource context to use, anything else will be ignored
267 * @param $old Integer old ID we want to show and diff with.
268 * @param $new String either 'prev' or 'next'.
269 * @param $rcid Integer ??? FIXME (default 0)
270 * @param $refreshCache boolean If set, refreshes the diff cache
271 * @param $unhide boolean If set, allow viewing deleted revs
273 * @return DifferenceEngine
275 public function getDifferenceEngine( IContextSource
$context, $old = 0, $new = 0, $rcid = 0, #FIMXE: use everywhere!
276 $refreshCache = false, $unhide = false ) {
278 $this->checkModelName( $context->getTitle()->getModelName() );
280 $diffEngineClass = $this->getDiffEngineClass();
282 return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
286 * Returns the name of the diff engine to use.
292 protected function getDiffEngineClass() {
293 return 'DifferenceEngine';
297 * attempts to merge differences between three versions.
298 * Returns a new Content object for a clean merge and false for failure or a conflict.
300 * This default implementation always returns false.
302 * @param $oldContent String
303 * @param $myContent String
304 * @param $yourContent String
305 * @return Content|Bool
307 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
312 * Return an applicable autosummary if one exists for the given edit.
314 * @param $oldContent Content|null: the previous text of the page.
315 * @param $newContent Content|null: The submitted text of the page.
316 * @param $flags Int bitmask: a bitmask of flags submitted for the edit.
318 * @return string An appropriate autosummary, or an empty string.
320 public function getAutosummary( Content
$oldContent = null, Content
$newContent = null, $flags ) {
323 // Decide what kind of autosummary is needed.
325 // Redirect autosummaries
327 $ot = !empty( $ot ) ?
$oldContent->getRedirectTarget() : false;
328 $rt = !empty( $rt ) ?
$newContent->getRedirectTarget() : false;
330 if ( is_object( $rt ) && ( !is_object( $ot ) ||
!$rt->equals( $ot ) ||
$ot->getFragment() != $rt->getFragment() ) ) {
332 $truncatedtext = $newContent->getTextForSummary(
334 - strlen( wfMsgForContent( 'autoredircomment' ) )
335 - strlen( $rt->getFullText() ) );
337 return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext );
340 // New page autosummaries
341 if ( $flags & EDIT_NEW
&& $newContent->getSize() > 0 ) {
342 // If they're making a new article, give its text, truncated, in the summary.
344 $truncatedtext = $newContent->getTextForSummary(
345 200 - strlen( wfMsgForContent( 'autosumm-new' ) ) );
347 return wfMsgForContent( 'autosumm-new', $truncatedtext );
350 // Blanking autosummaries
351 if ( $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
352 return wfMsgForContent( 'autosumm-blank' );
353 } elseif ( $oldContent->getSize() > 10 * $newContent->getSize() && $newContent->getSize() < 500 ) {
354 // Removing more than 90% of the article
356 $truncatedtext = $newContent->getTextForSummary(
357 200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) );
359 return wfMsgForContent( 'autosumm-replace', $truncatedtext );
362 // If we reach this point, there's no applicable autosummary for our case, so our
363 // autosummary is empty.
369 * Auto-generates a deletion reason
371 * @param $title Title: the page's title
372 * @param &$hasHistory Boolean: whether the page has a history
373 * @return mixed String containing deletion reason or empty string, or boolean false
374 * if no revision occurred
376 public function getAutoDeleteReason( Title
$title, &$hasHistory ) {
377 $dbw = wfGetDB( DB_MASTER
);
379 // Get the last revision
380 $rev = Revision
::newFromTitle( $title );
382 if ( is_null( $rev ) ) {
386 // Get the article's contents
387 $content = $rev->getContent();
390 // If the page is blank, use the text from the previous revision,
391 // which can only be blank if there's a move/import/protect dummy revision involved
392 if ( $content->getSize() == 0 ) {
393 $prev = $rev->getPrevious();
396 $content = $rev->getContent();
401 // Find out if there was only one contributor
402 // Only scan the last 20 revisions
403 $res = $dbw->select( 'revision', 'rev_user_text',
404 array( 'rev_page' => $title->getArticleID(), $dbw->bitAnd( 'rev_deleted', Revision
::DELETED_USER
) . ' = 0' ),
406 array( 'LIMIT' => 20 )
409 if ( $res === false ) {
410 // This page has no revisions, which is very weird
414 $hasHistory = ( $res->numRows() > 1 );
415 $row = $dbw->fetchObject( $res );
417 if ( $row ) { // $row is false if the only contributor is hidden
418 $onlyAuthor = $row->rev_user_text
;
419 // Try to find a second contributor
420 foreach ( $res as $row ) {
421 if ( $row->rev_user_text
!= $onlyAuthor ) { // Bug 22999
430 // Generate the summary with a '$1' placeholder
432 // The current revision is blank and the one before is also
433 // blank. It's just not our lucky day
434 $reason = wfMsgForContent( 'exbeforeblank', '$1' );
437 $reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
439 $reason = wfMsgForContent( 'excontent', '$1' );
443 if ( $reason == '-' ) {
444 // Allow these UI messages to be blanked out cleanly
448 // Max content length = max comment length - length of the comment (excl. $1)
449 $text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
451 // Now replace the '$1' placeholder
452 $reason = str_replace( '$1', $text, $reason );
458 * Get the Content object that needs to be saved in order to undo all revisions
459 * between $undo and $undoafter. Revisions must belong to the same page,
460 * must exist and must not be deleted
461 * @param $undo Revision
462 * @param $undoafter null|Revision Must be an earlier revision than $undo
463 * @return mixed string on success, false on failure
465 public function getUndoContent( Revision
$current, Revision
$undo, Revision
$undoafter = null ) {
466 $cur_content = $current->getContent();
468 if ( empty( $cur_content ) ) {
469 return false; // no page
472 $undo_content = $undo->getContent();
473 $undoafter_content = $undoafter->getContent();
475 if ( $cur_content->equals( $undo_content ) ) {
476 // No use doing a merge if it's just a straight revert.
477 return $undoafter_content;
480 $undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
482 return $undone_content;
487 abstract class TextContentHandler
extends ContentHandler
{
489 public function __construct( $modelName, $formats ) {
490 parent
::__construct( $modelName, $formats );
493 public function serialize( Content
$content, $format = null ) {
494 $this->checkFormat( $format );
495 return $content->getNativeData();
499 * attempts to merge differences between three versions.
500 * Returns a new Content object for a clean merge and false for failure or a conflict.
502 * This text-based implementation uses wfMerge().
504 * @param $oldContent String
505 * @param $myContent String
506 * @param $yourContent String
507 * @return Content|Bool
509 public function merge3( Content
$oldContent, Content
$myContent, Content
$yourContent ) {
510 $this->checkModelName( $oldContent->getModelName() );
511 #TODO: check that all Content objects have the same content model! #XXX: what to do if they don't?
513 $format = $this->getDefaultFormat();
515 $old = $this->serialize( $oldContent, $format );
516 $mine = $this->serialize( $myContent, $format );
517 $yours = $this->serialize( $yourContent, $format );
519 $ok = wfMerge( $old, $mine, $yours, $result );
526 return $this->emptyContent();
529 $mergedContent = $this->unserialize( $result, $format );
530 return $mergedContent;
535 class WikitextContentHandler
extends TextContentHandler
{
537 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
538 parent
::__construct( $modelName, array( 'application/x-wikitext' ) ); #FIXME: mime
541 public function unserialize( $text, $format = null ) {
542 $this->checkFormat( $format );
544 return new WikitextContent( $text );
547 public function emptyContent() {
548 return new WikitextContent( '' );
554 #TODO: make ScriptContentHandler base class with plugin interface for syntax highlighting!
556 class JavaScriptContentHandler
extends TextContentHandler
{
558 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
559 parent
::__construct( $modelName, array( 'text/javascript' ) ); #XXX: or use $wgJsMimeType? this is for internal storage, not HTTP...
562 public function unserialize( $text, $format = null ) {
563 return new JavaScriptContent( $text );
566 public function emptyContent() {
567 return new JavaScriptContent( '' );
571 class CssContentHandler
extends TextContentHandler
{
573 public function __construct( $modelName = CONTENT_MODEL_WIKITEXT
) {
574 parent
::__construct( $modelName, array( 'text/css' ) );
577 public function unserialize( $text, $format = null ) {
578 return new CssContent( $text );
581 public function emptyContent() {
582 return new CssContent( '' );