Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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   * @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  --shorten             Shorten result if necessary.
  38  --non-interactive     Perform the replacement without confirming.
  39  -h, --help            Print out this help.
  40  
  41  Example:
  42  \$ sudo -u www-data /usr/bin/php admin/tool/replace/cli/replace.php --search=//oldsitehost --replace=//newsitehost
  43  ";
  44  
  45  list($options, $unrecognized) = cli_get_params(
  46      array(
  47          'search'  => null,
  48          'replace' => null,
  49          'shorten' => false,
  50          'non-interactive' => false,
  51          'help'    => false,
  52      ),
  53      array(
  54          'h' => 'help',
  55      )
  56  );
  57  
  58  if ($options['help'] || $options['search'] === null || $options['replace'] === null) {
  59      echo $help;
  60      exit(0);
  61  }
  62  
  63  if (!$DB->replace_all_text_supported()) {
  64      cli_error(get_string('notimplemented', 'tool_replace'));
  65  }
  66  
  67  if (empty($options['shorten']) && core_text::strlen($options['search']) < core_text::strlen($options['replace'])) {
  68      cli_error(get_string('cannotfit', 'tool_replace'));
  69  }
  70  
  71  try {
  72      $search = validate_param($options['search'], PARAM_RAW);
  73      $replace = validate_param($options['replace'], PARAM_RAW);
  74  } catch (invalid_parameter_exception $e) {
  75      cli_error(get_string('invalidcharacter', 'tool_replace'));
  76  }
  77  
  78  if (!$options['non-interactive']) {
  79      echo get_string('excludedtables', 'tool_replace') . "\n\n";
  80      echo get_string('notsupported', 'tool_replace') . "\n\n";
  81      $prompt = get_string('cliyesnoprompt', 'admin');
  82      $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
  83      if ($input == get_string('clianswerno', 'admin')) {
  84          exit(1);
  85      }
  86  }
  87  
  88  if (!db_replace($search, $replace)) {
  89      cli_heading(get_string('error'));
  90      exit(1);
  91  }
  92  
  93  cli_heading(get_string('success'));
  94  exit(0);