Project

General

Profile

« Previous | Next » 

Revision 2003

Added by Dietmar almost 11 years ago

! /modules/news/upgrade.php optimize add and modify field tables, rebuild message
! /modules/news/modify_post.php and modify_post.php check posts folder if not exists then create
! /upgrade-script.php add ModuleReorgAbstract calling the Reorg Accessfiles from modules

View differences:

upgrade.php
49 49
//{
50 50
	function mod_news_Upgrade($bDebug=false)
51 51
	{
52
	// set environment
52 53
		global $OK ,$FAIL;
53
		$database=WbDatabase::getInstance();
54
		$mLang = Translate::getinstance();
55
		$sModName = basename(dirname(__FILE__));
56
		$mLang->enableAddon('modules\\'.$sModName);
57
		$msg    = array();
58
		$callingScript = $_SERVER["SCRIPT_NAME"];
59
// check if upgrade startet by upgrade-script to echo a message
60
		$tmp = 'upgrade-script.php';
61
		$globalStarted = substr_compare($callingScript, $tmp,(0-strlen($tmp)),strlen($tmp)) === 0;
62
// check for missing tables, if true stop the upgrade
63
		$aTable = array('mod_news_posts','mod_news_groups','mod_news_comments','mod_news_settings');
64
		$aPackage = UpgradeHelper::existsAllTables($aTable);
65
		if( sizeof($aPackage) > 0){
66
			$msg[] =  'TABLE '.implode(' missing! '.$FAIL.'<br />TABLE ',$aPackage).' missing! '.$FAIL;
67
			$msg[] = "WYSIWYG upgrade failed $FAIL";
68
			if($globalStarted) {
54
		$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) {
69 72
				echo '<strong>'.implode('<br />',$msg).'</strong><br />';
70 73
			}
71
			$mLang->disableAddon();
72
			return ( ($globalStarted==true ) ? $globalStarted : $msg);
73
		} else {
74
			/**
75
			 * check database engine
76
			 */
77
			 for($x=0; $x<sizeof($aTable);$x++) {
78
				if(($sOldType = $database->getTableEngine($database->TablePrefix.$aTable[$x]))) {
79
					if(('myisam' != strtolower($sOldType))) {
80
						if(!$database->query('ALTER TABLE `'.$database->TablePrefix.$aTable[$x].'` Engine = \'MyISAM\' ')) {
81
							$msg[] = $database->get_error()." $FAIL";
82
						} else{
83
							$msg[] = 'TABLE `'.$database->TablePrefix.$aTable[$x].'` changed to Engine = \'MyISAM\''." $OK";
84
						}
85
					} else {
86
						 $msg[] = 'TABLE `'.$database->TablePrefix.$aTable[$x].'` has Engine = \'MyISAM\''." $OK";
74
			$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";
87 85
					}
88 86
				} else {
89
					$msg[] = $database->get_error()." $FAIL";
87
					 $msg[] = 'TABLE `'.$oDb->TablePrefix.$aMandatoryTables[$x].'` has Engine = \'MyISAM\''." $OK";
90 88
				}
89
			} else {
90
				$msg[] = $oDb->get_error()." $FAIL";
91 91
			}
92

  
93
			$sPagesPath = WB_PATH.PAGES_DIRECTORY;
94
			$sPostsPath = $sPagesPath.'/posts';
95
// create /posts/ - directory if not exists
96
			if(is_writable($sPagesPath)) {
97
				if(!($bRetval = is_dir($sPostsPath))) {
98
					$iOldUmask = umask(0) ;
99
					// sanitize directory mode to 'o+rwx/g+x/u+x' and create path
100
					$bRetval = mkdir($sPostsPath, (OCTAL_DIR_MODE |0711), true); 
101
					umask($iOldUmask);
92
		}
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;
102 104
				}
103
				if($bRetval) {
104
					$msg[] = 'Directory "'.PAGES_DIRECTORY.'/posts/" already exists or created.'." $OK";
105
				}else {
106
					$msg[] = ($mLang->MESSAGE_PAGES_CANNOT_CREATE_ACCESS_FILE)." $FAIL";
107
				}
108
			}else {
109
					$msg[] = ($mLang->MESSAGE_PAGES_CANNOT_CREATE_ACCESS_FILE)." $FAIL";
105
				umask($iOldUmask);
106
			}else
107
			{
108
				$msg[] = 'Directory "'.str_replace($oReg->AppPath, '', $sPostsPath).'" is not writeable.'." $FAIL";
109
				$bRetval = false;
110 110
			}
111
	// check if new fields must be added
112
			$doImportDate = true;
113
			if(!$database->field_exists($database->TablePrefix.'mod_news_posts', 'created_when')) {
114
				if(!$database->field_add($database->TablePrefix.'mod_news_posts', 'created_when',
115
				                        'INT NOT NULL DEFAULT \'0\' AFTER `commenting`')) {
116
					$msg[] = $mLang->MESSAGE_RECORD_MODIFIED_FAILED." $FAIL";
117
				} else {
118
					$msg[] = 'TABLE `'.$database->TablePrefix.'mod_news_posts` Datafield `created_when` added.'." $OK";
119
				}
120
			} else { 
121
				$msg[] = 'TABLE `'.$database->TablePrefix.'mod_news_posts` Datafield `created_when` already exists.'." $OK";
122
				$doImportDate = false; 
111
			if(!$bRetval) { 
112
				$oLang->disableAddon();
113
				return ( ($bGlobalStarted==true ) ? $bGlobalStarted : $msg);
123 114
			}
124

  
125
			if(!$database->field_exists($database->TablePrefix.'mod_news_posts', 'created_by')) {
126
				if(!$database->field_add($database->TablePrefix.'mod_news_posts', 'created_by',
127
				                        'INT NOT NULL DEFAULT \'0\' AFTER `created_when`')) {
128
					$msg[] = $mLang->MESSAGE_RECORD_MODIFIED_FAILED." $FAIL";
129
				} else {
130
					$msg[] = 'TABLE `'.$database->TablePrefix.'mod_news_posts` Datafield `created_by` added.'." $OK";
131
				}
132
			} else { 
133
				$msg[] = 'TABLE `'.$database->TablePrefix.'mod_news_posts` Datafield `created_by` already exists.'." $OK";
134
				$doImportDate = false; 
115
		}
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);
135 134
			}
