Project

General

Profile

1 1457 Luisehahne
<?php
2
/**
3 1866 Luisehahne
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 1457 Luisehahne
 *
5 1866 Luisehahne
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9 1457 Luisehahne
 *
10 1866 Luisehahne
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17 1457 Luisehahne
 */
18
19 1866 Luisehahne
/**
20
 * upgrade.php
21
 *
22
 * @category     Module
23 1918 Luisehahne
 * @package      news
24 1866 Luisehahne
 * @subpackage   upgrade
25
 * @author       Dietmar Wöllbrink <dietmar.woellbrink@websitebaker.org>
26
 * @author       Werner v.d.Decken <wkl@isteam.de>
27
 * @copyright    Werner v.d.Decken <wkl@isteam.de>
28
 * @license      http://www.gnu.org/licenses/gpl.html   GPL License
29
 * @version      0.0.1
30
 * @revision     $Revision$
31
 * @link         $HeadURL$
32
 * @lastmodified $Date$
33
 * @since        File available since 17.01.2013
34
 * @description  xyz
35
 *
36
 */
37
38 1538 Luisehahne
/* -------------------------------------------------------- */
39 1587 darkviper
// Must include code to stop this file being accessed directly
40 1895 Luisehahne
if(!defined('WB_URL')) {
41
	require_once(dirname(dirname(dirname(__FILE__))).'/framework/globalExceptionHandler.php');
42
	throw new IllegalFileException();
43
}
44
45
46 1587 darkviper
/* -------------------------------------------------------- */
47 1457 Luisehahne
/* **** START UPGRADE ******************************************************* */
48 1895 Luisehahne
//if(!function_exists('mod_news_Upgrade'))
49
//{
50
	function mod_news_Upgrade($bDebug=false)
