Handler functions that modify $ok should generally return false to prevent further
hooks from further modifying $ok.
+'ContentGetParserOutput': Customize parser output for a given content object,
+called by AbstractContent::getParserOutput. May be used to override the normal
+model-specific rendering of page content.
+$content: The Content to render
+$title: Title of the page, as context
+$revId: The revision ID, as context
+$options: ParserOptions for rendering. To avoid confusing the parser cache,
+the output can only depend on parameters provided to this hook function, not on global state.
+$generateHtml: boolean, indicating whether full HTML should be generated. If false,
+generation of HTML may be skipped, but other information should still be present in the
+ParserOutput object.
+&$output: ParserOutput, to manipulate or replace
+
'ConvertContent': Called by AbstractContent::convert when a conversion to another
content model is requested.
$content: The Content object to be converted.
$article: The article object corresponding to the page
'ShowRawCssJs': Customise the output of raw CSS and JavaScript in page views.
-DEPRECATED, use the ContentHandler facility to handle CSS and JavaScript!
+DEPRECATED, use the ContentGetParserOutput hook instead!
$text: Text being shown
$title: Title of the custom script/stylesheet page
$output: Current OutputPage object
* Show a page view for a page formatted as CSS or JavaScript. To be called by
* Article::view() only.
*
- * This is hooked by SyntaxHighlight_GeSHi to do syntax highlighting of these
- * page views.
+ * This exists mostly to serve the deprecated ShowRawCssJs hook (used to customize these views).
+ * It has been replaced by the ContentGetParserOutput hook, which lets you do the same but with
+ * more flexibility.
*
* @param bool $showCacheHint Whether to show a message telling the user
* to clear the browser cache (default: true).
return $result;
}
+
+ /**
+ * Returns a ParserOutput object containing information derived from this content.
+ * Most importantly, unless $generateHtml was false, the return value contains an
+ * HTML representation of the content.
+ *
+ * Subclasses that want to control the parser output may override this, but it is
+ * preferred to override fillParserOutput() instead.
+ *
+ * Subclasses that override getParserOutput() itself should take care to call the
+ * ContentGetParserOutput hook.
+ *
+ * @since 1.24
+ *
+ * @param Title $title Context title for parsing
+ * @param int|null $revId Revision ID (for {{REVISIONID}})
+ * @param ParserOptions|null $options Parser options
+ * @param bool $generateHtml Whether or not to generate HTML
+ *
+ * @return ParserOutput Containing information derived from this content.
+ */
+ public function getParserOutput( Title $title, $revId = null,
+ ParserOptions $options = null, $generateHtml = true
+ ) {
+ if ( $options === null ) {
+ $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
+ }
+
+ $po = new ParserOutput();
+
+ if ( wfRunHooks( 'ContentGetParserOutput',
+ array( $this, $title, $revId, $options, $generateHtml, &$po ) ) ) {
+
+ $this->fillParserOutput( $title, $revId, $options, $generateHtml, $po );
+ }
+
+ return $po;
+ }
+
+ /**
+ * Fills the provided ParserOutput with information derived from the content.
+ * Unless $generateHtml was false, this includes an HTML representation of the content.
+ *
+ * This is called by getParserOutput() after consulting the ContentGetParserOutput hook.
+ * Subclasses are expected to override this method (or getParserOutput(), if need be).
+ * Subclasses of TextContent should generally override getHtml() instead.
+ *
+ * This placeholder implementation always throws an exception.
+ *
+ * @since 1.24
+ *
+ * @param Title $title Context title for parsing
+ * @param int|null $revId Revision ID (for {{REVISIONID}})
+ * @param ParserOptions|null $options Parser options
+ * @param bool $generateHtml Whether or not to generate HTML
+ * @param ParserOutput &$output The output object to fill (reference).
+ *
+ * @throws MWException
+ */
+ protected function fillParserOutput( Title $title, $revId,
+ ParserOptions $options, $generateHtml, ParserOutput &$output
+ ) {
+ // Don't make abstract, so subclasses that override getParserOutput() directly don't fail.
+ throw new MWException( 'Subclasses of AbstractContent must override fillParserOutput!' );
+ }
}
}
/**
- * Returns a generic ParserOutput object, wrapping the HTML returned by
- * getHtml().
+ * Fills the provided ParserOutput object with information derived from the content.
+ * Unless $generateHtml was false, this includes an HTML representation of the content
+ * provided by getHtml().
+ *
+ * For content models listed in $wgTextModelsToParse, this method will call the MediaWiki
+ * wikitext parser on the text to extract any (wikitext) links, magic words, etc.
+ *
+ * Subclasses may override this to provide custom content processing.
+ * For custom HTML generation alone, it is sufficient to override getHtml().
*
* @param Title $title Context title for parsing
* @param int $revId Revision ID (for {{REVISIONID}})
* @param ParserOptions $options Parser options
* @param bool $generateHtml Whether or not to generate HTML
- *
- * @return ParserOutput Representing the HTML form of the text.
+ * @param ParserOutput $output The output object to fill (reference).
*/
- public function getParserOutput( Title $title, $revId = null,
- ParserOptions $options = null, $generateHtml = true ) {
+ protected function fillParserOutput( Title $title, $revId,
+ ParserOptions $options, $generateHtml, ParserOutput &$output
+ ) {
global $wgParser, $wgTextModelsToParse;
- if ( !$options ) {
- //NOTE: use canonical options per default to produce cacheable output
- $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
- }
-
if ( in_array( $this->getModel(), $wgTextModelsToParse ) ) {
- // parse just to get links etc into the database
- $po = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
- } else {
- $po = new ParserOutput();
+ // parse just to get links etc into the database, HTML is replaced below.
+ $output = $wgParser->parse( $this->getNativeData(), $title, $options, true, true, $revId );
}
if ( $generateHtml ) {
$html = '';
}
- $po->setText( $html );
-
- return $po;
+ $output->setText( $html );
}
/**
* Generates an HTML version of the content, for display. Used by
- * getParserOutput() to construct a ParserOutput object.
+ * fillParserOutput() to provide HTML for the ParserOutput object.
*
* Subclasses may override this to provide a custom HTML rendering.
* If further information is to be derived from the content (such as
- * categories), the getParserOutput() method can be overridden instead.
+ * categories), the fillParserOutput() method can be overridden instead.
*
* For backwards-compatibility, this default implementation just calls
* getHighlightHtml().
* Returns a ParserOutput object resulting from parsing the content's text
* using $wgParser.
*
- * @since 1.21
- *
* @param Title $title
* @param int $revId Revision to pass to the parser (default: null)
* @param ParserOptions $options (default: null)
* @param bool $generateHtml (default: true)
- *
- * @return ParserOutput Representing the HTML form of the text
+ * @param &$output ParserOutput representing the HTML form of the text,
+ * may be manipulated or replaced.
*/
- public function getParserOutput( Title $title, $revId = null,
- ParserOptions $options = null, $generateHtml = true ) {
+ protected function fillParserOutput( Title $title, $revId,
+ ParserOptions $options, $generateHtml, ParserOutput &$output
+ ) {
global $wgParser;
- if ( !$options ) {
- //NOTE: use canonical options per default to produce cacheable output
- $options = $this->getContentHandler()->makeParserOptions( 'canonical' );
- }
-
list( $redir, $text ) = $this->getRedirectTargetAndText();
- $po = $wgParser->parse( $text, $title, $options, true, true, $revId );
+ $output = $wgParser->parse( $text, $title, $options, true, true, $revId );
// Add redirect indicator at the top
if ( $redir ) {
// Make sure to include the redirect link in pagelinks
- $po->addLink( $redir );
+ $output->addLink( $redir );
if ( $generateHtml ) {
$chain = $this->getRedirectChain();
- $po->setText(
+ $output->setText(
Article::getRedirectHeaderHtml( $title->getPageLanguage(), $chain, false ) .
- $po->getText()
+ $output->getText()
);
}
}
-
- return $po;
}
/**
) {
return new ParserOutput( $this->getNativeData() );
}
+
+ /**
+ * @see AbstractContent::fillParserOutput()
+ *
+ * @param Title $title Context title for parsing
+ * @param int|null $revId Revision ID (for {{REVISIONID}})
+ * @param ParserOptions|null $options Parser options
+ * @param bool $generateHtml Whether or not to generate HTML
+ * @param ParserOutput &$output The output object to fill (reference).
+ */
+ protected function fillParserOutput( Title $title, $revId,
+ ParserOptions $options, $generateHtml, ParserOutput &$output ) {
+ $output = new ParserOutput( $this->getNativeData() );
+ }
}
* @group Database
* ^--- needed, because we do need the database to test link updates
*/
-class CssContentTest extends MediaWikiTestCase {
+class CssContentTest extends JavaScriptContentTest {
protected function setUp() {
parent::setUp();
*/
class TextContentTest extends MediaWikiLangTestCase {
protected $context;
+ protected $savedContentGetParserOutput;
protected function setUp() {
+ global $wgHooks;
+
parent::setUp();
// Anon user
$user = new User();
$user->setName( '127.0.0.1' );
+ $this->context = new RequestContext( new FauxRequest() );
+ $this->context->setTitle( Title::newFromText( 'Test' ) );
+ $this->context->setUser( $user );
+
$this->setMwGlobals( array(
'wgUser' => $user,
'wgTextModelsToParse' => array(
'wgAlwaysUseTidy' => false,
) );
- $this->context = new RequestContext( new FauxRequest() );
- $this->context->setTitle( Title::newFromText( 'Test' ) );
- $this->context->setUser( $user );
+ // bypass hooks that force custom rendering
+ if ( isset( $wgHooks['ContentGetParserOutput'] ) ) {
+ $this->savedContentGetParserOutput = $wgHooks['ContentGetParserOutput'];
+ unset( $wgHooks['ContentGetParserOutput'] );
+ }
+ }
+
+ public function teardown() {
+ global $wgHooks;
+
+ // restore hooks that force custom rendering
+ if ( $this->savedContentGetParserOutput !== null ) {
+ $wgHooks['ContentGetParserOutput'] = $this->savedContentGetParserOutput;
+ }
+
+ parent::teardown();
}
public function newContent( $text ) {