Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 39 and 401]

   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   * @package    tool_replace
  21   * @copyright  1999 onwards Martin Dougiamas (http://dougiamas.com)
  22   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  define('CLI_SCRIPT', true);
  26  
  27  require(__DIR__.'/../../../../config.php');
  28  require_once($CFG->libdir.'/clilib.php');
  29  require_once($CFG->libdir.'/adminlib.php');
  30  
  31  $help =
  32      "Search and replace text throughout the whole database.
  33  
  34  Options:
  35  --search=STRING       String to search for.
  36  --replace=STRING      String to replace with.
  37  --skiptables=STRING   Skip these tables (comma separated list of tables).
  38  --shorten             Shorten result if necessary.
  39  --non-interactive     Perform the replacement without confirming.
  40  -h, --help            Print out this help.
  41  
  42  Example:
  43  \$ sudo -u www-data /usr/bin/php admin/tool/replace/cli/replace.php --search=//oldsitehost --replace=//newsitehost
  44  ";
  45  
  46  list($options, $unrecognized) = cli_get_params(
  47      array(
  48          'search'  => null,
  49          'replace' => null,
  50          'skiptables' => '',
  51          'shorten' => false,
  52          'non-interactive' => false,
  53          'help'    => false,
  54      ),
  55      array(
  56          'h' => 'help',
  57      )
  58  );
  59  
  60  if ($options['help'] || $options['search'] === null || $options['replace'] === null) {
  61      echo $help;
  62      exit(0);
  63  }
  64  
  65  if (!$DB->replace_all_text_supported()) {
  66      cli_error(get_string('notimplemented', 'tool_replace'));
  67  }
  68  
  69  if (empty($options['shorten']) && core_text::strlen($options['search']) < core_text::strlen($options['replace'])) {
  70      cli_error(get_string('cannotfit', 'tool_replace'));
  71  }
  72  
  73  try {
  74      $search = validate_param($options['search'], PARAM_RAW);
  75      $replace = validate_param($options['replace'], PARAM_RAW);
  76      $skiptables = validate_param($options['skiptables'], PARAM_RAW);
  77  } catch (invalid_parameter_exception $e) {
  78      cli_error(get_string('invalidcharacter', 'tool_replace'));
  79  }
  80  
  81  if (!$options['non-interactive']) {
  82      echo get_string('excludedtables', 'tool_replace') . "\n\n";
  83      echo get_string('notsupported', 'tool_replace') . "\n\n";
  84      $prompt = get_string('cliyesnoprompt', 'admin');
  85      $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
  86      if ($input == get_string('clianswerno', 'admin')) {
  87          exit(1);
  88      }
  89  }
  90  
  91  if (!db_replace($search, $replace, $skiptables)) {
  92      cli_heading(get_string('error'));
  93      exit(1);
  94  }
  95  
  96  cli_heading(get_string('success'));
  97  exit(0);