Introducing ContentGetParserOutput hook.
[lhc/web/wiklou.git] / includes / content / AbstractContent.php
index 68092c3..8c6e24a 100644 (file)
@@ -422,8 +422,8 @@ abstract class AbstractContent implements Content {
         * This base implementation calls the hook ConvertContent to enable custom conversions.
         * Subclasses may override this to implement conversion for "their" content model.
         *
-        * @param String  $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
-        * @param String  $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
+        * @param string  $toModel the desired content model, use the CONTENT_MODEL_XXX flags.
+        * @param string  $lossy flag, set to "lossy" to allow lossy conversion. If lossy conversion is
         * not allowed, full round-trip conversion is expected to work without losing information.
         *
         * @return Content|bool A content object with the content model $toModel, or false if
@@ -441,4 +441,57 @@ abstract class AbstractContent implements Content {
                wfRunHooks( 'ConvertContent', array( $this, $toModel, $lossy, &$result ) );
                return $result;
        }
+
+       /**
+        * Returns a ParserOutput object, filled by a call to fillParserOutput().
+        * Subclasses that want to control the parser output may override this, or
+        * they can override fillParserOutput(). If they override getParserOutput(),
+        * itself, they should take care to call the ContentGetParserOutput hook.
+        *
+        * @param $title Title Context title for parsing
+        * @param $revId int|null Revision ID (for {{REVISIONID}})
+        * @param $options ParserOptions|null Parser options
+        * @param $generateHtml bool 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 ( $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 object with the HTML returned by getHtml().
+        * Subclasses should override this (or getParserOutput) appropriately.
+        * This placeholder implementation always throws an exception.
+        *
+        * @param $title        Title Context title for parsing
+        * @param $revId        int|null Revision ID (for {{REVISIONID}})
+        * @param $options      ParserOptions|null Parser options
+        * @param $generateHtml bool Whether or not to generate HTML
+        * @param &$output      ParserOutput 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!' );
+       }
 }