Ori.livneh has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/300587

Change subject: Support reading/writing data to/from a static PHP array
......................................................................

Support reading/writing data to/from a static PHP array

If $wgTrustedXffFile ends with '.php', assume it is a PHP file that returns a
static associative array. This is to provide a nominal performance improvement
on HHVM, which caches the bytecode of PHP files, making relying on them more
efficient than hitting the disk for the cdb file each time.

Bug: T141120
Change-Id: Ia1976b91a897f7fd80f3ddf5a142d441ac0e88aa
---
M TrustedXFF.body.php
M generate.php
2 files changed, 41 insertions(+), 5 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/TrustedXFF 
refs/changes/87/300587/1

diff --git a/TrustedXFF.body.php b/TrustedXFF.body.php
index 02031d8..245c3ff 100644
--- a/TrustedXFF.body.php
+++ b/TrustedXFF.body.php
@@ -34,10 +34,16 @@
        }
 
        function getCdbHandle() {
+               global $wgTrustedXffFile;
+
                if ( !$this->cdb ) {
-                       global $wgTrustedXffFile;
-                       $this->cdb = CdbReader::open( $wgTrustedXffFile );
+                       if ( pathinfo( $wgTrustedXffFile, PATHINFO_EXTENSION ) 
=== 'php' ) {
+                               $this->cdb = new CdbReader\Hash( include( 
$wgTrustedXffFile ) );
+                       } else {
+                               $this->cdb = CdbReader::open( $wgTrustedXffFile 
);
+                       }
                }
+
                return $this->cdb;
        }
 
diff --git a/generate.php b/generate.php
index 778d691..149c460 100644
--- a/generate.php
+++ b/generate.php
@@ -10,6 +10,34 @@
 use Cdb\Exception as CdbException;
 use Cdb\Writer as CdbWriter;
 
+/**
+ * Emulates Cdb\Writer, but generates a PHP file that returns
+ * an associated array, instead.
+ */
+class StaticArrayWriter {
+       public function __construct( $filename ) {
+               $this->filename = $filename;
+               $this->data = [];
+       }
+
+       public static function open( $filename ) {
+               return new self( $filename );
+       }
+
+       public function set( $key, $value ) {
+               $this->data[ $key ] = $value;
+       }
+
+       public function close() {
+               $header = sprintf( "<?php\n// Generated by %s/%s on %s\n",
+                       basename( __DIR__ ), basename( __FILE__ ), gmdate( 'c' 
) );
+               $code = $header .  'return ' . var_export( $this->data, true ) 
. ";\n";
+               $code = preg_replace( '/(\d+ \=\>| (?=\())/i', '', $code );
+               $code = preg_replace( "/^ +/m", "\t", $code );
+               file_put_contents( $this->filename, $code );
+       }
+}
+
 $inFileName = 'trusted-hosts.txt';
 
 $inFile = fopen( $inFileName, 'r' );
@@ -27,7 +55,9 @@
        exit( 1 );
 }
 try {
-       $outFile = CdbWriter::open( $target );
+       $outFile = pathinfo( $target, PATHINFO_EXTENSION ) === 'php'
+               ? StaticArrayWriter::open( $target )
+               : CdbWriter::open( $target );
 } catch ( CdbException $e ) {
        echo "Unable to open output file \"$target\"\n";
        exit( 1 );
@@ -90,7 +120,7 @@
 
        if ( $start === $end ) {
                // Single host
-               $outFile->set( $start, '1' );
+               $outFile->set( $start, true );
                $numHosts++;
                showProgress( $i, count( $ranges ) );
                continue;
@@ -123,7 +153,7 @@
        for ( $j = $startNum; $j <= $endNum; $j++ ) {
                $hex = strtoupper( base_convert( $j, 10, 16 ) );
                $hex = str_pad( $hex, $suffixLength, '0', STR_PAD_LEFT );
-               $outFile->set( $prefix . $hex, '1' );
+               $outFile->set( $prefix . $hex, true );
                $numHosts++;
        }
        showProgress( $i, count( $ranges ) );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia1976b91a897f7fd80f3ddf5a142d441ac0e88aa
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/TrustedXFF
Gerrit-Branch: master
Gerrit-Owner: Ori.livneh <o...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to