136
// preset new fields `created_by` and `created_by` from existing values
137
			if($doImportDate) {
138
				$sql  = 'UPDATE `'.$database->TablePrefix.'mod_news_posts` ';
139
				$sql .= 'SET `created_by`=`posted_by`, `created_when`=`posted_when`';
140
				$database->query($sql);
135
		}
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);
141 143
			}
142
	if($doImportDate) {
143
	/**
144
	 * rebuild news post folder
145
	 */
146
//	$array = rebuildFolderProtectFile($sPostsPath);
147
		// now iterate through all existing accessfiles,
148
		// write its creation date into database
149
			if(is_writable($sPostsPath)) {
150
				$oDir = new DirectoryIterator($sPostsPath);
151
				$count = 0;
152
				foreach ($oDir as $fileinfo)
153
				{
154
					$fileName = $fileinfo->getFilename();
155
					if((!$fileinfo->isDot()) &&
156
					   ($fileName != 'index.php') &&
157
					   (substr_compare($fileName,PAGE_EXTENSION,(0-strlen(PAGE_EXTENSION)),strlen(PAGE_EXTENSION)) === 0)
158
					  )
159
					{
160
					// save creation date from old accessfile
161
						if($doImportDate) {
162
							$link = '/posts/'.preg_replace('/'.preg_quote(PAGE_EXTENSION).'$/i', '', $fileinfo->getFilename());
163
							$sql  = 'UPDATE `'.$database->TablePrefix.'mod_news_posts` ';
164
							$sql .= 'SET `created_when`='.$fileinfo->getMTime().' ';
165
							$sql .= 'WHERE `link`=\''.$link.'\'';
166
							if($database->query($sql)) {
167
								// delete old access file
168
								unlink($fileinfo->getPathname());
169
								$count++;
170
							}
171
						}
172
					}
144
		}
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++; }
173 176
				}
174
				unset($oDir);
175 177
			}
