Kipcool has submitted this change and it was merged.

Change subject: added functions to WikiDataAPI.php and added Omegawiki API. 
with corrections Change-Id: Iafc9fbb84045fc84d464b3ea0967075632f666ca
......................................................................


added functions to WikiDataAPI.php and added Omegawiki API.
with corrections
Change-Id: Iafc9fbb84045fc84d464b3ea0967075632f666ca
---
M App.php
M OmegaWiki/WikiDataAPI.php
A includes/api/OmegaWiki.i18n.php
A includes/api/OmegaWikiExt.php
A includes/api/owAddSyntrans.php
A includes/api/owDefine.php
6 files changed, 617 insertions(+), 55 deletions(-)

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



diff --git a/App.php b/App.php
index cfac608..b8aaf4c 100644
--- a/App.php
+++ b/App.php
@@ -5,7 +5,8 @@
 $dir = dirname( __FILE__ ) . '/';
 
 require_once( $dir . 'OmegaWiki/Wikidata.php' );
-require_once( $dir . '/SpecialLanguages.php' );
+require_once( $dir . 'SpecialLanguages.php' );
+require_once( $dir . 'includes/api/OmegaWikiExt.php');
 
 $wgExtensionCredits['other'][] = array(
        'path'            => __FILE__,
@@ -36,7 +37,7 @@
  * Should be repaired, if someone has the courage to go through
  * the undocumented code...
  */
-//$wgAPIModules['wikidata'] = 'ApiWikiData';
+// $wgAPIModules['wikidata'] = 'ApiWikiData';
 
 $wgExtensionMessagesFiles['Wikidata'] = $dir . 'Wikidata.i18n.php';
 
diff --git a/OmegaWiki/WikiDataAPI.php b/OmegaWiki/WikiDataAPI.php
index 06409b9..a8455c6 100644
--- a/OmegaWiki/WikiDataAPI.php
+++ b/OmegaWiki/WikiDataAPI.php
@@ -21,16 +21,16 @@
                        $this->dataset = $dc;
                }
        }
