upgrade.php 9.26 KB
Newer Older
1 2 3 4 5
<?php
/**
 *
 * @package    mahara
 * @subpackage admin
6
 * @author     Catalyst IT Ltd
7 8
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL version 3 or later
 * @copyright  For copyright information on Mahara, please see the README file distributed with this software.
9 10 11
 *
 */

12
define('INTERNAL', 1);
13
define('ADMIN', 1);
14
define('INSTALLER', 1);
15 16

require(dirname(dirname(__FILE__)).'/init.php');
17
require(get_config('libroot') . 'upgrade.php');
18

19
if (param_integer('finished', 0)) {
20
    foreach (site_warnings() as $w) {
21
        $SESSION->add_error_msg($w, FALSE);
22
    }
23 24 25 26 27 28
    if ($missing = check_installed_plugins()) {
        $message = get_string('installedpluginsmissing', 'admin') . ': ';
        $message .= join(', ', $missing) . '.';
        $message .= '  ' . get_string('ensurepluginsexist', 'admin', get_config('docroot'));
        $SESSION->add_error_msg($message);
    }
29
    if (get_config('new_registration_policy') == 1) {
30 31
        redirect('/admin/registersite.php');
    }
32 33 34
    redirect();
}

35
$smarty = smarty();
36

37
$upgrades = check_upgrades();
38 39 40
if (!empty($upgrades['core']->install)) {
    define('TITLE', get_string('installation', 'admin'));
    $smarty->assign('upgradeheading', get_string('performinginstallation', 'admin'));
41
    log_info('Starting Mahara installation...');
42 43 44
}
else {
    define('TITLE', get_string('upgrades', 'admin'));
45 46 47 48
    if (!db_is_utf8()) {
        global $SESSION;
        $SESSION->add_error_msg(get_string('dbnotutf8warning', 'admin'));
    }
49
    ensure_upgrade_sanity();
50 51 52
    $smarty->assign('upgradeheading', get_string('performingupgrades', 'admin'));
}

53 54
// Remove the "settings" component, which is not a real component (see check_upgrades())
unset($upgrades['settings']);
55

56
if (!$upgrades) {
57
    die_info('<p>' . get_string('noupgrades', 'admin') . '</p>');
58
}
59

60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
$start = time();
if (empty($upgrades['core']->install)) {
    // Insert a record into config before the upgrade starts, to prevent subsequent hits
    // on this page from starting a second simultaneous upgrade.

    // But let the admin run a second one if they really want to.
    if (param_integer('rerun', 0)) {
        delete_records('config', 'field', '_upgrade');
    }

    if (!$lastupgrade = get_field('config', 'value', 'field', '_upgrade')) {
        try {
            insert_record('config', (object) array('field' => '_upgrade', 'value' => $start));
        }
        catch (SQLException $e) {
            if (!$lastupgrade = get_field('config', 'value', 'field', '_upgrade')) {
                $lastupgrade = '???';
            }
        }
    }

    if (!empty($lastupgrade)) {
        $laststart = format_date($lastupgrade, 'strftimedatetimeshort');
        log_debug('Not upgrading; unfinished upgrade from ' . $laststart . ' still in progress');
        die_info(get_string('upgradeinprogress', 'admin', $laststart));
    }
}

88 89
$loadingicon = 'icon icon-spinner icon-pulse left';
$successicon = 'icon icon-check text-success left';
90 91
$failureicon = 'icon icon-exclamation-triangle left';
$warningicon = 'icon icon-exclamation-triangle left';
92

93

94 95
// Remove all files in the smarty and dwoo caches
// TODO post 1.2 remove the smarty part
96 97 98 99 100 101 102 103 104 105
require_once('file.php');
$basedir = get_config('dataroot') . 'smarty/compile/';
$dh = new DirectoryIterator($basedir);
foreach ($dh as $themedir) {
    if ($themedir->isDot()) continue;
    $themedirname = $basedir . $themedir->getFilename();
    rmdirr($themedirname);
    clearstatcache();
    check_dir_exists($themedirname);
}
106 107 108 109 110 111 112 113 114 115
$basedir = get_config('dataroot') . 'dwoo/compile/';
$dh = new DirectoryIterator($basedir);
foreach ($dh as $themedir) {
    if ($themedir->isDot()) continue;
    $themedirname = $basedir . $themedir->getFilename();
    rmdirr($themedirname);
    clearstatcache();
    check_dir_exists($themedirname);
}

116

117 118 119 120 121
$loadingstring = json_encode(get_string('upgradeloading', 'admin'));
$installsuccessstring = json_encode(get_string('installsuccess', 'admin'));
$successstring = json_encode(get_string('upgradesuccesstoversion', 'admin'));
$failurestring = json_encode(get_string('upgradefailure', 'admin'));
$coresuccess   = json_encode(get_string('coredatasuccess', 'admin'));
122
$localsuccess  = json_encode(get_string('localdatasuccess', 'admin'));
123

