Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/admin/tool/langimport/index.php
Go to the documentation of this file.
00001 <?php
00002 // This file is part of Moodle - http://moodle.org/
00003 //
00004 // Moodle is free software: you can redistribute it and/or modify
00005 // it under the terms of the GNU General Public License as published by
00006 // the Free Software Foundation, either version 3 of the License, or
00007 // (at your option) any later version.
00008 //
00009 // Moodle is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012 // GNU General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00016 
00031 require(dirname(dirname(dirname(dirname(__FILE__)))) . '/config.php');
00032 require_once($CFG->libdir.'/adminlib.php');
00033 require_once($CFG->libdir.'/filelib.php');
00034 require_once($CFG->libdir.'/componentlib.class.php');
00035 
00036 admin_externalpage_setup('toollangimport');
00037 
00038 if (empty($CFG->langotherroot)) {
00039     throw new moodle_exception('missingcfglangotherroot', 'tool_langimport');
00040 }
00041 
00042 $mode          = optional_param('mode', 0, PARAM_INT);              // action
00043 $pack          = optional_param_array('pack', array(), PARAM_SAFEDIR);    // pack to install
00044 $uninstalllang = optional_param('uninstalllang', '', PARAM_LANG);   // installed pack to uninstall
00045 $confirm       = optional_param('confirm', 0, PARAM_BOOL);          // uninstallation confirmation
00046 $purgecaches   = optional_param('purgecaches', false, PARAM_BOOL);  // explicit caches reset
00047 
00048 if ($purgecaches) {
00049     require_sesskey();
00050     get_string_manager()->reset_caches();
00051     redirect($PAGE->url);
00052 }
00053 
00054 if (!empty($CFG->skiplangupgrade)) {
00055     echo $OUTPUT->header();
00056     echo $OUTPUT->box(get_string('langimportdisabled', 'tool_langimport'));
00057     echo $OUTPUT->single_button(new moodle_url($PAGE->url, array('purgecaches' => 1)), get_string('purgestringcaches', 'tool_langimport'));
00058     echo $OUTPUT->footer();
00059     die;
00060 }
00061 
00062 define('INSTALLATION_OF_SELECTED_LANG', 2);
00063 define('DELETION_OF_SELECTED_LANG', 4);
00064 define('UPDATE_ALL_LANG', 5);
00065 
00066 get_string_manager()->reset_caches();
00067 
00068 $notice_ok    = array();
00069 $notice_error = array();
00070 
00071 if (($mode == INSTALLATION_OF_SELECTED_LANG) and confirm_sesskey() and !empty($pack)) {
00072     set_time_limit(0);
00073     make_temp_directory('');
00074     make_upload_directory('lang');
00075 
00076     $installer = new lang_installer($pack);
00077     $results = $installer->run();
00078     foreach ($results as $langcode => $langstatus) {
00079         switch ($langstatus) {
00080         case lang_installer::RESULT_DOWNLOADERROR:
00081             $a       = new stdClass();
00082             $a->url  = $installer->lang_pack_url($langcode);
00083             $a->dest = $CFG->dataroot.'/lang';
00084             print_error('remotedownloaderror', 'error', 'index.php', $a);
00085             break;
00086         case lang_installer::RESULT_INSTALLED:
00087             $notice_ok[] = get_string('langpackinstalled', 'tool_langimport', $langcode);
00088             break;
00089         case lang_installer::RESULT_UPTODATE:
00090             $notice_ok[] = get_string('langpackuptodate', 'tool_langimport', $langcode);
00091             break;
00092         }
00093     }
00094 }
00095 
00096 if ($mode == DELETION_OF_SELECTED_LANG and !empty($uninstalllang)) {
00097     if ($uninstalllang == 'en') {
00098         $notice_error[] = 'English language pack can not be uninstalled';
00099 
00100     } else if (!$confirm and confirm_sesskey()) {
00101         echo $OUTPUT->header();
00102         echo $OUTPUT->confirm(get_string('uninstallconfirm', 'tool_langimport', $uninstalllang),
00103                      'index.php?mode='.DELETION_OF_SELECTED_LANG.'&uninstalllang='.$uninstalllang.'&confirm=1',
00104                      'index.php');
00105         echo $OUTPUT->footer();
00106         die;
00107 
00108     } else if (confirm_sesskey()) {
00109         $dest1 = $CFG->dataroot.'/lang/'.$uninstalllang;
00110         $dest2 = $CFG->dirroot.'/lang/'.$uninstalllang;
00111         $rm1 = false;
00112         $rm2 = false;
00113         if (file_exists($dest1)){
00114             $rm1 = remove_dir($dest1);
00115         }
00116         if (file_exists($dest2)){
00117             $rm2 = remove_dir($dest2);
00118         }
00119         if ($rm1 or $rm2) {
00120             $notice_ok[] = get_string('langpackremoved', 'tool_langimport');
00121         } else {    //nothing deleted, possibly due to permission error
00122             $notice_error[] = 'An error has occurred, language pack is not completely uninstalled, please check file permissions';
00123         }
00124     }
00125 }
00126 
00127 if ($mode == UPDATE_ALL_LANG) {
00128     set_time_limit(0);
00129 
00130     $installer = new lang_installer();
00131 
00132     if (!$availablelangs = $installer->get_remote_list_of_languages()) {
00133         print_error('cannotdownloadlanguageupdatelist', 'error');
00134     }
00135     $md5array = array();    // (string)langcode => (string)md5
00136     foreach ($availablelangs as $alang) {
00137         $md5array[$alang[0]] = $alang[1];
00138     }
00139 
00140     // filter out unofficial packs
00141     $currentlangs = array_keys(get_string_manager()->get_list_of_translations(true));
00142     $updateablelangs = array();
00143     foreach ($currentlangs as $clang) {
00144         if (!array_key_exists($clang, $md5array)) {
00145             $notice_ok[] = get_string('langpackupdateskipped', 'tool_langimport', $clang);
00146             continue;
00147         }
00148         $dest1 = $CFG->dataroot.'/lang/'.$clang;
00149         $dest2 = $CFG->dirroot.'/lang/'.$clang;
00150 
00151         if (file_exists($dest1.'/langconfig.php') || file_exists($dest2.'/langconfig.php')){
00152             $updateablelangs[] = $clang;
00153         }
00154     }
00155 
00156     // then filter out packs that have the same md5 key
00157     $neededlangs = array();   // all the packs that needs updating
00158     foreach ($updateablelangs as $ulang) {
00159         if (!is_installed_lang($ulang, $md5array[$ulang])) {
00160             $neededlangs[] = $ulang;
00161         }
00162     }
00163 
00164     make_temp_directory('');
00165     make_upload_directory('lang');
00166 
00167     // clean-up currently installed versions of the packs
00168     foreach ($neededlangs as $packindex => $pack) {
00169         if ($pack == 'en') {
00170             continue;
00171         }
00172 
00173         // delete old directories
00174         $dest1 = $CFG->dataroot.'/lang/'.$pack;
00175         $dest2 = $CFG->dirroot.'/lang/'.$pack;
00176         $rm1 = false;
00177         $rm2 = false;
00178         if (file_exists($dest1)) {
00179             if (!remove_dir($dest1)) {
00180                 $notice_error[] = 'Could not delete old directory '.$dest1.', update of '.$pack.' failed, please check permissions.';
00181                 unset($neededlangs[$packindex]);
00182                 continue;
00183             }
00184         }
00185         if (file_exists($dest2)) {
00186             if (!remove_dir($dest2)) {
00187                 $notice_error[] = 'Could not delete old directory '.$dest2.', update of '.$pack.' failed, please check permissions.';
00188                 unset($neededlangs[$packindex]);
00189                 continue;
00190             }
00191         }
00192     }
00193 
00194     // install all needed language packs
00195     $installer->set_queue($neededlangs);
00196     $results = $installer->run();
00197     $updated = false;    // any packs updated?
00198     foreach ($results as $langcode => $langstatus) {
00199         switch ($langstatus) {
00200         case lang_installer::RESULT_DOWNLOADERROR:
00201             $a       = new stdClass();
00202             $a->url  = $installer->lang_pack_url($langcode);
00203             $a->dest = $CFG->dataroot.'/lang';
00204             print_error('remotedownloaderror', 'error', 'index.php', $a);
00205             break;
00206         case lang_installer::RESULT_INSTALLED:
00207             $updated = true;
00208             $notice_ok[] = get_string('langpackinstalled', 'tool_langimport', $langcode);
00209             break;
00210         case lang_installer::RESULT_UPTODATE:
00211             $notice_ok[] = get_string('langpackuptodate', 'tool_langimport', $langcode);
00212             break;
00213         }
00214     }
00215 
00216     if ($updated) {
00217         $notice_ok[] = get_string('langupdatecomplete', 'tool_langimport');
00218     } else {
00219         $notice_ok[] = get_string('nolangupdateneeded', 'tool_langimport');
00220     }
00221 
00222     unset($installer);
00223 }
00224 get_string_manager()->reset_caches();
00225 
00226 echo $OUTPUT->header();
00227 echo $OUTPUT->heading(get_string('langimport', 'tool_langimport'));
00228 
00229 $installedlangs = get_string_manager()->get_list_of_translations(true);
00230 
00231 $missingparents = array();
00232 foreach ($installedlangs as $installedlang => $unused) {
00233     $parent = get_parent_language($installedlang);
00234     if (empty($parent)) {
00235         continue;
00236     }
00237     if (!isset($installedlangs[$parent])) {
00238         $missingparents[$installedlang] = $parent;
00239     }
00240 }
00241 
00242 $installer = new lang_installer();
00243 
00244 if ($availablelangs = $installer->get_remote_list_of_languages()) {
00245     $remote = true;
00246 } else {
00247     $remote = false;
00248     $availablelangs = array();
00249     echo $OUTPUT->box_start();
00250     print_string('remotelangnotavailable', 'tool_langimport', $CFG->dataroot.'/lang/');
00251     echo $OUTPUT->box_end();
00252 }
00253 
00254 if ($notice_ok) {
00255     $info = implode('<br />', $notice_ok);
00256     echo $OUTPUT->notification($info, 'notifysuccess');
00257 }
00258 
00259 if ($notice_error) {
00260     $info = implode('<br />', $notice_error);
00261     echo $OUTPUT->notification($info, 'notifyproblem');
00262 }
00263 
00264 if ($missingparents) {
00265     foreach ($missingparents as $l=>$parent) {
00266         $a = new stdClass();
00267         $a->lang   = $installedlangs[$l];
00268         $a->parent = $parent;
00269         foreach ($availablelangs as $alang) {
00270             if ($alang[0] == $parent) {
00271                 $shortlang = $alang[0];
00272                 $a->parent = $alang[2].' ('.$shortlang.')';
00273             }
00274         }
00275         $info = get_string('missinglangparent', 'tool_langimport', $a);
00276         echo $OUTPUT->notification($info, 'notifyproblem');
00277     }
00278 }
00279 
00280 echo $OUTPUT->box_start();
00281 
00282 echo html_writer::start_tag('table');
00283 echo html_writer::start_tag('tr');
00284 
00285 // list of installed languages
00286 $url = new moodle_url('/admin/tool/langimport/index.php', array('mode' => DELETION_OF_SELECTED_LANG));
00287 echo html_writer::start_tag('td', array('valign' => 'top'));
00288 echo html_writer::start_tag('form', array('id' => 'uninstallform', 'action' => $url->out(), 'method' => 'post'));
00289 echo html_writer::start_tag('fieldset');
00290 echo html_writer::label(get_string('installedlangs', 'tool_langimport'), 'uninstalllang');
00291 echo html_writer::empty_tag('br');
00292 echo html_writer::select($installedlangs, 'uninstalllang', '', false, array('size' => 15));
00293 echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
00294 echo html_writer::empty_tag('br');
00295 echo html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('uninstall', 'tool_langimport')));
00296 echo html_writer::end_tag('fieldset');
00297 echo html_writer::end_tag('form');
00298 if ($remote) {
00299     $url = new moodle_url('/admin/tool/langimport/index.php', array('mode' => UPDATE_ALL_LANG));
00300     echo html_writer::start_tag('form', array('id' => 'updateform', 'action' => $url->out(), 'method' => 'post'));
00301     echo html_writer::tag('fieldset', html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('updatelangs','tool_langimport'))));
00302     echo html_writer::end_tag('form');
00303 }
00304 echo html_writer::end_tag('td');
00305 
00306 // list of available languages
00307 $options = array();
00308 foreach ($availablelangs as $alang) {
00309     if (!empty($alang[0]) and trim($alang[0]) !== 'en' and !is_installed_lang($alang[0], $alang[1])) {
00310         $options[$alang[0]] = $alang[2].' ('.$alang[0].')';
00311     }
00312 }
00313 if (!empty($options)) {
00314     echo html_writer::start_tag('td', array('valign' => 'top'));
00315     $url = new moodle_url('/admin/tool/langimport/index.php', array('mode' => INSTALLATION_OF_SELECTED_LANG));
00316     echo html_writer::start_tag('form', array('id' => 'installform', 'action' => $url->out(), 'method' => 'post'));
00317     echo html_writer::start_tag('fieldset');
00318     echo html_writer::label(get_string('availablelangs','install'), 'pack');
00319     echo html_writer::empty_tag('br');
00320     echo html_writer::select($options, 'pack[]', '', false, array('size' => 15, 'multiple' => 'multiple'));
00321     echo html_writer::empty_tag('input', array('type' => 'hidden', 'name' => 'sesskey', 'value' => sesskey()));
00322     echo html_writer::empty_tag('br');
00323     echo html_writer::empty_tag('input', array('type' => 'submit', 'value' => get_string('install','tool_langimport')));
00324     echo html_writer::end_tag('fieldset');
00325     echo html_writer::end_tag('form');
00326     echo html_writer::end_tag('td');
00327 }
00328 
00329 echo html_writer::end_tag('tr');
00330 echo html_writer::end_tag('table');
00331 echo $OUTPUT->box_end();
00332 echo $OUTPUT->footer();
00333 die();
00334 
00336 // Local functions /////////////////////////////////////////////////////////////
00338 
00346 function is_installed_lang($lang, $md5check) {
00347     global $CFG;
00348     $md5file = $CFG->dataroot.'/lang/'.$lang.'/'.$lang.'.md5';
00349     if (file_exists($md5file)){
00350         return (file_get_contents($md5file) == $md5check);
00351     }
00352     return false;
00353 }
 All Data Structures Namespaces Files Functions Variables Enumerations