Physikerwelt has uploaded a new change for review.

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

Change subject: Import script for mathosphere's JSON like output
......................................................................

Import script for mathosphere's JSON like output

Change-Id: Ie9e706805ffa81b151c8119304b5fcfd9957b0bc
---
M db/mathsemantics.sql
M db/snippets/mathidentifier.view.sql
A maintenance/ImportDefinitions.php
3 files changed, 113 insertions(+), 21 deletions(-)


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

diff --git a/db/mathsemantics.sql b/db/mathsemantics.sql
index 3c0dcc2..4ce8e89 100644
--- a/db/mathsemantics.sql
+++ b/db/mathsemantics.sql
@@ -1,8 +1,8 @@
-CREATE TABLE `mathsemantics` (
-  `pageId` int(5) NOT NULL,
-  `identifier` varchar(4) NOT NULL,
+CREATE TABLE /*_*/mathsemantics (
+  `revision_id` int(10) UNSIGNED NOT NULL,
+  `identifier` varchar(20) NOT NULL,
   `evidence` double NOT NULL,
   `noun` varchar(20) NOT NULL,
-  `sentence` text NOT NULL,
-  KEY `pageId` (`pageId`)
-) ENGINE=MyISAM DEFAULT CHARSET=utf8;
+  `sentence` text NULL,
+  KEY `revision_id` (`revision_id`)
+) /*$wgDBTableOptions*/;
diff --git a/db/snippets/mathidentifier.view.sql 
b/db/snippets/mathidentifier.view.sql
index 7300777..fc5a34f 100644
--- a/db/snippets/mathidentifier.view.sql
+++ b/db/snippets/mathidentifier.view.sql
@@ -1,16 +1,17 @@
-CREATE 
-    ALGORITHM = UNDEFINED 
-    DEFINER = `root`@`localhost` 
-    SQL SECURITY DEFINER
+-- This view is outdated
+CREATE
+  ALGORITHM = UNDEFINED
+  DEFINER = `root`@`localhost`
+  SQL SECURITY DEFINER
 VIEW `math_identifier` AS
-    select 
-        `S`.`identifier` AS `identifier`,
-        `S`.`noun` AS `noun`,
-        `S`.`evidence` AS `evidence`,
-        `S`.`sentence` AS `sentence`,
-        `S`.`sentenceHash` AS `sentenceHash`,
-        `M`.`pageTitle` AS `pageTitle`,
-        `M`.`pageId` AS `pageID`
-    from
-        (`mathsemantics` `S`
-        join `mathIdMap` `M` ON ((`S`.`pageId` = `M`.`pageId`)))
\ No newline at end of file
+  SELECT
+    `S`.`identifier`   AS `identifier`,
+    `S`.`noun`         AS `noun`,
+    `S`.`evidence`     AS `evidence`,
+    `S`.`sentence`     AS `sentence`,
+    `S`.`sentenceHash` AS `sentenceHash`,
+    `M`.`pageTitle`    AS `pageTitle`,
+    `M`.`pageId`       AS `pageID`
+  FROM
+    (`mathsemantics` `S`
+      JOIN `mathIdMap` `M` ON ((`S`.`pageId` = `M`.`pageId`)))
\ No newline at end of file
diff --git a/maintenance/ImportDefinitions.php 
b/maintenance/ImportDefinitions.php
new file mode 100644
index 0000000..98567d3
--- /dev/null
+++ b/maintenance/ImportDefinitions.php
@@ -0,0 +1,91 @@
+<?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 ( __DIR__ . '/../../../maintenance/Maintenance.php' );
+
+/**
+ * Class ImportDefinitions
+ */
+class ImportDefinitions extends Maintenance {
+       private $dir;
+       private $overwrite;
+
+       /**
+        *
+        */
+       public function __construct() {
+               parent::__construct();
+               $this->mDescription = 'Batch imports definitions from a 
folder.' .
+                       " \n Processes mathosphere files that follow the naming 
convention: \n *.json";
+               $this->addArg( 'dir', 'The directory to be read', true );
+               $this->addOption(
+                       'overwrite', 'Overwrite existing definitions with the 
same name.', false, false, "o"
+               );
+       }
+
+       /**
+        *
+        */
+       public function execute() {
+               $dbw = wfGetDB( DB_MASTER );
+               $this->dir = $this->getArg( 0 );
+               $this->overwrite = $this->getOption( 'overwrite' );
+               if ( $this->overwrite ) {
+                       $this->output( "Loaded with option overwrite enabled 
.\n" );
+               }
+               if ( !is_dir( $this->dir ) ) {
+                       $this->output( "{$this->dir} is not a directory.\n" );
+                       exit( 1 );
+               }
+               $files = new GlobIterator( $this->dir . '/*.json' );
+               foreach ( $files as $file ) {
+                       $handle = fopen( $file, 'r' );
+                       while ( !feof( $handle ) ) {
+                               $line = fgets( $handle );
+                               if ( preg_match( 
'/^[\s\[]*(?P<content>\{.*?\})[\s,\]]$/', $line, $matches ) ) {
+                                       $oJson = json_decode( 
$matches['content'] );
+                                       $title = Title::newFromText( 
$oJson->title );
+                                       $dbw->begin();
+                                       if ( $title->exists() ) {
+                                               $revId = 
$title->getLatestRevID();
+                                               foreach ( $oJson->relations as 
$relation ) {
+                                                       $dbw->insert( 
'mathsemantics', array(
+                                                               'revision_id' 
=> $revId,
+                                                               'identifier'  
=> $relation->identifier,
+                                                               'noun'        
=> $relation->definition,
+                                                               'evidence'    
=> $relation->score
+                                                       ) );
+                                                       $this->output( 
"{$title->getText()}: $relation->identifier is ".
+                                                               
"$relation->definition certainty $relation->score\n" );
+                                               }
+                                       } else {
+                                               $this->output( 
$title->getText() . " does not exist\n" );
+                                       }
+                                       $dbw->commit();
+                               }
+                       }
+               }
+       }
+}
+
+$maintClass = 'ImportDefinitions';
+/** @noinspection PhpIncludeInspection */
+require_once ( RUN_MAINTENANCE_IF_MAIN );

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie9e706805ffa81b151c8119304b5fcfd9957b0bc
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MathSearch
Gerrit-Branch: master
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