Kipcool has submitted this change and it was merged.

Change subject: API: Created Express; Modified Define
......................................................................


API: Created Express; Modified Define

Change-Id: Ia25c91472e23326d26b9fbf16c41383fb9d9683e
---
M includes/api/OmegaWikiExt.php
M includes/api/owDefine.php
A includes/api/owExpress.php
3 files changed, 190 insertions(+), 45 deletions(-)

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



diff --git a/includes/api/OmegaWikiExt.php b/includes/api/OmegaWikiExt.php
index cf674e9..2fec158 100644
--- a/includes/api/OmegaWikiExt.php
+++ b/includes/api/OmegaWikiExt.php
@@ -22,17 +22,19 @@
 
        // The URL to a wiki page/web page with information about the extension,
        // which will appear on Special:Version.
-       'url' => 'https://www.omegawiki.org/omegawiki_api',
+       'url' => 'https://www.omegawiki.org/Help:OmegaWiki_API',
 
 );
 
 // Map class name to filename for autoloading
        $wgAutoloadClasses['Define'] = dirname( __FILE__ ) . '/owDefine.php';
+       $wgAutoloadClasses['Express'] = dirname( __FILE__ ) . '/owExpress.php';
        $wgAutoloadClasses['AddSyntrans'] = dirname( __FILE__ ) . 
'/owAddSyntrans.php';
        $wgAutoloadClasses['AddAnnotation'] = dirname( __FILE__ ) . 
'/owAddAnnotation.php';
 
 // Map module name to class name
        $wgAPIModules['ow_define'] = 'Define';
+       $wgAPIModules['ow_express'] = 'Express';
        $wgAPIModules['ow_add_syntrans'] = 'AddSyntrans';
        $wgAPIModules['ow_add_annotation'] = 'AddAnnotation';
 
diff --git a/includes/api/owDefine.php b/includes/api/owDefine.php
index 3e86bb9..1e061e0 100644
--- a/includes/api/owDefine.php
+++ b/includes/api/owDefine.php
@@ -1,27 +1,17 @@
 <?php
 
-/*
+/** O m e g a W i k i   A P I ' s   D e f i n e   c l a s s
+ *
  * 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
  */
+
+/** HISTORY
+ * - 2013-05-23: Created separate defining and definingByAnyLanguage functions.
+ *             Can be useful for E x p r e s s   c l a s s  ~he
+ * - 2013-03-14: Creation date ~he
+ */
+
 require_once( 'extensions/WikiLexicalData/OmegaWiki/WikiDataAPI.php' );
 
 class Define extends ApiBase {
@@ -38,34 +28,16 @@
                // 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 );
-                       }
+               // Optional parameter
+               if ( $params['lang'] ) {
+                       $this->languageId = $params['lang'];
+                       $defined = defining( $params['dm'], $params['lang'], 
$this->getModuleName() );
                } else {
-                       $languageId = 
getDefinedMeaningDefinitionLanguageForAnyLanguage( $params['dm'] );
-                       $text = getDefinedMeaningDefinitionForAnyLanguage( 
$params['dm'] );
-                       $spelling = getDefinedMeaningSpellingForAnyLanguage( 
$params['dm'] );
-                       // spellingLanguageId is wrong
-                       $spellingLanguageId = 
getDefinedMeaningSpellingLanguageId( $params['dm'] );
+                       $defined = definingForAnyLanguage( $params['dm'], 
$this->getModuleName() );
                }
 
-               // 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
-                       ) ) );
+               $defined = $defined[ $this->getModuleName() ];
+               $this->getResult()->addValue( null, $this->getModuleName(), 
$defined );
                return true;
        }
 
@@ -112,4 +84,68 @@
                        'api.php?action=ow_define&dm=8218&lang=107&format=xml'
                );
        }
+
+}
+
+function defining( $definedMeaningId, $languageId, $moduleName = null ) {
+
+       if ( !$moduleName ) {
+               $moduleName = 'ow_define';
+       }
+
+       $text = getDefinedMeaningDefinitionForLanguage( $definedMeaningId, 
$languageId );
+       $spelling = getDefinedMeaningSpellingForLanguage( $definedMeaningId, 
$languageId );
+       $spellingLanguageId = $languageId ;
+
+       if ( !$text ) {
+               $languageId = 85;
+               $text = getDefinedMeaningDefinitionForLanguage( 
$definedMeaningId, $languageId );
+       }
+
+       // Add later when I have time
+       //$definitionSpelling = getSpellingForLanguage( $definedMeaningId, 
$languageCode, 'en' );
+
+       return array(
+               $moduleName => array(
+                       'dmid'=> $definedMeaningId,
+                       'spelling'=>$spelling,
+                       'spelllang'=>$spellingLanguageId,
+                       'definition' =>array (
+                       /*      'spelling'=>$definitionSpelling,
+                       */      'lang'=>$languageId,
+                               'text'=>$text
+                       )
+               )
+       );
+
+}
+
+function definingForAnyLanguage( $definedMeaningId, $moduleName = null ) {
+
+       if ( !$moduleName ) {
+               $moduleName = 'ow_define';
+       }
+
+       $languageId = getDefinedMeaningDefinitionLanguageForAnyLanguage( 
$definedMeaningId );
+       $text = getDefinedMeaningDefinitionForAnyLanguage( $definedMeaningId );
+       $spelling = getDefinedMeaningSpellingForAnyLanguage( $definedMeaningId 
);
+
+       $spellingLanguageId = getDefinedMeaningSpellingLanguageId( 
$definedMeaningId );
+
+       // Add later when I have time
+       //$definitionSpelling = getSpellingForLanguage( $definedMeaningId, 
$languageCode, 'en' );
+
+       return array(
+               $moduleName => array(
+                       'dmid'=> $definedMeaningId,
+                       'spelling'=>$spelling,
+                       'spelllang'=>$spellingLanguageId,
+                       'definition' =>array (
+                       /*      'spelling'=>$definitionSpelling,
+                       */      'lang'=>$languageId,
+                               'text'=>$text
+                       )
+               )
+       );
+
 }
