[ Index ]

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

title

Body

[close]

/admin/ -> multilangupgrade.php (source)

   1  <?php /// $Id: multilangupgrade.php,v 1.6 2007/10/10 12:19:40 skodak Exp $
   2        /// Search and replace strings throughout all texts in the whole database
   3  
   4  require_once ('../config.php');
   5  require_once($CFG->dirroot.'/course/lib.php');
   6  require_once($CFG->libdir.'/adminlib.php');
   7  
   8  admin_externalpage_setup('multilangupgrade');
   9  
  10  $go = optional_param('go', 0, PARAM_BOOL);
  11  
  12  ###################################################################
  13  admin_externalpage_print_header();
  14  
  15  print_heading(get_string('multilangupgrade', 'admin'));
  16  
  17  $strmultilangupgrade = get_String('multilangupgradeinfo', 'admin');
  18  
  19  if (!$go or !data_submitted() or !confirm_sesskey()) {   /// Print a form
  20      $optionsyes = array('go'=>1, 'sesskey'=>sesskey());
  21      notice_yesno($strmultilangupgrade, 'multilangupgrade.php', 'index.php', $optionsyes, null, 'post', 'get');
  22      admin_externalpage_print_footer();
  23      die;
  24  }
  25  
  26  
  27  if (!$tables = $db->Metatables() ) {    // No tables yet at all.
  28      error("no tables");
  29  }
  30  
  31  print_simple_box_start('center');
  32  
  33  /// Turn off time limits, sometimes upgrades can be slow.
  34  
  35  @set_time_limit(0);
  36  @ob_implicit_flush(true);
  37  while(@ob_end_flush());
  38  
  39  echo '<strong>Progress:</strong>';
  40  $i = 0;
  41  $skiptables = array($CFG->prefix.'config', $CFG->prefix.'user_students', $CFG->prefix.'user_teachers');//, $CFG->prefix.'sessions2');
  42  
  43  foreach ($tables as $table) {
  44      if (($CFG->prefix && strpos($table, $CFG->prefix) !== 0)
  45        or strpos($table, $CFG->prefix.'pma') === 0) { // Not our tables
  46          continue;
  47      }
  48      if (in_array($table, $skiptables)) { // Don't process these
  49          continue;
  50      }
  51      if ($columns = $db->MetaColumns($table, false)) {
  52          if (!array_key_exists('id', $columns) and !array_key_exists('ID', $columns)) {
  53              continue; // moodle tables have id
  54          }
  55          foreach ($columns as $column => $data) {
  56              if (in_array($data->type, array('text','mediumtext','longtext','varchar'))) {  // Text stuff only
  57                  // first find candidate records
  58                  $rs = get_recordset_sql("SELECT id, $column FROM $table WHERE $column LIKE '%</lang>%' OR $column LIKE '%<span lang=%'");
  59                  if ($rs) {
  60                      while (!$rs->EOF) {
  61                          $text = $rs->fields[$column];
  62                          $id   = $rs->fields['id'];
  63  
  64                          if ($i % 600 == 0) {
  65                              echo '<br />';
  66                          }
  67                          if ($i % 10 == 0) {
  68                              echo '.';
  69                          }
  70                          $i++;
  71                          $rs->MoveNext();
  72  
  73                          if (empty($text) or is_numeric($text)) {
  74                              continue; // nothing to do
  75                          }
  76  
  77                          $search = '/(<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)(\s*<(?:lang|span) lang="[a-zA-Z0-9_-]*".*?>.+?<\/(?:lang|span)>)+/is';
  78                          $newtext = preg_replace_callback($search, 'multilangupgrade_impl', $text);
  79  
  80                          if (is_null($newtext)) {
  81                              continue; // regex error
  82                          }
  83  
  84                          if ($newtext != $text) {
  85                              $newtext = addslashes($newtext);
  86                              execute_sql("UPDATE $table SET $column='$newtext' WHERE id=$id", false);
  87                          }
  88                      }
  89                      rs_close($rs);
  90                  }
  91              }
  92          }
  93      }
  94  }
  95  
  96  // set conversion flag - switches to new plugin automatically
  97  set_config('filter_multilang_converted', 1);
  98  
  99  print_simple_box_end();
 100  
 101  /// Rebuild course cache which might be incorrect now
 102  notify('Rebuilding course cache...', 'notifysuccess');
 103  rebuild_course_cache();
 104  notify('...finished', 'notifysuccess');
 105  
 106  print_continue('index.php');
 107  
 108  admin_externalpage_print_footer();
 109  die;
 110  
 111  
 112  function multilangupgrade_impl($langblock) {
 113      $searchtosplit = '/<(?:lang|span) lang="([a-zA-Z0-9_-]*)".*?>(.+?)<\/(?:lang|span)>/is';
 114      preg_match_all($searchtosplit, $langblock[0], $rawlanglist);
 115      $return = '';
 116      foreach ($rawlanglist[1] as $index=>$lang) {
 117          $return .= '<span lang="'.$lang.'" class="multilang">'.$rawlanglist[2][$index].'</span>';
 118      }
 119      return $return;
 120  }
 121  ?>


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