Exemplo n.º 1
3
 /**
  * Reset contents of all database tables to initial values, reset caches, etc.
  *
  * Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
  *
  * @static
  * @param bool $detectchanges
  *      true  - changes in global state and database are reported as errors
  *      false - no errors reported
  *      null  - only critical problems are reported as errors
  * @return void
  */
 public static function reset_all_data($detectchanges = false)
 {
     global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION;
     // Stop any message redirection.
     phpunit_util::stop_message_redirection();
     // Stop any message redirection.
     phpunit_util::stop_phpmailer_redirection();
     // Stop any message redirection.
     phpunit_util::stop_event_redirection();
     // We used to call gc_collect_cycles here to ensure desctructors were called between tests.
     // This accounted for 25% of the total time running phpunit - so we removed it.
     // Show any unhandled debugging messages, the runbare() could already reset it.
     self::display_debugging_messages();
     self::reset_debugging();
     // reset global $DB in case somebody mocked it
     $DB = self::get_global_backup('DB');
     if ($DB->is_transaction_started()) {
         // we can not reset inside transaction
         $DB->force_transaction_rollback();
     }
     $resetdb = self::reset_database();
     $warnings = array();
     if ($detectchanges === true) {
         if ($resetdb) {
             $warnings[] = 'Warning: unexpected database modification, resetting DB state';
         }
         $oldcfg = self::get_global_backup('CFG');
         $oldsite = self::get_global_backup('SITE');
         foreach ($CFG as $k => $v) {
             if (!property_exists($oldcfg, $k)) {
                 $warnings[] = 'Warning: unexpected new $CFG->' . $k . ' value';
             } else {
                 if ($oldcfg->{$k} !== $CFG->{$k}) {
                     $warnings[] = 'Warning: unexpected change of $CFG->' . $k . ' value';
                 }
             }
             unset($oldcfg->{$k});
         }
         if ($oldcfg) {
             foreach ($oldcfg as $k => $v) {
                 $warnings[] = 'Warning: unexpected removal of $CFG->' . $k;
             }
         }
         if ($USER->id != 0) {
             $warnings[] = 'Warning: unexpected change of $USER';
         }
         if ($COURSE->id != $oldsite->id) {
             $warnings[] = 'Warning: unexpected change of $COURSE';
         }
     }
     if (ini_get('max_execution_time') != 0) {
         // This is special warning for all resets because we do not want any
         // libraries to mess with timeouts unintentionally.
         // Our PHPUnit integration is not supposed to change it either.
         if ($detectchanges !== false) {
             $warnings[] = 'Warning: max_execution_time was changed to ' . ini_get('max_execution_time');
         }
         set_time_limit(0);
     }
     // restore original globals
     $_SERVER = self::get_global_backup('_SERVER');
     $CFG = self::get_global_backup('CFG');
     $SITE = self::get_global_backup('SITE');
     $_GET = array();
     $_POST = array();
     $_FILES = array();
     $_REQUEST = array();
     $COURSE = $SITE;
     // reinitialise following globals
     $OUTPUT = new bootstrap_renderer();
     $PAGE = new moodle_page();
     $FULLME = null;
     $ME = null;
     $SCRIPT = null;
     // Empty sessison and set fresh new not-logged-in user.
     \core\session\manager::init_empty_session();
     // reset all static caches
     \core\event\manager::phpunit_reset();
     accesslib_clear_all_caches(true);
     get_string_manager()->reset_caches(true);
     reset_text_filters_cache(true);
     events_get_handlers('reset');
     core_text::reset_caches();
     get_message_processors(false, true);
     filter_manager::reset_caches();
     // Reset internal users.
     core_user::reset_internal_users();
     //TODO MDL-25290: add more resets here and probably refactor them to new core function
     // Reset course and module caches.
     if (class_exists('format_base')) {
         // If file containing class is not loaded, there is no cache there anyway.
         format_base::reset_course_cache(0);
     }
     get_fast_modinfo(0, 0, true);
     // Reset other singletons.
     if (class_exists('core_plugin_manager')) {
         core_plugin_manager::reset_caches(true);
     }
     if (class_exists('\\core\\update\\checker')) {
         \core\update\checker::reset_caches(true);
     }
     if (class_exists('\\core\\update\\deployer')) {
         \core\update\deployer::reset_caches(true);
     }
     // purge dataroot directory
     self::reset_dataroot();
     // restore original config once more in case resetting of caches changed CFG
     $CFG = self::get_global_backup('CFG');
     // inform data generator
     self::get_data_generator()->reset();
     // fix PHP settings
     error_reporting($CFG->debug);
     // verify db writes just in case something goes wrong in reset
     if (self::$lastdbwrites != $DB->perf_get_writes()) {
         error_log('Unexpected DB writes in phpunit_util::reset_all_data()');
         self::$lastdbwrites = $DB->perf_get_writes();
     }
     if ($warnings) {
         $warnings = implode("\n", $warnings);
         trigger_error($warnings, E_USER_WARNING);
     }
 }
