Jdlrobson has submitted this change and it was merged.

Change subject: Use cURL to keep the connection alive
......................................................................


Use cURL to keep the connection alive

Change-Id: I3bb0bb3773fcae86b334bbf62adc9a87c4280428
---
A MinificationException.php
M Minifier.body.php
M Minifier.php
3 files changed, 61 insertions(+), 41 deletions(-)

Approvals:
  Jdlrobson: Verified; Looks good to me, approved



diff --git a/MinificationException.php b/MinificationException.php
new file mode 100644
index 0000000..18b5009
--- /dev/null
+++ b/MinificationException.php
@@ -0,0 +1,8 @@
+<?php
+
+class MinificationException extends MWException {
+       public function __construct( $message ) {
+               wfDebugLog( 'minifier', $message );
+               parent::__construct( $message );
+       }
+}
diff --git a/Minifier.body.php b/Minifier.body.php
index ee882fa..509c91c 100644
--- a/Minifier.body.php
+++ b/Minifier.body.php
@@ -1,48 +1,54 @@
 <?php
 
 class JSUglifier implements IResourceFilter {
-       private $options;
-
-       public function __construct() {
-               global $wgMinifierConnectionOptions;
-               $this->options = $wgMinifierConnectionOptions;
-               $this->options['method'] = 'POST';
-       }
+       /** @var Resource */
+       private static $curl;
 
        public function filter( $js ) {
-               wfProfileIn( __METHOD__ );
-               $req = $this->getRequest( array( 'id' => md5( $js ) ) );
-               $req->setData( array(
-                       'code' => 'minify',
-                       'text' => $js,
-               ) );
-               $status = $req->execute();
-               $err = '';
-               if ( !$status->isGood() ) {
-                       $err = "Error requesting minification: 
{$status->getMessage()}";
-               }
-               $statusCode = $req->getStatus();
-               $returnedText = $req->getContent();
-               if ( $statusCode != 200 ) {
-                       $err = "Request to minifier ended with code 
$statusCode: $returnedText";
-               }
-               wfProfileOut( __METHOD__ );
-               if ( $err ) {
-                       wfDebugLog( 'minifier', $err );
-                       throw new MWException( $err );
-               }
-               return $returnedText;
+               $profile = new ProfileSection( __METHOD__ );
+               return $this->performRequest(
+                       array( 'id' => md5( $js ) ),
+                       array(
+                               'code' => 'minify',
+                               'text' => $js,
+                       )
+               );
        }
 
        /**
-        * @param array $params
-        * @return MWHttpRequest
+        * @param array $urlParams
+        * @param array $postData
+        *
+        * @return string
         */
-       private function getRequest( array $params = array() ) {
-               global $wgMinifierHosts;
+       private function performRequest( array $urlParams, array $postData ) {
+               global $wgMinifierConnectionOptions, $wgMinifierHost;
 
-               $host = ArrayUtils::pickRandom( $wgMinifierHosts );
-               $query = $params ? '?' . wfArrayToCgi( $params ) : '';
-               return MWHttpRequest::factory( "http://$host/$query";, 
$this->options );
+               if ( !self::$curl ) {
+                       $options = $wgMinifierConnectionOptions + array(
+                                       CURLOPT_POST => true,
+                                       CURLOPT_RETURNTRANSFER => true,
+                               );
+                       self::$curl = curl_init();
+                       curl_setopt_array( self::$curl, $options );
+               }
+               $query = $urlParams ? '?' . wfArrayToCgi( $urlParams ) : '';
+               $url = "http://$wgMinifierHost/$query";;
+               curl_setopt( self::$curl, CURLOPT_URL, $url );
+               curl_setopt( self::$curl, CURLOPT_POSTFIELDS, $postData );
+
+               $result = curl_exec( self::$curl );
+               if ( $result === false ) {
+                       throw new MinificationException( "Error performing 
minification request to $url: "
+                               . curl_error( self::$curl)
+                       );
+               }
+
+               $httpCode = curl_getinfo( self::$curl, CURLINFO_HTTP_CODE );
+               if ( $httpCode != 200 ) {
+                       throw new MinificationException( "Minification request 
to $url ended with code $httpCode: $result" );
+               }
+
+               return $result;
        }
 }
\ No newline at end of file
diff --git a/Minifier.php b/Minifier.php
index 09df9d4..4a75d56 100644
--- a/Minifier.php
+++ b/Minifier.php
@@ -1,7 +1,7 @@
 <?php
 
 if ( !defined( 'MEDIAWIKI' ) ) {
-       echo( "This is an extension to the MediaWiki package and cannot be run 
standalone.\n" );
+       echo( "This is a MediaWiki extension and cannot be run standalone.\n" );
        die;
 }
 
@@ -14,12 +14,18 @@
 );
 
 $wgAutoloadClasses['JSUglifier'] = __DIR__ . '/Minifier.body.php';
+$wgAutoloadClasses['MinificationException'] = __DIR__ . 
'/MinificationException.php';
 $wgResourceFilters['minify-js'] = 'JSUglifier';
 
-$wgMinifierHosts = array();
+/**
+ * Host used by the minifier service with optional port
+ */
+$wgMinifierHost = 'localhost:8888';
 
+/**
+ * Array of cURL options
+ */
 $wgMinifierConnectionOptions = array(
-       // Examples:
-       //'connectTimeout' => 10,
-       //'timeout' => 10000,
+       CURLOPT_CONNECTTIMEOUT_MS => 1000,
+       CURLOPT_TIMEOUT_MS => 10000,
 );
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/118419
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I3bb0bb3773fcae86b334bbf62adc9a87c4280428
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Minifier
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>
Gerrit-Reviewer: Catrope <[email protected]>
Gerrit-Reviewer: JGonera <[email protected]>
Gerrit-Reviewer: Jdlrobson <[email protected]>
Gerrit-Reviewer: Kaldari <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to