From bccf2755d47ec6d1eddfcdd91fe9cfc2acab0f5e Mon Sep 17 00:00:00 2001 From: jeroendedauw Date: Tue, 16 Oct 2012 20:20:43 +0200 Subject: [PATCH] Moved ContentHandler deriving classes into their own files for better discoverability and more manageable line count per file Change-Id: I37db53e1d1420e61d3af4566322bcdba85ec778b --- includes/AutoLoader.php | 8 +- includes/content/ContentHandler.php | 236 ------------------ includes/content/CssContentHandler.php | 43 ++++ includes/content/JavaScriptContentHandler.php | 45 ++++ includes/content/TextContentHandler.php | 90 +++++++ includes/content/WikitextContentHandler.php | 63 +++++ 6 files changed, 245 insertions(+), 240 deletions(-) create mode 100644 includes/content/CssContentHandler.php create mode 100644 includes/content/JavaScriptContentHandler.php create mode 100644 includes/content/TextContentHandler.php create mode 100644 includes/content/WikitextContentHandler.php diff --git a/includes/AutoLoader.php b/includes/AutoLoader.php index 182104d358..1cf66348d6 100644 --- a/includes/AutoLoader.php +++ b/includes/AutoLoader.php @@ -294,14 +294,14 @@ $wgAutoloadLocalClasses = array( 'AbstractContent' => 'includes/content/AbstractContent.php', 'ContentHandler' => 'includes/content/ContentHandler.php', 'Content' => 'includes/content/Content.php', - 'CssContentHandler' => 'includes/content/ContentHandler.php', + 'CssContentHandler' => 'includes/content/CssContentHandler.php', 'CssContent' => 'includes/content/CssContent.php', - 'JavaScriptContentHandler' => 'includes/content/ContentHandler.php', + 'JavaScriptContentHandler' => 'includes/content/JavaScriptContentHandler.php', 'JavaScriptContent' => 'includes/content/JavaScriptContent.php', 'MessageContent' => 'includes/content/MessageContent.php', - 'TextContentHandler' => 'includes/content/ContentHandler.php', + 'TextContentHandler' => 'includes/content/TextContentHandler.php', 'TextContent' => 'includes/content/TextContent.php', - 'WikitextContentHandler' => 'includes/content/ContentHandler.php', + 'WikitextContentHandler' => 'includes/content/WikitextContentHandler.php', 'WikitextContent' => 'includes/content/WikitextContent.php', # includes/actions diff --git a/includes/content/ContentHandler.php b/includes/content/ContentHandler.php index 4bb4e408e2..bf21021a7e 100644 --- a/includes/content/ContentHandler.php +++ b/includes/content/ContentHandler.php @@ -1081,239 +1081,3 @@ abstract class ContentHandler { } } -/** - * @since 1.21 - */ -class TextContentHandler extends ContentHandler { - - public function __construct( $modelId = CONTENT_MODEL_TEXT, $formats = array( CONTENT_FORMAT_TEXT ) ) { - parent::__construct( $modelId, $formats ); - } - - /** - * Returns the content's text as-is. - * - * @param $content Content - * @param $format string|null - * @return mixed - */ - 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(). - * - * @param $oldContent \Content|string String - * @param $myContent \Content|string String - * @param $yourContent \Content|string String - * - * @return Content|Bool - */ - public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) { - $this->checkModelID( $oldContent->getModel() ); - $this->checkModelID( $myContent->getModel() ); - $this->checkModelID( $yourContent->getModel() ); - - $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 ) { - return false; - } - - if ( !$result ) { - return $this->makeEmptyContent(); - } - - $mergedContent = $this->unserializeContent( $result, $format ); - return $mergedContent; - } - - /** - * Unserializes a Content object of the type supported by this ContentHandler. - * - * @since 1.21 - * - * @param $text string serialized form of the content - * @param $format null|String the format used for serialization - * - * @return Content the TextContent object wrapping $text - */ - public function unserializeContent( $text, $format = null ) { - $this->checkFormat( $format ); - - return new TextContent( $text ); - } - - /** - * Creates an empty TextContent object. - * - * @since 1.21 - * - * @return Content - */ - public function makeEmptyContent() { - return new TextContent( '' ); - } -} - -/** - * @since 1.21 - */ -class WikitextContentHandler extends TextContentHandler { - - public function __construct( $modelId = CONTENT_MODEL_WIKITEXT ) { - parent::__construct( $modelId, array( CONTENT_FORMAT_WIKITEXT ) ); - } - - public function unserializeContent( $text, $format = null ) { - $this->checkFormat( $format ); - - return new WikitextContent( $text ); - } - - /** - * @see ContentHandler::makeEmptyContent - * - * @return Content - */ - public function makeEmptyContent() { - return new WikitextContent( '' ); - } - - - /** - * Returns a WikitextContent object representing a redirect to the given destination page. - * - * @see ContentHandler::makeRedirectContent - * - * @param Title $destination the page to redirect to. - * - * @return Content - */ - public function makeRedirectContent( Title $destination ) { - $mwRedir = MagicWord::get( 'redirect' ); - $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destination->getPrefixedText() . "]]\n"; - - return new WikitextContent( $redirectText ); - } - - /** - * Returns true because wikitext supports sections. - * - * @return boolean whether sections are supported. - */ - public function supportsSections() { - return true; - } - - /** - * Returns true, because wikitext supports caching using the - * ParserCache mechanism. - * - * @since 1.21 - * @return bool - */ - public function isParserCacheSupported() { - return true; - } -} - -# XXX: make ScriptContentHandler base class, do highlighting stuff there? - -/** - * @since 1.21 - */ -class JavaScriptContentHandler extends TextContentHandler { - - public function __construct( $modelId = CONTENT_MODEL_JAVASCRIPT ) { - parent::__construct( $modelId, array( CONTENT_FORMAT_JAVASCRIPT ) ); - } - - public function unserializeContent( $text, $format = null ) { - $this->checkFormat( $format ); - - return new JavaScriptContent( $text ); - } - - public function makeEmptyContent() { - return new JavaScriptContent( '' ); - } - - /** - * Returns the english language, because JS is english, and should be handled as such. - * - * @return Language wfGetLangObj( 'en' ) - * - * @see ContentHandler::getPageLanguage() - */ - public function getPageLanguage( Title $title, Content $content = null ) { - return wfGetLangObj( 'en' ); - } - - /** - * Returns the english language, because CSS is english, and should be handled as such. - * - * @return Language wfGetLangObj( 'en' ) - * - * @see ContentHandler::getPageViewLanguage() - */ - public function getPageViewLanguage( Title $title, Content $content = null ) { - return wfGetLangObj( 'en' ); - } -} - -/** - * @since 1.21 - */ -class CssContentHandler extends TextContentHandler { - - public function __construct( $modelId = CONTENT_MODEL_CSS ) { - parent::__construct( $modelId, array( CONTENT_FORMAT_CSS ) ); - } - - public function unserializeContent( $text, $format = null ) { - $this->checkFormat( $format ); - - return new CssContent( $text ); - } - - public function makeEmptyContent() { - return new CssContent( '' ); - } - - /** - * Returns the english language, because CSS is english, and should be handled as such. - * - * @return Language wfGetLangObj( 'en' ) - * - * @see ContentHandler::getPageLanguage() - */ - public function getPageLanguage( Title $title, Content $content = null ) { - return wfGetLangObj( 'en' ); - } - - /** - * Returns the english language, because CSS is english, and should be handled as such. - * - * @return Language wfGetLangObj( 'en' ) - * - * @see ContentHandler::getPageViewLanguage() - */ - public function getPageViewLanguage( Title $title, Content $content = null ) { - return wfGetLangObj( 'en' ); - } -} diff --git a/includes/content/CssContentHandler.php b/includes/content/CssContentHandler.php new file mode 100644 index 0000000000..e2199c41b2 --- /dev/null +++ b/includes/content/CssContentHandler.php @@ -0,0 +1,43 @@ +checkFormat( $format ); + + return new CssContent( $text ); + } + + public function makeEmptyContent() { + return new CssContent( '' ); + } + + /** + * Returns the english language, because CSS is english, and should be handled as such. + * + * @return Language wfGetLangObj( 'en' ) + * + * @see ContentHandler::getPageLanguage() + */ + public function getPageLanguage( Title $title, Content $content = null ) { + return wfGetLangObj( 'en' ); + } + + /** + * Returns the english language, because CSS is english, and should be handled as such. + * + * @return Language wfGetLangObj( 'en' ) + * + * @see ContentHandler::getPageViewLanguage() + */ + public function getPageViewLanguage( Title $title, Content $content = null ) { + return wfGetLangObj( 'en' ); + } +} \ No newline at end of file diff --git a/includes/content/JavaScriptContentHandler.php b/includes/content/JavaScriptContentHandler.php new file mode 100644 index 0000000000..8b080bf5c8 --- /dev/null +++ b/includes/content/JavaScriptContentHandler.php @@ -0,0 +1,45 @@ +checkFormat( $format ); + + return new JavaScriptContent( $text ); + } + + public function makeEmptyContent() { + return new JavaScriptContent( '' ); + } + + /** + * Returns the english language, because JS is english, and should be handled as such. + * + * @return Language wfGetLangObj( 'en' ) + * + * @see ContentHandler::getPageLanguage() + */ + public function getPageLanguage( Title $title, Content $content = null ) { + return wfGetLangObj( 'en' ); + } + + /** + * Returns the english language, because CSS is english, and should be handled as such. + * + * @return Language wfGetLangObj( 'en' ) + * + * @see ContentHandler::getPageViewLanguage() + */ + public function getPageViewLanguage( Title $title, Content $content = null ) { + return wfGetLangObj( 'en' ); + } +} \ No newline at end of file diff --git a/includes/content/TextContentHandler.php b/includes/content/TextContentHandler.php new file mode 100644 index 0000000000..9dff67edd4 --- /dev/null +++ b/includes/content/TextContentHandler.php @@ -0,0 +1,90 @@ +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(). + * + * @param $oldContent \Content|string String + * @param $myContent \Content|string String + * @param $yourContent \Content|string String + * + * @return Content|Bool + */ + public function merge3( Content $oldContent, Content $myContent, Content $yourContent ) { + $this->checkModelID( $oldContent->getModel() ); + $this->checkModelID( $myContent->getModel() ); + $this->checkModelID( $yourContent->getModel() ); + + $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 ) { + return false; + } + + if ( !$result ) { + return $this->makeEmptyContent(); + } + + $mergedContent = $this->unserializeContent( $result, $format ); + return $mergedContent; + } + + /** + * Unserializes a Content object of the type supported by this ContentHandler. + * + * @since 1.21 + * + * @param $text string serialized form of the content + * @param $format null|String the format used for serialization + * + * @return Content the TextContent object wrapping $text + */ + public function unserializeContent( $text, $format = null ) { + $this->checkFormat( $format ); + + return new TextContent( $text ); + } + + /** + * Creates an empty TextContent object. + * + * @since 1.21 + * + * @return Content + */ + public function makeEmptyContent() { + return new TextContent( '' ); + } +} \ No newline at end of file diff --git a/includes/content/WikitextContentHandler.php b/includes/content/WikitextContentHandler.php new file mode 100644 index 0000000000..c6ac2ba879 --- /dev/null +++ b/includes/content/WikitextContentHandler.php @@ -0,0 +1,63 @@ +checkFormat( $format ); + + return new WikitextContent( $text ); + } + + /** + * @see ContentHandler::makeEmptyContent + * + * @return Content + */ + public function makeEmptyContent() { + return new WikitextContent( '' ); + } + + + /** + * Returns a WikitextContent object representing a redirect to the given destination page. + * + * @see ContentHandler::makeRedirectContent + * + * @param Title $destination the page to redirect to. + * + * @return Content + */ + public function makeRedirectContent( Title $destination ) { + $mwRedir = MagicWord::get( 'redirect' ); + $redirectText = $mwRedir->getSynonym( 0 ) . ' [[' . $destination->getPrefixedText() . "]]\n"; + + return new WikitextContent( $redirectText ); + } + + /** + * Returns true because wikitext supports sections. + * + * @return boolean whether sections are supported. + */ + public function supportsSections() { + return true; + } + + /** + * Returns true, because wikitext supports caching using the + * ParserCache mechanism. + * + * @since 1.21 + * @return bool + */ + public function isParserCacheSupported() { + return true; + } +} \ No newline at end of file -- 2.20.1