Exemplo n.º 2
0
/**
 * Purge all caches when settings changed.
 */
function filter_tex_updatedcallback($name)
{
    global $CFG, $DB;
    reset_text_filters_cache();
    if (file_exists("{$CFG->dataroot}/filter/tex")) {
        remove_dir("{$CFG->dataroot}/filter/tex");
    }
    if (file_exists("{$CFG->dataroot}/filter/algebra")) {
        remove_dir("{$CFG->dataroot}/filter/algebra");
    }
    if (file_exists("{$CFG->tempdir}/latex")) {
        remove_dir("{$CFG->tempdir}/latex");
    }
    $DB->delete_records('cache_filters', array('filter' => 'tex'));
    $DB->delete_records('cache_filters', array('filter' => 'algebra'));
    if (!isset($CFG->filter_tex_pathlatex)) {
        // detailed settings not present yet
        return;
    }
    $pathlatex = trim($CFG->filter_tex_pathlatex, " '\"");
    $pathdvips = trim($CFG->filter_tex_pathdvips, " '\"");
    $pathconvert = trim($CFG->filter_tex_pathconvert, " '\"");
    if (!(is_file($pathlatex) && is_executable($pathlatex) && is_file($pathdvips) && is_executable($pathdvips) && is_file($pathconvert) && is_executable($pathconvert))) {
        // LaTeX, dvips or convert are not available, and mimetex can only produce GIFs so...
        set_config('filter_tex_convertformat', 'gif');
    }
}
Exemplo n.º 3
0
Arquivo: lib.php Projeto: r007/PMoodle
/**
 * Purge all caches when settings changed.
 */
function filter_tex_updatedcallback($name)
{
    global $CFG;
    reset_text_filters_cache();
    if (file_exists("{$CFG->dataroot}/filter/tex")) {
        remove_dir("{$CFG->dataroot}/filter/tex");
    }
    if (file_exists("{$CFG->dataroot}/filter/algebra")) {
        remove_dir("{$CFG->dataroot}/filter/algebra");
    }
    if (file_exists("{$CFG->dataroot}/temp/latex")) {
        remove_dir("{$CFG->dataroot}/temp/latex");
    }
    delete_records('cache_filters', 'filter', 'tex');
    delete_records('cache_filters', 'filter', 'algebra');
}
Exemplo n.º 4
0
/**
 * Purge all caches when settings changed.
 */
function filter_tex_updatedcallback($name)
{
    global $CFG;
    reset_text_filters_cache();
    if (file_exists("{$CFG->dataroot}/filter/tex")) {
        remove_dir("{$CFG->dataroot}/filter/tex");
    }
    if (file_exists("{$CFG->dataroot}/filter/algebra")) {
        remove_dir("{$CFG->dataroot}/filter/algebra");
    }
    if (file_exists("{$CFG->dataroot}/temp/latex")) {
        remove_dir("{$CFG->dataroot}/temp/latex");
    }
    delete_records('cache_filters', 'filter', 'tex');
    delete_records('cache_filters', 'filter', 'algebra');
    if (!(is_file($CFG->filter_tex_pathlatex) && is_executable($CFG->filter_tex_pathlatex) && is_file($CFG->filter_tex_pathdvips) && is_executable($CFG->filter_tex_pathdvips) && is_file($CFG->filter_tex_pathconvert) && is_executable($CFG->filter_tex_pathconvert))) {
        // LaTeX, dvips or convert are not available, and mimetex can only produce GIFs so...
        set_config('filter_tex_convertformat', 'gif');
    }
}
Exemplo n.º 5
0
/**
 * Purge all caches when settings changed.
 */
