Merge branch 'wip-mdl-45719-m27' of https://github.com/deraadt/moodle into MOODLE_27_...
[moodle.git] / auth / db / cli / sync_users.php
blobf84bf2f7ca513c5eb1d2181b14767356afa564a6
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.
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/>.
17 /**
18 * Extdb user sync script.
20 * This script is meant to be called from a system cronjob to
21 * sync moodle user accounts with external database.
22 * It is required when using internal passwords (== passwords not defined in external database).
24 * Sample cron entry:
25 * # 5 minutes past 4am
26 * 5 4 * * * sudo -u www-data /usr/bin/php /var/www/moodle/auth/db/cli/sync_users.php
28 * Notes:
29 * - it is required to use the web server account when executing PHP CLI scripts
30 * - you need to change the "www-data" to match the apache user account
31 * - use "su" if "sudo" not available
32 * - If you have a large number of users, you may want to raise the memory limits
33 * by passing -d memory_limit=256M
34 * - For debugging & better logging, you are encouraged to use in the command line:
35 * -d log_errors=1 -d error_reporting=E_ALL -d display_errors=0 -d html_errors=0
37 * Performance notes:
38 * + The code is simpler, but not as optimized as its LDAP counterpart.
40 * @package auth_db
41 * @copyright 2006 Martin Langhoff
42 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45 define('CLI_SCRIPT', true);
47 require(__DIR__.'/../../../config.php');
48 require_once("$CFG->libdir/clilib.php");
50 // Now get cli options.
51 list($options, $unrecognized) = cli_get_params(array('noupdate'=>false, 'verbose'=>false, 'help'=>false), array('n'=>'noupdate', 'v'=>'verbose', 'h'=>'help'));
53 if ($unrecognized) {
54 $unrecognized = implode("\n ", $unrecognized);
55 cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
58 if ($options['help']) {
59 $help =
60 "Execute user account sync with external database.
61 The auth_db plugin must be enabled and properly configured.
63 Options:
64 -n, --noupdate Skip update of existing users
65 -v, --verbose Print verbose progress information
66 -h, --help Print out this help
68 Example:
69 \$ sudo -u www-data /usr/bin/php auth/db/cli/sync_users.php
71 Sample cron entry:
72 # 5 minutes past 4am
73 5 4 * * * sudo -u www-data /usr/bin/php /var/www/moodle/auth/db/cli/sync_users.php
76 echo $help;
77 die;
80 if (!is_enabled_auth('db')) {
81 cli_error('auth_db plugin is disabled, synchronisation stopped', 2);
84 if (empty($options['verbose'])) {
85 $trace = new null_progress_trace();
86 } else {
87 $trace = new text_progress_trace();
90 $update = empty($options['noupdate']);
92 /** @var auth_plugin_db $dbauth */
93 $dbauth = get_auth_plugin('db');
94 $result = $dbauth->sync_users($trace, $update);
96 exit($result);