upgrade.php 8.4 KB
Newer Older
1 2
<?php
/**
Francois Marier's avatar
Francois Marier committed
3
 * Mahara: Electronic portfolio, weblog, resume builder and social networking
4 5
 * Copyright (C) 2006-2009 Catalyst IT Ltd and others; see:
 *                         http://wiki.mahara.org/Contributors
6
 *
Francois Marier's avatar
Francois Marier committed
7 8 9 10
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
11
 *
Francois Marier's avatar
Francois Marier committed
12 13 14 15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
16
 *
Francois Marier's avatar
Francois Marier committed
17 18
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 20 21
 *
 * @package    mahara
 * @subpackage admin
22
 * @author     Catalyst IT Ltd
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL
24
 * @copyright  (C) 2006-2009 Catalyst IT Ltd http://catalyst.net.nz
25 26 27
 *
 */

28
define('INTERNAL', 1);
29
define('ADMIN', 1);
30
define('INSTALLER', 1);
31 32

require(dirname(dirname(__FILE__)).'/init.php');
33
require(get_config('libroot') . 'upgrade.php');
34

35
if (param_integer('finished', 0)) {
36 37 38
    foreach (site_warnings() as $w) {
        $SESSION->add_error_msg($w);
    }
39 40 41 42 43 44 45 46 47
    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);
    }
    redirect();
}

48
$smarty = smarty();
49

50
$upgrades = check_upgrades();
51 52 53
if (!empty($upgrades['core']->install)) {
    define('TITLE', get_string('installation', 'admin'));
    $smarty->assign('upgradeheading', get_string('performinginstallation', 'admin'));
54
    log_info('Starting Mahara installation...');
55 56 57
}
else {
    define('TITLE', get_string('upgrades', 'admin'));
58 59 60 61
    if (!db_is_utf8()) {
        global $SESSION;
        $SESSION->add_error_msg(get_string('dbnotutf8warning', 'admin'));
    }
62
    ensure_upgrade_sanity();
63 64 65
    $smarty->assign('upgradeheading', get_string('performingupgrades', 'admin'));
}

66 67 68
if (empty($upgrades['disablelogin'])) {
    auth_setup();
}
69
unset($upgrades['disablelogin']);
70

71 72 73
if (!$upgrades) {
    die_info(get_string('noupgrades', 'admin'));
}
74

75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
$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));
    }
}

103 104 105
$loadingicon = $THEME->get_url('images/loading.gif');
$successicon = $THEME->get_url('images/success.gif');
$failureicon = $THEME->get_url('images/failure.gif');
106

107 108
// Remove all files in the smarty and dwoo caches
// TODO post 1.2 remove the smarty part
109 110 111 112 113 114 115 116 117 118
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);
}
119 120 121 122 123 124 125 126 127 128
$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);
}

129

130 131 132 133 134
$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'));
135
$localsuccess  = json_encode(get_string('localdatasuccess', 'admin'));
136

137 138 139
// Check if Mahara is being installed. An extra hook is required to insert core
// data if so.
if (!empty($upgrades['core']->install)) {
140
    $upgrades['firstcoredata'] = true;
141
    $upgrades['localpreinst'] = true;
142
    $upgrades['lastcoredata'] = true;
143
    $upgrades['localpostinst'] = true;
144
    $smarty->assign('install', true);
145
}                   
146

147
$js = <<< EOJS
Penny Leach's avatar
Penny Leach committed
148 149 150
            function processNext() {
                var element = todo.shift();

151 152
                if (!element) {
                    $('finished').style.visibility = 'visible';
153
                    window.scrollTo(0, 5000000);
154
                    return; // done
Penny Leach's avatar
Penny Leach committed
155 156
                }

157
                $(element).innerHTML = '<img src="{$loadingicon}" alt="' + {$loadingstring} + '" />';
Penny Leach's avatar
Penny Leach committed
158

159
                sendjsonrequest('upgrade.json.php', { 'name': element, 'last': todo.length == 0 }, 'GET', function (data) {
160
                    if ( !data.error ) {
161
                        var message;
162 163 164
                        if (data.coredata) {
                            message = {$coresuccess};
                        } 
165 166 167
                        else if (data.localdata) {
                            message = {$localsuccess};
                        }
168
                        else {
169 170 171 172 173 174
                            if (data.install) {
                                message = {$installsuccessstring};
                            }
                            else {
                                message = {$successstring};
                            }
175
                            message += data.newversion ? data.newversion : '?';
176
                        }
177
                        $(data.key).innerHTML = '<img src="{$successicon}" alt=":)" />  ' + message;
178 179 180 181 182
                        if (data.feedback) {
                            var feedback_element = DIV();
                            feedback_element.innerHTML = data.feedback;
                            appendChildNodes('messages', feedback_element);
                        }
183
                        processNext();
Penny Leach's avatar
Penny Leach committed
184 185
                    }
                    else {
186
                        var message = '';
187 188
                        if (data.errormessage) {
                            message = data.errormessage;
189 190
                        } 
                        else {
191
                            message = {$failurestring};
192
                        }
193
                        $(data.key).innerHTML = '<img src="{$failureicon}" alt=":(" /> ' + message;
Penny Leach's avatar
Penny Leach committed
194
                    }
195 196
                }, 
                function () {
197
                    $(element).innerHTML = '<img src="{$failureicon}" alt=":(" /> ' + {$failurestring};
198 199
                },
                true);
Penny Leach's avatar
Penny Leach committed
200 201 202 203 204
            }

            addLoadEvent( processNext );
EOJS;

205 206
uksort($upgrades, 'sort_upgrades');
$js .= "\n" . 'var todo = ' . json_encode(array_keys($upgrades)) . ";\n";
207
$smarty->assign('INLINEJAVASCRIPT', $js);
Penny Leach's avatar
Penny Leach committed
208

209
$smarty->assign_by_ref('upgrades', $upgrades);
210 211 212
if (isset($upgrades['core'])) {
    $smarty->assign('releaseargs', array($upgrades['core']->torelease, $upgrades['core']->to));
}
213 214
$smarty->display('admin/upgrade.tpl');

215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
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";
                }
            }
        }
    }
234

235 236
    return $missing;
}