From f05f9e08cfae4024ba6bc2ec6023f20298e33f55 Mon Sep 17 00:00:00 2001 From: Dan Poltawski Date: Thu, 3 Mar 2016 15:32:27 +0800 Subject: [PATCH] MDL-53247 search: add cli indexer script --- search/cli/indexer.php | 72 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 search/cli/indexer.php diff --git a/search/cli/indexer.php b/search/cli/indexer.php new file mode 100644 index 00000000000..b40a20b7a0d --- /dev/null +++ b/search/cli/indexer.php @@ -0,0 +1,72 @@ +. + +/** + * CLI search indexer + * + * @package search + * @copyright 2016 Dan Poltawski + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later + */ + +define('CLI_SCRIPT', true); + +require(__DIR__.'/../../config.php'); +require_once($CFG->libdir.'/clilib.php'); // cli only functions + +list($options, $unrecognized) = cli_get_params(array('help' => false, 'force' => false, 'reindex' => false), + array('h' => 'help', 'f' => 'force', 'r' => 'reindex')); + +if ($unrecognized) { + $unrecognized = implode("\n ", $unrecognized); + cli_error(get_string('cliunknowoption', 'admin', $unrecognized)); +} + +if ($options['help']) { + $help = +"Index search data + +Options: +-h, --help Print out this help +-r, --reindex Reindex data +-f, --force Allow indexer to run, even if global search is disabled. + +Example: +\$ sudo -u www-data /usr/bin/php search/cli/indexer.php --reindex +"; + + echo $help; + die; +} + +if (!\core_search\manager::is_global_search_enabled() && empty($options['force'])) { + cli_error('Global search is disabled. Use --force if you want to force an index while disabled'); +} + +$globalsearch = \core_search\manager::instance(); + +if (empty($options['reindex'])) { + echo "Running full index of site\n"; + echo "==========================\n"; + $globalsearch->index(); +} else { + echo "Running full reindex of site\n"; + echo "============================\n"; + $globalsearch->index(true); +} + +// Optimize index at last. +$globalsearch->optimize_index(); -- 2.43.0