function filter_texwjax_updatedcallback($name)
{
    global $CFG, $DB;
    reset_text_filters_cache();
    if (file_exists("{$CFG->dataroot}/filter/texwjax")) {
        remove_dir("{$CFG->dataroot}/filter/texwjax");
    }
    if (file_exists("{$CFG->dataroot}/filter/algebra")) {
        remove_dir("{$CFG->dataroot}/filter/algebra");
    }
    if (file_exists("{$CFG->tempdir}/latex")) {
        remove_dir("{$CFG->tempdir}/latex");
    }
    $DB->delete_records('cache_filters', array('filter' => 'texwjax'));
    $DB->delete_records('cache_filters', array('filter' => 'algebra'));
    $pathlatex = get_config('filter_texwjax', 'pathlatex');
    if ($pathlatex === false) {
        // detailed settings not present yet
        return;
    }
    $pathlatex = trim($pathlatex, " '\"");
    $pathdvips = trim(get_config('filter_texwjax', 'pathdvips'), " '\"");
    $pathconvert = trim(get_config('filter_texwjax', 'pathconvert'), " '\"");
    $pathdvisvgm = trim(get_config('filter_texwjax', 'pathdvisvgm'), " '\"");
    $supportedformats = array('gif');
    if (is_file($pathlatex) && is_executable($pathlatex) && (is_file($pathdvips) && is_executable($pathdvips))) {
        if (is_file($pathconvert) && is_executable($pathconvert)) {
            $supportedformats[] = 'png';
        }
        if (is_file($pathdvisvgm) && is_executable($pathdvisvgm)) {
            $supportedformats[] = 'svg';
        }
    }
    if (!in_array(get_config('filter_texwjax', 'convertformat'), $supportedformats)) {
        set_config('convertformat', array_pop($supportedformats), 'filter_texwjax');
    }
}
Exemplo n.º 6
0
 public function execute()
 {
     global $CFG, $DB;
     require_once $CFG->libdir . '/adminlib.php';
     // various admin-only functions
     require_once $CFG->libdir . '/classes/plugin_manager.php';
     $filtername = $this->arguments[0];
     $newstate = $this->arguments[1];
     if ($newstate != 1 && $newstate != -1 && $newstate != -9999) {
         cli_error("Invalid filter value, use: 1 for on, -1 per course, -9999 for off");
     }
     // Clean up bogus filter states first.
     $plugininfos = core_plugin_manager::instance()->get_plugins_of_type('filter');
     $filters = array();
     $states = filter_get_global_states();
     foreach ($states as $state) {
         if (!isset($plugininfos[$state->filter]) and !get_config('filter_' . $state->filter, 'version')) {
             // Purge messy leftovers after incorrectly uninstalled plugins and unfinished installs.
             $DB->delete_records('filter_active', array('filter' => $state->filter));
             $DB->delete_records('filter_config', array('filter' => $state->filter));
             error_log('Deleted bogus "filter_' . $state->filter . '" states and config data.');
         } else {
             $filters[$state->filter] = $state;
         }
     }
     if (!isset($filters[$filtername])) {
         cli_error("Invalid filter name: '{$filtername}''. Possible values: " . implode(",", array_keys($filters)) . '.');
     }
     filter_set_global_state($filtername, $newstate);
     if ($newstate == TEXTFILTER_DISABLED) {
         filter_set_applies_to_strings($filtername, false);
     }
     reset_text_filters_cache();
     core_plugin_manager::reset_caches();
     echo "Updated {$filtername} to state = {$newstate}\n";
 }
