Hiong3-eng5 has uploaded a new change for review.
https://gerrit.wikimedia.org/r/134299
Change subject: Refactor Database API
......................................................................
Refactor Database API
Mostly from Kip's suggestions. Language refactoring, incomplete.
Change-Id: I8a57dbad7bdf3bb582e22813f7bfb0dcbc9bd284
---
M OmegaWiki/OmegaWikiDatabaseAPI.php
M OmegaWiki/WikiDataAPI.php
M OmegaWiki/languages.php
3 files changed, 128 insertions(+), 84 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/WikiLexicalData
refs/changes/99/134299/1
diff --git a/OmegaWiki/OmegaWikiDatabaseAPI.php
b/OmegaWiki/OmegaWikiDatabaseAPI.php
index 30a252a..8c8d12b 100644
--- a/OmegaWiki/OmegaWikiDatabaseAPI.php
+++ b/OmegaWiki/OmegaWikiDatabaseAPI.php
@@ -16,6 +16,8 @@
*
* @brief This is the unified PHP API class to access the
* WikiLexical OmegaWiki database.
+ *
+ * @see OwDBAPILanguage.php for Language functions
*/
class OwDatabaseAPI {
@@ -48,16 +50,29 @@
*/
/**
+ * Returns a SQL query string for fetching language names in a given
language.
+ * @param $lang_code the language in which to retrieve the language
names
+ * @param $lang_subset an array in the form ( 85, 89, ...) that
restricts the language_id that are returned
+ * this array can be generated with
ViewInformation->getFilterLanguageList() according to user preferences
+ *
+ * @see WLDLanguage::getSQLForNames
+ */
+ function getSQLForLanguageNames( $lang_code, $lang_subset = array() ) {
+ $api = new OwDatabaseAPI;
+ $api->settings( 'language' );
+ return $api->Language->getSQLForNames( $lang_code, $lang_subset
= array() );
+ }
+
+ /**
* @brief returns the User Language Id
*
* @return language id
* @return if not exist, null
- *
- * @todo transfer function to language api class.
*/
public static function getUserLanguageId() {
global $wgLang;
- if ( !$userLanguageId = getLanguageIdForCode(
$wgLang->getCode() ) ) {
+ $userLanguageId = getLanguageIdForCode( $wgLang->getCode() );
+ if ( !$userLanguageId ) {
$code = OwDatabaseAPI::getLanguageCodeForIso639_3(
$wgLang->getCode() );
$userLanguageId = getLanguageIdForCode( $code );
}
@@ -65,48 +80,57 @@
}
/**
- * @brief returns the User Language Id
+ * @brief returns the User Language Code
*
- * @return language id
+ * @return language (wikimedia) code
* @return if not exist, null
*
- * @todo transfer function to language api class.
+ * @todo refactor this in the future with getUserLanguageCode, as
suggested
+ * by Kip. ~he
*/
public static function getUserLanguage() {
global $wgLang;
- if ( !$userLanguageId = getLanguageIdForCode(
$wgLang->getCode() ) ) {
+ if ( !getLanguageIdForCode( $wgLang->getCode() ) ) {
$userLanguage =
OwDatabaseAPI::getLanguageCodeForIso639_3( $wgLang->getCode() );
} else {
$userLanguage = $wgLang->getCode();
}
-
return $userLanguage;
}
/**
* @param $purge purge cache
- * @return array of language names for the user's language preference
+ * @param $code the language code
+ *
+ * @return an array containing all language names translated into the
language
+ * indicated by $code ( if it exists ), with a fallback in English
where the language
+ * names aren't present in that language.
+ * @return In case $code is not given, an array of language names for
the
+ * user's language preference is given, with a fallback in English
where the language
+ * names aren't present in that language.
+ *
+ * @see WLDLanguage::getNames
+ * @todo Should we change the name to getLanguageNames instead? ~he
*/
- static function getOwLanguageNames( $purge = false ) {
+ static function getOwLanguageNames( $purge = false, $code = null ) {
static $owLanguageNames = null;
if ( is_null( $owLanguageNames ) && !$purge ) {
- $userLanguage = owDatabaseAPI::getUserLanguage();
- $owLanguageNames = getLangNames( $userLanguage );
+ // if code is not given, get user Language.
+ if ( !$code ) {
+ $code = owDatabaseAPI::getUserLanguage();
+ }
+ $api = new OwDatabaseAPI;
+ $api->settings( 'language' );
+ return $api->Language->getNames( $code );
}
return $owLanguageNames;
}
- /* @return Return an array containing all language names translated
into the language
- * indicated by $code, with fallbacks in English where the
language names
- * aren't present in that language.
- * @see WLDLanguage::getNames
+ /**
+ * @param iso639_3 int OmegaWiki's improvised iso
+ * @return the wikimedia code corresponding to the iso639_3 $code
+ * @see OwDatabaseAPI::getCodeForIso639_3
*/
- static function getLangNames( $code ) {
- $api = new OwDatabaseAPI;
- $api->settings( 'language' );
- return $api->Language->getNames( $code );
- }
-
static function getLanguageCodeForIso639_3( $iso639_3 ) {
$api = new OwDatabaseAPI;
$api->settings( 'language' );
@@ -206,13 +230,16 @@
* @param dc opt'l str The WikiLexicalData dataset
*/
protected function settings( $class, $dc = null ) {
- $this->getDc();
+ $this->getDc( $dc );
- if ( $class == 'attributes' ) { $this->Attributes = new
Attributes; }
- if ( $class == 'syntrans' ) { $this->Syntrans = new Syntrans; }
- if ( $class == 'definedMeaning' ) { $this->DefinedMeaning = new
DefinedMeanings; }
- if ( $class == 'transaction' ) { $this->Transaction = new
Transactions; }
- if ( $class == 'language' ) { $this->Language = new
WLDLanguage; }
+ switch( $class ) {
+ case 'attributes': $this->Attributes = new Attributes;
break;
+ case 'definedMeaning': $this->DefinedMeaning = new
DefinedMeanings; break;
+ case 'language': $this->Language = new WLDLanguage;
break;
+ case 'syntrans': $this->Syntrans = new Syntrans; break;
+ case 'transaction': $this->Transaction = new
Transactions; break;
+ }
+
}
/**
diff --git a/OmegaWiki/WikiDataAPI.php b/OmegaWiki/WikiDataAPI.php
index f360854..74aa70d 100644
--- a/OmegaWiki/WikiDataAPI.php
+++ b/OmegaWiki/WikiDataAPI.php
@@ -2593,8 +2593,7 @@
* @param $definedMeaningId
*/
function definedMeaningExpression( $definedMeaningId ) {
- $code = OwDatabaseAPI::getUserLanguage();
- $userLanguageId = getLanguageIdForCode( $code);
+ $userLanguageId = OwDatabaseAPI::getUserLanguageId();
$result = '';
diff --git a/OmegaWiki/languages.php b/OmegaWiki/languages.php
index 2301905..50e81e2 100644
--- a/OmegaWiki/languages.php
+++ b/OmegaWiki/languages.php
@@ -13,17 +13,22 @@
* aren't present in that language.
* @see use OwDatabaseAPI::getOwLanguageNames instead
*/
- function getNames( $code ) {
+ static function getNames( $code ) {
$dbr = wfGetDB( DB_SLAVE );
$names = array();
- $sql = getSQLForLanguageNames( $code );
- $lang_res = $dbr->query( $sql ); // function
getSQLForLanguageNames creates SQL with MySQL prefix
+ $sql = WLDLanguage::getSQLForNames( $code );
+ $lang_res = $dbr->query( $sql ); // function getSQLForNames
creates SQL with MySQL prefix
while ( $lang_row = $dbr->fetchObject( $lang_res ) )
$names[$lang_row->row_id] = $lang_row->language_name;
return $names;
}
- function getCodeForIso639_3( $iso639_3 ) {
+ /**
+ * @param iso639_3 int OmegaWiki's improvised iso
+ * @return the wikimedia code corresponding to the iso639_3 $code
+ * @see use OwDatabaseAPI::getLanguageCodeForIso639_3 instead
+ */
+ static function getCodeForIso639_3( $iso639_3 ) {
$dbr = wfGetDB( DB_SLAVE );
$wikimediaKey = $dbr->selectField(
'language',
@@ -40,6 +45,60 @@
return null;
}
+ /**
+ * Returns a SQL query string for fetching language names in a given
language.
+ * @param $lang_code the language in which to retrieve the language
names
+ * @param $lang_subset an array in the form ( 85, 89, ...) that
restricts the language_id that are returned
+ * this array can be generated with
ViewInformation->getFilterLanguageList() according to user preferences
+ **/
+ static function getSQLForNames( $lang_code, $lang_subset = array() ) {
+ /* Use a simpler query if the user's language is English. */
+ /* getLanguageIdForCode( 'en' ) = 85 */
+ $dbr = wfGetDB( DB_SLAVE );
+ $lang_id = getLanguageIdForCode( $lang_code );
+
+ if ( $lang_code == 'en' || is_null( $lang_id ) ) {
+ $cond = array( 'name_language_id' =>
WLD_ENGLISH_LANG_ID );
+ if ( ! empty( $lang_subset ) ) {
+ $cond['language_id'] = $lang_subset;
+ }
+ $sqlQuery = $dbr->selectSQLText(
+ 'language_names',
+ array( 'row_id' => 'language_id',
'language_name' ),
+ $cond,
+ __METHOD__
+ );
+
+ } else {
+ /* Fall back on English in cases where a language name
is not present in the
+ user's preferred language. */
+ $cond = array( 'eng.name_language_id' =>
WLD_ENGLISH_LANG_ID );
+
+ if ( ! empty( $lang_subset ) ) {
+ $cond['eng.language_id'] = $lang_subset;
+ }
+
+ $sqlQuery = $dbr->selectSQLText(
+ array( 'eng' => 'language_names', 'ln2' =>
'language_names' ),
+ array( /* fields to select */
+ 'row_id' => 'eng.language_id',
+ 'language_name' =>
'COALESCE(ln2.language_name,eng.language_name)' ),
+ $cond,
+ __METHOD__,
+ array(),
+ array( /* JOIN */
+ 'ln2' => array( 'LEFT JOIN', array(
+ 'eng.language_id =
ln2.language_id',
+ 'ln2.name_language_id' =>
$lang_id
+ )
+ )
+ )
+ );
+ }
+
+ return $sqlQuery;
+ }
+
}
/**
@@ -48,8 +107,8 @@
* @todo for deprecation, use OwDatabaseAPI::getOwLanguageNames instead
**/
function getOwLanguageNames( $purge = false ) {
- require_once( 'OmegaWikiDatabaseAPI.php' );
- return OwDatabaseAPI::getOwLanguageNames( $purge );
+ require_once( 'OmegaWikiDatabaseAPI.php' );
+ return OwDatabaseAPI::getOwLanguageNames( $purge );
}
/* @return Return an array containing all language names translated into the
language
@@ -59,7 +118,7 @@
*/
function getLangNames( $code ) {
require_once( 'OmegaWikiDatabaseAPI.php' );
- return OwDatabaseAPI::getLangNames( $code );
+ return OwDatabaseAPI::getOwLanguageNames( null, $code );
}
function getLanguageIdForCode( $code ) {
@@ -164,58 +223,17 @@
}
-/**
+/* @return Return an array containing all language names translated into the
language
* Returns a SQL query string for fetching language names in a given language.
* @param $lang_code the language in which to retrieve the language names
* @param $lang_subset an array in the form ( 85, 89, ...) that restricts the
language_id that are returned
* this array can be generated with ViewInformation->getFilterLanguageList()
according to user preferences
- **/
+ *
+ * @todo for deprecation, use OwDatabaseAPI::getSQLForLanguageNames instead
+ */
function getSQLForLanguageNames( $lang_code, $lang_subset = array() ) {
- /* Use a simpler query if the user's language is English. */
- /* getLanguageIdForCode( 'en' ) = 85 */
- $dbr = wfGetDB( DB_SLAVE );
- $lang_id = getLanguageIdForCode( $lang_code );
-
- if ( $lang_code == 'en' || is_null( $lang_id ) ) {
- $cond = array( 'name_language_id' => WLD_ENGLISH_LANG_ID );
- if ( ! empty( $lang_subset ) ) {
- $cond['language_id'] = $lang_subset;
- }
- $sqlQuery = $dbr->selectSQLText(
- 'language_names',
- array( 'row_id' => 'language_id', 'language_name' ),
- $cond,
- __METHOD__
- );
-
- } else {
- /* Fall back on English in cases where a language name is not
present in the
- user's preferred language. */
- $cond = array( 'eng.name_language_id' => WLD_ENGLISH_LANG_ID );
-
- if ( ! empty( $lang_subset ) ) {
- $cond['eng.language_id'] = $lang_subset;
- }
-
- $sqlQuery = $dbr->selectSQLText(
- array( 'eng' => 'language_names', 'ln2' =>
'language_names' ),
- array( /* fields to select */
- 'row_id' => 'eng.language_id',
- 'language_name' =>
'COALESCE(ln2.language_name,eng.language_name)' ),
- $cond,
- __METHOD__,
- array(),
- array( /* JOIN */
- 'ln2' => array( 'LEFT JOIN', array(
- 'eng.language_id = ln2.language_id',
- 'ln2.name_language_id' => $lang_id
- )
- )
- )
- );
- }
-
- return $sqlQuery;
+ require_once( 'OmegaWikiDatabaseAPI.php' );
+ return OwDatabaseAPI::getSQLForLanguageNames( $lang_code, $lang_subset
= array() );
}
function getLanguageIdLanguageNameFromIds( $languageId, $nameLanguageId ) {
--
To view, visit https://gerrit.wikimedia.org/r/134299
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I8a57dbad7bdf3bb582e22813f7bfb0dcbc9bd284
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/WikiLexicalData
Gerrit-Branch: master
Gerrit-Owner: Hiong3-eng5 <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits