| 1 | 
           efrain | 
           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 | 
              | 
        
        
           | 11 | 
           efrain | 
           60 | 
           if ($unrecognized) {
  | 
        
        
            | 
            | 
           61 | 
               $unrecognized = implode("\n  ", $unrecognized);
  | 
        
        
            | 
            | 
           62 | 
               cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
  | 
        
        
            | 
            | 
           63 | 
           }
  | 
        
        
            | 
            | 
           64 | 
              | 
        
        
            | 
            | 
           65 | 
           // Ensure that user has populated both the search/replace parameters.
  | 
        
        
            | 
            | 
           66 | 
           if ($options['help'] || !is_string($options['search']) || !is_string($options['replace'])) {
  | 
        
        
           | 1 | 
           efrain | 
           67 | 
               echo $help;
  | 
        
        
            | 
            | 
           68 | 
               exit(0);
  | 
        
        
            | 
            | 
           69 | 
           }
  | 
        
        
            | 
            | 
           70 | 
              | 
        
        
            | 
            | 
           71 | 
           if (!$DB->replace_all_text_supported()) {
  | 
        
        
            | 
            | 
           72 | 
               cli_error(get_string('notimplemented', 'tool_replace'));
  | 
        
        
            | 
            | 
           73 | 
           }
  | 
        
        
            | 
            | 
           74 | 
              | 
        
        
            | 
            | 
           75 | 
           if (empty($options['shorten']) && core_text::strlen($options['search']) < core_text::strlen($options['replace'])) {
  | 
        
        
            | 
            | 
           76 | 
               cli_error(get_string('cannotfit', 'tool_replace'));
  | 
        
        
            | 
            | 
           77 | 
           }
  | 
        
        
            | 
            | 
           78 | 
              | 
        
        
            | 
            | 
           79 | 
           try {
  | 
        
        
            | 
            | 
           80 | 
               $search = validate_param($options['search'], PARAM_RAW);
  | 
        
        
            | 
            | 
           81 | 
               $replace = validate_param($options['replace'], PARAM_RAW);
  | 
        
        
            | 
            | 
           82 | 
               $skiptables = validate_param($options['skiptables'], PARAM_RAW);
  | 
        
        
            | 
            | 
           83 | 
           } catch (invalid_parameter_exception $e) {
  | 
        
        
            | 
            | 
           84 | 
               cli_error(get_string('invalidcharacter', 'tool_replace'));
  | 
        
        
            | 
            | 
           85 | 
           }
  | 
        
        
            | 
            | 
           86 | 
              | 
        
        
            | 
            | 
           87 | 
           if (!$options['non-interactive']) {
  | 
        
        
            | 
            | 
           88 | 
               echo get_string('excludedtables', 'tool_replace') . "\n\n";
  | 
        
        
            | 
            | 
           89 | 
               echo get_string('notsupported', 'tool_replace') . "\n\n";
  | 
        
        
            | 
            | 
           90 | 
               $prompt = get_string('cliyesnoprompt', 'admin');
  | 
        
        
            | 
            | 
           91 | 
               $input = cli_input($prompt, '', array(get_string('clianswerno', 'admin'), get_string('cliansweryes', 'admin')));
  | 
        
        
            | 
            | 
           92 | 
               if ($input == get_string('clianswerno', 'admin')) {
  | 
        
        
            | 
            | 
           93 | 
                   exit(1);
  | 
        
        
            | 
            | 
           94 | 
               }
  | 
        
        
            | 
            | 
           95 | 
           }
  | 
        
        
            | 
            | 
           96 | 
              | 
        
        
            | 
            | 
           97 | 
           if (!db_replace($search, $replace, $skiptables)) {
  | 
        
        
            | 
            | 
           98 | 
               cli_heading(get_string('error'));
  | 
        
        
            | 
            | 
           99 | 
               exit(1);
  | 
        
        
            | 
            | 
           100 | 
           }
  | 
        
        
            | 
            | 
           101 | 
              | 
        
        
            | 
            | 
           102 | 
           cli_heading(get_string('success'));
  | 
        
        
            | 
            | 
           103 | 
           exit(0);
  |