<?php
/*
 * $RCSfile: UpgradeCoreModuleStep.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2005 Bharat Mediratta
 *
 * 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 2 of the License, or (at
 * your option) any later version.
 *
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */

class UpgradeCoreModuleStep extends UpgradeStep {
    function stepName() {
	return _('Upgrade Core');
    }

    function processRequest() {
	global $gallery;
	if (!$gallery->getDebug()) {
	    $gallery->setDebug('buffered');
	}

	/*
	 * We'll need a translator to load up the core module for the
	 * GalleryPlugin::setGroup() method to work.
	 */
	$translator =& $gallery->getTranslator();
	if (empty($translator)) {
	    $ret = $gallery->initTranslator(true /* dontUseDatabase */ );
	    if ($ret->isError()) {
		return $ret->wrap(__FILE__, __LINE__);
	    }
	}

	require_once(dirname(__FILE__) . '/../../modules/core/module.inc');
	require_once(dirname(__FILE__) . '/../../modules/core/CoreModuleExtras.inc');
	$core = new CoreModule();
	$versions = $core->getInstalledVersions();
	$this->resetL10Domain();

	/* We didn't have these values before 2.0-alpha-3 when the core version was 0.8 */
	if (!isset($versions['core'])) {
	    $versions['core'] = '0.8';
	}
	if (!isset($versions['gallery'])) {
	    $versions['gallery'] = '2.0-alpha-3';
	}

	$templateData['installed'] = $versions;
	$templateData['coreVersion'] = $core->getVersion();
	$templateData['storeConfig'] = $gallery->getConfig('storage.config');

	$templateData['isConfigUpgradeRequired'] =
	    CoreModuleExtras::isConfigUpgradeRequired($versions['core']);
	$platform = $gallery->getPlatform();
	$configFilePath = GALLERY_CONFIG_DIR . '/config.php';
	$templateData['isConfigWriteable'] = $platform->is_writeable($configFilePath);
	$templateData['canChmod'] = strncasecmp(PHP_OS, 'win', 3);

	$template = new StatusTemplate();

	if (isset($_POST['confirm'])) {
	    $template->renderHeader(true);
	    /*
	     * Do the upgrade in bootstrap mode so that we avoid going to the
	     * database for anything that's not vital.
	     */
	    $ret = $core->installOrUpgrade(true, $template);
	    if ($ret->isSuccess()) {
		list ($ret,  $ignored) = $core->activate();
		if ($ret->isSuccess()) {
		    $storage =& $gallery->getStorage();
		    $ret = $storage->commitTransaction();

		    if ($templateData['isConfigUpgradeRequired']) {
			$ret = CoreModuleExtras::performConfigUpgrade($versions['core']);
		    }
		}
	    }
	    $this->resetL10Domain();
	    if ($ret->isError()) {
		$templateData['stackTrace'] = $ret->getAsHtml();
		$templateData['debug'] = $gallery->getDebugBuffer();
		$templateData['bodyFile'] = 'UpgradeCoreModuleError.html';
	    } else {
		$templateData['bodyFile'] = 'UpgradeCoreModuleSuccess.html';
		$this->setComplete(true);
		$this->_performedUpgrade = true;
	    }
	    $template->hideStatusBlock();
	} else {
	    $template->renderHeader();
	    if (($cmp = version_compare($versions['core'], $core->getVersion())) == 0) {
		$templateData['bodyFile'] = isset($this->_performedUpgrade)
		    ? 'UpgradeCoreModuleSuccess.html' : 'UpgradeCoreModuleUpToDate.html';
		$this->setComplete(true);
	    } else {
		/* The core module requires an upgrade */
		$templateData['bodyFile'] = 'UpgradeCoreModuleRequest.html';
		$this->setComplete(false);

		if ($cmp != -1) {
		    /*
		     * The new version is older than the installed version, don't
		     * let the user continue this silly attempt to break his G2
		     */
		    $templateData['isTryingToDowngrade'] = 1;
		}
	    }
	}

	$template->renderBodyAndFooter($templateData);
    }
}
?>