diff --git a/includes/api/owExpress.php b/includes/api/owExpress.php
new file mode 100644
index 0000000..fb50482
--- /dev/null
+++ b/includes/api/owExpress.php
@@ -0,0 +1,107 @@
+<?php
+
+/** OmegaWiki API's Express class
+ * Created on May 21, 2013
+ *
+ * Returns an array of datas similar to the one given by the Expression 
Namespace.
+ *
+ */
+
+/** PURPOSE
+ * To provide a way for Developers and/or contributors of Wiktionary
+ * to access OmegaWiki's data that can be easily parsed.
+ */
+
+ /** TODO
+ * - Improve definition by improving owDefine.php
+ *             Need to make Class Define return output similar to what
+ *             OmegaWiki outputs in the DefinedMeaning Namespace.
+ * - Add param lang (optional). To set the primary language.
+ * - Add param limit_lang (optional). To limit search to just a language or 
more.
+ * - Add param options (optional). To limit the display to only
+ *             data one is interested in. Example:
+ *             & o p t i o n s = p i n y i n | I P A | h y p h e n a t i o n
+ */
+
+require_once( 'extensions/WikiLexicalData/OmegaWiki/WikiDataAPI.php' );
+require_once( 'owDefine.php' );
+
+class Express extends ApiBase {
+
+       public function __construct( $main, $action ) {
+               parent :: __construct( $main, $action, null);
+       }
+
+       public function execute() {
+
+               global $wgScriptPath;
+
+               // Get the parameters
+               $params = $this->extractRequestParams();
+
+               // Parameter checks
+               if ( !isset($params['search']) ) {
+                       $this->dieUsage( 'parameter search is missing', 'param 
search is missing' );
+               }
+
+               $spelling = $params['search'];
+
+               // Check if spelling exist
+               if ( existSpelling( $spelling ) ) {
+                       $dmlist = getExpressionMeaningIds( $spelling );
+                       // There are duplicates using getExpressionMeaningIds 
!!!
+                       $dmlist = array_unique ( $dmlist );
+                       $express['expression'] = $spelling;
+                       $dmlistCtr = 1;
+                       foreach ( $dmlist as $dmrow ) {
+                               $defining = definingForAnyLanguage( $dmrow );
+                               $express['dm' . $dmlistCtr] = $defining;
+                               $dmlistCtr += 1;
+                       }
+                       $this->getResult()->addValue( null, 
$this->getModuleName(), array ( 'expression'=>$express ) );
+       
+               } else {
+                       $this->dieUsage( 'the search word does not exist.', 
'non-existent spelling' );
+               }
+
+               return true;
+       }
+
+       // Version
+       public function getVersion() {
+               return __CLASS__ . ': $Id$';
+       }
+
+       // Description
+       public function getDescription() {
+               return 'View Omegawiki Expression.' ;
+       }
+
+       // Parameters.
+       public function getAllowedParams() {
+               return array(
+                       'search' => array (
+                               ApiBase::PARAM_TYPE => 'string',
+                               ApiBase::PARAM_REQUIRED => true
+                       )
+               );
+       }
+
+       // Describe the parameter
+       public function getParamDescription() {
+               return array(
+                       'search' => 'The expression to view'
+               );
+       }
+
+       // Get examples
+       public function getExamples() {
+               return array(
+                       'View Expression',
+                       'api.php?action=ow_express&search=acusar&format=xml',
+                       'api.php?action=ow_express&search=pig&format=xml',
+                       'api.php?action=ow_express&search=咱&format=xml'
+               );
+       }
+
+}

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ia25c91472e23326d26b9fbf16c41383fb9d9683e
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikiLexicalData
Gerrit-Branch: master
Gerrit-Owner: Hiong3-eng5 <hiong3.e...@gmail.com>
Gerrit-Reviewer: Hiong3-eng5 <hiong3.e...@gmail.com>
Gerrit-Reviewer: Kipcool <kipmas...@gmail.com>

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

Reply via email to