Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403]

   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  
  17  /**
  18   * Search and replace strings throughout all texts in the whole database.
  19   *
  20   * Unfortunately it was a bad idea to use spans for multilang because it
  21   * can not support nesting. Hopefully this will get thrown away soon....
  22   *
  23   * @package    tool
  24   * @subpackage multilangupgrade
  25   * @copyright  2006 Petr Skoda (http://skodak.org)
  26   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  27   */
  28  
  29  define('NO_OUTPUT_BUFFERING', true);
  30  
  31  require('../../../config.php');
  32  require_once($CFG->dirroot.'/course/lib.php');
  33  require_once($CFG->libdir.'/adminlib.php');
  34  
  35  admin_externalpage_setup('toolmultilangupgrade');
  36  
  37  $go = optional_param('go', 0, PARAM_BOOL);
  38  
  39  ###################################################################
  40  echo $OUTPUT->header();
  41  
  42  echo $OUTPUT->heading(get_string('pluginname', 'tool_multilangupgrade'));
  43  
  44  $strmultilangupgrade = get_String('multilangupgradeinfo', 'tool_multilangupgrade');
  45  
  46  if (!$go or !data_submitted() or !confirm_sesskey()) {   /// Print a form
  47      $optionsyes = array('go'=>1, 'sesskey'=>sesskey());
  48      echo $OUTPUT->confirm($strmultilangupgrade, new moodle_url('/admin/tool/multilangupgrade/index.php', $optionsyes), new moodle_url('/admin/'));
  49      echo $OUTPUT->footer();
  50      die;
  51  }
  52  
  53  
  54  if (!$tables = $DB->get_tables() ) {    // No tables yet at all.
  55      throw new \moodle_exception('notables', 'debug');
  56  }
  57  
  58  echo $OUTPUT->box_start();
  59  
  60  /// Turn off time limits, sometimes upgrades can be slow.
  61  
  62  core_php_time_limit::raise();
  63  
  64  echo '<strong>Progress:</strong>';
  65  $i = 0;
  66  $skiptables = array('config', 'block_instances', 'sessions'); // we can not process tables with serialised data here
  67  
  68  foreach ($tables as $table) {
  69      if (strpos($table,'pma') === 0) { // Not our tables
  70          continue;
  71      }
  72      if (in_array($table, $skiptables)) { // Don't process these
  73          continue;
  74      }
  75      $fulltable = $DB->get_prefix().$table;
  76      if ($columns = $DB->get_columns($table)) {
  77          if (!array_key_exists('id', $columns)) {
  78              continue; // moodle tables have id
  79          }
  80          foreach ($columns as $column => $data) {
  81              if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) {  // Text stuff only
  82                  // first find candidate records
  83                  $sql = "SELECT id, $column FROM $fulltable WHERE $column LIKE '%</lang>%' OR $column LIKE '%<span lang=%'";
  84                  $rs = $DB->get_recordset_sql($sql);
  85                  foreach ($rs as $data) {
  86                      $text = $data->$column;
  87                      $id   = $data->id;
  88                      if ($i % 600 == 0) {
  89                          echo '<br />';
  90                      }
  91                      if ($i % 10 == 0) {
  92                          echo '.';
  93                      }
  94                      $i++;
  95  
  96                      if (empty($text) or is_numeric($text)) {
  97                          continue; // nothing to do
  98                      }
  99  
 100                      $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)+/is';
 101                      $newtext = preg_replace_callback($search, 'multilangupgrade_impl', $text);
 102  
 103                      if (is_null($newtext)) {
 104                          continue; // regex error
 105                      }
 106  
 107                      if ($newtext != $text) {
 108                          $DB->execute("UPDATE $fulltable SET $column=? WHERE id=?", array($newtext, $id));
 109                      }
 110                  }
 111                  $rs->close();
 112              }
 113          }
 114      }
 115  }
 116  
 117  // set conversion flag - switches to new plugin automatically
 118  set_config('filter_multilang_converted', 1);
 119  
 120  echo $OUTPUT->box_end();
 121  
 122  /// Rebuild course cache which might be incorrect now
 123  echo $OUTPUT->notification('Rebuilding course cache...', 'notifysuccess');
 124  rebuild_course_cache(0, true);
 125  echo $OUTPUT->notification('...finished', 'notifysuccess');
 126  
 127  echo $OUTPUT->continue_button(new moodle_url('/admin/'));
 128  
 129  echo $OUTPUT->footer();
 130  die;
 131  
 132  
 133  function multilangupgrade_impl($langblock) {
 134      $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is';
 135      preg_match_all($searchtosplit, $langblock[0], $rawlanglist);
 136      $return = '';
 137      foreach ($rawlanglist[1] as $index=>$lang) {
 138          $return .= '<span lang="'.$lang.'" class="multilang">'.$rawlanglist[2][$index].'</span>';
 139      }
 140      return $return;
 141  }
 142