-       
+
        function createNewInDatabase() {
                $this->pageId = $this->createPage();
                createInitialRevisionForPage( $this->pageId, 'Created by adding 
expression' );
        }
-       
+
        function createPage() {
                return createPage( NS_EXPRESSION, getPageTitle( $this->spelling 
) );
        }
-       
+
        function isBoundToDefinedMeaning( $definedMeaningId ) {
                return expressionIsBoundToDefinedMeaning( $definedMeaningId, 
$this->id );
        }
@@ -38,7 +38,7 @@
        function bindToDefinedMeaning( $definedMeaningId, $identicalMeaning ) {
                createSynonymOrTranslation( $definedMeaningId, $this->id, 
$identicalMeaning );
        }
-       
+
        function assureIsBoundToDefinedMeaning( $definedMeaningId, 
$identicalMeaning ) {
                if ( !$this->isBoundToDefinedMeaning( $definedMeaningId ) ) {
                        $this->bindToDefinedMeaning( $definedMeaningId, 
$identicalMeaning );
@@ -159,7 +159,7 @@
 function createExpressionId( $spelling, $languageId ) {
        $dc = wdGetDataSetContext();
        $dbw = wfGetDB( DB_MASTER );
-       
+
        $expressionId = newObjectId( "{$dc}_expression" );
        $updateId = getUpdateTransactionId();
        $dbw->insert(
@@ -228,12 +228,12 @@
 }
 function createInitialRevisionForPage( $pageId, $comment ) {
        global $wgUser;
-               
+
        $dbw = wfGetDB( DB_MASTER );
        $userId = $wgUser->getID();
        $userName = $wgUser->getName();
        $timestamp = $dbw->timestamp();
-       
+
        $dbw->insert(
                'revision',
                array( 'rev_page' => $pageId,
@@ -247,7 +247,7 @@
 
        $revisionId = $dbw->insertId();
        setPageLatestRevision( $pageId, $revisionId );
-       
+
        return $revisionId;
 }
 
@@ -346,14 +346,14 @@
 }
 
 function createSynonymOrTranslation( $definedMeaningId, $expressionId, 
$identicalMeaning = "true" ) {
-       
+
        $dc = wdGetDataSetContext();
        $synonymId = getSynonymId( $definedMeaningId, $expressionId );
-       
+
        if ( $synonymId == 0 ) {
                $synonymId = newObjectId( "{$dc}_syntrans" );
        }
-       
+
        $dbw = wfGetDB( DB_MASTER );
        if ( $identicalMeaning == "true" ) {
                $identicalMeaningInteger = 1;
@@ -380,7 +380,7 @@
 function expressionIsBoundToDefinedMeaning( $definedMeaningId, $expressionId ) 
{
        $dc = wdGetDataSetContext();
        $dbr = wfGetDB( DB_SLAVE );
-       
+
        $syntransId = $dbr->selectField(
                "{$dc}_syntrans",
                'syntrans_sid',
@@ -389,7 +389,7 @@
                        'remove_transaction_id' => null
                ), __METHOD__
        );
-       
+
        if ( $syntransId ) {
                return true;
        }
@@ -489,7 +489,7 @@
  * get an array of defined meaning id's found on the right hand side. And vice 
versa.
  * If you don't specify a relation type dmid but do give either a right hand 
side or
  * left hand side, you'll get all relations that exist in which the dm you did 
specify
- * is involved. 
+ * is involved.
  *
  * @param unknown_type $relationTypeId dmid of the relationtype, optional.
  * @param unknown_type $lhs dmid of the left hand side, optional.
@@ -844,7 +844,7 @@
 
 function updateDefinedMeaningDefinition( $definedMeaningId, $languageId, $text 
) {
        $definitionId = getDefinedMeaningDefinitionId( $definedMeaningId );
-       
+
        if ( $definitionId != 0 ) {
                updateTranslatedText( $definitionId, $languageId, $text );
        }
@@ -852,7 +852,7 @@
 
 function updateOrAddDefinedMeaningDefinition( $definedMeaningId, $languageId, 
$text ) {
        $definitionId = getDefinedMeaningDefinitionId( $definedMeaningId );
-       
+
        if ( $definitionId != 0 ) {
                updateTranslatedText( $definitionId, $languageId, $text );
        } else {
@@ -864,7 +864,7 @@
        removeTranslatedText( $setId, $languageId );
        addTranslatedText( $setId, $languageId, $text );
 }
- 
+
 function createText( $text ) {
        $dc = wdGetDataSetContext();
        $dbw = wfGetDB( DB_MASTER );
@@ -970,7 +970,7 @@
 
 function addDefinedMeaningDefinition( $definedMeaningId, $languageId, $text ) {
        $definitionId = getDefinedMeaningDefinitionId( $definedMeaningId );
-       
+
        if ( $definitionId == 0 ) {
                addDefinedMeaningDefiningDefinition( $definedMeaningId, 
$languageId, $text );
        } else {
@@ -994,7 +994,7 @@
 
 function addDefinedMeaningAlternativeDefinition( $definedMeaningId, 
$languageId, $text, $sourceMeaningId ) {
        $translatedContentId = newTranslatedContentId();
-       
+
        createDefinedMeaningAlternativeDefinition( $definedMeaningId, 
$translatedContentId, $sourceMeaningId );
        addTranslatedText( $translatedContentId, $languageId, $text );
 }
@@ -1029,11 +1029,11 @@
 }
 
 function removeDefinedMeaningAlternativeDefinition( $definedMeaningId, 
$definitionId ) {
-       // Dilemma: 
+       // Dilemma:
        // Should we also remove the translated texts when removing an
        // alternative definition? There are pros and cons. For
        // now it is easier to not remove them so they can be rolled
-       // back easier.      
+       // back easier.
 //     removeTranslatedTexts($definitionId);
 
        $dc = wdGetDataSetContext();
@@ -1052,7 +1052,7 @@
 
 function removeDefinedMeaningDefinition( $definedMeaningId, $languageId ) {
        $definitionId = getDefinedMeaningDefinitionId( $definedMeaningId );
-       
+
        if ( $definitionId != 0 ) {
                removeTranslatedText( $definitionId, $languageId );
        }
@@ -1205,7 +1205,7 @@
        $spelling = $expression->spelling;
        $pageId = createPage( NS_DEFINEDMEANING, getPageTitle( "$spelling 
($definedMeaningId)" ) );
        createInitialRevisionForPage( $pageId, 'Created by adding defined 
meaning' );
-       
+
        return $definedMeaningId;
 }
 
@@ -1221,7 +1221,7 @@
        $dc = wdGetDataSetContext();
        $textValueAttributeId = newObjectId( "{$dc}_text_attribute_values" );
        createTextAttributeValue( $textValueAttributeId, $objectId, 
$textAttributeId, $text );
-       
+
        return $textValueAttributeId;
 }
 
@@ -1349,7 +1349,7 @@
        $dc = wdGetDataSetContext();
        $translatedTextValueAttributeId = newObjectId( 
"{$dc}_translated_content_attribute_values" );
        $translatedContentId = newTranslatedContentId();
-       
+
        createTranslatedTextAttributeValue( $translatedTextValueAttributeId, 
$objectId, $attributeId, $translatedContentId );
        addTranslatedText( $translatedContentId, $languageId, $text );
 }
@@ -1374,12 +1374,12 @@
        $translatedTextAttribute = getTranslatedTextAttribute( $valueId );
        $transactionId = getUpdateTransactionId();
 
-       // Dilemma: 
+       // Dilemma:
        // Should we also remove the translated texts when removing a
        // translated content attribute? There are pros and cons. For
        // now it is easier to not remove them so they can be rolled
-       // back easier.      
-//     removeTranslatedTexts($translatedTextAttribute->value_tcid);  
+       // back easier.
+//     removeTranslatedTexts($translatedTextAttribute->value_tcid);
 
        $dbw->update( "{$dc}_translated_content_attribute_values",
                array( /* SET */
@@ -1522,6 +1522,81 @@
 }
 
 /**
+ * returns a spelling that is one of the possible translations of a given DM
+ * in a given language
+ */
+function getDefinedMeaningSpellingForLanguage( $definedMeaning, $language) {
+       $dc = wdGetDataSetContext();
+       $dbr = wfGetDB( DB_SLAVE );
+
+       $spelling = $dbr->selectField(
+               array( "{$dc}_expression" , "{$dc}_syntrans" ),
+               'spelling',
+               array(
+                       "{$dc}_syntrans.defined_meaning_id" => $definedMeaning,
+                       "{$dc}_expression.language_id" => $language,
+                       "{$dc}_expression.expression_id = 
{$dc}_syntrans.expression_id",
+                       "{$dc}_syntrans.remove_transaction_id" => null
+               ), __METHOD__
+       );
+
+       if ( $spelling ) {
+               return $spelling;
+       }
+       return "";
+}
+
+/**
+ * returns a spelling that is one of the possible translations of a given DM
+ * in any language
+ */
+function getDefinedMeaningSpellingForAnyLanguage( $definedMeaning ) {
+       $dc = wdGetDataSetContext();
+       $dbr = wfGetDB( DB_SLAVE );
+
+       $spelling = $dbr->selectField(
+               array( "{$dc}_expression" , "{$dc}_syntrans" ),
+               'spelling',
+               array(
+                       "{$dc}_syntrans.defined_meaning_id" => $definedMeaning,
+                       "{$dc}_expression.expression_id = 
{$dc}_syntrans.expression_id",
+                       "{$dc}_syntrans.remove_transaction_id" => null
+               ), __METHOD__
+       );
+
+       if ( $spelling ) {
+               return $spelling;
+       }
+       return "";
+}
+
+/**
+ * Returns the language id of a definedMeaning in any language
+ * according to which definition comes up first in the SQL query
+ * @param $definedMeaning
+ */
+function getDefinedMeaningSpellingLanguageId( $definedMeaning ) {
+       $dc = wdGetDataSetContext();
+       $dbr = wfGetDB( DB_SLAVE );
+
+       $languageId = $dbr->selectField(
+               array( "{$dc}_expression" , "{$dc}_syntrans" ),
+               'language_id',
+               array(
+                       "{$dc}_syntrans.defined_meaning_id" => $definedMeaning,
+                       "{$dc}_expression.expression_id = 
{$dc}_syntrans.expression_id",
+                       "{$dc}_syntrans.remove_transaction_id" => null
+               ), __METHOD__
+       );
+
+       if ( $languageId ) {
+               return $languageId;
+       }
+       return "";
+}
+
+
+/**
  * Returns the definition of a definedMeaning in a given language
  * @param $definedMeaningId
  * @param $languageId
@@ -1595,7 +1670,7 @@
 function getDefinedMeaningDefinition( $definedMeaningId ) {
        global $wgLang;
        $userLanguageId = getLanguageIdForCode( $wgLang->getCode() ) ;
-       
+
        if ( $userLanguageId > 0 ) {
                $result = getDefinedMeaningDefinitionForLanguage( 
$definedMeaningId, $userLanguageId );
        } else {
@@ -1603,7 +1678,7 @@
        }
        if ( $result == "" ) {
                $result = getDefinedMeaningDefinitionForLanguage( 
$definedMeaningId, 85 );
-               
+
                if ( $result == "" ) {
                        $result = getDefinedMeaningDefinitionForAnyLanguage( 
$definedMeaningId );
                }
@@ -1611,9 +1686,39 @@
        return $result;
 }
 
+/**
+ * returns a language_id in which a definition exists for the given 
definedMeaning
+ * returns "" if not found
+ */
+function getDefinedMeaningDefinitionLanguageForAnyLanguage( $definedMeaningId 
) {
+       $dc = wdGetDataSetContext();
+       $dbr = wfGetDB( DB_SLAVE );
+
+       $languageId = $dbr->selectField(
+               array(
+                       'dm' => "{$dc}_defined_meaning",
+                       'tc' => "{$dc}_translated_content",
+                       't' => "{$dc}_text"
+               ),
+               'tc.language_id',
+               array(
+                       'dm.defined_meaning_id' => $definedMeaningId,
+                       'dm.remove_transaction_id' => null,
+                       'tc.remove_transaction_id' => null,
+                       'dm.meaning_text_tcid = tc.translated_content_id',
+                       't.text_id = tc.text_id'
+               ), __METHOD__
+       );
+
+       if ( $languageId ) {
+               return $languageId;
+       }
+       return "";
+}
+
 
 /**
-* returns one of the possible translations of 
+* returns one of the possible translations of
 * a given DefinedMeaning ( $definedMeaningId )
 * preferably in a given language ( $languageCode )
 * or in English otherwise.
@@ -1719,7 +1824,7 @@
 
 function getCollectionContents( $collectionId ) {
        global $wgWikidataDataSet;
-       
+
        $dc = wdGetDataSetContext();
        $dbr = wfGetDB( DB_SLAVE );
        $queryResult = $dbr->query(
@@ -1729,19 +1834,19 @@
                        array( equals( 
$wgWikidataDataSet->collectionMemberships->collectionId, $collectionId ) )
                )
        );
-       
+
        $collectionContents = array();
-       
+
        while ( $collectionEntry = $dbr->fetchObject( $queryResult ) ) {
                $collectionContents[$collectionEntry->internal_member_id] = 
$collectionEntry->member_mid;
        }
-       
+
        return $collectionContents;
 }
 
 /**
  * Returns an array containing the ids of the defined meanings belonging to 
the collection
- * with the given id. 
+ * with the given id.
  *
  * @param unknown_type $collectionId
  * @param unknown_type $dc
@@ -1824,7 +1929,7 @@
        );
 
        $dmlist = array();
-       
+
        foreach ( $queryResult as $synonymRecord ) {
                $dmlist[] = $synonymRecord->defined_meaning_id;
        }
@@ -1903,8 +2008,8 @@
        $dbr = wfGetDB( DB_SLAVE );
 
        $uuid_array = array();
-       $uuid = - 1;
-    
+       $uuid = - 1;
+
        foreach ( $concepts as $dc => $dm_id ) {
                $collid = getCollectionIdForDC( $dc );
                $uuid_array[$dc] = getMapping( $dc, $collid, $dm_id );
@@ -1912,14 +2017,14 @@
                        $uuid = $uuid_array[$dc];
                }
        }
-       
+
        if ( $uuid == - 1 ) {
                $query = "SELECT uuid() AS id";
                $queryResult = $dbr->query( $query );
                $row = $dbr->fetchObject( $queryResult );
                $uuid = isset( $row->id ) ? $row->id : - 1;
        }
-       
+
        foreach ( $concepts as $dc => $dm_id ) {
                if ( $uuid_array[$dc] == - 1 ) {
                        $uuid_array[$dc] = $uuid;
@@ -1957,7 +2062,7 @@
        global $wgUser;
        // if(is_null($dc)) {
        //      $dc=wdGetDataSetContext();
-       // } 
+       // }
        $dbw = wfGetDB( DB_MASTER );
 
        $add_transaction_id = $override_transaction;
@@ -2174,9 +2279,9 @@
        global $wgLang;
 
        $userLanguageId = getLanguageIdForCode( $wgLang->getCode() ) ;
-       
+
        list( $definingExpressionId, $definingExpression, 
$definingExpressionLanguage ) = definingExpressionRow( $definedMeaningId );
-       
+
        if ( $userLanguageId > 0 ) {
                $result = definedMeaningExpressionForLanguage( 
$definedMeaningId, $userLanguageId );
        } else {
@@ -2185,10 +2290,10 @@
        if ( $result == "" ) {
                // if no expression exists for the specified language : look 
for an expression in English
                $result = definedMeaningExpressionForLanguage( 
$definedMeaningId, 85 );
-               
+
                if ( $result == "" ) {
                        $result = definedMeaningExpressionForAnyLanguage( 
$definedMeaningId );
-                       
+
                        if ( $result == "" ) {
                                $result = $definingExpression;
                        }
@@ -2262,7 +2367,7 @@
 
 class ClassAttributes {
        protected $classAttributes;
-       
+
        public function __construct( $definedMeaningId ) {
                $dc = wdGetDataSetContext();
                $dbr = wfGetDB( DB_SLAVE );
@@ -2279,39 +2384,81 @@
                );
 
                $this->classAttributes = array();
-               
+
                foreach ( $queryResult as $row ) {
                        $classAttribute = new ClassAttribute();
                        $classAttribute->attributeId = $row->attribute_mid;
                        $classAttribute->type = $row->attribute_type;
                        $classAttribute->levelName = $row->name;
-                       
+
                        $this->classAttributes[] = $classAttribute;
                }
        }
 
        public function filterClassAttributesOnLevelAndType( $levelName, $type 
) {
                $result = array();
-               
+
                foreach ( $this->classAttributes as $classAttribute ) {
                        if ( $classAttribute->levelName == $levelName && 
$classAttribute->type == $type ) {
                                $result[] = $classAttribute->attributeId;
                        }
                }
-               
+
                return $result;
        }
 
        public function filterClassAttributesOnLevel( $levelName ) {
                $result = array();
-               
+
                foreach ( $this->classAttributes as $classAttribute ) {
                        if ( $classAttribute->levelName == $levelName ) {
                                $result[] = $classAttribute->attributeId;
                        }
                }
-               
+
                return $result;
        }
 }
 
+/**
+ * returns the value of column if exist
+ * null if not found
+ * @param $table  table name
+ * @param $column column nane
+ * @param $value  column value
+ * @param $isDc   if has DataSet Context(boolean)
+ */
+function verifyColumn( $table, $column, $value, $isDc ) {
+       if ($isDc == 1) { $dc = wdGetDataSetContext() . "_"; }
+       else {$dc = '';}
+       $dbr = wfGetDB( DB_SLAVE );
+
+       $existId = $dbr->selectField(
+               "{$dc}{$table}",
+               "$column",
+               array(
+                       "$column" => $value
+               ), __METHOD__
+       );
+
+       if ( $existId ) {
+               return $existId;
+       }
+       return null;
+}
+
+/**
+ * returns back the language id if it exist
+ * null if not found
+ */
+function verifyLanguageId( $languageId ) {
+       return verifyColumn('language', 'language_id', $languageId, 0 );
+}
+
+/**
+ * returns back the definedMeaningId if it exist
+ * null if not found
+ */
+function verifyDefinedMeaningId( $definedMeaningId ) {
+       return verifyColumn('defined_meaning', 'defined_meaning_id', 
$definedMeaningId, 1 );
+}
diff --git a/includes/api/OmegaWiki.i18n.php b/includes/api/OmegaWiki.i18n.php
new file mode 100644
index 0000000..6dad769
--- /dev/null
+++ b/includes/api/OmegaWiki.i18n.php
@@ -0,0 +1,14 @@
+<?php
+$messages = array();
+
+/** English
+ */
+$messages['en'] = array(
+       'apiow-desc' => "OmegaWiki's WikiLexicalData extension of the mediawiki 
api.php",
+);
+
+/** Message documentation (Message documentation)
+ */
+$messages['qqq'] = array(
+       'apiow-desc' => "The message that appears in Special:Version when the 
omegawiki API is installed",
+);
diff --git a/includes/api/OmegaWikiExt.php b/includes/api/OmegaWikiExt.php
new file mode 100644
index 0000000..a7d93d0
--- /dev/null
+++ b/includes/api/OmegaWikiExt.php
@@ -0,0 +1,45 @@
+<?php
+
+// Take credit for your work.
+$wgExtensionCredits[][] = array(
+
+       // The full path and filename of the file. This allows MediaWiki
+       // to display the Subversion revision number on Special:Version.
+       'path' => __FILE__,
+
+       // The name of the extension, which will appear on Special:Version.
+       'name' => 'OmegaWiki',
+
+       // A description of the extension, which will appear on Special:Version.
+       'description' => 'An Omegawiki API extension',
+
+       // Alternatively, you can specify a message key for the description.
+       'descriptionmsg' => 'apiow-desc',
+
+       // The version of the extension, which will appear on Special:Version.
+       // This can be a number or a string.
+       'version' => '1.0',
+
+       // Your name, which will appear on Special:Version.
+       'author' => 'Hiong3-eng5 and Kip',
+
+       // The URL to a wiki page/web page with information about the extension,
+       // which will appear on Special:Version.
+       'url' => 'https://www.omegawiki.org/index.php/omegawiki api',
+
+);
+
+// Map class name to filename for autoloading
+       $wgAutoloadClasses['define'] = dirname( __FILE__ ) . '/owDefine.php';
+       $wgAutoloadClasses['addSyntrans'] = dirname( __FILE__ ) . 
'/owAddSyntrans.php';
+
+// Map module name to class name
+       $wgAPIModules['ow_define'] = 'define';
+       $wgAPIModules['ow_add_syntrans'] = 'addSyntrans';
+
+// Load the internationalization file
+       $wgExtensionMessagesFiles['myextension']
+       = dirname( __FILE__ ) . '/OmegaWiki.i18n.php';
+
+// Return true so that MediaWiki continues to load extensions.
+       return true;
diff --git a/includes/api/owAddSyntrans.php b/includes/api/owAddSyntrans.php
new file mode 100644
index 0000000..8e07144
--- /dev/null
+++ b/includes/api/owAddSyntrans.php
@@ -0,0 +1,240 @@
+<?php
+
+/*
+ * Created on March 19, 2013
+ *
+ * API for WikiData
+ *
+ * Some portion of this script was taken from Kipcool's maintenance
+ * script importMinnan.
+ *
+ * Copyright (C) 2013
+ *
+ * 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
+ */
+
+require_once( 'extensions/WikiLexicalData/OmegaWiki/WikiDataAPI.php' );
+require_once( 'extensions/WikiLexicalData/OmegaWiki/Transaction.php' );
+
+class addSyntrans extends ApiBase {
+
+       public $spelling, $dm, $languageId, $identicalMeaning, $result, $fp;
+
+       public function __construct( $main, $action ) {
+               parent :: __construct( $main, $action, null);
+       }
+
+       public function execute() {
+               global $wgUser, $wgOut;
+
+               // limit access to bots
+               if ( ! $wgUser->isAllowed( 'bot' ) ) {
+                       $this->dieUsage( 'you must have a bot flag to use this 
API function', 'bot_only' );
+               }
+
+               // keep blocked bots out
+               if ( $wgUser->isBlocked() ) {
+                       $this->dieUsage( 'your account is blocked.', 'blocked' 
);
+               }
+
+               // Get the parameters
+               $params = $this->extractRequestParams();
+
+               // If file, use batch processing
+               if ( $params['file'] ) {
+                       $file = $params['file'];
+                       $this->getResult()->addValue( null, 
$this->getModuleName(), array (
+                               'file' => $file ,
+                               'process' => 'batch processing'
+                               )
+                       );
+                       if ( ! $fp = openFile( $file) ) {
+                               $this->getResult()->addValue( null, 
$this->getModuleName(),
+                                       array ( 'open' => array (
+                                               'note' => "Can not open $file.",
+                                       ) )
+                               );
+                               return true;
+                       }
+                       else {
+                               $ctr = 0;
+                               while ( ! feof( $fp ) ) {
+                                       $inputData = fgetcsv( $fp, 1024, ',', 
'"', '\\' );
+                                       $ctr = $ctr + 1;
+                                       // file is in the form 
spelling,language_id,defined_meaning_id
+                                       $inputDataCount = count( $inputData );
+                                       if ( $inputDataCount == 1 ) {
+                                               $result = array ( 'note' => 
"skipped blank line");
+                                               $this->getResult()->addValue( 
null, $this->getModuleName(),
+                                                       array ( 'result' . $ctr 
=> $result )
+                                               );
+                                               continue;
+                                       }
+                                       $spelling = $inputData[0];
+                                       $languageId = $inputData[1];
+                                       $definedMeaningId = $inputData[2];
+                                       if ( $inputDataCount == 4 )
+                                               $identicalMeaning = 
$inputData[3];
+                                       if ( $inputDataCount == 3 )
+                                               $identicalMeaning = 1;
+                                       if ( !is_numeric($languageId) || 
!is_numeric($definedMeaningId) ) {
+                                               if($ctr == 1) {
+                                                       $result = array ( 
'note' => "either $languageId or $definedMeaningId is not an int or probably 
just the CSV header");
+                                               } else {
+                                                       $result = array ( 
'note' => "either $languageId or $definedMeaningId is not an int");
+                                               }
+                                       } else {
+                                               $result = array ( 'note' => 
owAddSynonymOrTranslation( $spelling, $languageId, $definedMeaningId, 
$identicalMeaning ) );
+                                       }
+                                       $this->getResult()->addValue( null, 
$this->getModuleName(),
+                                               array ( 'result' . $ctr => 
$result )
+                                       );
+                               }
+                       }
+                       return true;
+               }
+               // if not, add just one syntrans
+               $spelling = $params['e'];
+               $definedMeaningId = $params['dm'];
+               $languageId = $params['lang'];
+               $identicalMeaning = $params['im'];
+               $this->getResult()->addValue( null, $this->getModuleName(), 
array (
+                       'spelling' => $spelling ,
+                       'dmid' => $definedMeaningId ,
+                       'lang' => $languageId ,
+                       'im' => $identicalMeaning
+                       )
+               );
+               $result = owAddSynonymOrTranslation( $spelling, $languageId, 
$definedMeaningId, $identicalMeaning );
+               $this->getResult()->addValue( null, $this->getModuleName(),
+                       array ( 'result' => array ( 'note' => $result, )
+                       )
+               );
+               return true;
+       }
+
+       // Version
+       public function getVersion() {
+               return __CLASS__ . ': $Id$';
+       }
+
+       // Description
+       public function getDescription() {
+               return 'Add expressions, synonyms/translations to Omegawiki.' ;
+       }
+
+       // Parameters.
+       public function getAllowedParams() {
+               return array(
+                       'e' => array (
+                               ApiBase::PARAM_TYPE => 'string',
+                       ),
+                       'dm' => array (
+                               ApiBase::PARAM_TYPE => 'integer',
+                       ),
+                       'lang' => array (
+                               ApiBase::PARAM_TYPE => 'integer',
+                       ),
+                       'im' => array (
+                               ApiBase::PARAM_TYPE => 'integer',
+                       ),
+                       'file' => array (
+                               ApiBase::PARAM_TYPE => 'string',
+                       )
+               );
+       }
+
+       // Describe the parameter
+       public function getParamDescription() {
+               return array(
+                       'e' => 'The expression to be added' ,
+                       'dm' => 'The defined meaning id where the expression 
will be added' ,
+                       'lang' => 'The language id of the expression' ,
+                       'im' => 'The identical meaning value. (boolean)' ,
+                       'file' => 'The file to process. (csv format)'
+               );
+       }
+
+       // Get examples
+       public function getExamples() {
+       return array(
+               'Add a synonym/translation to the defined meaning definition',
+               'If the expression is already present. Nothing happens',
+               
'api.php?action=ow_add_syntrans&e=欠席&dm=334562&lang=387&im=1&format=xml',
+               'You can also add synonym/translation using a CSV file.  The 
file must ',
+               'contain at least 3 columns (and 1 optional column):',
+               ' spelling           (string)',
+               ' language_id        (int)',
+               ' defined_meaning_id (int)',
+               ' identical meaning  (boolean 1 or 2, optional)',
+               
'api.php?action=ow_add_syntrans&file=D:\xampp\data\add_expression_to_dm.csv&format=xml'
+               );
+       }
+}
+
+function openFile( $filename ) {
+       $exist = file_exists($filename);
+       if ($exist == 1) {
+               return fopen( $filename, 'r' );
+       } else {
+               return null;
+       }
+}
+
+function owAddSynonymOrTranslation( $spelling, $languageId, $definedMeaningId, 
$identicalMeaning ) {
+       global $wgUser;
+       $dc = wdGetDataSetContext();
+
+       // check that the language_id exists
+       if ( ! verifyLanguageId( $languageId ) ) {
+               return 'Non existent language id.';
+       }
+
+       // check that defined_meaning_id exists
+       if ( ! verifyDefinedMeaningId( $definedMeaningId ) ) {
+               return 'Non existent dm id.';
+       }
+
+       // trim spelling
+       $spelling = trim( $spelling );
+
+       if ( $identicalMeaning == 1 ) {
+               $identicalMeaning = "true";
+       }
+       else {
+               $identicalMeaning = "false";
+       }
+
+       // first check if it exists, then create the transaction and put it in 
db
+       $expression = findExpression( $spelling, $languageId );
+       if ( $expression ) {
+               // the expression exists, check if it has this syntrans
+               $bound = expressionIsBoundToDefinedMeaning ( $definedMeaningId, 
$expression->id );
+               if (  $bound == true ) {
+                       $synonymId = getSynonymId( $definedMeaningId, 
$expression->id );
+                       return "sid=$synonymId - exists: $spelling , lang_id = 
$languageId , dm_id = $definedMeaningId im = $identicalMeaning";
+               }
+       }
+       // adding the expression
+       startNewTransaction( $wgUser->getID(), "0.0.0.0", "", $dc);
+
+       addSynonymOrTranslation( $spelling, $languageId, $definedMeaningId, 
$identicalMeaning );
+
+       $expressionId = getExpressionId( $spelling, $languageId );
+       $synonymId = getSynonymId( $definedMeaningId, $expressionId );
+       return "sid=$synonymId - adding: $spelling , lang_id = $languageId , 
dm_id = $definedMeaningId, im = $identicalMeaning";
+
+}
diff --git a/includes/api/owDefine.php b/includes/api/owDefine.php
new file mode 100644
index 0000000..ba87937
--- /dev/null
+++ b/includes/api/owDefine.php
@@ -0,0 +1,115 @@
+<?php
+
+/*
+ * Created on March 14, 2013
+ *
+ * API for WikiData
+ *
+ * Copyright (C) 2013
+ *
+ * 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
+ */
+require_once( 'extensions/WikiLexicalData/OmegaWiki/WikiDataAPI.php' );
+
+class define extends ApiBase {
+
+       public $languageId, $text, $spelling, $spellingLanguageId;
+
+       public function __construct( $main, $action ) {
+               parent :: __construct( $main, $action, null);
+       }
+
+       public function execute() {
+               global $wgUser, $wgOut;
+
+               // Get the parameters
+               $params = $this->extractRequestParams();
+
+               if ($params['lang']) {
+                       $languageId = $params['lang'];
+                       $text = getDefinedMeaningDefinitionForLanguage( 
$params['dm'], $languageId );
+                       $spelling = getDefinedMeaningSpellingForLanguage( 
$params['dm'], $languageId );
+                       $spellingLanguageId = $languageId ;
+                       if (!$text) {
+                               $languageId = 85;
+                               $text = getDefinedMeaningDefinitionForLanguage( 
$params['dm'], $languageId );
+                       }
+               } else {
+                       $languageId = 
getDefinedMeaningDefinitionLanguageForAnyLanguage( $params['dm'] );
+                       $text = getDefinedMeaningDefinitionForAnyLanguage( 
$params['dm'] );
+                       $spelling = getDefinedMeaningSpellingForAnyLanguage( 
$params['dm'] );
+                       // spellingLanguageId is wrong
+                       $spellingLanguageId = 
getDefinedMeaningSpellingLanguageId( $params['dm'] );
+               }
+
+               // Top level
+               $this->getResult()->addValue( null, $this->getModuleName(), 
array ( 'dmid' => $params['dm'] ) );
+               $this->getResult()->addValue( null, $this->getModuleName(), 
array ( 'spelling' => $spelling ) );
+               $this->getResult()->addValue( null, $this->getModuleName(), 
array ( 'spelllang' => $spellingLanguageId ) );
+
+               // definition
+               $this->getResult()->addValue( null, $this->getModuleName()
+                       , array ( 'definition' => array (
+                               'lang' => $languageId ,
+                               'text' => $text
+                       ) ) );
+               return true;
+       }
+
+       // Version
+       public function getVersion() {
+               return __CLASS__ . ': $Id$';
+       }
+
+       // Description
+       public function getDescription() {
+               return 'Get the definition of a defined meaning.' ;
+       }
+
+       // Parameters.
+       public function getAllowedParams() {
+               return array(
+                       'dm' => array (
+                               ApiBase::PARAM_TYPE => 'integer',
+                               ApiBase::PARAM_REQUIRED => true
+                       ),
+                       'lang' => array (
+                               ApiBase::PARAM_TYPE => 'integer',
+                       ),
+               );
+       }
+
+       // Describe the parameter
+       public function getParamDescription() {
+               return array(
+                       'dm' => 'The defined meaning id to be defined' ,
+                       'lang' => 'The language id to be defined'
+               );
+       }
+
+       // Get examples
+       public function getExamples() {
+               return array(
+                       'Get a definition from a defined meaning id',
+                       'api.php?action=ow_define&dm=8218&format=xml',
+                       'Get a definition from a defined meaning id and a 
language id.',
+                       'When a definition is not available for a language id, 
',
+                       'the definition will default to English',
+                       'api.php?action=ow_define&dm=8218&lang=87&format=xml' ,
+                       'api.php?action=ow_define&dm=8218&lang=107&format=xml'
+               );
+       }
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Iafc9fbb84045fc84d464b3ea0967075632f666ca
Gerrit-PatchSet: 5
Gerrit-Project: mediawiki/extensions/WikiLexicalData
Gerrit-Branch: master
Gerrit-Owner: Hiong3-eng5 <[email protected]>
Gerrit-Reviewer: Hiong3-eng5 <[email protected]>
Gerrit-Reviewer: Kipcool <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>

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

Reply via email to