Index: branches/2.8.x/CHANGELOG
===================================================================
--- branches/2.8.x/CHANGELOG	(revision 2011)
+++ branches/2.8.x/CHANGELOG	(revision 2012)
@@ -11,6 +11,10 @@
 ! = Update/Change
 ===============================================================================
 
+03 Nov-2013 Build 2012 Manuela v.d.Decken(DarkViper)
+! upgrade-script:: now repairs mismatched pagelinks from older versions
+! upgrade-script:: /temp/TranslationTable/cache/ will be deleted now during upgrade
++ UpgradeHelper::checkSetupFiles() the old config.php will be changed into setup.ini.php now.
 03 Nov-2013 Build 2011 Manuela v.d.Decken(DarkViper)
 ! WbDatabase added method fetchObject()
 ! WbAdaptor little typofixes
Index: branches/2.8.x/wb/upgrade-script.php
===================================================================
--- branches/2.8.x/wb/upgrade-script.php	(revision 2011)
+++ branches/2.8.x/wb/upgrade-script.php	(revision 2012)
@@ -34,6 +34,8 @@
  * @deprecated   
  * @description  xyz
  */
+include_once(dirname(__FILE__).'/framework/UpgradeHelper.php');
+
 // --- delete fatal disturbing files before upgrade starts -------------------------------
 $aPreDeleteFiles = array(
 // list of files
@@ -58,11 +60,8 @@
 	}
 	if($sMsg) {
 	// stop script if there's an error occured
-		$sMsg = 'Fatal error occured during initial startup.<br /><br />'.PHP_EOL.$sMsg
-		      . '<br />'.PHP_EOL.'Please delete all of the files above manually and '
-		      . 'then <a href="http://'.$_SERVER["HTTP_HOST"].$_SERVER["SCRIPT_NAME"].'" '
-		      . 'title="restart">klick here to restart the upgrade-script</a>.<br />'.PHP_EOL;
-		die($sMsg);
+		$sMsg = $sMsg.'<br />'.PHP_EOL.'Please delete all of the files above manually!';
+		UpgradeHelper::dieWithMessage($sMsg);
 	}
 }
 unset($aPreDeleteFiles);
@@ -70,12 +69,12 @@
 // ---------------------------------------------------------------------------------------
 // Include config file
 $config_file = dirname(__FILE__).'/config.php';
-if(file_exists($config_file) && !defined('WB_URL'))
-{
-	require($config_file);
+if (is_readable($config_file) && !defined('WB_URL')) {
+	require_once($config_file);
 }
-if(!class_exists('admin', false))
-{ 
+UpgradeHelper::checkSetupFiles($config_file);
+
+if (!class_exists('admin', false)) {
 	include(WB_PATH.'/framework/class.admin.php');
 }
 $admin = new admin('Addons', 'modules', false, false);
@@ -915,6 +914,15 @@
      */
 	echo '<h4>Upgrade pages directory '.PAGES_DIRECTORY.'/  access files</h4>';
 
+	/**********************************************************
+	 * Repair inconsistent PageTree
+	 */
+	$iCount = UpgradeHelper::sanitizePagesTreeLinkStructure();
+	if (false === $iCount) {
+		echo '<span><strong>Repair PageTree links </strong></span> '.$FAIL.'<br />';
+	} else {
+		echo '<span><strong>'.$iCount.' PageTree links repaired.</strong></span> '.$OK.'<br />';
+	}
     /**********************************************************
      *  - Reformat/rebuild all existing access files
      */
@@ -1153,7 +1161,6 @@
 		closedir($handle);
 	}
 	echo '<strong><span>'.$iLoaded.' Templates reloaded,</span> found '.$iFound.' directories in folder /templates/</strong><br />';
-
 	$iFound = 0;
 	$iLoaded = 0;
 	////delete languages
@@ -1169,6 +1176,14 @@
 		closedir($handle);
 	}
 	echo '<strong><span>'.$iLoaded.' Languages reloaded,</span> found '.$iFound.' files in folder /languages/</strong><br />';
