[ Index ]

PHP Cross Reference of Moodle 1.9.3 [Build 15-Oct-2008]

title

Body

[close]

/admin/ -> langimport.php (source)

   1  <?php //$Id: langimport.php,v 1.36.2.8 2008/04/02 06:50:20 skodak Exp $
   2  ///This file only manages the installation of 1.6 lang packs.
   3  ///in downloads.moodle.org, they are store in separate directory /lang16
   4  ///in local server, they are stored in $CFG->dataroot/lang
   5  ///This helps to avoid confusion.
   6  
   7      require_once ('../config.php');
   8      require_once($CFG->libdir.'/adminlib.php');
   9      require_once($CFG->libdir.'/filelib.php');
  10      require_once($CFG->libdir.'/componentlib.class.php');
  11  
  12      admin_externalpage_setup('langimport');
  13  
  14      $mode          = optional_param('mode', 0, PARAM_INT);     //phase
  15      $pack          = optional_param('pack', array(), PARAM_FILE);   //pack to install
  16      $displaylang   = $pack;
  17      $uninstalllang = optional_param('uninstalllang', '', PARAM_FILE);
  18      $confirm       = optional_param('confirm', 0, PARAM_BOOL);
  19      $sitelang      = optional_param('sitelangconfig', '', PARAM_FILE);
  20  
  21      define('INSTALLATION_OF_SELECTED_LANG', 2);
  22      define('DELETION_OF_SELECTED_LANG', 4);
  23      define('UPDATE_ALL_LANG', 5);
  24  
  25      $strlang         = get_string('langimport','admin');
  26      $strlanguage     = get_string('language');
  27      $strthislanguage = get_string('thislanguage');
  28      $title           = $strlang;
  29  
  30      //reset and diagnose lang cache permissions
  31      @unlink($CFG->dataroot.'/cache/languages');
  32      if (file_exists($CFG->dataroot.'/cache/languages')) {
  33          error('Language cache can not be deleted, please fix permissions in dataroot/cache/languages!');
  34      }
  35      get_list_of_languages(true); //refresh lang cache
  36  
  37      $notice_ok     = array();
  38      $notice_error = array();
  39  
  40      switch ($mode){
  41  
  42          case INSTALLATION_OF_SELECTED_LANG:    ///installation of selected language pack
  43  
  44              if (confirm_sesskey() and !empty($pack)) {
  45                  set_time_limit(0);
  46                  @mkdir ($CFG->dataroot.'/temp/');    //make it in case it's a fresh install, it might not be there
  47                  @mkdir ($CFG->dataroot.'/lang/');
  48  
  49                  if (is_array($pack)) {
  50                      $packs = $pack;
  51                  } else {
  52                      $packs = array($pack);
  53                  }
  54  
  55                  foreach ($packs as $pack) {
  56                      if ($cd = new component_installer('http://download.moodle.org', 'lang16',
  57                                                          $pack.'.zip', 'languages.md5', 'lang')) {
  58                          $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED)
  59                          switch ($status) {
  60  
  61                              case COMPONENT_ERROR:
  62                                  if ($cd->get_error() == 'remotedownloaderror') {
  63                                      $a = new object();
  64                                      $a->url = 'http://download.moodle.org/lang16/'.$pack.'.zip';
  65                                      $a->dest= $CFG->dataroot.'/lang';
  66                                      print_error($cd->get_error(), 'error', 'langimport.php', $a);
  67                                  } else {
  68                                      print_error($cd->get_error(), 'error', 'langimport.php');
  69                                  }
  70                              break;
  71  
  72                              case COMPONENT_INSTALLED:
  73                                  $notice_ok[] = get_string('langpackinstalled','admin',$pack);
  74                              break;
  75  
  76                              case COMPONENT_UPTODATE:
  77                              break;
  78  
  79                          }
  80                      } else {
  81                          notify('Had an unspecified error with the component installer, sorry.');
  82                      }
  83                  }
  84              }
  85          break;
  86  
  87          case DELETION_OF_SELECTED_LANG:    //delete a directory(ies) containing a lang pack completely
  88  
  89              if ($uninstalllang == 'en_utf8') {
  90                  $notice_error[] = 'en_utf8 can not be uninstalled!';
  91  
  92              } else if (!$confirm && confirm_sesskey()) {
  93                  admin_externalpage_print_header();
  94                  notice_yesno(get_string('uninstallconfirm', 'admin', $uninstalllang),
  95                               'langimport.php?mode='.DELETION_OF_SELECTED_LANG.'&amp;uninstalllang='.$uninstalllang.'&amp;confirm=1&amp;sesskey='.sesskey(),
  96                               'langimport.php');
  97                  print_footer();
  98                  die;
  99  
 100              } else if (confirm_sesskey()) {
 101                  $dest1 = $CFG->dataroot.'/lang/'.$uninstalllang;
 102                  $dest2 = $CFG->dirroot.'/lang/'.$uninstalllang;
 103                  $rm1 = false;
 104                  $rm2 = false;
 105                  if (file_exists($dest1)){
 106                      $rm1 = remove_dir($dest1);
 107                  }
 108                  if (file_exists($dest2)){
 109                      $rm2 = remove_dir($dest2);
 110                  }
 111                  get_list_of_languages(true); //refresh lang cache
 112                  //delete the direcotries
 113                  if ($rm1 or $rm2) {
 114                      $notice_ok[] = get_string('langpackremoved','admin');
 115                  } else {    //nothing deleted, possibly due to permission error
 116                      $notice_error[] = 'An error has occurred, language pack is not completely uninstalled, please check file permissions';
 117                  }
 118              }
 119          break;
 120  
 121          case UPDATE_ALL_LANG:    //1 click update for all updatable language packs
 122              set_time_limit(0);
 123  
 124              //0th pull a list from download.moodle.org,
 125              //key = langname, value = md5
 126              $md5array = array();
 127              $updated = 0;    //any packs updated?
 128              $alllangs = array_keys(get_list_of_languages(false, true)); //get all available langs
 129              $lang16 = array();   //all the Moodle 1.6 unicode lang packs (updated and not updated)
 130              $packs = array();    //all the packs that needs updating
 131  
 132  
 133              if (!$availablelangs = get_remote_list_of_languages()) {
 134                  print_error('cannotdownloadlanguageupdatelist');
 135              }
 136  
 137              //and build an associative array
 138              foreach ($availablelangs as $alang) {
 139                  $md5array[$alang[0]] = $alang[1];
 140              }
 141  
 142              //filtering out non-16 and unofficial packs
 143              foreach ($alllangs as $clang) {
 144                  if (!array_key_exists($clang, $md5array)) {
 145                      $notice_ok[] = get_string('langpackupdateskipped', 'admin', $clang);
 146                      continue;
 147                  }
 148                  $dest1 = $CFG->dataroot.'/lang/'.$clang;
 149                  $dest2 = $CFG->dirroot.'/lang/'.$clang;
 150  
 151                  if (file_exists($dest1.'/langconfig.php') || file_exists($dest2.'/langconfig.php')){
 152                      $lang16[] = $clang;
 153                  }
 154              }
 155  
 156              //then filter out packs that have the same md5 key
 157              foreach ($lang16 as $clang) {
 158                  if (!is_installed_lang($clang, $md5array[$clang])){
 159                      $packs[] = $clang;
 160                  }
 161              }
 162  
 163              @mkdir ($CFG->dataroot.'/temp/');
 164              @mkdir ($CFG->dataroot.'/lang/');
 165              foreach ($packs as $pack){    //for each of the remaining in the list, we
 166                  if ($pack == 'en_utf8') {    // no update for en_utf8
 167                      continue;
 168                  }
 169  
 170                  //1. delete old director(ies)
 171  
 172                  $dest1 = $CFG->dataroot.'/lang/'.$pack;
 173                  $dest2 = $CFG->dirroot.'/lang/'.$pack;
 174                  $rm1 = false;
 175                  $rm2 = false;
 176                  if (file_exists($dest1)) {
 177                      if (!remove_dir($dest1)) {
 178                          $notice_error[] = 'Could not delete old directory '.$dest1.', update of '.$pack.' failed, please check permissions.';
 179                          continue;
 180                      }
 181                  }
 182                  if (file_exists($dest2)) {
 183                      if (!remove_dir($dest2)) {
 184                          $notice_error[] = 'Could not delete old directory '.$dest2.', update of '.$pack.' failed, please check permissions.';
 185                          continue;
 186                      }
 187                  }
 188  
 189                  //2. copy & unzip into new
 190  
 191                  if ($cd = new component_installer('http://download.moodle.org', 'lang16',
 192                                         $pack.'.zip', 'languages.md5', 'lang')) {
 193                  $status = $cd->install(); //returns COMPONENT_(ERROR | UPTODATE | INSTALLED)
 194                  switch ($status) {
 195  
 196                      case COMPONENT_ERROR:
 197                          if ($cd->get_error() == 'remotedownloaderror') {
 198                              $a = new stdClass();
 199                              $a->url = 'http://download.moodle.org/lang16/'.$pack.'.zip';
 200                              $a->dest= $CFG->dataroot.'/lang';
 201                              print_error($cd->get_error(), 'error', "", $a); // not probable
 202                          } else {
 203                              print_error($cd->get_error(), 'error'); // not probable
 204                          }
 205                      break;
 206                      case COMPONENT_UPTODATE:
 207                          //Print error string or whatever you want to do
 208                      break;
 209                      case COMPONENT_INSTALLED:
 210                          $notice_ok[] = get_string('langpackupdated', 'admin', $pack);
 211                          $updated = true;
 212                          //Print/do whatever you want
 213                      break;
 214                      default:
 215                      }
 216                  } else {
 217  
 218                  }
 219              }
 220  
 221              if ($updated) {
 222                  $notice_ok[] = get_string('langupdatecomplete','admin');
 223              } else {
 224                  $notice_ok[] = get_string('nolangupdateneeded','admin');
 225              }
 226  
 227          break;
 228      }    //close of main switch
 229  
 230  
 231      admin_externalpage_print_header();
 232  
 233      $installedlangs = get_list_of_languages(true, true);
 234  
 235      $missingparents = array();
 236      $oldlang = isset($SESSION->lang) ? $SESSION->lang : null; // override current lang
 237  
 238      foreach($installedlangs as $l=>$unused) {
 239          $SESSION->lang = $l;
 240          $parent = get_string('parentlanguage');
 241          if ($parent == 'en_utf8') {
 242              continue;
 243          }
 244          if (strpos($parent, '[[') !== false) {
 245              continue; // no parent
 246          }
 247          if (!isset($installedlangs[$parent])) {
 248              $missingparents[$l] = $parent;
 249          }
 250      }
 251      if (isset($oldlang)) {
 252          $SESSION->lang = $oldlang;
 253      } else {
 254          unset($SESSION->lang);
 255      }
 256  
 257      if ($availablelangs = get_remote_list_of_languages()) {
 258          $remote = 1;
 259      } else {
 260          $remote = 0;    //flag for reading from remote or local
 261          $availablelangs = get_local_list_of_languages();
 262      }
 263  
 264      if (!$remote) {
 265          print_box_start();
 266          print_string('remotelangnotavailable', 'admin', $CFG->dataroot.'/lang/');
 267          print_box_end();
 268      }
 269  
 270      if ($notice_ok) {
 271          $info = implode('<br />', $notice_ok);
 272          notify($info, 'notifysuccess');
 273      }
 274  
 275      if ($notice_error) {
 276          $info = implode('<br />', $notice_error);
 277          notify($info, 'notifyproblem');
 278      }
 279  
 280      if ($missingparents) {
 281          foreach ($missingparents as $l=>$parent) {
 282              $a = new object();
 283              $a->lang   = $installedlangs[$l];
 284              $a->parent = $parent;
 285              foreach ($availablelangs as $alang) {
 286                  if ($alang[0] == $parent) {
 287                      if (substr($alang[0], -5) == '_utf8') {   //Remove the _utf8 suffix from the lang to show
 288                          $shortlang = substr($alang[0], 0, -5);
 289                      } else {
 290                          $shortlang = $alang[0];
 291                      }
 292                      $a->parent = $alang[2].' ('.$shortlang.')';
 293                  }
 294              }
 295              $info = get_string('missinglangparent', 'admin', $a);
 296              notify($info, 'notifyproblem');
 297          }
 298      }
 299  
 300      print_box_start();
 301      echo '<table summary="">';
 302      echo '<tr><td align="center" valign="top">';
 303      echo '<form id="uninstallform" action="langimport.php?mode='.DELETION_OF_SELECTED_LANG.'" method="post">';
 304      echo '<fieldset class="invisiblefieldset">';
 305      echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
 306  
 307      /// display installed langs here
 308  
 309      echo '<label for="uninstalllang">'.get_string('installedlangs','admin')."</label><br />\n";
 310      echo '<select name="uninstalllang" id="uninstalllang" size="15">';
 311      foreach ($installedlangs as $clang =>$ilang){
 312          echo '<option value="'.$clang.'">'.$ilang.'</option>';
 313      }
 314      echo '</select>';
 315      echo '<br /><input type="submit" value="'.get_string('uninstall','admin').'" />';
 316      echo '</fieldset>';
 317      echo '</form>';
 318  
 319      if ($remote) {
 320          echo '<form id="updateform" action="langimport.php?mode='.UPDATE_ALL_LANG.'" method="post">';
 321          echo '<div>';
 322          echo '<br /><input type="submit" value="'.get_string('updatelangs','admin').'" />';
 323          echo '</div>';
 324          echo '</form>';
 325      }
 326  
 327      /// Display option to change site language
 328  
 329      /// display to be installed langs here
 330  
 331      echo '</td><td align="center" valign="top">';
 332      //availabe langs table
 333      $empty = 1;    //something to pring
 334  
 335      /// if this language pack is not already installed, then we allow installation
 336  
 337      echo '<form id="installform" method="post" action="langimport.php?mode='.INSTALLATION_OF_SELECTED_LANG.'">';
 338      echo '<fieldset class="invisiblefieldset">';
 339      echo '<input name="sesskey" type="hidden" value="'.sesskey().'" />';
 340      echo '<label for="pack">'.get_string('availablelangs','admin')."</label><br />\n";
 341      if ($remote) {
 342          echo '<select name="pack[]" id="pack" size="15" multiple="multiple">';
 343      }
 344  
 345      foreach ($availablelangs as $alang) {
 346          if ($alang[0] == '') {
 347              continue;
 348          }
 349          if (trim($alang[0]) != "en_utf8") {
 350              if ($remote) {
 351                  if (substr($alang[0], -5) == '_utf8') {   //Remove the _utf8 suffix from the lang to show
 352                      $shortlang = substr($alang[0], 0, -5);
 353                  } else {
 354                      $shortlang = $alang[0];
 355                  }
 356                  if (!is_installed_lang($alang[0], $alang[1])){    //if not already installed
 357                      echo '<option value="'.$alang[0].'">'.$alang[2].' ('.$shortlang.')</option>';
 358                  }
 359              } else {    //print list in local format, and instruction to install
 360                  echo '<tr><td>'.$alang[2].'</td><td><a href="http://download.moodle.org/lang16/'.$alang[0].'.zip">'.get_string('download','admin').'</a></td></tr>';
 361              }
 362              $empty = 0;
 363          }
 364      }
 365      if ($remote) {
 366          echo '</select>';
 367          echo '<br /><input type="submit" value="'.$THEME->larrow.' '.get_string('install','admin').'" />';
 368      }
 369      echo '</fieldset>';
 370      echo '</form>';
 371  
 372      if ($empty) {
 373          echo '<br />';
 374          print_string('nolanguagetodownload','admin');
 375      }
 376  
 377      //close available langs table
 378      echo '</td></tr></table>';
 379      print_box_end();
 380  
 381      admin_externalpage_print_footer();
 382  
 383      /**
 384       * Returns a list of available language packs from a
 385       * local copy shipped with standard moodle distro
 386       * this is for site that can't download components.
 387       * @return array
 388       */
 389      function get_local_list_of_languages() {
 390          global $CFG;
 391          $source = $CFG->dirroot.'/lib/languages.md5';
 392          $availablelangs = array();
 393          if ($fp = fopen($source, 'r')) {
 394              while(!feof ($fp)) {
 395                  $availablelangs[] = split(',', fgets($fp,1024));
 396              }
 397          }
 398          return $availablelangs;
 399      }
 400  
 401      /**
 402       * checks the md5 of the zip file, grabbed from download.moodle.org,
 403       * against the md5 of the local language file from last update
 404       * @param string $lang
 405       * @param string $md5check
 406       * @return bool
 407       */
 408      function is_installed_lang($lang, $md5check) {
 409          global $CFG;
 410          $md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';
 411          if (file_exists($md5file)){
 412              return (file_get_contents($md5file) == $md5check);
 413          }
 414          return false;
 415      }
 416  
 417      /**
 418       * Returns the latest list of available language packs from
 419       * moodle.org
 420       * @return array or false if can not download
 421       */
 422      function get_remote_list_of_languages() {
 423          $source = 'http://download.moodle.org/lang16/languages.md5';
 424          $availablelangs = array();
 425  
 426          if ($content = download_file_content($source)) {
 427              $alllines = split("\n", $content);
 428              foreach($alllines as $line) {
 429                  if (!empty($line)){
 430                      $availablelangs[] = split(',', $line);
 431                  }
 432              }
 433              return $availablelangs;
 434  
 435          } else {
 436              return false;
 437          }
 438      }
 439  ?>


Generated: Wed Jan 14 11:33:29 2009 Cross-referenced by PHPXref 0.7