*/
abstract class ContentHandler {
++
+ /**
+ * Conveniance function for getting flat text from a Content object. This shleould only
+ * be used in the context of backwards compatibility with code that is not yet able
+ * to handle Content objects!
+ *
+ * If $content is equal to null or false, this method returns the empty string.
+ *
+ * If $content is an instance of TextContent, this method returns the flat text as returned by $content->getnativeData().
+ *
+ * If $content is not a TextContent object, the bahaviour of this method depends on the global $wgContentHandlerTextFallback:
+ * 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.
+ * Otherwise, this method returns null.
+ *
+ * @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'.
+ */
++
+ /**
+ * @abstract
+ * @param Content $content
+ * @param null $format
+ * @return String
+ */
+ public abstract function serialize( Content $content, $format = null );
+
+ /**
+ * TODO: calling unserialize on a ContentHandler returns a Content?!! Something looks wrong here...
+ *
+ * @abstract
+ * @param $blob String
+ * @param null $format
+ * @return Content
+ */
+ public abstract function unserialize( $blob, $format = null );
+
+ /**
+ * FIXME: bad method name: suggests it empties the content of an instance rather then creating a new empty one
+ */
+ public abstract function emptyContent();
+
++
public static function getContentText( Content $content = null ) {
global $wgContentHandlerTextFallback;
return null;
}
+ /**
+ * Conveniance function for creating a Content object from a given textual representation.
+ *
+ * $text will be deserialized into a Content object of the model specified by $modelName (or,
+ * if that is not given, $title->getContentModelName()) using the given format.
+ *
+ * @static
+ * @param $text the textual represenation, will be unserialized to create the Content object
+ * @param Title $title the title of the page this text belongs to, required as a context for deserialization
+ * @param null|String $modelName the model to deserialize to. If not provided, $title->getContentModelName() is used.
+ * @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
+ */
public static function makeContent( $text, Title $title, $modelName = null, $format = null ) {
- if ( !$modelName ) {
+
+ if ( is_null( $modelName ) ) {
$modelName = $title->getContentModelName();
}
return $wgContentHandlers[$modelName];
}
- # ----------------------------------------------------------------------------------------------------------
+ // ----------------------------------------------------------------------------------------------------------
+
+ /**
+ * Constructor, initializing the ContentHandler instance with it's model name and a list of supported formats.
+ * Values for the parameters are typically provided as literals by subclasses' constructors.
+ *
+ * @param String $modelName (use CONTENT_MODEL_XXX constants).
+ * @param array $formats list for supported serialization formats (typically as MIME types)
+ */
public function __construct( $modelName, $formats ) {
$this->mModelName = $modelName;
$this->mSupportedFormats = $formats;
}
+ /**
+ *
+ * @return String the model name
+ */
public function getModelName() {
- # for wikitext: wikitext; in the future: wikiast, wikidom?
- # for wikidata: wikidata
+ // for wikitext: wikitext; in the future: wikiast, wikidom?
+ // for wikidata: wikidata
return $this->mModelName;
}