+	$sTransCachePath = WB_PATH.'/temp/TranslationTable/cache/';
+	if (is_writeable($sTransCachePath)) {
+		if (rm_full_dir($sTransCachePath, true)) {
+			echo '<strong><span>Translation Cache cleaned</span></strong> '.$OK.'<br />';
+		} else {
+			echo '<strong><span>Clean Translation Cache</span></strong> '.$FAIL.'<br />';
+		}
+	}
 	echo '</div>';
 
 /**********************************************************
Index: branches/2.8.x/wb/admin/interface/version.php
===================================================================
--- branches/2.8.x/wb/admin/interface/version.php	(revision 2011)
+++ branches/2.8.x/wb/admin/interface/version.php	(revision 2012)
@@ -51,5 +51,5 @@
 
 // check if defined to avoid errors during installation (redirect to admin panel fails if PHP error/warnings are enabled)
 if(!defined('VERSION')) define('VERSION', '2.8.3');
-if(!defined('REVISION')) define('REVISION', '2011');
+if(!defined('REVISION')) define('REVISION', '2012');
 if(!defined('SP')) define('SP', '');
Index: branches/2.8.x/wb/framework/UpgradeHelper.php
===================================================================
--- branches/2.8.x/wb/framework/UpgradeHelper.php	(revision 2011)
+++ branches/2.8.x/wb/framework/UpgradeHelper.php	(revision 2012)
@@ -45,10 +45,10 @@
 		$aTablesList = array_flip($aTablesList);
 		$oDb = WbDatabase::getInstance();
 		$sPattern = addcslashes ( $oDb->TablePrefix, '%_' );
-		if(($oTables = $oDb->query( 'SHOW TABLES LIKE "'.$sPattern.'%"'))) {
-			while($aTable = $oTables->fetchRow(MYSQL_NUM)) {
+		if (($oTables = $oDb->query( 'SHOW TABLES LIKE "'.$sPattern.'%"'))) {
+			while ($aTable = $oTables->fetchRow(MYSQL_NUM)) {
 				$sTable =  preg_replace('/^'.preg_quote($oDb->TablePrefix, '/').'/s', '', $aTable[0]);
-				if(isset($aTablesList[$sTable])) {
+				if (isset($aTablesList[$sTable])) {
 					unset($aTablesList[$sTable]);
 				}
 			}
@@ -64,7 +64,199 @@
 	{
 		return self::getMissingTables($aTablesList);
 	}
+/**
+ * Sanitize and repair Pagetree links
+ * @return boolean|int number of updated records or false on error
+ */
+	public static function sanitizePagesTreeLinkStructure()
+	{
+		$oDb = WbDatabase::getInstance();
+		$iCounter = 0;
+		$aPages = array();
+		try {
+			$sql = 'SELECT `page_id`, `link`, `page_trail` '
+				 . 'FROM `'.$oDb->TablePrefix.'pages` '
+				 . 'ORDER BY `page_id`';
+			$oPages = $oDb->doQuery($sql);
+			// read 'page_id', 'link' and 'page_trail' from all pages
+			while ($aPage = $oPages->fetchRow(MYSQL_ASSOC)) {
+				// extact filename only from complete link
+				$aPages[$aPage['page_id']]['filename'] = preg_replace('/.*?\/([^\/]*$)/', '\1', $aPage['link']);
+				$aPages[$aPage['page_id']]['page_trail'] = $aPage['page_trail'];
+				$aPages[$aPage['page_id']]['link'] = $aPage['link'];
+			}
+			foreach ($aPages as $iKey=>$aRecord) {
+			// iterate all pages
+				$aTmp = array();
+				$aIds = explode(',', $aRecord['page_trail']);
+				// rebuild link from filenames using page_trail
+				foreach($aIds as $iId) {
+					$aTmp[] = $aPages[$iId]['filename'];
+				}
+				$sLink = '/'.implode('/', $aTmp);
+				if ($sLink != $aPages[$iKey]['link']) {
+				// update page if old link is different to new generated link
+					$sql = 'UPDATE `'.$oDb->TablePrefix.'pages` '
+						 . 'SET `link`=\''.$sLink.'\' '
+						 . 'WHERE `page_id`='.$iKey;
+					$oDb->doQuery($sql);
+					$iCounter++;
+				}
+			}
+		} catch(WbDatabaseException $e) {
+			return false;
+		}
+		return $iCounter;
+	}
+/**
+ *
+ * @param string $sMsg Message to show
+ */
+	public static function dieWithMessage($sMsg)
+	{
+		$sMsg = 'Fatal error occured during initial startup.<br /><br />'.PHP_EOL.$sMsg
+			  . '<br />'.PHP_EOL.'Please correct these errors and then '
+			  . '<a href="http://'.$_SERVER["HTTP_HOST"].$_SERVER["SCRIPT_NAME"].'" '
+			  . 'title="restart">klick here to restart the upgrade-script</a>.<br />'.PHP_EOL;
+		die($sMsg);
+	}
+/**
+ *
+ * @param string $sAppPath path to the current installation
+ * @return boolean
+ */
+	public static function checkSetupFiles($sAppPath)
+	{
+		if (defined('DB_PASSWORD')) {
+		// old config.php is active
+			if (!is_writable($sAppPath.'config.php') || !is_writable($sAppPath.'config.php.new') || !is_writable($sAppPath.'setup.ini.php.new'))
+			{
+			// stop script if there's an error occured
+				$sMsg = 'Following files must exist and be writable to run upgrade:<br />'
+				      . '<ul><li>config.php</li>'.(file_exists($sAppPath.'config.php.new') ? '<li>config.php.new</li>' : '')
+				      . '<li>setup.ini.php.new</li></ul>';
+				self::dieWithMessage($sMsg);
+			} else { // ok, let's change the files now!
+				if (file_exists($aAppPath.'setup.ini.php')) {
+				// if 'setup.ini.php' exists
+					if (!is_writeable($aAppPath.'setup.ini.php')) {
+					// but it's not writable
+						$sMsg = 'The file \'setup.ini.php\' already exists but is not writeable!';
+						self::dieWithMessage($sMsg);
+					}
+				} else {
+				// try to rename 'setup.ini.php.new' into 'setup.ini.php'
+					if (!rename($aAppPath.'setup.ini.php.new', $aAppPath.'setup.ini.php')) {
+						$sMsg = 'Can not rename \''.$aAppPath.'setup.ini.php.new\' into \''.$aAppPath.'setup.ini.php\' !!<br />'
+						      . 'Create an empty file \''.$aAppPath.'setup.ini.php\' and make it writeable for the server!';
+						self::dieWithMessage($sMsg);
+					}
+				}
+			// now first read constants from old config.php
+				$sContent = file_get_contents($sAppPath.'config.php');
+				$sPattern = '/^\s*define\s*\(\s*([\'\"])(.*?)\1\s*,.*;\s*$/m';
+				if (preg_match_all($sPattern, $sContent, $aMatches)) {
+				// get all already defined consts
+					$aAllConsts = get_defined_constants(true);
+					$aSetupConsts = array();
+				// collect the needed values from defined consts
+					foreach($aMatches[2] as $sConstName) {
+						$aSetupConsts[$sConstName] = (isset($aAllConsts['user'][$sConstName]) ? $aAllConsts['user'][$sConstName] : '');
+					}
+				// try to sanitize available values
+					$aSetupConsts['DB_TYPE'] = ((isset($aSetupConsts['DB_TYPE']) && $aSetupConsts['DB_TYPE'] != '') ? $aSetupConsts['DB_TYPE'] : 'mysql');
+					$aSetupConsts['DB_PORT'] = ((isset($aSetupConsts['DB_PORT']) && $aSetupConsts['DB_PORT'] != '') ? $aSetupConsts['DB_PORT'] : '3306');
+					$aSetupConsts['DB_CHARSET'] = (isset($aSetupConsts['DB_CHARSET']) ? $aSetupConsts['DB_CHARSET'] : '');
+					$aSetupConsts['DB_CHARSET'] = preg_replace('/[^0-9a-z]*/i', '', $aSetupConsts['DB_CHARSET']);
+					$aSetupConsts['TABLE_PREFIX'] = (isset($aSetupConsts['TABLE_PREFIX']) ? $aSetupConsts['TABLE_PREFIX'] : '');
+					$aSetupConsts['ADMIN_DIRECTORY'] = trim(str_replace('\\', '/', preg_replace('/^'.preg_quote(WB_PATH, '/').'/', '', ADMIN_PATH)), '/').'/';
+					$aSetupConsts['WB_URL'] = rtrim(str_replace('\\', '/', WB_URL), '/').'/';
+				// Try and write settings to config file
+					if (writeSetupIni($sAppPath, $aSetupConsts)) {
+						if (writeConfig($sAppPath)) {
+							return true;
+						} else {
+							$sMsg ='Error writing \''.$sAppPath.'config.php\'!';
+						}
+					} else {
+						$sMsg ='Error writing \''.$sAppPath.'setup.ini.php\'!';
+					}
+				} else {
+					$sMsg = 'No valid content found in \''.$sAppPath.'config.php\'!';
+				}
+				self::dieWithMessage($sMsg);
+			}
+		} else {
+			$sFileContent = file_get_contents($sAppPath.'config.php');
+			if (preg_match('/\s*define\s*\(.*\)\s*;/i', $sFileContent)) {
+			// if config.php does not contain any defines
+				if (is_writable($sAppPath.'config.php')) {
+				// overwrite config.php with default content for compatibility
+					if (self::writeConfig($sAppPath.'config.php')) {
+						return true;
+					} else  {
+						$sMsg ='Error writing \''.$sAppPath.'config.php\'!';
+					}
+				} else {
+					$sMsg ='File is not writable \''.$sAppPath.'config.php\'!';
+				}
+				self::dieWithMessage($sMsg);
+			}
+		}
+	}
+/**
+ *
+ * @param string $sAppPath the path where setup.ini.php is located
+ * @param array  $aSetupValues
+ * @return boolean
+ */
+	public static function writeSetupIni($sAppPath, array $aSetupValues)
+	{
+		$sSetupContent =
+			";<?php die('sorry, illegal file access'); ?>#####\n"
+		   .";################################################\n"
+		   ."; WebsiteBaker configuration file\n"
+		   ."; auto generated ".date('Y-m-d h:i:s A e ')."\n"
+		   .";################################################\n"
+		   ."[Constants]\n"
+		   ."DEBUG   = false\n"
+		   ."AppUrl  = \"".$aSetupValues['WB_URL']."\"\n"
+		   ."AcpDir  = \"".$aSetupValues['ADMIN_DIRECTORY']."\"\n"
+		   .";##########\n"
+		   ."[DataBase]\n"
+		   ."type    = \"".$aSetupValues['DB_TYPE']."\"\n"
+		   ."user    = \"".$aSetupValues['DB_USERNAME']."\"\n"
+		   ."pass    = \"".$aSetupValues['DB_PASSWORD']."\"\n"
+		   ."host    = \"".$aSetupValues['DB_HOST']."\"\n"
+		   ."port    = \"".$aSetupValues['DB_PORT']."3306\"\n"
+		   ."name    = \"".$aSetupValues['DB_NAME']."\"\n"
+		   ."charset = \"".$aSetupValues['DB_CHARSET']."\"\n"
+		   ."table_prefix = \"".$aSetupValues['TABLE_PREFIX']."\"\n"
+		   .";\n"
+		   .";################################################\n";
+		$sSetupFile = $sAppPath.'setup.ini.php';
+		if (file_put_contents($sSetupFile, $sSetupContent)) {
+			return true;
+		}
+		return false;
+	}
+/**
+ * 
+ * @param string $sAppPath the path where config.php is located
+ * @return boolean
+ */
+	public static function writeConfig($sAppPath)
+	{
+		$sConfigContent = "<?php\n"
+			."/* this file is for backward compatibility only */\n"
+			."/* never put any code in this file! */\n"
+			."include_once(dirname(__FILE__).'/framework/initialize.php');\n";
+		$sConfigFile = $sAppPath.'config.php';
+		if (file_put_contents($sConfigFile, $sConfigContent)) {
+			return true;
+		}
+		return false;
+	}
 
-
 } // end of class UpgradeHelper
 
