moved rendering logic from Content class to ContentHandler class

This commit is contained in:
daniel 2012-06-06 17:38:39 +02:00
parent 5bd4d3407b
commit 607f9341ec
3 changed files with 154 additions and 110 deletions

View file

@ -20,6 +20,9 @@ abstract class Content {
*
* @return String a string representing the content in a way useful for building a full text search index.
* If no useful representation exists, this method returns an empty string.
*
* @todo: test that this actually works
* @todo: make sure this also works with LuceneSearch / WikiSearch
*/
public abstract function getTextForSearchIndex( );
@ -281,6 +284,9 @@ abstract class Content {
public abstract function isCountable( $hasLinks = null ) ;
/**
* Convenience method, shorthand for
* $this->getContentHandler()->getParserOutput( $this, $title, $revId, $options, $generateHtml )
*
* @param Title $title
* @param null $revId
* @param null|ParserOptions $options
@ -292,19 +298,13 @@ abstract class Content {
*
* @return ParserOutput
*/
public abstract function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ); #TODO: move to ContentHandler; #TODO: rename to getRenderOutput()
#TODO: make RenderOutput and RenderOptions base classes
public function getParserOutput( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
return $this->getContentHandler()->getParserOutput( $this, $title, $revId, $options, $generateHtml );
}
/**
* 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.
* Convenience method, shorthand for
* $this->getContentHandler()->getSecondaryDataUpdates( $this, $title, $old, $recursive )
*
* @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
@ -315,9 +315,8 @@ abstract class Content {
*
* @since WD.1
*/
public function getSecondaryDataUpdates( Title $title, Content $old = null, $recursive = false ) {
$po = $this->getParserOutput( $title, null, null, false );
return $po->getSecondaryDataUpdates( $title, $recursive );
public function getSecondaryDataUpdates( Title $title, Content $old = null, $recursive = false ) { #TODO: remove!
return $this->getContentHandler()->getSecondaryDataUpdates( $this, $title, $old, $recursive );
}
/**
@ -549,35 +548,6 @@ abstract class TextContent extends Content {
return $this->getNativeData();
}
/**
* Returns a generic ParserOutput object, wrapping the HTML returned by getHtml().
*
* @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( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
# generic implementation, relying on $this->getHtml()
if ( $generateHtml ) $html = $this->getHtml( $options );
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
*
* @return String
*/
protected abstract function getHtml( );
/**
* Diff this content object with another content object..
*
@ -621,35 +591,6 @@ class WikitextContent extends TextContent {
parent::__construct($text, CONTENT_MODEL_WIKITEXT);
}
protected function getHtml( ) {
throw new MWException( "getHtml() not implemented for wikitext. Use getParserOutput()->getText()." );
}
/**
* Returns a ParserOutput object resulting from parsing the content's text using $wgParser.
*
* @since WD.1
*
* @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( Title $title, $revId = null, ParserOptions $options = null, $generateHtml = true ) {
global $wgParser;
if ( !$options ) {
$options = new ParserOptions();
}
$po = $wgParser->parse( $this->mText, $title, $options, true, true, $revId );
return $po;
}
/**
* Returns the section with the given id.
*
@ -817,6 +758,8 @@ class WikitextContent extends TextContent {
return $hasLinks;
}
return false;
}
public function getTextForSummary( $maxlength = 250 ) {
@ -850,15 +793,13 @@ class MessageContent extends TextContent {
}
$this->mOptions = $options;
$this->mHtmlOptions = null;
}
/**
* Returns the message as rendered HTML, using the options supplied to the constructor plus "parse".
* @return String the message text, parsed
*/
protected function getHtml( ) {
public function getHtml( ) {
$opt = array_merge( $this->mOptions, array('parse') );
return wfMsgExt( $this->mMessageKey, $this->mParameters, $opt );
@ -886,15 +827,6 @@ class JavaScriptContent extends TextContent {
parent::__construct($text, CONTENT_MODEL_JAVASCRIPT);
}
protected function getHtml( ) {
$html = "";
$html .= "<pre class=\"mw-code mw-js\" dir=\"ltr\">\n";
$html .= htmlspecialchars( $this->getNativeData() );
$html .= "\n</pre>\n";
return $html;
}
}
/**
@ -904,13 +836,4 @@ class CssContent extends TextContent {
public function __construct( $text ) {
parent::__construct($text, CONTENT_MODEL_CSS);
}
protected function getHtml( ) {
$html = "";
$html .= "<pre class=\"mw-code mw-css\" dir=\"ltr\">\n";
$html .= htmlspecialchars( $this->getNativeData() );
$html .= "\n</pre>\n";
return $html;
}
}

View file

@ -709,8 +709,51 @@ abstract class ContentHandler {
return $reason;
}
#@TODO: getSecondaryUpdatesForDeletion( Content ) returns an array of DataUpdate objects
#... or do that in the Content class?
/**
* 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 );
}
/**
* Get the Content object that needs to be saved in order to undo all revisions
@ -837,6 +880,37 @@ abstract class TextContentHandler extends ContentHandler {
return $mergedContent;
}
/**
* 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 );
}
@ -859,6 +933,34 @@ class WikitextContentHandler extends TextContentHandler {
return new WikitextContent( '' );
}
/**
* 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()." );
}
}
@ -882,6 +984,15 @@ class JavaScriptContentHandler extends TextContentHandler {
public function makeEmptyContent() {
return new JavaScriptContent( '' );
}
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;
}
}
/**
@ -903,4 +1014,13 @@ class CssContentHandler extends TextContentHandler {
return new CssContent( '' );
}
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;
}
}

View file

@ -285,6 +285,22 @@ class DummyContentHandlerForTesting extends ContentHandler {
{
return new DummyContentForTesting( '' );
}
/**
* @param Content $content
* @param Title $title
* @param null $revId
* @param null|ParserOptions $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.
*
* @return ParserOutput
*/
public function getParserOutput( Content $content, Title $title, $revId = null, ParserOptions $options = NULL, $generateHtml = true )
{
return new ParserOutput( $content->getNativeData() );
}
}
class DummyContentForTesting extends Content {
@ -381,20 +397,5 @@ class DummyContentForTesting extends Content {
{
return false;
}
/**
* @param Title $title
* @param null $revId
* @param null|ParserOptions $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.
*
* @return ParserOutput
*/
public function getParserOutput( Title $title, $revId = null, ParserOptions $options = NULL, $generateHtml = true )
{
return new ParserOutput( $this->data );
}
}