124 125 126
// Check if Mahara is being installed. An extra hook is required to insert core
// data if so.
if (!empty($upgrades['core']->install)) {
127
    $upgrades['firstcoredata'] = true;
128
    $upgrades['localpreinst'] = true;
129
    $upgrades['lastcoredata'] = true;
130
    $upgrades['localpostinst'] = true;
131
    $smarty->assign('install', true);
Aaron Wells's avatar
Aaron Wells committed
132
}
133 134 135 136 137
foreach ($upgrades as $key => $upgrade) {
    if (isset($upgrade->newinstall)) {
        unset($upgrades[$key]);
    }
}
138

139
$js = <<< EOJS
Penny Leach's avatar
Penny Leach committed
140 141
            function processNext() {
                var element = todo.shift();
142
                if (!element) {
143
                    jQuery('#installdone').removeClass('hidden');
144
                    jQuery('html, body').animate({ scrollTop: jQuery('#installdone').offset().top }, 'slow');
145
                    return; // done
Penny Leach's avatar
Penny Leach committed
146 147
                }

148
                $(element).innerHTML = '<span class="{$loadingicon}" title="' + {$loadingstring} + '" role="presentation" aria-hidden="true"></span>';
Penny Leach's avatar
Penny Leach committed
149

150
                sendjsonrequest('upgrade.json.php', { 'name': element, 'last': todo.length == 0 }, 'GET', function (data) {
151
                    if ( !data.error ) {
152
                        var message;
153 154
                        if (data.coredata) {
                            message = {$coresuccess};
155
                            $(data.key).innerHTML = '<span class="{$successicon}" title=":)" role="presentation" aria-hidden="true"></span>' + message;
156
                        }
157 158
                        else if (data.localdata) {
                            message = {$localsuccess};
159
                            $(data.key).innerHTML = '<span class="{$successicon}" title=":)" role="presentation" aria-hidden="true"></span>' + message;
160
                        }
161
                        else if (data.install || data.upgrade) {
162 163 164 165
                            if (data.install) {
                                message = {$installsuccessstring};
                            }
                            else {
166 167 168 169 170 171
                                if (data.message) {
                                    message = data.message;
                                }
                                else {
                                    message = {$successstring};
                                }
172
                            }
173
                            message += data.newversion ? data.newversion : '';
174
                            $(data.key).innerHTML = '<span class="{$successicon}" title=":)" role="presentation" aria-hidden="true"></span>' + message;
175
                        }
176 177
                        else if (data.done) {
                            message = data.message;
178
                            $(data.key).innerHTML = '<span class="{$warningicon}" title=":|" role="presentation" aria-hidden="true"></span> ' + message;
179
                        }
180 181
                        else {
                            message = data.message;
182
                            $(data.key).innerHTML = '<span class="{$failureicon}" title=":(" role="presentation" aria-hidden="true"></span>' + message;
183
                        }
184 185 186 187 188
                        if (data.feedback) {
                            var feedback_element = DIV();
                            feedback_element.innerHTML = data.feedback;
                            appendChildNodes('messages', feedback_element);
                        }
189
                        processNext();
Penny Leach's avatar
Penny Leach committed
190 191
                    }
                    else {
192
                        var message = '';
193 194
                        if (data.errormessage) {
                            message = data.errormessage;
Aaron Wells's avatar
Aaron Wells committed
195
                        }
196
                        else {
197
                            message = {$failurestring};
198
                        }
199
                        $(data.key).innerHTML = '<span class="{$failureicon}" title=":(" role="presentation" aria-hidden="true"></span>' + message;
Penny Leach's avatar
Penny Leach committed
200
                    }
Aaron Wells's avatar
Aaron Wells committed
201
                },
202
                function () {
203
                    $(element).innerHTML = '<span class="{$failureicon}" title=":("  role="presentation" aria-hidden="true"></span>' + {$failurestring};
204 205
                },
                true);
Penny Leach's avatar
Penny Leach committed
206 207 208 209 210
            }

            addLoadEvent( processNext );
EOJS;

211 212
uksort($upgrades, 'sort_upgrades');
$js .= "\n" . 'var todo = ' . json_encode(array_keys($upgrades)) . ";\n";
213
$smarty->assign('INLINEJAVASCRIPT', $js);
Penny Leach's avatar
Penny Leach committed
214

215
$smarty->assign_by_ref('upgrades', $upgrades);
216 217 218
if (isset($upgrades['core'])) {
    $smarty->assign('releaseargs', array($upgrades['core']->torelease, $upgrades['core']->to));
}
219 220
$smarty->display('admin/upgrade.tpl');

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
function check_installed_plugins() {
    $missing = array();

    foreach (plugin_types() as $plugintype) {
        if ($installed = plugins_installed($plugintype, true)) {
            foreach ($installed as $i) {
                $key = $i->name;
                if ($plugintype == 'blocktype') {
                    $key = blocktype_single_to_namespaced($i->name, $i->artefactplugin);
                }
                try {
                    safe_require($plugintype, $key);
                }
                catch (SystemException $e) {
                    $missing[] = "$plugintype:$key";
                }
            }
        }
    }
240

241 242
    return $missing;
}