Exemplo n.º 7
0
/**
 * Invalidates browser caches and cached data in temp.
 *
 * IMPORTANT - If you are adding anything here to do with the cache directory you should also have a look at
 * {@link phpunit_util::reset_dataroot()}
 *
 * @return void
 */
function purge_all_caches()
{
    global $CFG, $DB;
    reset_text_filters_cache();
    js_reset_all_caches();
    theme_reset_all_caches();
    get_string_manager()->reset_caches();
    core_text::reset_caches();
    if (class_exists('core_plugin_manager')) {
        core_plugin_manager::reset_caches();
    }
    // Bump up cacherev field for all courses.
    try {
        increment_revision_number('course', 'cacherev', '');
    } catch (moodle_exception $e) {
        // Ignore exception since this function is also called before upgrade script when field course.cacherev does not exist yet.
    }
    $DB->reset_caches();
    cache_helper::purge_all();
    // Purge all other caches: rss, simplepie, etc.
    clearstatcache();
    remove_dir($CFG->cachedir . '', true);
    // Make sure cache dir is writable, throws exception if not.
    make_cache_directory('');
    // This is the only place where we purge local caches, we are only adding files there.
    // The $CFG->localcachedirpurged flag forces local directories to be purged on cluster nodes.
    remove_dir($CFG->localcachedir, true);
    set_config('localcachedirpurged', time());
    make_localcache_directory('', true);
    \core\task\manager::clear_static_caches();
}
Exemplo n.º 8
0
/**
 * Invalidates browser caches and cached data in temp
 * @return void
 */
function purge_all_caches()
{
    global $CFG;
    reset_text_filters_cache();
    js_reset_all_caches();
    theme_reset_all_caches();
    get_string_manager()->reset_caches();
    // purge all other caches: rss, simplepie, etc.
    remove_dir($CFG->dataroot . '/cache', true);
    // make sure cache dir is writable, throws exception if not
    make_upload_directory('cache');
    clearstatcache();
}
Exemplo n.º 9
0
/**
 * Invalidates browser caches and cached data in temp
 * @return void
 */
