2012-02-01 22:07:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
2012-06-03 17:27:23 +00:00
|
|
|
/**
|
|
|
|
|
* Exception representing a failure to serialize or unserialize a content object.
|
|
|
|
|
*/
|
2012-03-23 21:28:47 +00:00
|
|
|
class MWContentSerializationException extends MWException {
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
2012-02-01 22:07:47 +00:00
|
|
|
/**
|
|
|
|
|
* A content handler knows how do deal with a specific type of content on a wiki page.
|
|
|
|
|
* Content is stored in the database in a serialized form (using a serialization format aka mime type)
|
2012-06-03 17:27:23 +00:00
|
|
|
* and is be unserialized into it's native PHP representation (the content model), which is wrapped in
|
2012-04-16 09:01:56 +00:00
|
|
|
* an instance of the appropriate subclass of Content.
|
2012-04-16 10:35:38 +00:00
|
|
|
*
|
|
|
|
|
* ContentHandler instances are stateless singletons that serve, among other things, as a factory for
|
|
|
|
|
* Content objects. Generally, there is one subclass of ContentHandler and one subclass of Content
|
|
|
|
|
* for every type of content model.
|
|
|
|
|
*
|
2012-06-03 17:27:23 +00:00
|
|
|
* Some content types have a flat model, that is, their native representation is the
|
2012-02-01 22:07:47 +00:00
|
|
|
* same as their serialized form. Examples would be JavaScript and CSS code. As of now,
|
|
|
|
|
* this also applies to wikitext (mediawiki's default content type), but wikitext
|
|
|
|
|
* content may be represented by a DOM or AST structure in the future.
|
2012-04-26 11:24:13 +00:00
|
|
|
*
|
|
|
|
|
* @since 1.WD
|
2012-02-01 22:07:47 +00:00
|
|
|
*/
|
|
|
|
|
abstract class ContentHandler {
|
2012-03-05 11:53:21 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
/**
|
2012-06-03 17:27:23 +00:00
|
|
|
* Convenience function for getting flat text from a Content object. This should only
|
2012-04-25 16:24:33 +00:00
|
|
|
* be used in the context of backwards compatibility with code that is not yet able
|
|
|
|
|
* to handle Content objects!
|
|
|
|
|
*
|
|
|
|
|
* If $content is null, this method returns the empty string.
|
|
|
|
|
*
|
|
|
|
|
* If $content is an instance of TextContent, this method returns the flat text as returned by $content->getNativeData().
|
|
|
|
|
*
|
2012-06-03 17:27:23 +00:00
|
|
|
* If $content is not a TextContent object, the behavior of this method depends on the global $wgContentHandlerTextFallback:
|
2012-04-25 16:24:33 +00:00
|
|
|
* * If $wgContentHandlerTextFallback is 'fail' and $content is not a TextContent object, an MWException is thrown.
|
|
|
|
|
* * If $wgContentHandlerTextFallback is 'serialize' and $content is not a TextContent object, $content->serialize()
|
|
|
|
|
* is called to get a string form of the content.
|
|
|
|
|
* * If $wgContentHandlerTextFallback is 'ignore' and $content is not a TextContent object, this method returns null.
|
|
|
|
|
* * otherwise, the behaviour is undefined.
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @static
|
|
|
|
|
* @param Content|null $content
|
|
|
|
|
* @return null|string the textual form of $content, if available
|
|
|
|
|
* @throws MWException if $content is not an instance of TextContent and $wgContentHandlerTextFallback was set to 'fail'.
|
|
|
|
|
*/
|
|
|
|
|
public static function getContentText( Content $content = null ) {
|
|
|
|
|
global $wgContentHandlerTextFallback;
|
|
|
|
|
|
|
|
|
|
if ( is_null( $content ) ) {
|
2012-04-10 17:47:46 +00:00
|
|
|
return '';
|
|
|
|
|
}
|
2012-03-05 15:53:25 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
if ( $content instanceof TextContent ) {
|
|
|
|
|
return $content->getNativeData();
|
|
|
|
|
}
|
2012-03-05 15:53:25 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
if ( $wgContentHandlerTextFallback == 'fail' ) {
|
2012-05-13 22:02:29 +00:00
|
|
|
throw new MWException( "Attempt to get text from Content with model " . $content->getModel() );
|
2012-04-10 17:47:46 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
if ( $wgContentHandlerTextFallback == 'serialize' ) {
|
2012-04-10 17:47:46 +00:00
|
|
|
return $content->serialize();
|
|
|
|
|
}
|
2012-03-09 17:23:05 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-06-03 17:27:23 +00:00
|
|
|
* Convenience function for creating a Content object from a given textual representation.
|
2012-04-25 16:24:33 +00:00
|
|
|
*
|
2012-05-13 22:02:29 +00:00
|
|
|
* $text will be deserialized into a Content object of the model specified by $modelId (or,
|
|
|
|
|
* if that is not given, $title->getContentModel()) using the given format.
|
2012-04-25 16:24:33 +00:00
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @static
|
2012-06-03 17:27:23 +00:00
|
|
|
* @param string $text the textual representation, will be unserialized to create the Content object
|
2012-04-25 16:24:33 +00:00
|
|
|
* @param Title $title the title of the page this text belongs to, required as a context for deserialization
|
2012-05-13 22:02:29 +00:00
|
|
|
* @param null|String $modelId the model to deserialize to. If not provided, $title->getContentModel() is used.
|
2012-04-25 16:24:33 +00:00
|
|
|
* @param null|String $format the format to use for deserialization. If not given, the model's default format is used.
|
|
|
|
|
*
|
|
|
|
|
* @return Content a Content object representing $text
|
|
|
|
|
* @throw MWException if $model or $format is not supported or if $text can not be unserialized using $format.
|
|
|
|
|
*/
|
2012-05-13 22:02:29 +00:00
|
|
|
public static function makeContent( $text, Title $title, $modelId = null, $format = null ) {
|
2012-04-25 16:24:33 +00:00
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
if ( is_null( $modelId ) ) {
|
|
|
|
|
$modelId = $title->getContentModel();
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
$handler = ContentHandler::getForModelID( $modelId );
|
2012-04-25 16:24:33 +00:00
|
|
|
return $handler->unserializeContent( $text, $format );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the name of the default content model to be used for the page with the given title.
|
|
|
|
|
*
|
|
|
|
|
* Note: There should rarely be need to call this method directly.
|
2012-05-13 22:02:29 +00:00
|
|
|
* To determine the actual content model for a given page, use Title::getContentModel().
|
2012-04-25 16:24:33 +00:00
|
|
|
*
|
|
|
|
|
* Which model is to be used per default for the page is determined based on several factors:
|
|
|
|
|
* * The global setting $wgNamespaceContentModels specifies a content model per namespace.
|
|
|
|
|
* * The hook DefaultModelFor may be used to override the page's default model.
|
|
|
|
|
* * Pages in NS_MEDIAWIKI and NS_USER default to the CSS or JavaScript model if they end in .js or .css, respectively.
|
|
|
|
|
* * Pages in NS_MEDIAWIKI default to the wikitext model otherwise.
|
|
|
|
|
* * 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.
|
|
|
|
|
* * The hook TitleIsWikitextPage may be used to force a page to use the wikitext model.
|
|
|
|
|
*
|
|
|
|
|
* If none of the above applies, the wikitext model is used.
|
|
|
|
|
*
|
2012-05-13 22:02:29 +00:00
|
|
|
* Note: this is used by, and may thus not use, Title::getContentModel()
|
2012-04-25 16:24:33 +00:00
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @static
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @return null|string default model name for the page given by $title
|
|
|
|
|
*/
|
|
|
|
|
public static function getDefaultModelFor( Title $title ) {
|
|
|
|
|
global $wgNamespaceContentModels;
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
// NOTE: this method must not rely on $title->getContentModel() directly or indirectly,
|
2012-06-03 17:27:23 +00:00
|
|
|
// because it is used to initialized the mContentModel member.
|
2012-04-25 16:24:33 +00:00
|
|
|
|
|
|
|
|
$ns = $title->getNamespace();
|
|
|
|
|
|
|
|
|
|
$ext = false;
|
|
|
|
|
$m = null;
|
|
|
|
|
$model = null;
|
|
|
|
|
|
|
|
|
|
if ( !empty( $wgNamespaceContentModels[ $ns ] ) ) {
|
|
|
|
|
$model = $wgNamespaceContentModels[ $ns ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// hook can determin default model
|
2012-04-26 13:12:40 +00:00
|
|
|
if ( !wfRunHooks( 'ContentHandlerDefaultModelFor', array( $title, &$model ) ) ) {
|
2012-04-25 16:24:33 +00:00
|
|
|
if ( !is_null( $model ) ) {
|
2012-04-10 17:47:46 +00:00
|
|
|
return $model;
|
|
|
|
|
}
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
2012-03-05 11:53:21 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
// Could this page contain custom CSS or JavaScript, based on the title?
|
|
|
|
|
$isCssOrJsPage = NS_MEDIAWIKI == $ns && preg_match( '!\.(css|js)$!u', $title->getText(), $m );
|
|
|
|
|
if ( $isCssOrJsPage ) {
|
2012-04-10 17:47:46 +00:00
|
|
|
$ext = $m[1];
|
|
|
|
|
}
|
2012-03-05 11:53:21 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
// hook can force js/css
|
|
|
|
|
wfRunHooks( 'TitleIsCssOrJsPage', array( $title, &$isCssOrJsPage ) );
|
2012-03-05 11:53:21 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
// Is this a .css subpage of a user page?
|
|
|
|
|
$isJsCssSubpage = NS_USER == $ns && !$isCssOrJsPage && preg_match( "/\\/.*\\.(js|css)$/", $title->getText(), $m );
|
|
|
|
|
if ( $isJsCssSubpage ) {
|
2012-04-10 17:47:46 +00:00
|
|
|
$ext = $m[1];
|
|
|
|
|
}
|
2012-03-05 11:53:21 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
// is this wikitext, according to $wgNamespaceContentModels or the DefaultModelFor hook?
|
|
|
|
|
$isWikitext = is_null( $model ) || $model == CONTENT_MODEL_WIKITEXT;
|
|
|
|
|
$isWikitext = $isWikitext && !$isCssOrJsPage && !$isJsCssSubpage;
|
2012-03-05 11:53:21 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
// hook can override $isWikitext
|
|
|
|
|
wfRunHooks( 'TitleIsWikitextPage', array( $title, &$isWikitext ) );
|
2012-03-05 11:53:21 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
if ( !$isWikitext ) {
|
2012-04-10 17:47:46 +00:00
|
|
|
switch ( $ext ) {
|
|
|
|
|
case 'js':
|
|
|
|
|
return CONTENT_MODEL_JAVASCRIPT;
|
|
|
|
|
case 'css':
|
|
|
|
|
return CONTENT_MODEL_CSS;
|
|
|
|
|
default:
|
|
|
|
|
return is_null( $model ) ? CONTENT_MODEL_TEXT : $model;
|
|
|
|
|
}
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// we established that is must be wikitext
|
|
|
|
|
|
|
|
|
|
return CONTENT_MODEL_WIKITEXT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* returns the appropriate ContentHandler singleton for the given title
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @static
|
|
|
|
|
* @param Title $title
|
|
|
|
|
* @return ContentHandler
|
|
|
|
|
*/
|
|
|
|
|
public static function getForTitle( Title $title ) {
|
2012-05-13 22:02:29 +00:00
|
|
|
$modelId = $title->getContentModel();
|
|
|
|
|
return ContentHandler::getForModelID( $modelId );
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* returns the appropriate ContentHandler singleton for the given Content object
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @static
|
|
|
|
|
* @param Content $content
|
|
|
|
|
* @return ContentHandler
|
|
|
|
|
*/
|
|
|
|
|
public static function getForContent( Content $content ) {
|
2012-05-13 22:02:29 +00:00
|
|
|
$modelId = $content->getModel();
|
|
|
|
|
return ContentHandler::getForModelID( $modelId );
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-13 22:02:29 +00:00
|
|
|
* returns the ContentHandler singleton for the given model id. Use the CONTENT_MODEL_XXX constants to
|
2012-04-25 16:24:33 +00:00
|
|
|
* identify the desired content model.
|
|
|
|
|
*
|
|
|
|
|
* ContentHandler singletons are take from the global $wgContentHandlers array. Keys in that array are
|
|
|
|
|
* model names, the values are either ContentHandler singleton objects, or strings specifying the appropriate
|
|
|
|
|
* subclass of ContentHandler.
|
|
|
|
|
*
|
|
|
|
|
* If a class name in encountered when looking up the singleton for a given model name, the class is
|
|
|
|
|
* instantiated and the class name is replaced by te resulting singleton in $wgContentHandlers.
|
|
|
|
|
*
|
2012-05-13 22:02:29 +00:00
|
|
|
* If no ContentHandler is defined for the desired $modelId, the ContentHandler may be provided by the
|
2012-06-03 17:27:23 +00:00
|
|
|
* a ContentHandlerForModelID hook. if no ContentHandler can be determined, an MWException is raised.
|
2012-04-25 16:24:33 +00:00
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @static
|
2012-05-13 22:02:29 +00:00
|
|
|
* @param $modelId int the id of the content model for which to get a handler. Use CONTENT_MODEL_XXX constants.
|
|
|
|
|
* @return ContentHandler the ContentHandler singleton for handling the model given by $modelId
|
|
|
|
|
* @throws MWException if no handler is known for $modelId.
|
2012-04-25 16:24:33 +00:00
|
|
|
*/
|
2012-05-13 22:02:29 +00:00
|
|
|
public static function getForModelID( $modelId ) {
|
2012-04-25 16:24:33 +00:00
|
|
|
global $wgContentHandlers;
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
if ( empty( $wgContentHandlers[$modelId] ) ) {
|
2012-04-25 16:24:33 +00:00
|
|
|
$handler = null;
|
2012-03-27 12:15:30 +00:00
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
wfRunHooks( 'ContentHandlerForModelID', array( $modelId, &$handler ) );
|
2012-04-25 16:24:33 +00:00
|
|
|
|
|
|
|
|
if ( $handler ) { // NOTE: may be a string or an object, either is fine!
|
2012-05-13 22:02:29 +00:00
|
|
|
$wgContentHandlers[$modelId] = $handler;
|
2012-04-25 16:24:33 +00:00
|
|
|
} else {
|
2012-05-13 22:02:29 +00:00
|
|
|
throw new MWException( "No handler for model #$modelId registered in \$wgContentHandlers" );
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
if ( is_string( $wgContentHandlers[$modelId] ) ) {
|
|
|
|
|
$class = $wgContentHandlers[$modelId];
|
|
|
|
|
$wgContentHandlers[$modelId] = new $class( $modelId );
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
return $wgContentHandlers[$modelId];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the appropriate mime type for a given content format,
|
|
|
|
|
* or null if no mime type is known for this format.
|
|
|
|
|
*
|
|
|
|
|
* Mime types can be registered in the global array $wgContentFormatMimeTypes.
|
|
|
|
|
*
|
|
|
|
|
* @static
|
|
|
|
|
* @param int $id the content format id, as given by a CONTENT_FORMAT_XXX constant
|
|
|
|
|
* or returned by Revision::getContentFormat().
|
|
|
|
|
*
|
|
|
|
|
* @return String|null the content format's mime type.
|
|
|
|
|
*/
|
|
|
|
|
public static function getContentFormatMimeType( $id ) {
|
|
|
|
|
global $wgContentFormatMimeTypes;
|
|
|
|
|
|
|
|
|
|
if ( !isset( $wgContentFormatMimeTypes[ $id ] ) ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $wgContentFormatMimeTypes[ $id ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the content format if for a given mime type,
|
|
|
|
|
* or null if no format id if known for this mime type.
|
|
|
|
|
*
|
|
|
|
|
* Mime types can be registered in the global array $wgContentFormatMimeTypes.
|
|
|
|
|
*
|
|
|
|
|
* @static
|
|
|
|
|
* @param String $mime the mime type
|
|
|
|
|
*
|
|
|
|
|
* @return int|null the format id, as defined by a CONTENT_FORMAT_XXX constant
|
|
|
|
|
*/
|
|
|
|
|
public static function getContentFormatID( $mime ) {
|
|
|
|
|
global $wgContentFormatMimeTypes;
|
|
|
|
|
|
|
|
|
|
static $format_ids = null;
|
|
|
|
|
|
|
|
|
|
if ( $format_ids === null ) {
|
|
|
|
|
$format_ids = array_flip( $wgContentFormatMimeTypes );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( !isset( $format_ids[ $mime ] ) ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $format_ids[ $mime ];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns the localized name for a given content model,
|
|
|
|
|
* or null of no mime type is known.
|
|
|
|
|
*
|
|
|
|
|
* Model names are localized using system messages. Message keys
|
2012-06-03 17:27:23 +00:00
|
|
|
* have the form content-model-$id.
|
2012-05-13 22:02:29 +00:00
|
|
|
*
|
|
|
|
|
* @static
|
|
|
|
|
* @param int $id the content model id, as given by a CONTENT_MODEL_XXX constant
|
|
|
|
|
* or returned by Revision::getContentModel().
|
|
|
|
|
*
|
|
|
|
|
* @return String|null the content format's mime type.
|
|
|
|
|
*/
|
|
|
|
|
public static function getContentModelName( $id ) {
|
|
|
|
|
$key = "content-model-$id";
|
|
|
|
|
|
|
|
|
|
if ( wfEmptyMsg( $key ) ) return null;
|
|
|
|
|
else return wfMsg( $key );
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------------------------------------
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
protected $mModelID;
|
|
|
|
|
protected $mSupportedFormats;
|
|
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
/**
|
2012-05-13 22:02:29 +00:00
|
|
|
* Constructor, initializing the ContentHandler instance with it's model id and a list of supported formats.
|
2012-04-25 16:24:33 +00:00
|
|
|
* Values for the parameters are typically provided as literals by subclasses' constructors.
|
|
|
|
|
*
|
2012-05-13 22:02:29 +00:00
|
|
|
* @param int $modelId (use CONTENT_MODEL_XXX constants).
|
2012-04-25 16:24:33 +00:00
|
|
|
* @param array $formats list for supported serialization formats (typically as MIME types)
|
|
|
|
|
*/
|
2012-05-13 22:02:29 +00:00
|
|
|
public function __construct( $modelId, $formats ) {
|
|
|
|
|
$this->mModelID = $modelId;
|
2012-04-25 16:24:33 +00:00
|
|
|
$this->mSupportedFormats = $formats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Serializes Content object of the type supported by this ContentHandler.
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @abstract
|
|
|
|
|
* @param Content $content the Content object to serialize
|
|
|
|
|
* @param null $format the desired serialization format
|
|
|
|
|
* @return String serialized form of the content
|
|
|
|
|
*/
|
|
|
|
|
public abstract function serializeContent( Content $content, $format = null );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Unserializes a Content object of the type supported by this ContentHandler.
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @abstract
|
|
|
|
|
* @param $blob String serialized form of the content
|
|
|
|
|
* @param null $format the format used for serialization
|
|
|
|
|
* @return Content the Content object created by deserializing $blob
|
|
|
|
|
*/
|
|
|
|
|
public abstract function unserializeContent( $blob, $format = null );
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Creates an empty Content object of the type supported by this ContentHandler.
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:34:36 +00:00
|
|
|
* @return Content
|
2012-04-25 16:24:33 +00:00
|
|
|
*/
|
|
|
|
|
public abstract function makeEmptyContent();
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-13 22:02:29 +00:00
|
|
|
* Returns the model id that identifies the content model this ContentHandler can handle.
|
2012-04-25 16:24:33 +00:00
|
|
|
* Use with the CONTENT_MODEL_XXX constants.
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-05-13 22:02:29 +00:00
|
|
|
* @return int the model id
|
2012-04-25 16:24:33 +00:00
|
|
|
*/
|
2012-05-13 22:02:29 +00:00
|
|
|
public function getModelID() {
|
|
|
|
|
return $this->mModelID;
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-05-13 22:02:29 +00:00
|
|
|
* Throws an MWException if $model_id is not the id of the content model
|
|
|
|
|
* supported by this ContentHandler.
|
2012-04-25 16:24:33 +00:00
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-05-13 22:02:29 +00:00
|
|
|
* @param int $model_id the model to check
|
2012-06-03 17:27:23 +00:00
|
|
|
*
|
|
|
|
|
* @throws MWException
|
2012-04-25 16:24:33 +00:00
|
|
|
*/
|
2012-05-13 22:02:29 +00:00
|
|
|
protected function checkModelID( $model_id ) {
|
|
|
|
|
if ( $model_id !== $this->mModelID ) {
|
|
|
|
|
$model_name = ContentHandler::getContentModelName( $model_id );
|
|
|
|
|
$own_model_name = ContentHandler::getContentModelName( $this->mModelID );
|
|
|
|
|
|
|
|
|
|
throw new MWException( "Bad content model: expected {$this->mModelID} ($own_model_name) but got found $model_id ($model_name)." );
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a list of serialization formats supported by the serializeContent() and unserializeContent() methods of
|
|
|
|
|
* this ContentHandler.
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @return array of serialization formats as MIME type like strings
|
|
|
|
|
*/
|
|
|
|
|
public function getSupportedFormats() {
|
|
|
|
|
return $this->mSupportedFormats;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* The format used for serialization/deserialization per default by this ContentHandler.
|
|
|
|
|
*
|
|
|
|
|
* This default implementation will return the first element of the array of formats
|
|
|
|
|
* that was passed to the constructor.
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-06-03 17:27:23 +00:00
|
|
|
* @return String the name of the default serialization format as a MIME type
|
2012-04-25 16:24:33 +00:00
|
|
|
*/
|
|
|
|
|
public function getDefaultFormat() {
|
|
|
|
|
return $this->mSupportedFormats[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true if $format is a serialization format supported by this ContentHandler,
|
|
|
|
|
* and false otherwise.
|
|
|
|
|
*
|
|
|
|
|
* Note that if $format is null, this method always returns true, because null
|
|
|
|
|
* means "use the default format".
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:34:36 +00:00
|
|
|
* @param String $format the serialization format to check
|
2012-04-25 16:24:33 +00:00
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function isSupportedFormat( $format ) {
|
|
|
|
|
|
|
|
|
|
if ( !$format ) {
|
2012-04-11 10:49:43 +00:00
|
|
|
return true; // this means "use the default"
|
|
|
|
|
}
|
2012-03-27 12:15:30 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
return in_array( $format, $this->mSupportedFormats );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Throws an MWException if isSupportedFormat( $format ) is not true. Convenient
|
|
|
|
|
* for checking whether a format provided as a parameter is actually supported.
|
|
|
|
|
*
|
2012-04-25 16:34:36 +00:00
|
|
|
* @param String $format the serialization format to check
|
2012-06-03 17:27:23 +00:00
|
|
|
*
|
|
|
|
|
* @throws MWException
|
2012-04-25 16:24:33 +00:00
|
|
|
*/
|
|
|
|
|
protected function checkFormat( $format ) {
|
|
|
|
|
if ( !$this->isSupportedFormat( $format ) ) {
|
2012-05-13 22:02:29 +00:00
|
|
|
throw new MWException( "Format $format is not supported for content model " . $this->getModelID() );
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-05-23 07:30:26 +00:00
|
|
|
/**
|
|
|
|
|
* Returns if the content is consistent with the database, that is if saving it to the database would not violate any
|
|
|
|
|
* global constraints.
|
|
|
|
|
*
|
|
|
|
|
* Content needs to be valid using this method before it can be saved.
|
|
|
|
|
*
|
|
|
|
|
* This default implementation always returns true.
|
|
|
|
|
*
|
|
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-06-03 17:27:23 +00:00
|
|
|
* @param \Content $content
|
|
|
|
|
*
|
2012-05-23 07:30:26 +00:00
|
|
|
* @return boolean
|
|
|
|
|
*/
|
|
|
|
|
public function isConsistentWithDatabase( Content $content ) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
/**
|
|
|
|
|
* Returns overrides for action handlers.
|
|
|
|
|
* Classes listed here will be used instead of the default one when
|
|
|
|
|
* (and only when) $wgActions[$action] === true. This allows subclasses
|
|
|
|
|
* to override the default action handlers.
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @return Array
|
|
|
|
|
*/
|
|
|
|
|
public function getActionOverrides() {
|
|
|
|
|
return array();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-06-03 17:27:23 +00:00
|
|
|
* Factory creating an appropriate DifferenceEngine for this content model.
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-06-03 17:27:23 +00:00
|
|
|
* @param $context IContextSource context to use, anything else will be ignored
|
|
|
|
|
* @param $old Integer old ID we want to show and diff with.
|
|
|
|
|
* @param int|String $new String either 'prev' or 'next'.
|
|
|
|
|
* @param $rcid Integer ??? FIXME (default 0)
|
|
|
|
|
* @param $refreshCache boolean If set, refreshes the diff cache
|
|
|
|
|
* @param $unhide boolean If set, allow viewing deleted revs
|
2012-04-10 17:47:46 +00:00
|
|
|
*
|
|
|
|
|
* @return DifferenceEngine
|
2012-04-25 16:24:33 +00:00
|
|
|
*/
|
|
|
|
|
public function createDifferenceEngine( IContextSource $context, $old = 0, $new = 0, $rcid = 0, #FIMXE: use everywhere!
|
|
|
|
|
$refreshCache = false, $unhide = false ) {
|
2012-03-23 15:18:20 +00:00
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
$this->checkModelID( $context->getTitle()->getContentModel() );
|
2012-03-27 12:15:30 +00:00
|
|
|
|
2012-04-12 14:01:33 +00:00
|
|
|
$diffEngineClass = $this->getDiffEngineClass();
|
2012-03-23 15:18:20 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
return new $diffEngineClass( $context, $old, $new, $rcid, $refreshCache, $unhide );
|
|
|
|
|
}
|
2012-02-01 22:07:47 +00:00
|
|
|
|
2012-04-12 14:01:33 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the name of the diff engine to use.
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
2012-04-12 14:01:33 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
protected function getDiffEngineClass() {
|
|
|
|
|
return 'DifferenceEngine';
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
/**
|
|
|
|
|
* attempts to merge differences between three versions.
|
|
|
|
|
* Returns a new Content object for a clean merge and false for failure or a conflict.
|
|
|
|
|
*
|
|
|
|
|
* This default implementation always returns false.
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-06-03 17:27:23 +00:00
|
|
|
* @param Content|String $oldContent String
|
|
|
|
|
* @param Content|String $myContent String
|
|
|
|
|
* @param Content|String $yourContent String
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @return Content|Bool
|
|
|
|
|
*/
|
|
|
|
|
public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2012-06-03 17:27:23 +00:00
|
|
|
* Return an applicable auto-summary if one exists for the given edit.
|
2012-04-25 16:24:33 +00:00
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @param $oldContent Content|null: the previous text of the page.
|
|
|
|
|
* @param $newContent Content|null: The submitted text of the page.
|
2012-06-03 17:27:23 +00:00
|
|
|
* @param $flags Int bit mask: a bit mask of flags submitted for the edit.
|
2012-04-25 16:24:33 +00:00
|
|
|
*
|
2012-06-03 17:27:23 +00:00
|
|
|
* @return string An appropriate auto-summary, or an empty string.
|
2012-04-25 16:24:33 +00:00
|
|
|
*/
|
|
|
|
|
public function getAutosummary( Content $oldContent = null, Content $newContent = null, $flags ) {
|
|
|
|
|
global $wgContLang;
|
|
|
|
|
|
2012-06-03 17:27:23 +00:00
|
|
|
// Decide what kind of auto-summary is needed.
|
2012-04-25 16:24:33 +00:00
|
|
|
|
2012-06-03 17:27:23 +00:00
|
|
|
// Redirect auto-summaries
|
2012-04-25 16:24:33 +00:00
|
|
|
|
2012-04-25 16:34:36 +00:00
|
|
|
/**
|
|
|
|
|
* @var $ot Title
|
|
|
|
|
* @var $rt Title
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
$ot = !is_null( $oldContent ) ? $oldContent->getRedirectTarget() : null;
|
|
|
|
|
$rt = !is_null( $newContent ) ? $newContent->getRedirectTarget() : null;
|
2012-04-25 16:24:33 +00:00
|
|
|
|
|
|
|
|
if ( is_object( $rt ) && ( !is_object( $ot ) || !$rt->equals( $ot ) || $ot->getFragment() != $rt->getFragment() ) ) {
|
|
|
|
|
|
|
|
|
|
$truncatedtext = $newContent->getTextForSummary(
|
|
|
|
|
250
|
|
|
|
|
- strlen( wfMsgForContent( 'autoredircomment' ) )
|
|
|
|
|
- strlen( $rt->getFullText() ) );
|
|
|
|
|
|
|
|
|
|
return wfMsgForContent( 'autoredircomment', $rt->getFullText(), $truncatedtext );
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-03 17:27:23 +00:00
|
|
|
// New page auto-summaries
|
2012-04-25 16:24:33 +00:00
|
|
|
if ( $flags & EDIT_NEW && $newContent->getSize() > 0 ) {
|
|
|
|
|
// If they're making a new article, give its text, truncated, in the summary.
|
|
|
|
|
|
|
|
|
|
$truncatedtext = $newContent->getTextForSummary(
|
|
|
|
|
200 - strlen( wfMsgForContent( 'autosumm-new' ) ) );
|
|
|
|
|
|
|
|
|
|
return wfMsgForContent( 'autosumm-new', $truncatedtext );
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-03 17:27:23 +00:00
|
|
|
// Blanking auto-summaries
|
2012-04-25 16:24:33 +00:00
|
|
|
if ( !empty( $oldContent ) && $oldContent->getSize() > 0 && $newContent->getSize() == 0 ) {
|
|
|
|
|
return wfMsgForContent( 'autosumm-blank' );
|
|
|
|
|
} elseif ( !empty( $oldContent ) && $oldContent->getSize() > 10 * $newContent->getSize() && $newContent->getSize() < 500 ) {
|
|
|
|
|
// Removing more than 90% of the article
|
|
|
|
|
|
|
|
|
|
$truncatedtext = $newContent->getTextForSummary(
|
|
|
|
|
200 - strlen( wfMsgForContent( 'autosumm-replace' ) ) );
|
|
|
|
|
|
|
|
|
|
return wfMsgForContent( 'autosumm-replace', $truncatedtext );
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-03 17:27:23 +00:00
|
|
|
// If we reach this point, there's no applicable auto-summary for our case, so our
|
|
|
|
|
// auto-summary is empty.
|
2012-04-25 16:24:33 +00:00
|
|
|
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Auto-generates a deletion reason
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @param $title Title: the page's title
|
|
|
|
|
* @param &$hasHistory Boolean: whether the page has a history
|
|
|
|
|
* @return mixed String containing deletion reason or empty string, or boolean false
|
|
|
|
|
* if no revision occurred
|
|
|
|
|
*
|
|
|
|
|
* @XXX &$hasHistory is extremely ugly, it's here because WikiPage::getAutoDeleteReason() and Article::getReason() have it / want it.
|
|
|
|
|
*/
|
|
|
|
|
public function getAutoDeleteReason( Title $title, &$hasHistory ) {
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
|
|
|
|
|
// Get the last revision
|
|
|
|
|
$rev = Revision::newFromTitle( $title );
|
|
|
|
|
|
|
|
|
|
if ( is_null( $rev ) ) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get the article's contents
|
|
|
|
|
$content = $rev->getContent();
|
|
|
|
|
$blank = false;
|
|
|
|
|
|
|
|
|
|
// If the page is blank, use the text from the previous revision,
|
|
|
|
|
// which can only be blank if there's a move/import/protect dummy revision involved
|
|
|
|
|
if ( $content->getSize() == 0 ) {
|
|
|
|
|
$prev = $rev->getPrevious();
|
|
|
|
|
|
|
|
|
|
if ( $prev ) {
|
2012-06-06 12:06:22 +00:00
|
|
|
$content = $prev->getContent();
|
2012-04-25 16:24:33 +00:00
|
|
|
$blank = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find out if there was only one contributor
|
|
|
|
|
// Only scan the last 20 revisions
|
|
|
|
|
$res = $dbw->select( 'revision', 'rev_user_text',
|
|
|
|
|
array( 'rev_page' => $title->getArticleID(), $dbw->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' ),
|
|
|
|
|
__METHOD__,
|
|
|
|
|
array( 'LIMIT' => 20 )
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
if ( $res === false ) {
|
|
|
|
|
// This page has no revisions, which is very weird
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$hasHistory = ( $res->numRows() > 1 );
|
|
|
|
|
$row = $dbw->fetchObject( $res );
|
|
|
|
|
|
|
|
|
|
if ( $row ) { // $row is false if the only contributor is hidden
|
|
|
|
|
$onlyAuthor = $row->rev_user_text;
|
|
|
|
|
// Try to find a second contributor
|
|
|
|
|
foreach ( $res as $row ) {
|
|
|
|
|
if ( $row->rev_user_text != $onlyAuthor ) { // Bug 22999
|
|
|
|
|
$onlyAuthor = false;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$onlyAuthor = false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Generate the summary with a '$1' placeholder
|
|
|
|
|
if ( $blank ) {
|
|
|
|
|
// The current revision is blank and the one before is also
|
|
|
|
|
// blank. It's just not our lucky day
|
|
|
|
|
$reason = wfMsgForContent( 'exbeforeblank', '$1' );
|
|
|
|
|
} else {
|
|
|
|
|
if ( $onlyAuthor ) {
|
|
|
|
|
$reason = wfMsgForContent( 'excontentauthor', '$1', $onlyAuthor );
|
|
|
|
|
} else {
|
|
|
|
|
$reason = wfMsgForContent( 'excontent', '$1' );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( $reason == '-' ) {
|
|
|
|
|
// Allow these UI messages to be blanked out cleanly
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Max content length = max comment length - length of the comment (excl. $1)
|
|
|
|
|
$text = $content->getTextForSummary( 255 - ( strlen( $reason ) - 2 ) );
|
|
|
|
|
|
|
|
|
|
// Now replace the '$1' placeholder
|
|
|
|
|
$reason = str_replace( '$1', $text, $reason );
|
|
|
|
|
|
|
|
|
|
return $reason;
|
|
|
|
|
}
|
|
|
|
|
|
2012-06-06 15:38:39 +00:00
|
|
|
/**
|
|
|
|
|
* Parse the Content object and generate a ParserObject from the result. $result->getText() can
|
|
|
|
|
* be used to obtain the generated HTML. If no HTML is needed, $generateHtml can be set to false;
|
|
|
|
|
* in that case, $result->getText() may return null.
|
|
|
|
|
*
|
|
|
|
|
* @param Content $content the content to render
|
|
|
|
|
* @param Title $title the page title to use as a context for rendering
|
|
|
|
|
* @param null|int $revId the revision being rendered (optional)
|
|
|
|
|
* @param null|ParserOptions $options any parser options
|
|
|
|
|
* @param Boolean $generateHtml whether to generate Html (default: true). If false,
|
|
|
|
|
* the result of calling getText() on the ParserOutput object returned by
|
|
|
|
|
* this method is undefined.
|
|
|
|
|
*
|
|
|
|
|
* @since WD.1
|
|
|
|
|
*
|
|
|
|
|
* @return ParserOutput
|
|
|
|
|
*/
|
|
|
|
|
public abstract function getParserOutput( Content $content, Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true );
|
|
|
|
|
#TODO: make RenderOutput and RenderOptions base classes
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns a list of DataUpdate objects for recording information about this Content in some secondary
|
|
|
|
|
* data store. If the optional second argument, $old, is given, the updates may model only the changes that
|
|
|
|
|
* need to be made to replace information about the old content with information about the new content.
|
|
|
|
|
*
|
|
|
|
|
* This default implementation calls $this->getParserOutput( $title, null, null, false ), and then
|
|
|
|
|
* calls getSecondaryDataUpdates( $title, $recursive ) on the resulting ParserOutput object.
|
|
|
|
|
*
|
|
|
|
|
* Subclasses may implement this to determine the necessary updates more efficiently, or make use of information
|
|
|
|
|
* about the old content.
|
|
|
|
|
*
|
|
|
|
|
* @param Title $title the context for determining the necessary updates
|
|
|
|
|
* @param Content|null $old a Content object representing the previous content, i.e. the content being
|
|
|
|
|
* replaced by this Content object.
|
|
|
|
|
* @param bool $recursive whether to include recursive updates (default: false).
|
|
|
|
|
*
|
|
|
|
|
* @return Array. A list of DataUpdate objects for putting information about this content object somewhere.
|
|
|
|
|
*
|
|
|
|
|
* @since WD.1
|
|
|
|
|
*/
|
|
|
|
|
public function getSecondaryDataUpdates( Content $content, Title $title, Content $old = null, $recursive = false ) {
|
|
|
|
|
$po = $this->getParserOutput( $content, $title, null, null, false );
|
|
|
|
|
return $po->getSecondaryDataUpdates( $title, $recursive );
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get the Content object that needs to be saved in order to undo all revisions
|
|
|
|
|
* between $undo and $undoafter. Revisions must belong to the same page,
|
|
|
|
|
* must exist and must not be deleted
|
2012-05-10 19:22:11 +00:00
|
|
|
*
|
|
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @param $current Revision the current text
|
|
|
|
|
* @param $undo Revision the revision to undo
|
|
|
|
|
* @param $undoafter Revision Must be an earlier revision than $undo
|
2012-05-10 19:22:11 +00:00
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @return mixed string on success, false on failure
|
|
|
|
|
*/
|
|
|
|
|
public function getUndoContent( Revision $current, Revision $undo, Revision $undoafter ) {
|
|
|
|
|
$cur_content = $current->getContent();
|
|
|
|
|
|
|
|
|
|
if ( empty( $cur_content ) ) {
|
|
|
|
|
return false; // no page
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$undo_content = $undo->getContent();
|
|
|
|
|
$undoafter_content = $undoafter->getContent();
|
|
|
|
|
|
|
|
|
|
if ( $cur_content->equals( $undo_content ) ) {
|
|
|
|
|
// No use doing a merge if it's just a straight revert.
|
|
|
|
|
return $undoafter_content;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$undone_content = $this->merge3( $undo_content, $undoafter_content, $cur_content );
|
|
|
|
|
|
|
|
|
|
return $undone_content;
|
|
|
|
|
}
|
2012-04-24 10:06:23 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Returns true for content models that support caching using the ParserCache mechanism.
|
|
|
|
|
* See WikiPage::isParserCacheUser().
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-04-25 16:34:36 +00:00
|
|
|
* @return bool
|
2012-04-24 10:06:23 +00:00
|
|
|
*/
|
|
|
|
|
public function isParserCacheSupported() {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
2012-04-30 13:32:31 +00:00
|
|
|
|
|
|
|
|
/**
|
2012-06-06 14:43:12 +00:00
|
|
|
* Returns a lost of updates to perform when the given content is deleted.
|
|
|
|
|
* The necessary updates may be taken from the Content object, or depend on the current state of the database.
|
|
|
|
|
*
|
2012-05-10 19:22:11 +00:00
|
|
|
* @since WD.1
|
|
|
|
|
*
|
2012-06-06 14:43:12 +00:00
|
|
|
* @param \Content $content the Content object for deletion
|
|
|
|
|
* @param \Title $title the title of the deleted page
|
|
|
|
|
* @param null|\ParserOutput $parserOutput optional parser output object for efficient access to meta-information
|
|
|
|
|
* about the content object. Provide if you have one handy.
|
2012-04-30 13:32:31 +00:00
|
|
|
*
|
2012-05-15 13:21:08 +00:00
|
|
|
* @return array a list of DataUpdate instances that will clean up the database ofter deletion.
|
2012-04-30 13:32:31 +00:00
|
|
|
*/
|
2012-06-06 14:43:12 +00:00
|
|
|
public function getDeletionUpdates( Content $content, Title $title, ParserOutput $parserOutput = null ) {
|
2012-04-30 13:32:31 +00:00
|
|
|
return array(
|
2012-06-06 14:43:12 +00:00
|
|
|
new LinksDeletionUpdate( $title ),
|
2012-04-30 13:32:31 +00:00
|
|
|
);
|
|
|
|
|
}
|
2012-02-01 22:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-10 19:22:11 +00:00
|
|
|
/**
|
|
|
|
|
* @since WD.1
|
|
|
|
|
*/
|
2012-03-06 17:35:46 +00:00
|
|
|
abstract class TextContentHandler extends ContentHandler {
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
public function __construct( $modelId, $formats ) {
|
|
|
|
|
parent::__construct( $modelId, $formats );
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-15 06:59:19 +00:00
|
|
|
/**
|
|
|
|
|
* Returns the content's text as-is.
|
|
|
|
|
*
|
|
|
|
|
* @param Content $content
|
|
|
|
|
* @param String|null $format
|
|
|
|
|
* @return mixed
|
|
|
|
|
*/
|
2012-04-25 16:24:33 +00:00
|
|
|
public function serializeContent( Content $content, $format = null ) {
|
|
|
|
|
$this->checkFormat( $format );
|
|
|
|
|
return $content->getNativeData();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* attempts to merge differences between three versions.
|
|
|
|
|
* Returns a new Content object for a clean merge and false for failure or a conflict.
|
|
|
|
|
*
|
|
|
|
|
* All three Content objects passed as parameters must have the same content model.
|
|
|
|
|
*
|
|
|
|
|
* This text-based implementation uses wfMerge().
|
|
|
|
|
*
|
2012-06-03 17:27:23 +00:00
|
|
|
* @param \Content|String $oldContent String
|
|
|
|
|
* @param \Content|String $myContent String
|
|
|
|
|
* @param \Content|String $yourContent String
|
|
|
|
|
*
|
2012-04-25 16:24:33 +00:00
|
|
|
* @return Content|Bool
|
|
|
|
|
*/
|
|
|
|
|
public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) {
|
2012-05-13 22:02:29 +00:00
|
|
|
$this->checkModelID( $oldContent->getModel() );
|
|
|
|
|
$this->checkModelID( $myContent->getModel() );
|
|
|
|
|
$this->checkModelID( $yourContent->getModel() );
|
2012-04-25 16:24:33 +00:00
|
|
|
|
|
|
|
|
$format = $this->getDefaultFormat();
|
|
|
|
|
|
|
|
|
|
$old = $this->serializeContent( $oldContent, $format );
|
|
|
|
|
$mine = $this->serializeContent( $myContent, $format );
|
|
|
|
|
$yours = $this->serializeContent( $yourContent, $format );
|
|
|
|
|
|
|
|
|
|
$ok = wfMerge( $old, $mine, $yours, $result );
|
|
|
|
|
|
|
|
|
|
if ( !$ok ) {
|
2012-04-10 17:47:46 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
if ( !$result ) {
|
2012-04-17 16:08:23 +00:00
|
|
|
return $this->makeEmptyContent();
|
2012-04-10 17:47:46 +00:00
|
|
|
}
|
2012-03-19 17:49:00 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
$mergedContent = $this->unserializeContent( $result, $format );
|
|
|
|
|
return $mergedContent;
|
|
|
|
|
}
|
2012-03-19 17:49:00 +00:00
|
|
|
|
2012-06-06 15:38:39 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
|
|
|
|
|
*
|
|
|
|
|
* @param Content $content the content to render
|
|
|
|
|
* @param Title $title context title for parsing
|
|
|
|
|
* @param int|null $revId revision id (the parser wants that for some reason)
|
|
|
|
|
* @param ParserOptions|null $options parser options
|
|
|
|
|
* @param bool $generateHtml whether or not to generate HTML
|
|
|
|
|
*
|
|
|
|
|
* @return ParserOutput representing the HTML form of the text
|
|
|
|
|
*/
|
|
|
|
|
public function getParserOutput( Content $content, Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
|
|
|
|
|
# generic implementation, relying on $this->getHtml()
|
|
|
|
|
|
|
|
|
|
if ( $generateHtml ) $html = $this->getHtml( $content );
|
|
|
|
|
else $html = '';
|
|
|
|
|
|
|
|
|
|
$po = new ParserOutput( $html );
|
|
|
|
|
return $po;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Generates an HTML version of the content, for display.
|
|
|
|
|
* Used by getParserOutput() to construct a ParserOutput object
|
|
|
|
|
*
|
|
|
|
|
* @param Content $content the content to render
|
|
|
|
|
*
|
|
|
|
|
* @return String
|
|
|
|
|
*/
|
|
|
|
|
protected abstract function getHtml( Content $content );
|
|
|
|
|
|
2012-03-19 17:49:00 +00:00
|
|
|
|
2012-02-01 22:07:47 +00:00
|
|
|
}
|
2012-05-10 19:22:11 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @since WD.1
|
|
|
|
|
*/
|
2012-03-06 17:35:46 +00:00
|
|
|
class WikitextContentHandler extends TextContentHandler {
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
public function __construct( $modelId = CONTENT_MODEL_WIKITEXT ) {
|
|
|
|
|
parent::__construct( $modelId, array( CONTENT_FORMAT_WIKITEXT ) );
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
2012-03-06 17:35:46 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
public function unserializeContent( $text, $format = null ) {
|
|
|
|
|
$this->checkFormat( $format );
|
2012-03-27 12:15:30 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
return new WikitextContent( $text );
|
|
|
|
|
}
|
2012-03-06 17:35:46 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
public function makeEmptyContent() {
|
|
|
|
|
return new WikitextContent( '' );
|
|
|
|
|
}
|
2012-03-09 17:51:10 +00:00
|
|
|
|
2012-06-06 15:38:39 +00:00
|
|
|
/**
|
|
|
|
|
* Returns a ParserOutput object resulting from parsing the content's text using $wgParser.
|
|
|
|
|
*
|
|
|
|
|
* @since WD.1
|
|
|
|
|
*
|
|
|
|
|
* @param Content $content the content to render
|
|
|
|
|
* @param \Title $title
|
|
|
|
|
* @param null $revId
|
|
|
|
|
* @param null|ParserOptions $options
|
|
|
|
|
* @param bool $generateHtml
|
|
|
|
|
*
|
|
|
|
|
* @internal param \IContextSource|null $context
|
|
|
|
|
* @return ParserOutput representing the HTML form of the text
|
|
|
|
|
*/
|
|
|
|
|
public function getParserOutput( Content $content, Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
|
|
|
|
|
global $wgParser;
|
|
|
|
|
|
|
|
|
|
if ( !$options ) {
|
|
|
|
|
$options = new ParserOptions();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$po = $wgParser->parse( $content->getNativeData(), $title, $options, true, true, $revId );
|
|
|
|
|
return $po;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected function getHtml( Content $content ) {
|
|
|
|
|
throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
|
|
|
|
|
}
|
2012-03-20 09:57:41 +00:00
|
|
|
|
2012-03-06 17:35:46 +00:00
|
|
|
}
|
|
|
|
|
|
2012-04-25 16:10:25 +00:00
|
|
|
#XXX: make ScriptContentHandler base class with plugin interface for syntax highlighting?
|
2012-03-27 12:15:30 +00:00
|
|
|
|
2012-05-10 19:22:11 +00:00
|
|
|
/**
|
|
|
|
|
* @since WD.1
|
|
|
|
|
*/
|
2012-03-06 17:35:46 +00:00
|
|
|
class JavaScriptContentHandler extends TextContentHandler {
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) {
|
|
|
|
|
parent::__construct( $modelId, array( CONTENT_FORMAT_JAVASCRIPT ) );
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
2012-03-06 17:35:46 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
public function unserializeContent( $text, $format = null ) {
|
|
|
|
|
$this->checkFormat( $format );
|
2012-04-20 19:33:13 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
return new JavaScriptContent( $text );
|
|
|
|
|
}
|
2012-02-01 22:07:47 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
public function makeEmptyContent() {
|
|
|
|
|
return new JavaScriptContent( '' );
|
|
|
|
|
}
|
2012-06-06 15:38:39 +00:00
|
|
|
|
|
|
|
|
protected function getHtml( Content $content ) {
|
|
|
|
|
$html = "";
|
|
|
|
|
$html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
|
|
|
|
|
$html .= htmlspecialchars( $content->getNativeData() );
|
|
|
|
|
$html .= "\n</pre>\n";
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
2012-02-01 22:07:47 +00:00
|
|
|
}
|
|
|
|
|
|
2012-05-10 19:22:11 +00:00
|
|
|
/**
|
|
|
|
|
* @since WD.1
|
|
|
|
|
*/
|
2012-03-06 17:35:46 +00:00
|
|
|
class CssContentHandler extends TextContentHandler {
|
|
|
|
|
|
2012-05-13 22:02:29 +00:00
|
|
|
public function __construct( $modelId = CONTENT_MODEL_CSS ) {
|
|
|
|
|
parent::__construct( $modelId, array( CONTENT_FORMAT_CSS ) );
|
2012-04-25 16:24:33 +00:00
|
|
|
}
|
2012-03-06 17:35:46 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
public function unserializeContent( $text, $format = null ) {
|
|
|
|
|
$this->checkFormat( $format );
|
2012-04-20 19:33:13 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
return new CssContent( $text );
|
|
|
|
|
}
|
2012-03-06 17:35:46 +00:00
|
|
|
|
2012-04-25 16:24:33 +00:00
|
|
|
public function makeEmptyContent() {
|
|
|
|
|
return new CssContent( '' );
|
|
|
|
|
}
|
2012-03-09 17:51:10 +00:00
|
|
|
|
2012-06-06 15:38:39 +00:00
|
|
|
|
|
|
|
|
protected function getHtml( Content $content ) {
|
|
|
|
|
$html = "";
|
|
|
|
|
$html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
|
|
|
|
|
$html .= htmlspecialchars( $content->getNativeData() );
|
|
|
|
|
$html .= "\n</pre>\n";
|
|
|
|
|
|
|
|
|
|
return $html;
|
|
|
|
|
}
|
2012-02-01 22:07:47 +00:00
|
|
|
}
|