Physikerwelt has uploaded a new change for review.

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

Change subject: Exports the MathCache table
......................................................................

Exports the MathCache table

This is required for testing of texvcjs.

Change-Id: Ib602aba8f73fc18e30ebf0efc609c8fbb2d122ef
---
M MathRenderer.php
A maintenance/ExportMathCache.php
2 files changed, 96 insertions(+), 1 deletion(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Math 
refs/changes/29/174729/1

diff --git a/MathRenderer.php b/MathRenderer.php
index 72e2a6d..ded7a36 100644
--- a/MathRenderer.php
+++ b/MathRenderer.php
@@ -251,7 +251,7 @@
         * @param binary $hash
         * @return string md5
         */
-       private static function dbHash2md5( $hash ) {
+       public static function dbHash2md5( $hash ) {
                $dbr = wfGetDB( DB_SLAVE );
                $xhash = unpack( 'H32md5', $dbr->decodeBlob( $hash ) . "        
        " );
                return $xhash['md5'];
diff --git a/maintenance/ExportMathCache.php b/maintenance/ExportMathCache.php
new file mode 100644
index 0000000..b18909f
--- /dev/null
+++ b/maintenance/ExportMathCache.php
@@ -0,0 +1,95 @@
+<?php
+/**
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @ingroup Maintenance
+ */
+
+require_once( dirname( __FILE__ ) . '/../../../maintenance/Maintenance.php' );
+
+class ExportMathCache extends Maintenance {
+       const DEFAULT_TABLE = 'mathoid';
+       private static $allowedTables = array( 'mathoid' , 'mathlatexml');
+       private static $inputColumns = array( 'mathoid' => 'math_input' , 
'mathlatexml' => 'math_inputtex');
+
+       public function __construct() {
+               parent::__construct();
+               $this->mDescription = 'Exports a json file that consists of the 
input hashes and the texvc input from the database cache.';
+               $this->addArg( 'table', "The math table to be used (mathoid or 
latexml).", false );
+               $this->addOption( 'offset', "If set the first n equations on 
the table are skipped", false, true, "o" );
+               $this->addOption( 'length', "If set the only n equations are 
exported processed", false, true, "l" );
+               $this->addOption( 'sort' , 'If set the result is sorted 
according to the input', false, false, 's' );
+       }
+
+       /**
+        * @param $table
+        * @param $offset
+        * @param $length
+        * @return bool|ResultWrapper
+        */
+       private static function getMathTagsFromDatabase( $table , $offset , 
$length , $sort ) {
+               $out = array();
+               $dbr = wfGetDB( DB_SLAVE );
+               $inputColumn = self::$inputColumns[ $table ];
+               $options = array(
+                       'OFFSET'   => $offset,
+                       'LIMIT'    => $length );
+               if ( $sort === true ) {
+                       $options['ORDER BY'] = $inputColumn;
+               }
+               $res =  $dbr->select(
+                       $table,
+                       array( 'math_inputhash', $inputColumn ),
+                       ''
+                       , __METHOD__
+                       , $options      );
+               if ( $res === false ) return false;
+               // Convert result wrapper to array
+               foreach ( $res as $row ){
+                       $out[] = array(
+                               // the binary encoded input-hash is no valid 
json output
+                               'inputhash' => 
MathRenderer::dbHash2md5($row->math_inputhash) ,
+                               'input'     => $row->$inputColumn );
+               }
+               return $out;
+       }
+
+       public function execute() {
+               $table = $this->getArg( 0, self::DEFAULT_TABLE );
+               if( !in_array( $table, self::$allowedTables ) ){
+                       echo "Error:  '$table' is not allowed.\n";
+                       return;
+               }
+               $offset = $this->getOption( 'offset', 0 );
+               $length = $this->getOption( 'length', PHP_INT_MAX );
+               $sort = $this->hasOption( 'sort' );
+               $allEquations = self::getMathTagsFromDatabase( $table, $offset, 
$length, $sort );
+               if ( !is_array( $allEquations ) ) {
+                       echo "Could not get equations from table '$table'\n";
+                       return;
+               }
+               $out = json_encode( $allEquations, JSON_PRETTY_PRINT );
+               if( $out === false){
+                       echo "Could not encode result als json string 
'$table'\n";
+               } else {
+                       echo $out;
+               }
+       }
+}
+
+$maintClass = "ExportMathCache";
+require_once( RUN_MAINTENANCE_IF_MAIN );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib602aba8f73fc18e30ebf0efc609c8fbb2d122ef
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Math
Gerrit-Branch: debug
Gerrit-Owner: Physikerwelt <w...@physikerwelt.de>

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

Reply via email to