public function initialise_theme_and_output() {
        global $OUTPUT, $PAGE, $SITE, $CFG;

        if (!empty($this->_wherethemewasinitialised)) {
            return;
        }

        if (!during_initial_install()) {
            // Detect PAGE->context mess.
            $this->magic_get_context();
        }

        if (!$this->_course && !during_initial_install()) {
            $this->set_course($SITE);
        }

        if (is_null($this->_theme)) {
            $themename = $this->resolve_theme();
            $this->_theme = theme_config::load($themename);
        }

        $this->_theme->setup_blocks($this->pagelayout, $this->blocks);

        if ($this === $PAGE) {
            $target = null;
            if ($this->pagelayout === 'maintenance') {
                // If the page is using the maintenance layout then we're going to force target to maintenance.
                // This leads to a special core renderer that is designed to block access to API's that are likely unavailable for this
                // page layout.
                $target = RENDERER_TARGET_MAINTENANCE;
            }
            $OUTPUT = $this->get_renderer('core', null, $target);
        }

        if (!during_initial_install()) {
            $filtermanager = filter_manager::instance();
            $filtermanager->setup_page_for_globally_available_filters($this);
        }

        $this->_wherethemewasinitialised = debug_backtrace();
    }