function purge_all_caches()
{
    global $CFG;
    reset_text_filters_cache();
    js_reset_all_caches();
    theme_reset_all_caches();
    get_string_manager()->reset_caches();
    textlib::reset_caches();
    // purge all other caches: rss, simplepie, etc.
    remove_dir($CFG->cachedir . '', true);
    // make sure cache dir is writable, throws exception if not
    make_cache_directory('');
    // hack: this script may get called after the purifier was initialised,
    // but we do not want to verify repeatedly this exists in each call
    make_cache_directory('htmlpurifier');
}
Exemplo n.º 10
0
        filter_delete_all_for_filter($filterpath);
        $a = new stdClass();
        $a->filter = $filtername;
        $a->directory = $filterpath;
        echo $OUTPUT->box(get_string('deletefilterfiles', 'admin', $a), 'generalbox', 'notice');
        echo $OUTPUT->continue_button($returnurl);
        echo $OUTPUT->footer();
        exit;
}
// Add any missing filters to the DB table.
foreach ($newfilters as $filter => $notused) {
    filter_set_global_state($filter, TEXTFILTER_DISABLED);
}
// Reset caches and return
if ($action) {
    reset_text_filters_cache();
    redirect($returnurl);
}
/// End of process actions =====================================================
/// Print the page heading.
admin_externalpage_print_header();
echo $OUTPUT->heading(get_string('filtersettings', 'admin'));
$activechoices = array(TEXTFILTER_DISABLED => get_string('disabled', 'filters'), TEXTFILTER_OFF => get_string('offbutavailable', 'filters'), TEXTFILTER_ON => get_string('on', 'filters'));
$applytochoices = array(0 => get_string('content', 'filters'), 1 => get_string('contentandheadings', 'filters'));
$filters = filter_get_global_states();
// In case any new filters have been installed, but not put in the table yet.
$filternames = filter_get_all_installed();
$newfilters = $filternames;
foreach ($filters as $filter => $notused) {
    unset($newfilters[$filter]);
}
Exemplo n.º 11
0
 /**
  * Reset contents of all database tables to initial values, reset caches, etc.
  *
  * Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
  *
  * @static
  * @param bool $logchanges log changes in global state and database in error log
  * @return void
  */
 public static function reset_all_data($logchanges = false)
 {
     global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION;
     // Stop any message redirection.
     phpunit_util::stop_message_redirection();
     // Release memory and indirectly call destroy() methods to release resource handles, etc.
     gc_collect_cycles();
     // Show any unhandled debugging messages, the runbare() could already reset it.
     self::display_debugging_messages();
     self::reset_debugging();
     // reset global $DB in case somebody mocked it
     $DB = self::get_global_backup('DB');
     if ($DB->is_transaction_started()) {
         // we can not reset inside transaction
         $DB->force_transaction_rollback();
     }
     $resetdb = self::reset_database();
     $warnings = array();
     if ($logchanges) {
         if ($resetdb) {
             $warnings[] = 'Warning: unexpected database modification, resetting DB state';
         }
         $oldcfg = self::get_global_backup('CFG');
         $oldsite = self::get_global_backup('SITE');
         foreach ($CFG as $k => $v) {
             if (!property_exists($oldcfg, $k)) {
                 $warnings[] = 'Warning: unexpected new $CFG->' . $k . ' value';
             } else {
                 if ($oldcfg->{$k} !== $CFG->{$k}) {
                     $warnings[] = 'Warning: unexpected change of $CFG->' . $k . ' value';
                 }
             }
             unset($oldcfg->{$k});
         }
         if ($oldcfg) {
             foreach ($oldcfg as $k => $v) {
                 $warnings[] = 'Warning: unexpected removal of $CFG->' . $k;
             }
         }
         if ($USER->id != 0) {
             $warnings[] = 'Warning: unexpected change of $USER';
         }
         if ($COURSE->id != $oldsite->id) {
             $warnings[] = 'Warning: unexpected change of $COURSE';
         }
     }
     // restore original globals
     $_SERVER = self::get_global_backup('_SERVER');
     $CFG = self::get_global_backup('CFG');
     $SITE = self::get_global_backup('SITE');
     $COURSE = $SITE;
     // reinitialise following globals
     $OUTPUT = new bootstrap_renderer();
     $PAGE = new moodle_page();
     $FULLME = null;
     $ME = null;
     $SCRIPT = null;
     $SESSION = new stdClass();
     $_SESSION['SESSION'] =& $SESSION;
     // set fresh new not-logged-in user
     $user = new stdClass();
     $user->id = 0;
     $user->mnethostid = $CFG->mnet_localhost_id;
     session_set_user($user);
     // reset all static caches
     accesslib_clear_all_caches(true);
     get_string_manager()->reset_caches(true);
     reset_text_filters_cache(true);
     events_get_handlers('reset');
     textlib::reset_caches();
     if (class_exists('repository')) {
         repository::reset_caches();
     }
     filter_manager::reset_caches();
     //TODO MDL-25290: add more resets here and probably refactor them to new core function
     // Reset course and module caches.
     if (class_exists('format_base')) {
         // If file containing class is not loaded, there is no cache there anyway.
         format_base::reset_course_cache(0);
     }
     get_fast_modinfo(0, 0, true);
     // Reset other singletons.
     if (class_exists('plugin_manager')) {
         plugin_manager::reset_caches(true);
     }
     if (class_exists('available_update_checker')) {
         available_update_checker::reset_caches(true);
     }
     if (class_exists('available_update_deployer')) {
         available_update_deployer::reset_caches(true);
     }
     // purge dataroot directory
     self::reset_dataroot();
     // restore original config once more in case resetting of caches changed CFG
     $CFG = self::get_global_backup('CFG');
     // inform data generator
     self::get_data_generator()->reset();
     // fix PHP settings
     error_reporting($CFG->debug);
     // verify db writes just in case something goes wrong in reset
     if (self::$lastdbwrites != $DB->perf_get_writes()) {
         error_log('Unexpected DB writes in phpunit_util::reset_all_data()');
         self::$lastdbwrites = $DB->perf_get_writes();
     }
     if ($warnings) {
         $warnings = implode("\n", $warnings);
         trigger_error($warnings, E_USER_WARNING);
     }
 }
