X-Git-Url: https://git.heureux-cyclage.org/?a=blobdiff_plain;f=includes%2Fcontent%2FJavaScriptContent.php;h=6d23656009188e095859494fcec8a37d7abf9571;hb=192f1018e45095580c39b0894a03a9428994b139;hp=442b705282aea089b9946d5d9f2e75a6495c5569;hpb=b2645d82849ca74b0e6b8df6a3e28e81d0561a58;p=lhc%2Fweb%2Fwiklou.git diff --git a/includes/content/JavaScriptContent.php b/includes/content/JavaScriptContent.php index 442b705282..6d23656009 100644 --- a/includes/content/JavaScriptContent.php +++ b/includes/content/JavaScriptContent.php @@ -32,11 +32,17 @@ */ class JavaScriptContent extends TextContent { + /** + * @var bool|Title|null + */ + private $redirectTarget = false; + /** * @param string $text JavaScript code. + * @param string $modelId the content model name */ - public function __construct( $text ) { - parent::__construct( $text, CONTENT_MODEL_JAVASCRIPT ); + public function __construct( $text, $modelId = CONTENT_MODEL_JAVASCRIPT ) { + parent::__construct( $text, $modelId ); } /** @@ -57,7 +63,7 @@ class JavaScriptContent extends TextContent { $text = $this->getNativeData(); $pst = $wgParser->preSaveTransform( $text, $title, $user, $popts ); - return new JavaScriptContent( $pst ); + return new static( $pst ); } /** @@ -72,4 +78,46 @@ class JavaScriptContent extends TextContent { return $html; } + /** + * If this page is a redirect, return the content + * if it should redirect to $target instead + * + * @param Title $target + * @return JavaScriptContent + */ + public function updateRedirect( Title $target ) { + if ( !$this->isRedirect() ) { + return $this; + } + + return $this->getContentHandler()->makeRedirectContent( $target ); + } + + /** + * @return Title|null + */ + public function getRedirectTarget() { + if ( $this->redirectTarget !== false ) { + return $this->redirectTarget; + } + $this->redirectTarget = null; + $text = $this->getNativeData(); + if ( strpos( $text, '/* #REDIRECT */' ) === 0 ) { + // Extract the title from the url + preg_match( '/title=(.*?)\\\\u0026action=raw/', $text, $matches ); + if ( isset( $matches[1] ) ) { + $title = Title::newFromText( $matches[1] ); + if ( $title ) { + // Have a title, check that the current content equals what + // the redirect content should be + if ( $this->equals( $this->getContentHandler()->makeRedirectContent( $title ) ) ) { + $this->redirectTarget = $title; + } + } + } + } + + return $this->redirectTarget; + } + }