Physikerwelt has uploaded a new change for review.

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

Change subject: Seperate ResultSubmission from UI
......................................................................

Seperate ResultSubmission from UI

Step 2: Use ImportCsv class for logic of the Specialpage

Change-Id: I463e8566c9d34c8e2f4c678dcee55a7436c51a31
---
M MathSearch.php
M SpecialMathDownloadResult.php
M SpecialUploadResult.php
M includes/ImportCsv.php
4 files changed, 92 insertions(+), 166 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MathSearch 
refs/changes/86/181986/1

diff --git a/MathSearch.php b/MathSearch.php
index 532c7eb..a029d32 100644
--- a/MathSearch.php
+++ b/MathSearch.php
@@ -55,6 +55,7 @@
 $wgAutoloadClasses['BaseXSession'] = $dir . 'BaseXSession.php';
 $wgAutoloadClasses['BaseXQuery'] = $dir . 'BaseXQuery.php';
 $wgAutoloadClasses['BaseXError'] = $dir . 'BaseXError.php';
+$wgAutoloadClasses['ImportCsv'] = $dir . 'includes/ImportCsv.php';
 
 $wgMessagesDirs['MathSeach'] = __DIR__ . '/i18n';
 $wgExtensionMessagesFiles['MathSearch'] = $dir . 'MathSearch.i18n.php';
