r35894 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r35893‎ | r35894 | r35895 >
Date:23:36, 4 June 2008
Author:kim
Status:old
Tags:
Comment:
Added language selection at login.
(Note that this changes the database schema!)
Modified paths:
  • /trunk/extensions/Wikidata/util/voctrain/controller.php (modified) (history)
  • /trunk/extensions/Wikidata/util/voctrain/i18n/language.php (modified) (history)
  • /trunk/extensions/Wikidata/util/voctrain/i18n/messages/Messages_Default.php (modified) (history)
  • /trunk/extensions/Wikidata/util/voctrain/i18n/messages/Messages_eng.php (modified) (history)
  • /trunk/extensions/Wikidata/util/voctrain/i18n/messages/Messages_nld.php (modified) (history)
  • /trunk/extensions/Wikidata/util/voctrain/model.php (modified) (history)
  • /trunk/extensions/Wikidata/util/voctrain/trainer.sql (modified) (history)
  • /trunk/extensions/Wikidata/util/voctrain/view.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Wikidata/util/voctrain/i18n/messages/Messages_Default.php
@@ -2,6 +2,6 @@
33
44 $fallback=false;
55
6 -require_once("Messages_eng.php");
 6+include("Messages_eng.php");
77
88 ?>
Index: trunk/extensions/Wikidata/util/voctrain/i18n/messages/Messages_eng.php
@@ -54,6 +54,14 @@
5555 "Omegawiki"=>"Omegawiki",
5656 "Exercise complete"=>"Exercise complete",
5757 "Exercise terminated"=>"Exercise terminated",
58 - "Start a new exercise"=>"Start a new exercise"
 58+ "Start a new exercise"=>"Start a new exercise",
 59+ "User name"=>"User name",
 60+ "Password"=>"Password",
 61+ "Login"=>"Login",
 62+ "Create new user"=>"Create new user",
 63+ "Switch language"=>"Switch language",
 64+ "Language"=>"Language",
 65+ "Log in"=>"Log in",
 66+ "Omegawiki vocabulary trainer"=>"Omegawiki vocabulary trainer"
5967 );
6068 ?>
Index: trunk/extensions/Wikidata/util/voctrain/i18n/messages/Messages_nld.php
@@ -21,9 +21,9 @@
2222 "Set up your exercise"=>"Stel uw oefening op",
2323 "Number of questions"=>"Aantal vragen",
2424 "Languages"=>"Talen",
25 - "Please specify the languages you want to test in"=>"Geef aub de talen op waarin je wilt oefen",
 25+ "Please specify the languages you want to test in"=>"Geef aub de talen op waarin u wilt oefen",
2626 "(eg, eng for English, deu for Deutch (German))."=>"(bijv, eng voor English (Engels), deu voor Deutch (Duits)).",
27 - "Depending on your test set, some combinations might work better than others."=>"Afhankelijk van je testset, werken sommige combinaties beter dan anderen.",
 27+ "Depending on your test set, some combinations might work better than others."=>"Afhankelijk van uw testset, werken sommige combinaties beter dan anderen.",
2828 "Questions"=>"Vragen",
2929 "Answers"=>"Antwoorden",
3030 "start exercise"=>"start oefening",
@@ -32,7 +32,7 @@
3333 "There are %questions_remaining questions remaining, out of a total of %questions_total."=>
3434 "Er zikn nog %questions_remaining over, uit een totaal van %questions_total.",
3535 "Definition"=>"Definitie",
36 - "Dictionary definition to help you"=>"Dictionary definitie om je te helpen",
 36+ "Dictionary definition to help you"=>"Woordenboek definitie om u te helpen",
3737 "Word"=>"Woord",
3838 "Please type your answer here"=>"Typ alstublieft hier uw antwoord.",
3939 "submit answer"=>"antwoord submitten",
@@ -52,6 +52,13 @@
5353 "Omegawiki"=>"Omegawiki",
5454 "Exercise complete"=>"Oefening voltooid",
5555 "Exercise terminated"=>"Oefening afgebroken",
56 - "Start a new exercise"=>"Start een nieuwe oefening"
 56+ "Start a new exercise"=>"Start een nieuwe oefening",
 57+ "User name"=>"Gebruikersnaam",
 58+ "Password"=>"Wachtwoord",
 59+ "Login"=>"Inloggen",
 60+ "Create new user"=>"Nieuwe gebruiker aanmaken",
 61+ "Switch language"=>"Wijzig taal",
 62+ "Language"=>"Taal"
 63+
