Project

General

Profile

1 1538 Luisehahne
<?php
2
/**
3
 *
4
 * @category        modules
5
 * @package         wysiwyg
6
 * @author          WebsiteBaker Project
7 1576 darkviper
 * @copyright       2009-2012, Website Baker Org. e.V.
8 1890 Luisehahne
 * @link            http://www.websitebaker2.org/
9 1538 Luisehahne
 * @license         http://www.gnu.org/licenses/gpl.html
10 1652 Luisehahne
 * @platform        WebsiteBaker 2.8.3
11 1538 Luisehahne
 * @requirements    PHP 5.2.2 and higher
12 1890 Luisehahne
 * @version         $Id$
13
 * @filesource      $HeadURL$
14 1538 Luisehahne
 * @lastmodified    $Date$
15
 *
16
 */
17
/* -------------------------------------------------------- */
18 1576 darkviper
// Must include code to stop this file being accessed directly
19
if(!defined('WB_PATH')) {
20 1652 Luisehahne
	require_once(dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
21 1576 darkviper
	throw new IllegalFileException();
22 1538 Luisehahne
}
23
/* -------------------------------------------------------- */
24 1754 Luisehahne
25 1890 Luisehahne
	function mod_wysiwyg_upgrade ($bDebug=false) {
26
		global $OK ,$FAIL;
27 2058 darkviper
		$oDb = WbDatabase::getInstance();
28
        $oReg = WbAdaptor::getInstance();
29 1890 Luisehahne
		$msg = array();
30 1875 Luisehahne
		$callingScript = $_SERVER["SCRIPT_NAME"];
31
		// check if upgrade startet by upgrade-script to echo a message
32
		$tmp = 'upgrade-script.php';
33
		$globalStarted = substr_compare($callingScript, $tmp,(0-strlen($tmp)),strlen($tmp)) === 0;
34 1890 Luisehahne
// check for missing tables, if true stop the upgrade
35 1875 Luisehahne
		$aTable = array('mod_wysiwyg');
36 1890 Luisehahne
		$aPackage = UpgradeHelper::existsAllTables($aTable);
37
		if( sizeof($aPackage) > 0){
38
			$msg[] =  'TABLE '.implode(' missing! '.$FAIL.'<br />TABLE ',$aPackage).' missing! '.$FAIL;
39
			$msg[] = 'WYSIWYG upgrade failed '." $FAIL";
40
			if($globalStarted) {
41
				echo '<strong>'.implode('<br />',$msg).'</strong><br />';
42
			}
43
			return ( ($globalStarted==true ) ? $globalStarted : $msg);
44
		} else {
45
			for($x=0; $x<sizeof($aTable);$x++) {
46 2058 darkviper
				if(($sOldType = $oDb->getTableEngine($oDb->TablePrefix.$aTable[$x]))) {
47 1890 Luisehahne
					if(('myisam' != strtolower($sOldType))) {
48 2058 darkviper
						if(!$oDb->query('ALTER TABLE `'.$oDb->TablePrefix.$aTable[$x].'` Engine = \'MyISAM\' ')) {
49
							$msg[] = $oDb->get_error();
50 1890 Luisehahne
						} else{
51 2058 darkviper
							$msg[] = 'TABLE `'.$oDb->TablePrefix.$aTable[$x].'` changed to Engine = \'MyISAM\''." $OK";
52 1890 Luisehahne
						}
53
					} else {
54 2058 darkviper
						 $msg[] = 'TABLE `'.$oDb->TablePrefix.$aTable[$x].'` has Engine = \'MyISAM\''." $OK";
55 1875 Luisehahne
					}
56
				} else {
57 2058 darkviper
					$msg[] = $oDb->get_error();
58 1875 Luisehahne
				}
59 1890 Luisehahne
			}
60
// add change or missing index
61 2058 darkviper
			$sTable = $oDb->TablePrefix.'mod_wysiwyg';
62
			if($oDb->index_exists($sTable, 'PRIMARY')) {
63
				$sql = 'ALTER TABLE `'.$oDb->DbName.'`.`'.$sTable.'` DROP PRIMARY KEY';
64
				if(!$oDb->query($sql)) {
65
					$msg[] = ''.$oDb->get_error();
66 1890 Luisehahne
				}
67
			}
68 2058 darkviper
			if(!$oDb->index_add($sTable, '', 'section_id', 'PRIMARY')) {
69
				$msg[] = ''.$oDb->get_error();
70 1875 Luisehahne
			} else {
71 1890 Luisehahne
				$msg[] = 'Create PRIMARY KEY ( `section_id` )'." $OK";
72 1875 Luisehahne
			}
73 1890 Luisehahne
// change table structure
74 2058 darkviper
			$sTable = $oDb->TablePrefix.'mod_wysiwyg';
75 1890 Luisehahne
			$sDescription = 'LONGTEXT NOT NULL';
76
			$sFieldName = 'text';
77 2058 darkviper
			if(!$oDb->field_modify($sTable,$sFieldName,$sDescription)) {
78
				$msg[] = ''.$oDb->get_error();
79 1890 Luisehahne
			} else {
80
				$msg[] = 'Field ( `text` ) description has been changed successfully'." $OK";
81 1875 Luisehahne
			}
82 1890 Luisehahne
			$sFieldName = 'content';
83 2058 darkviper
			if(!$oDb->field_modify($sTable,$sFieldName,$sDescription)) {
84
				$msg[] = ''.$oDb->get_error();
85 1890 Luisehahne
			} else {
86
				$msg[] = 'Field ( `content` ) description has been changed successfully'." $OK";
87
			}
88 2058 darkviper
// change internal absolute links into Sysvar placeholders and repair already existing entries
89
			$sTable = $oDb->TablePrefix.'mod_wysiwyg';
90
            $sql = 'SELECT `section_id`, `content` FROM `'.$oDb->TablePrefix.'mod_wysiwyg` ';
91
            if (($oEntrySet = $oDb->doQuery($sql))) {
92
                $iRecords = 0;
93
                $iReplaced = 0;
94
                $aSearch = array( '/\{SYSVAR\:MEDIA_REL\}[\/\\\\]?/sU',
95
                                  '/\{SYSVAR\:WB_URL\}[\/\\\\]?/sU',
96
                                  '/(\{SYSVAR\:AppUrl\.MediaDir\})[\/\\\\]?/sU',
97
                                  '/(\{SYSVAR\:AppUrl\})[\/\\\\]?/sU'
98
                                );
99
                $aReplace = array( '{SYSVAR:AppUrl.MediaDir}', '{SYSVAR:AppUrl}', '\1', '\1' );
100
                while (($aEntry = $oEntrySet->fetchRow(MYSQL_ASSOC))) {
101
                    $iCount = 0;
102
                    $aEntry['content'] = preg_replace($aSearch, $aReplace, $aEntry['content'], -1, $iCount);
103
                    if ($iCount > 0) {
104
                        $iReplaced += $iCount;
105
                        $sql = 'UPDATE `'.$oDb->TablePrefix.'mod_wysiwyg` '
106
                             . 'SET `content`=\''.$oDb->escapeString($aEntry['content']).'\' '
107
                             . 'WHERE `section_id`='.$aEntry['section_id'];
108
                        $oDb->doQuery($sql);
109
                        $iRecords++;
110
                    }
111
                }
112
                $msg[] = '['.$iRecords.'] records with ['.$iReplaced.'] SYSVAR placeholder(s) repaired'." $OK";
113
            }
114
            try {
115
                $sql  = 'UPDATE `'.$sTable.'` ';
116
                $sql .= 'SET `content` = REPLACE(`content`, \'"'.$oReg->AppPath.$oReg->MediaDir.'\', \'"{SYSVAR:AppPath.MediaDir}\')';
117
                $oDb->doQuery($sql);
118
				$msg[] = 'Change internal absolute Media links into SYSVAR placeholders'." $OK";
119
            } catch(WbDatabaseException $e) {
120
				$msg[] = ''.$oDb->get_error();
121
            }
122
            try {
123
                $sql  = 'UPDATE `'.$sTable.'` ';
124
                $sql .= 'SET `content` = REPLACE(`content`, \'"'.$oReg->AppPath.'\', \'"{SYSVAR:AppPath}\')';
125
                $oDb->doQuery($sql);
126
				$msg[] = 'Change internal absolute links into SYSVAR placeholders'." $OK";
127
            } catch(WbDatabaseException $e) {
128
				$msg[] = ''.$oDb->get_error();
129
            }
130 1890 Luisehahne
// only for $callingScript upgrade-script.php
131
			if($globalStarted) {
132
				if($bDebug) {
133
					echo '<strong>'.implode('<br />',$msg).'</strong><br />';
134 1875 Luisehahne
				}
135
			}
136
		}
137 1890 Luisehahne
		$msg[] = 'WYSIWYG upgrade successfull finished'." $OK";
138
		if($globalStarted) {
139
			echo "<strong>WYSIWYG upgrade successfull finished $OK</strong><br />";
140
		}
141
		return ( ($globalStarted==true ) ? $globalStarted : $msg);
142 1875 Luisehahne
	}
143 1890 Luisehahne
// ------------------------------------
144
$bDebugModus = ((isset($bDebugModus)) ? $bDebugModus : false);
145
if( is_array($msg = mod_wysiwyg_upgrade($bDebugModus)) ) {
146
	echo '<strong>'.implode('<br />',$msg).'</strong><br />';
147 1576 darkviper
}