From d722bc51f4091463f46ebf115d8256516ba6115d Mon Sep 17 00:00:00 2001 From: Marko Obrovac Date: Thu, 2 Jun 2016 16:30:48 +0200 Subject: [PATCH] Send a no-cache content request on mathpurge=true When the user specifies the mathpurge=true query parameter, the Math extension needs to prompt RESTBase to regenerate the renders for all formulae on the page and consequently issue Varnish purge requests. This behaviour is achieved by including the `Cache-Control: no-cache` header when requesting a render. Since RESTBase regenerates all of the renders when it receives the header, send it only for the first content request; if subsequent content requests are made, RESTBase will already have the new renders. Bug: T136205 Change-Id: I44482ccc1d8afdf6d40dcf8965d8debe83ef7e17 --- MathMathML.php | 7 ++++--- MathRenderer.php | 1 + MathRestbaseInterface.php | 20 ++++++++++++++++++++ 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/MathMathML.php b/MathMathML.php index 26ee1b3a9..722dfc994 100644 --- a/MathMathML.php +++ b/MathMathML.php @@ -81,12 +81,16 @@ class MathMathML extends MathRenderer { public function render( $forceReRendering = false ) { global $wgMathFullRestbaseURL; try { + if ( $forceReRendering ) { + $this->setPurge( true ); + } if ( in_array( $this->inputType, $this->restbaseInputTypes ) && $this->mode == 'mathml' ) { if ( !$this->rbi ) { $this->rbi = new MathRestbaseInterface( $this->getTex(), $this->getInputType() ); + $this->rbi->setPurge( $this->isPurge() ); } $rbi = $this->rbi; if ( $rbi->getSuccess() ) { @@ -99,9 +103,6 @@ class MathMathML extends MathRenderer { $this->changed = false; return $rbi->getSuccess(); } - if ( $forceReRendering ) { - $this->setPurge( true ); - } if ( $this->renderingRequired() ) { return $this->doRender(); } diff --git a/MathRenderer.php b/MathRenderer.php index e5ed80444..87c427a48 100644 --- a/MathRenderer.php +++ b/MathRenderer.php @@ -385,6 +385,7 @@ abstract class MathRenderer { */ public function setRestbaseInterface( $param ) { $this->rbi = $param; + $this->rbi->setPurge( $this->isPurge() ); } /** diff --git a/MathRestbaseInterface.php b/MathRestbaseInterface.php index 3f4283289..bf2e59053 100644 --- a/MathRestbaseInterface.php +++ b/MathRestbaseInterface.php @@ -18,6 +18,8 @@ class MathRestbaseInterface { private $error; private $mathoidStyle; private $mml; + /** @var boolean is there a request to purge the existing mathematical content */ + private $purge = false; /** * MathRestbaseInterface constructor. @@ -66,6 +68,18 @@ class MathRestbaseInterface { } } + /** + * Lets this instance know if this is a purge request. When set to true, + * it will cause the object to issue the first content request with a + * 'Cache-Control: no-cache' header to prompt the regeneration of the + * renders. + * + * @param bool $purge whether this is a purge request + */ + public function setPurge( $purge = true ) { + $this->purge = $purge; + } + /** * @return string MathML code * @throws MWException @@ -383,6 +397,12 @@ class MathRestbaseInterface { 'method' => 'GET', 'url' => $this->getUrl( "media/math/render/$type/{$this->hash}" ) ]; + if ( $this->purge ) { + $request['headers'] = [ + 'Cache-Control' => 'no-cache' + ]; + $this->purge = false; + } return $request; }