5764 );
5865 ?>
Index: trunk/extensions/Wikidata/util/voctrain/i18n/language.php
@@ -10,25 +10,28 @@
1111 class Language {
1212
1313 private $code; #language code, mostly for debugging purposes
14 - private $messages;
15 - private $fallback=false;
 14+ private $messages; # assoc array of translations
 15+ private $fallback=false; #a different Language object to fall back to
 16+ # if we can't find a particular entry in $messages
1617
17 - public function __construct($code=null) {
 18+ public function __construct($code="Default") {
 19+ if (!$code)
 20+ $code="Default";
1821 $this->code=$code;
19 - if ($code)
20 - $this->loadMessages($code);
 22+ $this->loadMessages($code);
2123
 24+
2225 }
2326
2427 public function loadMessages($code="Default") {
25 - $messages="Messages_$code.php";
26 - $path=dirname(__FILE__)."/messages/$messages";
 28+ $messages_filename="Messages_$code.php";
 29+ $path=dirname(__FILE__)."/messages/$messages_filename";
2730 if (file_exists($path)) {
2831 include($path);
2932 if (isset($messages)) {
3033 $this->messages=$messages; #messages is from the included file
3134 } else {
32 - throw new LocalisationException("messages file format problem, there's no $messages");
 35+ throw new LocalisationException("messages file format problem, there's no messages in $path");
3336 }
3437
3538 if (isset($fallback)) {
@@ -58,13 +61,11 @@
5962 $languageNames=Language::getAllLanguageNames();
6063
6164 $messageFiles = glob( dirname(__FILE__)."/messages/Messages_*.php" );
62 - var_dump($messageFiles);
6365 $names = array();
6466 foreach ( $messageFiles as $file ) {
6567 $m = array();
6668 if( preg_match( '/Messages_([a-z_]+)\.php$/', $file, $m ) ) {
6769 $code = str_replace( '_', '-', strtolower( $m[1] ) );
68 - var_dump($code);
6970 if ( isset( $languageNames[$code] ) ) {
7071 $names[$code] = $languageNames[$code];
7172 }
@@ -81,7 +82,8 @@
8283 }
8384 }
8485
85 - /** translate the phrase, but doesn't do any substitutions. use printf,sprintf, or vsprintf for that */
 86+ /** translate the phrase, but doesn't do any substitutions.
 87+ * Use printf,sprintf, or vsprintf etc... for subsitutions */
8688 public function translate($phrase) {
8789 if ($this->translation_exists($phrase)) {
8890 return $this->messages[$phrase];
@@ -94,6 +96,7 @@
9597
9698
9799 # == Diverse sprintf-ish functions
 100+ # (see also: php documentation for non-i18nified versions)
98101
99102 /** i18nsprint is a simpler way to go about things, will do i18n replacement
100103 on antyhing enclosed in <| |>, any %signs in these substrings
@@ -111,6 +114,7 @@
112115 }
113116
114117
 118+ # internationalized printf
115119 public function printf($phrase) {
116120 $args=func_get_args();
117121 $str=$this->vsprintf($phrase, $args);
@@ -129,10 +133,10 @@
130134 return vsprintf($this->translate($phrase),$array);
131135 }
132136
133 - /** assoc variant on vsprintf,
 137+ /** assoc variant of vsprintf,
134138 * (modified from sprintf2 by "matt", 10-Mar-2008 06:13,
135139 * http://nl2.php.net/manual/en/function.sprintf.php )
136 - * original Apparently Copyright © 2001-2008 The PHP Group, copied here
 140+ * original apparently Copyright © 2001-2008 The PHP Group, copied here
137141 * on condition that copyright notice is retained. )
138142 */
139143 function vsprintf2($phrase='', $vars=array(), $char='%') {
Index: trunk/extensions/Wikidata/util/voctrain/model.php
@@ -45,6 +45,24 @@
4646 $empty=DBTools::doQuery("UPDATE exercises SET completion=100 WHERE id='$id'");
4747 }
4848
 49+
 50+ public function setUserLanguage($username, $language) {
 51+ global $mysql_info;
 52+ DBTools::connect($mysql_info);
 53+
 54+ $language=mysql_real_escape_string($language);
 55+ $empty=DBTools::doQuery("UPDATE auth SET uiLanguage=\"$language\"");
 56+ }
 57+
 58+ public function getUserLanguage($username) {
 59+ global $mysql_info;
 60+ DBTools::connect($mysql_info);
 61+
 62+ $username=mysql_real_escape_string($username);
 63+ $row=DBTools::doQuery("SELECT uiLanguage from auth where username=\"$username\"");
 64+ return $row["uiLanguage"];
 65+ }
 66+
4967 /** retrieve the exercice we're using for this session */
5068 public function getExercise($username) {
5169
Index: trunk/extensions/Wikidata/util/voctrain/view.php
@@ -15,25 +15,59 @@
1616 }
1717 # I shouldn't be dealing with $_REQUEST here neither... but this function
1818 # breaks all the other rules anyways :-P
 19+
 20+ $language=new Language('Default');
1921
20 - echo "<h1>Log In. Omegawiki vocabulary trainer</h1>";
 22+ $language->i18nprint("<h1><|Log in|>. <|Omegawiki vocabulary trainer|></h1>");
2123 if (isset($_REQUEST["defaultCollection"])) {
2224 $defaultCollection=(int) $_REQUEST["defaultCollection"];
2325 echo "<form method=\"post\" action=\"trainer.php?defaultCollection=$defaultCollection\">";
2426 } else {
2527 echo "<form method=\"post\" action=\"trainer.php\">";
2628 }
27 - echo '<fieldset class="settings">';
28 - echo"<div class='datarow'><label>User name: </label><input type=\"text\" name=\"username\" /></div><br/>";
29 - echo "<div class='datarow'><label>Password: </label><input type=\"password\" name=\"password\" /></div><br/>";
30 - echo '</fieldset>';
31 - echo '<fieldset class="settings">';
32 - echo '<div class="datarow"><input type="submit" value="Login"/> <input type="submit" value="Create new user" name="new_user"></div>';
33 - echo '</fieldset>';
34 - echo "</form>";
 29+ $language->i18nprint('
 30+ <fieldset class="settings">
 31+ <div class="datarow"><label><|User name|>: </label><input type="text" name="username" /></div><br/>
 32+ <div class="datarow"><label><|Password|>: </label><input type="password" name="password" /></div><br/>
 33+ <div class="datarow"><label><|Language|>: </label>'._langSelect().'</div><br/>
 34+ </fieldset>
 35+ <fieldset class="settings">
 36+ <div class="datarow">
 37+ <input type="submit" value="<|Login|>"/>
 38+ <input type="submit" value="<|Create new user|>" name="new_user">
 39+ <!--<input type="submit" value="<|Switch language|>" name="switch_language">-->
 40+ </div>
 41+ </fieldset>
 42+ </form>');
3543 }
3644
 45+function _langSelect($default="eng") {
 46+ $names=Language::getI18NLanguageNames();
3747
 48+ $select="<select name='userLanguage'>\n";
 49+ foreach ($names as $iso639_3=>$name){
 50+ $select .= "
 51+ <option
 52+ value='$iso639_3' ";
 53+
 54+ if ($iso639_3 == $default) { # check-mark default language
 55+ $select.= "
 56+ selected";
 57+ }
 58+
 59+ $select.= "
 60+ >
 61+ ".$name."
 62+ </option>
 63+ ";
 64+ }
 65+ $select.= "</select>\n";
 66+ return $select;
 67+
 68+}
 69+
 70+
 71+
3872 /** ~MVC: Generate html for user interface */
3973 class View {
4074
@@ -41,9 +75,12 @@
4276 public $language;
4377
4478 public function __construct($language_code="Default") {
45 - $this->language=new language($language_code);
 79+ $this->setLanguage_byCode($language_code);
4680 }
4781
 82+ public function setLanguage_byCode($code) {
 83+ $this->language=new Language($code);
 84+ }
4885 /** print everyones favorite friendly message! */
4986 public function hello() {
5087 $this->language->i18nprint("<h1><|Hello World|></h1>");
@@ -128,7 +165,6 @@
129166 (".$collection->count.")
130167 </option>
131168 ";
132 - flush();
133169 }
134170 $select.= "</select>\n";
135171 return $select;
Index: trunk/extensions/Wikidata/util/voctrain/trainer.sql
@@ -7,6 +7,7 @@
88 CREATE TABLE `auth` (
99 username VARCHAR(50) default '' NOT NULL,
1010 password VARCHAR(32) default '' NOT NULL,
 11+ `uiLanguage` CHAR(3) ,
1112 PRIMARY KEY (username),
1213 KEY (password)
1314 ) ENGINE=INNODB;
Index: trunk/extensions/Wikidata/util/voctrain/controller.php
@@ -37,8 +37,10 @@
3838
3939 $this->login();
4040 $logged_in=$this->auth->checkAuth();
 41+ $this->setLanguage();
4142 if ($_REQUEST["action"]=="logout")
4243 $logged_in=false;
 44+
4345 $this->view->header($logged_in);
4446 $logged_in=$this->auth->checkAuth();
4547 if ($logged_in) {
@@ -71,6 +73,13 @@
7274 $this->run_exercise(true);
7375 }
7476
 77+ /** sets the ui language */
 78+ public function setLanguage() {
 79+ $username=$this->auth->getUsername();
 80+ $userLanguage=$this->model->getUserLanguage($username);
 81+ $this->view->setLanguage_byCode($userLanguage);
 82+ }
 83+
7584 /**create a new exercise using $this->view->exercise_setup();
7685 * This function sort of grew over time. TODO Cleanup. */
7786 public function create_exercise() {
@@ -122,7 +131,6 @@
123132 public function run_exercise($continue=false) {
124133 # obtain an exercise
125134 $userName=$this->auth->getUsername();
126 -
127135 $exercise=$this->model->getExercise($userName);
128136 if ($exercise===null) {
129137 $exercise=$this->create_exercise();
@@ -216,9 +224,17 @@
217225 $auth=new Auth("DB", $options, "_displayLogin");
218226 $username=$_REQUEST["username"];
219227 $password=$_REQUEST["password"];
 228+ #to be implemented
 229+ #if ($username="") {
 230+ # $this->view->provideUsername();
 231+ # $this->view->footer();
 232+ # exit;
 233+ #}
220234 $success=$auth->addUser($username, $password);
221235 if ($success===true) {
222236 $this->auth->setAuth($username);
 237+ $this->model->setUserLanguage($username,$_REQUEST["userLanguage"]);
 238+ $this->setLanguage();
223239 $this->view->header(true);
224240 $this->view->userAdded($username);
225241 $this->view->footer();

Status & tagging log