Exemplo n.º 12
0
 /**
  * Reset contents of all database tables to initial values, reset caches, etc.
  *
  * Note: this is relatively slow (cca 2 seconds for pg and 7 for mysql) - please use with care!
  *
  * @static
  * @param bool $logchanges log changes in global state and database in error log
  * @return void
  */
 public static function reset_all_data($logchanges = false)
 {
     global $DB, $CFG, $USER, $SITE, $COURSE, $PAGE, $OUTPUT, $SESSION, $GROUPLIB_CACHE;
     // reset global $DB in case somebody mocked it
     $DB = self::get_global_backup('DB');
     if ($DB->is_transaction_started()) {
         // we can not reset inside transaction
         $DB->force_transaction_rollback();
     }
     $resetdb = self::reset_database();
     $warnings = array();
     if ($logchanges) {
         if ($resetdb) {
             $warnings[] = 'Warning: unexpected database modification, resetting DB state';
         }
         $oldcfg = self::get_global_backup('CFG');
         $oldsite = self::get_global_backup('SITE');
         foreach ($CFG as $k => $v) {
             if (!property_exists($oldcfg, $k)) {
                 $warnings[] = 'Warning: unexpected new $CFG->' . $k . ' value';
             } else {
                 if ($oldcfg->{$k} !== $CFG->{$k}) {
                     $warnings[] = 'Warning: unexpected change of $CFG->' . $k . ' value';
                 }
             }
             unset($oldcfg->{$k});
         }
         if ($oldcfg) {
             foreach ($oldcfg as $k => $v) {
                 $warnings[] = 'Warning: unexpected removal of $CFG->' . $k;
             }
         }
         if ($USER->id != 0) {
             $warnings[] = 'Warning: unexpected change of $USER';
         }
         if ($COURSE->id != $oldsite->id) {
             $warnings[] = 'Warning: unexpected change of $COURSE';
         }
     }
     // restore original globals
     $_SERVER = self::get_global_backup('_SERVER');
     $CFG = self::get_global_backup('CFG');
     $SITE = self::get_global_backup('SITE');
     $COURSE = $SITE;
     // reinitialise following globals
     $OUTPUT = new bootstrap_renderer();
     $PAGE = new moodle_page();
     $FULLME = null;
     $ME = null;
     $SCRIPT = null;
     $SESSION = new stdClass();
     $_SESSION['SESSION'] =& $SESSION;
     // set fresh new not-logged-in user
     $user = new stdClass();
     $user->id = 0;
     $user->mnethostid = $CFG->mnet_localhost_id;
     session_set_user($user);
     // reset all static caches
     accesslib_clear_all_caches(true);
     get_string_manager()->reset_caches(true);
     reset_text_filters_cache(true);
     events_get_handlers('reset');
     textlib::reset_caches();
     if (class_exists('repository')) {
         repository::reset_caches();
     }
     $GROUPLIB_CACHE = null;
     //TODO MDL-25290: add more resets here and probably refactor them to new core function
     // purge dataroot directory
     self::reset_dataroot();
     // restore original config once more in case resetting of caches changed CFG
     $CFG = self::get_global_backup('CFG');
     // inform data generator
     self::get_data_generator()->reset();
     // fix PHP settings
     error_reporting($CFG->debug);
     // verify db writes just in case something goes wrong in reset
     if (self::$lastdbwrites != $DB->perf_get_writes()) {
         error_log('Unexpected DB writes in phpunit_util::reset_all_data()');
         self::$lastdbwrites = $DB->perf_get_writes();
     }
     if ($warnings) {
         $warnings = implode("\n", $warnings);
         trigger_error($warnings, E_USER_WARNING);
     }
 }