51 1457 Luisehahne
	{
52 2003 Luisehahne
	// set environment
53 1895 Luisehahne
		global $OK ,$FAIL;
54 2003 Luisehahne
		$aMandatoryTables = array('mod_news_posts','mod_news_groups','mod_news_comments','mod_news_settings');
55
		$oDb              = WbDatabase::getInstance();
56
		$oLang            = Translate::getinstance();
57
		$oReg             = WbAdaptor::getInstance();
58
		$msg              = array();
59
		$sCallingScript   = $_SERVER['SCRIPT_NAME'];
60
		$sPagesPath       = $oReg->AppPath.$oReg->PagesDir;
61
		$sPostsPath       = $sPagesPath.'/posts';
62
		$sModName         = basename(dirname(__FILE__));
63
		$oLang->enableAddon('modules\\'.$sModName);
64
	// check if upgrade startet by upgrade-script to echo a message
65
		$bGlobalStarted = preg_match('/upgrade\-script\.php$/', $sCallingScript);
66
/* --- check for missing tables, if true stop the upgrade ----------------------------- */
67
		$aMissingTables = UpgradeHelper::getMissingTables($aMandatoryTables);
68
		if( sizeof($aMissingTables) > 0){
69
			$msg[] = 'TABLE '.implode(' missing! '.$FAIL.'<br />TABLE ', $aMissingTables).' missing! '.$FAIL;
70
			$msg[] = 'WYSIWYG upgrade failed '.$FAIL;
71
			if($bGlobalStarted) {
72 1895 Luisehahne
				echo '<strong>'.implode('<br />',$msg).'</strong><br />';
73
			}
74 2003 Luisehahne
			$oLang->disableAddon();
75
			return ( ($bGlobalStarted==true ) ? $bGlobalStarted : $msg);
76
		}
77
/* --- check for database engine ------------------------------------------------------ */
78
		for($x=0; $x<sizeof($aMandatoryTables);$x++) {
79
			if(($sOldType = $oDb->getTableEngine($oDb->TablePrefix.$aMandatoryTables[$x]))) {
80
				if(('myisam' != strtolower($sOldType))) {
81
					if(!$oDb->query('ALTER TABLE `'.$oDb->TablePrefix.$aMandatoryTables[$x].'` Engine = \'MyISAM\' ')) {
82
						$msg[] = $oDb->get_error()." $FAIL";
83
					}else {
84
						$msg[] = 'TABLE `'.$oDb->TablePrefix.$aMandatoryTables[$x].'` changed to Engine = \'MyISAM\''." $OK";
85 1866 Luisehahne
					}
86
				} else {
87 2003 Luisehahne
					 $msg[] = 'TABLE `'.$oDb->TablePrefix.$aMandatoryTables[$x].'` has Engine = \'MyISAM\''." $OK";
88 1866 Luisehahne
				}
89 2003 Luisehahne
			} else {
90
				$msg[] = $oDb->get_error()." $FAIL";
91 1866 Luisehahne
			}
92 2003 Luisehahne
		}
93
/* --- create posts/ - directory if not exists ---------------------------------------- */
94
		if(!($bRetval = is_dir($sPostsPath)))
95
		{
96
		// /posts - dir missing
97
			if(is_writable($sPagesPath))
98
			{
99
			// try to create the directory
100
				$iOldUmask = umask(0) ;
101
				if(!($bRetval = mkdir($sPostsPath, $oReg->OctalDirMode, true)))
102
				{
103
					$msg[] = 'Not able to create directory "'.str_replace($oReg->AppPath, '', $sPostsPath).'". '.$FAIL;
104 1895 Luisehahne
				}
105 2003 Luisehahne
				umask($iOldUmask);
106
			}else
107
			{
108
				$msg[] = 'Directory "'.str_replace($oReg->AppPath, '', $sPostsPath).'" is not writeable.'." $FAIL";
109
				$bRetval = false;
110 1866 Luisehahne
			}
111 2003 Luisehahne
			if(!$bRetval) {
112
				$oLang->disableAddon();
113
				return ( ($bGlobalStarted==true ) ? $bGlobalStarted : $msg);
114 1457 Luisehahne
			}
115 2003 Luisehahne
		}
116
		$msg[] = 'Directory "'.str_replace($oReg->AppPath, '', $sPostsPath).'" exists.'." $OK";
117
/* --- create new db fields if `created_when` and `created_by` is missing ------------- */
118
		$doImportDate = 0;
119
		$aMsgList = array(
120
			0 => 'Datafield `'.$oDb->TablePrefix.'mod_news_posts`.`created_when` exists. '.$OK,
121
			1 => 'Datafield `'.$oDb->TablePrefix.'mod_news_posts`.`created_by` exists. '.$OK,
122
			2 => 'missing Datafield `'.$oDb->TablePrefix.'mod_news_posts`.`created_when`. '.$FAIL,
123
			3 => 'missing Datafield `'.$oDb->TablePrefix.'mod_news_posts`.`created_by`. '.$FAIL,
124
			4 => 'Datafield `'.$oDb->TablePrefix.'mod_news_posts`.`created_when` created. '.$OK,
125
			5 => 'Datafield `'.$oDb->TablePrefix.'mod_news_posts`.`created_by` created. '.$OK,
126
		);
127
		if($oDb->field_exists($oDb->TablePrefix.'mod_news_posts', 'created_when')) {
128
			$doImportDate |= pow(2, 0);
129
		}else {
130
			if($oDb->field_add($oDb->TablePrefix.'mod_news_posts', 'created_when', 'INT NOT NULL DEFAULT \'0\' AFTER `commenting`')) {
131
				$doImportDate |= (pow(2, 0) | pow(2, 4));
132
			}else {
133
				$doImportDate |= pow(2, 2);
134 1457 Luisehahne
			}
135 2003 Luisehahne
		}
136
		if($oDb->field_exists($oDb->TablePrefix.'mod_news_posts', 'created_by')) {
137
			$doImportDate |= pow(2, 1);
138
		}else {
139
			if($oDb->field_add($oDb->TablePrefix.'mod_news_posts', 'created_by', 'INT NOT NULL DEFAULT \'0\' AFTER `created_when`')) {
140
				$doImportDate |= (pow(2, 1) | pow(2, 5));
141
			}else {
142
				$doImportDate |= pow(2, 3);
143 1895 Luisehahne
			}
144 2003 Luisehahne
		}
145
		// build messages
146
		foreach($aMsgList as $iKey => $sMsgText) {
147
			if($doImportDate & pow(2, $iKey)) { $msg[] = $sMsgText; }
148
		}
149
		// break if not all fields exists now
150
		if(!($doImportDate & (pow(2, 0) | pow(2, 1)))) {
151
			$oLang->disableAddon();
152
			return ($bGlobalStarted ? $bGlobalStarted : $msg);
153
		}
154
/* --- import creation date from old style accessfiles -------------------------------- */
155
		// preset new fields `created_by` and `created_by` from existing values
156
		// if both fields are created new
157
		if($doImportDate & (pow(2, 4) | pow(2, 5)))
158
		{
159
		// first copy values inside the table and exchange all \ by / in field `link`
160
			$sql  = 'UPDATE `'.$oDb->TablePrefix.'mod_news_posts` '
161
			      . 'SET `created_by`=`posted_by`, '
162
			      .     '`created_when`=`posted_when` '
163
			      .     '`link`= REPLACE(`link`, \'\\\', \'/\')';
164
			$oDb->query($sql);
165
		// read Timestamps from old styled accessfiles
166
			$iCount = 0;
167
			$aMatches = glob($sPostsPath.'*'.$oReg->PageExtension);
168
			if(is_array($aMatches)) {
169
				foreach($aMatches as $sFile) {
170
					$sLink = str_replace($sPagesPath, '', str_replace('\\', '/', $sFile));
171
					$sLink = '/'.preg_replace('/'.preg_quote($oReg->PageExtension).'$/i', '', $sLink);
172
					$sql = 'UPDATE `'.$oDb->TablePrefix.'mod_news_posts` '
173
					     . 'SET `created_when`='.filemtime($sFile).' '
174
					     . 'WHERE `link`=\''.$sLink.'\'';
175
					if(($oDb->query('$sql'))) { $iCount++; }
176 1457 Luisehahne
				}
177
			}
178 2003 Luisehahne
			if($iCount) {
179
				$msg[] = 'Creation date of &#62;'.$iCount.'&#60; posts has been imported from old styled accessfiles.'." $OK";
180 1895 Luisehahne
			}
181 2003 Luisehahne
		// Check the validity of 'create-file-timestamp' and balance against 'posted-timestamp'
182
			$sql = 'UPDATE `'.$oDb->TablePrefix.'mod_news_posts` '
183 1990 darkviper
			     . 'SET `created_when`=`published_when` '
184
			     . 'WHERE `published_when`<`created_when`';
185 2003 Luisehahne
			$oDb->query($sql);
186
			$sql = 'UPDATE `'.$oDb->TablePrefix.'mod_news_posts` '
187 1990 darkviper
			     . 'SET `created_when`=`posted_when` '
188
			     . 'WHERE `published_when`=0 OR `published_when`>`posted_when`';
189 2003 Luisehahne
			$oDb->query($sql);
190 1895 Luisehahne
		}
191 2069 darkviper
/* --- insert SYSVAR placeholders and repair already existing ------------------------- */
192
        $sql = 'SELECT `post_id`, `content_long`, `content_short` '
193
             . 'FROM `'.$oDb->TablePrefix.'mod_news_posts` ';
194
        if (($oEntrySet = $oDb->doQuery($sql))) {
195
            $iRecords = 0;
196
            $iReplaced = 0;
197
            $aSearch = array( '/\{SYSVAR\:MEDIA_REL\}[\/\\\\]?/sU',
198
                              '/\{SYSVAR\:WB_URL\}[\/\\\\]?/sU',
199
                              '/(\{SYSVAR\:AppUrl\.MediaDir\})[\/\\\\]?/sU',
200
                              '/(\{SYSVAR\:AppUrl\})[\/\\\\]?/sU'
201
                            );
202
            $aReplace = array( '{SYSVAR:AppUrl.MediaDir}', '{SYSVAR:AppUrl}', '\1', '\1' );
203
            while (($aEntry = $oEntrySet->fetchRow(MYSQL_ASSOC))) {
204
                $iCount = 0;
205
                $aSubject = array($aEntry['content_long'], $aEntry['content_short']);
206
                $aNewContents = preg_replace($aSearch, $aReplace, $aSubject, -1, $iCount);
207
                if ($iCount > 0) {
208
                    $iReplaced += $iCount;
209
                    $sql = 'UPDATE `'.$oDb->TablePrefix.'mod_news_posts` '
210
                         . 'SET `content_long`=\''.$oDb->escapeString($aNewContents[0]).'\', '
211
                         .     '`content_short`=\''.$oDb->escapeString($aNewContents[1]).'\' '
212
                         . 'WHERE `post_id`='.$aEntry['post_id'];
213
                    $oDb->doQuery($sql);
214
                    $iRecords++;
215
                }
216
            }
217
            $msg[] = '['.$iRecords.'] records with ['.$iReplaced.'] SYSVAR placeholder(s) repaired'." $OK";
218
        }
219
        try {
220
            $sql  = 'UPDATE `'.$oDb->TablePrefix.'mod_news_posts` ';
221
            $sql .= 'SET `content` = REPLACE(`content`, \'"'.$oReg->AppPath.$oReg->MediaDir.'\', \'"{SYSVAR:AppPath.MediaDir}\')';
222
            $oDb->doQuery($sql);
223
            $msg[] = 'Change internal absolute Media links into SYSVAR placeholders'." $OK";
224
        } catch(WbDatabaseException $e) {
225
            $msg[] = ''.$oDb->get_error();
226
        }
227
        try {
228
            $sql  = 'UPDATE `'.$oDb->TablePrefix.'mod_news_posts` ';
229
            $sql .= 'SET `content` = REPLACE(`content`, \'"'.$oReg->AppPath.'\', \'"{SYSVAR:AppPath}\')';
230
            $oDb->doQuery($sql);
231
            $msg[] = 'Change internal absolute links into SYSVAR placeholders'." $OK";
232
        } catch(WbDatabaseException $e) {
233
            $msg[] = ''.$oDb->get_error();
234
        }
235 2003 Luisehahne
/* --- rebuild all access files ------------------------------------------------------- */
236
		$aReport = array('FilesDeleted'=>0,'FilesCreated'=>0,);
237
		$oReorg = new m_news_Reorg(ModuleReorgAbstract::LOG_EXTENDED);
238
		$oReorg->execute(); // show details
239
		$aReport = $oReorg->getReport();
240
		unset($oReorg);
241
/* --- for running from upgrade-script.php only --------------------------------------- */
242
		if($bGlobalStarted && $bDebug) {
243
			echo '<strong>'.implode('<br />',$msg).'</strong><br />';
244
		}
245
/* ------------------------------------------------------------------------------------ */
246 1985 Luisehahne
		$msg[] = '<strong>Number of new formated access files: '.$aReport['FilesCreated'].'</strong>';
247
		$msg[] = "<strong>News upgrade successfull finished</strong>";
248 2003 Luisehahne
		if($bGlobalStarted) {
249 1895 Luisehahne
			echo "<strong>News upgrade successfull finished $OK</strong><br />";
250
		}
251 2003 Luisehahne
		$oLang->disableAddon();
252
		return ($bGlobalStarted ? $bGlobalStarted : $msg);
253 1457 Luisehahne
	}
254 1765 Luisehahne
// end mod_news_Upgrade
255 1477 Luisehahne
256 1538 Luisehahne
// ------------------------------------
257 1985 Luisehahne
// Don't show the messages twice
258 2003 Luisehahne
	$bDebugModus = ((isset($bDebugModus)) ? $bDebugModus : false);
259
	if( is_array($msg = mod_news_Upgrade($bDebugModus)) ) {
260 1985 Luisehahne
// only show if manuell upgrade
261 2003 Luisehahne
		echo implode('<br />', $msg).'<br />';
262
	}
263 1866 Luisehahne
/* **** END UPGRADE ********************************************************* */