176
			if($count > 0) {
177
				$msg[] = 'Save date of creation from '.$count.' old accessfiles and delete these files.'." $OK";
178
			if($iCount) {
179
				$msg[] = 'Creation date of &#62;'.$iCount.'&#60; posts has been imported from old styled accessfiles.'." $OK";
178 180
			}
179
	}
180
// ************************************************
181
// Check the validity of 'create-file-timestamp' and balance against 'posted-timestamp'
182
			$sql = 'UPDATE `'.$database->TablePrefix.'mod_news_posts` '
181
		// Check the validity of 'create-file-timestamp' and balance against 'posted-timestamp'
182
			$sql = 'UPDATE `'.$oDb->TablePrefix.'mod_news_posts` '
183 183
			     . 'SET `created_when`=`published_when` '
184 184
			     . 'WHERE `published_when`<`created_when`';
185
			$database->query($sql);
186
			$sql = 'UPDATE `'.$database->TablePrefix.'mod_news_posts` '
185
			$oDb->query($sql);
186
			$sql = 'UPDATE `'.$oDb->TablePrefix.'mod_news_posts` '
187 187
			     . 'SET `created_when`=`posted_when` '
188 188
			     . 'WHERE `published_when`=0 OR `published_when`>`posted_when`';
189
			$database->query($sql);
190
// ************************************************
191
// rebuild all access files
192
			$aReport = array('FilesDeleted'=>0,'FilesCreated'=>0,);
193
        	if( !$globalStarted && class_exists('m_news_Reorg') ) {
194
        		$oReorg = new m_news_Reorg(ModuleReorgAbstract::LOG_EXTENDED);
195
				$oReorg->execute(); // show details
196
                $aReport = $oReorg->getReport();
197
                unset($oReorg);
198
        	}
199
// ************************************************
200
			// only for upgrade-script
201
			if($globalStarted) {
202
				if($bDebug) {
203
					echo '<strong>'.implode('<br />',$msg).'</strong><br />';
204
				}
205
			} 
189
			$oDb->query($sql);
206 190
		}
207

  
208
//		$msg[] = '<strong>'.$aReport['FilesDeleted'].' Files successful deleted</strong>';
191
/* --- rebuild all access files ------------------------------------------------------- */
192
		$aReport = array('FilesDeleted'=>0,'FilesCreated'=>0,);
193
		$oReorg = new m_news_Reorg(ModuleReorgAbstract::LOG_EXTENDED);
194
		$oReorg->execute(); // show details
195
		$aReport = $oReorg->getReport();
196
		unset($oReorg);
197
/* --- for running from upgrade-script.php only --------------------------------------- */
198
		if($bGlobalStarted && $bDebug) {
199
			echo '<strong>'.implode('<br />',$msg).'</strong><br />';
200
		}
201
/* ------------------------------------------------------------------------------------ */
209 202
		$msg[] = '<strong>Number of new formated access files: '.$aReport['FilesCreated'].'</strong>';
210 203
		$msg[] = "<strong>News upgrade successfull finished</strong>";
211
		if($globalStarted) {
204
		if($bGlobalStarted) {
212 205
			echo "<strong>News upgrade successfull finished $OK</strong><br />";
213 206
		}
214
		$mLang->disableAddon();
215
		return ( ($globalStarted==true ) ? $globalStarted : $msg);
207
		$oLang->disableAddon();
208
		return ($bGlobalStarted ? $bGlobalStarted : $msg);
216 209
	}
217
//}
218 210
// end mod_news_Upgrade
219 211

  
220 212
// ------------------------------------
221 213
// Don't show the messages twice
222
$bDebugModus = ((isset($bDebugModus)) ? $bDebugModus : false);
223
if( is_array($msg = mod_news_Upgrade($bDebugModus)) ) {
214
	$bDebugModus = ((isset($bDebugModus)) ? $bDebugModus : false);
215
	if( is_array($msg = mod_news_Upgrade($bDebugModus)) ) {
224 216
// only show if manuell upgrade
225
	echo ''.implode('<br />',$msg).'<br />';
226
}
217
		echo implode('<br />', $msg).'<br />';
218
	}
227 219
/* **** END UPGRADE ********************************************************* */

Also available in: Unified diff