diff --git a/SpecialMathDownloadResult.php b/SpecialMathDownloadResult.php
index 81559f9..e2cbab3 100644
--- a/SpecialMathDownloadResult.php
+++ b/SpecialMathDownloadResult.php
@@ -10,7 +10,7 @@
        }
 
        public static function run2CSV( $runId ){
-               $out = SpecialUploadResult::getCsvColumnHeader() . "\n";
+               $out = ImportCsv::getCsvColumnHeader() . "\n";
                $dbr = wfGetDB( DB_SLAVE );
                $res = $dbr->select( 'math_wmc_results',
                        array( 'qId' , 'oldId' , 'fId' ),
diff --git a/SpecialUploadResult.php b/SpecialUploadResult.php
index c16da70..29e1140 100644
--- a/SpecialUploadResult.php
+++ b/SpecialUploadResult.php
@@ -7,23 +7,33 @@
  * @author Yaron Koren (This class is baaed on DT_ImportCSV from the 
DataTransfer extension)
  */
 class SpecialUploadResult extends SpecialPage {
-       private static $columnHeaders = array( 'queryId', 'formulaId' );
-       private $warnings = array();
-       private $results = array();
+       /** @type ImportCsv $importer */
+       private $importer;
+       /** @type bool $runId */
        protected $runId = false;
-       private $validQIds = array();
 
-       public static function getCsvColumnHeader(){
-               return implode( ',', self::$columnHeaders );
-       }
+       /**
+        * @param string $name
+        */
        public function __construct( $name = 'MathUpload' ) {
                parent::__construct( $name );
        }
 
+       /**
+        * @param $errors
+        *
+        * @return string
+        */
        private static function formatErrors( $errors ) {
                return wfMessage( 'math-wmc-Warnings' )->text() . "<br />" . 
implode( "<br />", $errors );
        }
 
+       /**
+        * @param null|string $query
+        *
+        * @throws MWException
+        * @throws PermissionsError
+        */
        function execute( $query ) {
                global $wgMathUploadEnabled;
                $this->setHeaders();
@@ -32,6 +42,7 @@
                }
 
                $this->getOutput()->addWikiText( wfMessage( 
'math-wmc-Introduction' )->text() );
+               $this->importer = new ImportCsv( $this->getUser() );
                $formDescriptor = $this->printRunSelector();
                $formDescriptor['File'] = array(
                        'label-message' => 'math-wmc-FileLabel',
@@ -57,6 +68,11 @@
 
        }
 
+       /**
+        * @param string $type
+        *
+        * @return array
+        */
        protected function printRunSelector( $type = 'selectorother' ) {
                $dbr = wfGetDB( DB_SLAVE );
                $formFields = array();
@@ -80,37 +96,31 @@
                return $formFields;
        }
 
+       /**
+        * @param $run
+        *
+        * @return bool|int|string
+        * @throws MWException
+        */
        function runSelectorFilter( $run ) {
                if ( $run == '' ) {
                        return date( 'Y-m-d H:i:s (e)' );
                }
-               $dbw = wfGetDB( DB_MASTER );
-               $uID = $this->getUser()->getId();
-               $res = $dbw->selectField( 'math_wmc_runs', 'runName',
-                       array( 'isDraft' => true, 'userID' => $uID, 'runId' => 
$run ) );
-               if ( ! $res ) {
-                       $exists = $dbw->selectField( 'math_wmc_runs', 'runId',
-                               array( 'userID' => $uID, 'runName' => $run ) );
-                       if ( ! $exists ) {
-                               $success = $dbw->insert( 'math_wmc_runs',
-                                       array( 'isDraft' => true, 'userID' => 
$uID, 'runName' => $run ) );
-                               if ( $success ) {
-                                       $this->runId = $dbw->insertId();
-                                       $this->getOutput()->addWikiMsg( 
'math-wmc-RunAdded', $run, $this->runId );
-                               } else {
-                                       $this->runId = false;
-                                       $this->getOutput()->addWikiMsg( 
'math-wmc-RunAddError', $run );
-                               }
-                       } else {
-                               $this->getOutput()->addWikiMsg( 
'math-wmc-RunAddExist', $run, $exists );
-                               $this->runId = false;
+               $this->runId  = $this->importer->validateRunId( $run );
+               /** @type array $warnings */
+               $warnings = $this->importer->getWarnings();
+               if ( $warnings ){
+                       echo "bad wqarni";
+                       foreach( $warnings as $warning ) {
+                               $this->getOutput()->addWikiText( $warning );
                        }
-               } else {
-                       $this->runId = $run;
                }
                return $run;
        }
 
+       /**
+        * @return bool|string
+        */
        function runValidatorFilter() {
                $dbr = wfGetDB( DB_SLAVE );
                $uID = $this->getUser()->getId();
@@ -123,9 +133,12 @@
                }
        }
 
+       /**
+        * @return bool|null|string
+        * @throws MWException
+        */
        function runFileCheck( ) {
                $out = $this->getOutput();
-
 
                $uploadResult = ImportStreamSource::newFromUpload( 'wpFile' );
 
@@ -135,31 +148,26 @@
 
                $source = $uploadResult->value;
 
-               $this->results = array();
                $out->addWikiMsg( 'math-wmc-importing' );
-               $error_msg = $this->importFromFile( $source->mHandle );
+               $error_msg = $this->importer->importFromFile( $source->mHandle 
);
 
                if ( ! is_null( $error_msg ) ) {
                        return $error_msg;
                }
-               if ( sizeof( $this->warnings ) ) {
-                       $out->addWikiText( self::formatErrors( $this->warnings 
) );
+               if ( sizeof( $this->importer->getWarnings() ) ) {
+                       $out->addWikiText( self::formatErrors( 
$this->importer->getWarnings() ) );
                }
 
                return true;
        }
 
-       public function deleteRun( $runID ) {
-               if ( !$this->getRequest()->getBool( "wpattachResults" ) ) {
-                       $dbw = wfGetDB( DB_MASTER );
-                       $dbw->delete( 'math_wmc_results', array( 'runId' => 
$runID ) );
-               }
-       }
-
-       function processInput(  ) {
+       /**
+        * @return bool
+        */
+       function processInput( ) {
                $this->getOutput()->addWikiMsg( "math-wmc-SubmissionSuccess" );
-               $this->deleteRun( $this->runId );
-               $dbw = wfGetDB( DB_MASTER );
+               $this->importer->setOverwrite( !$this->getRequest()->getBool( 
"wpattachResults" ) );
+               $this->importer->processInput();
                //TODO: Find adequate API call
                $this->getOutput()->addHTML('<table border="1" 
style="width:100%">
   <tr>
@@ -168,9 +176,7 @@
     <th>rank</th>
     <th>rendering</th>
   </tr>');
-               foreach ( $this->results as $result ) {
-                       $result['runId'] = $this->runId; //make sure that runId 
is correct
-                       $dbw->insert( 'math_wmc_results', $result );
+               foreach ( $this->importer->getResults() as $result ) {
                        $this->printResultRow( $result );
                }
                $this->getOutput()->addHTML('</table>');
@@ -178,6 +184,9 @@
                return true;
        }
 
+       /**
+        * @param $row
+        */
        private function printResultRow( $row ){
                $md5 = MathObject::hash2md5( $row['math_inputhash'] );
                if( $this->getRequest()->getBool( "wpdisplayFormulae" ) ){
@@ -192,126 +201,14 @@
                        $renderedMath = $md5;
                }
                $formulaId = MathSearchHooks::generateMathAnchorString( 
$row['oldId'], $row['fId']  );
-               $link=Revision::newFromId( $row['oldId'] 
)->getTitle()->getCanonicalURL().$formulaId;
-               $this->getOutput()->addHTML("<tr><td>${row['qId']}</td><td><a 
href=\"${link}\" >$formulaId</a></td>
+               $link=Revision::newFromId( $row['oldId'] 
)->getTitle()->getLinkURL().$formulaId;
+               $this->getOutput()->addHTML("<tr><td>${row['qId']}</td><td><a 
href=\"$link\" >$formulaId</a></td>
                        <td>${row['rank']}</td><td>$renderedMath</td></tr>");
        }
 
        /**
-        * @param $csv_file
-        * @return null
+        * @throws MWException
         */
-       protected function importFromFile( $csv_file ) {
-               if ( is_null( $csv_file ) ) {
-                       return wfMessage( 'emptyfile' )->text();
-               }
-
-               $table = array();
-
-               while ( $line = fgetcsv( $csv_file ) ) {
-                       array_push( $table, $line );
-               }
-               fclose( $csv_file );
-
-               // Get rid of the "byte order mark", if it's there - this is
-               // a three-character string sometimes put at the beginning
-               // of files to indicate its encoding.
-               // Code copied from:
-               // 
http://www.dotvoid.com/2010/04/detecting-utf-bom-byte-order-mark/
-               $byteOrderMark = pack( "CCC", 0xef, 0xbb, 0xbf );
-               if ( 0 == strncmp( $table[0][0], $byteOrderMark, 3 ) ) {
-                       $table[0][0] = substr( $table[0][0], 3 );
-                       // If there were quotation marks around this value,
-                       // they didn't get removed, so remove them now.
-                       $table[0][0] = trim( $table[0][0], '"' );
-               }
-
-               return $this->importFromArray( $table );
-
-       }
-
-       private function isValidQId( $qId ) {
-               if( array_key_exists( $qId, $this->validQIds) ){
-                       return $this->validQIds[$qId];
-               }
-               $dbr = wfGetDB( DB_SLAVE );
-               if ( $dbr->selectField( 'math_wmc_ref', 'qId', array( 'qId' => 
$qId ) ) ) {
-                       $this->validQIds[$qId] = true;
-                       return true;
-               } else {
-                       $this->validQIds[$qId] = false;
-                       return false;
-               }
-       }
-
-       private function  getInputHash( $pId, $eId ) {
-               $dbr = wfGetDB( DB_SLAVE );
-               return $dbr->selectField( 'mathindex', 'mathindex_inputhash',
-                       array( 'mathindex_page_id' => $pId, 'mathindex_anchor' 
=> $eId ) );
-       }
-
-       protected function importFromArray( $table ) {
-               global $wgMathWmcMaxResults;
-               define( 'ImportPattern', '/math\.(\d+)\.(\d+)/' );
-
-               // check header line
-               $uploadedHeaders = $table[0];
-               if ( $uploadedHeaders != self::columnHeaders ) {
-                       $error_msg = wfMessage( 'math-wmc-bad-header' )->text();
-                       return $error_msg;
-               }
-               $rank = 0;
-               $lastQueryID = 0;
-               foreach ( $table as $i => $line ) {
-                       if ( $i == 0 )
-                               continue;
-                       $pId = false;
-                       $eId = false;
-                       $fHash = false;
-                       $qId = trim( $line[0] );
-                       $fId = trim( $line[1] );
-                       $qValid = $this->isValidQId( $qId );
-                       if ( $qValid == false ) {
-                               $this->warnings[] = wfMessage( 
'math-wmc-wrong-query-reference', $i, $qId )->text();
-                       }
-                       if ( preg_match( ImportPattern, $fId, $m ) ) {
-                               $pId = (int)$m[1];
-                               $eId = (int)$m[2];
-                               $fHash = $this->getInputHash( $pId, $eId );
-                               if ( $fHash == false ) {
-                                       $this->warnings[] = wfMessage( 
'math-wmc-wrong-formula-reference', $i, $pId, $eId )->text();
-                               }
-                       } else {
-                               $this->warnings[] = wfMessage( 
'math-wmc-malformed-formula-reference', $i, $fId, ImportPattern )->text();
-                       }
-                       if ( $qValid === true && $fHash !== false ) {
-                               // a valid result has been submitted
-                               if ( $qId == $lastQueryID ) {
-                                       $rank ++;
-                               } else {
-                                       $lastQueryID = $qId;
-                                       $rank = 1;
-                               }
-                               if ( $rank <= $wgMathWmcMaxResults ) {
-                                       $this->addValidatedResult( $qId, $pId, 
$eId, $fHash, $rank );
-                               } else {
-                                       $this->warnings[] = wfMessage( 
'math-wmc-too-many-results', $i, $qId, $fId, $rank, $wgMathWmcMaxResults 
)->text();
-                               }
-                       }
-               }
-               return null;
-       }
-
-       private function addValidatedResult( $qId, $pId, $eId, $fHash, $rank ) {
-               $this->results[] = array(
-                       'runId' => $this->runId,
-                       'qId'   => $qId,
-                       'oldId' => $pId,
-                       'fId'   => $eId,
-                       'rank' => $rank,
-                       'math_inputhash' => $fHash );
-       }
-
        private function displayFeedback(){
                $runId=$this->runId;
                $dbr=wfGetDB(DB_SLAVE);
diff --git a/includes/ImportCsv.php b/includes/ImportCsv.php
index 86b2b45..1a3470a 100644
--- a/includes/ImportCsv.php
+++ b/includes/ImportCsv.php
@@ -113,7 +113,7 @@
         * @param $csv_file
         * @return null
         */
-       protected function importFromFile( $csv_file ) {
+       public function importFromFile( $csv_file ) {
                if ( is_null( $csv_file ) ) {
                        return wfMessage( 'emptyfile' )->text();
                }
@@ -146,13 +146,13 @@
         * @param $table
         * @return null|string
         */
-       protected function importFromArray( $table ) {
+       public function importFromArray( $table ) {
                global $wgMathWmcMaxResults;
                define( 'ImportPattern', '/math\.(\d+)\.(\d+)/' );
 
                // check header line
                $uploadedHeaders = $table[0];
-               if ( $uploadedHeaders != self::columnHeaders ) {
+               if ( $uploadedHeaders != self::$columnHeaders ) {
                        $error_msg = wfMessage( 'math-wmc-bad-header' )->text();
                        return $error_msg;
                }
@@ -284,4 +284,32 @@
                }
        }
 
+       /**
+        * @return array
+        */
+       public function getWarnings() {
+               return $this->warnings;
+       }
+
+       /**
+        * @return boolean
+        */
+       public function isOverwrite() {
+               return $this->overwrite;
+       }
+
+       /**
+        * @param boolean $overwrite
+        */
+       public function setOverwrite( $overwrite ) {
+               $this->overwrite = $overwrite;
+       }
+
+       /**
+        * @return array
+        */
+       public function getResults() {
+               return $this->results;
+       }
+
 }
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I463e8566c9d34c8e2f4c678dcee55a7436c51a31
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MathSearch
Gerrit-Branch: master
Gerrit-Owner: Physikerwelt <[email protected]>

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

Reply via email to