PageRenderTime 50ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/OX/Plugin/UpgradeComponentGroup.php

https://github.com/orchestra-io/sample-openx
PHP | 320 lines | 254 code | 23 blank | 43 comment | 29 complexity | 72533b1d49f382522bd409944eca218a MD5 | raw file
  1. <?php
  2. /*
  3. +---------------------------------------------------------------------------+
  4. | OpenX v${RELEASE_MAJOR_MINOR} |
  5. | =======${RELEASE_MAJOR_MINOR_DOUBLE_UNDERLINE} |
  6. | |
  7. | Copyright (c) 2003-2009 OpenX Limited |
  8. | For contact details, see: http://www.openx.org/ |
  9. | |
  10. | This program is free software; you can redistribute it and/or modify |
  11. | it under the terms of the GNU General Public License as published by |
  12. | the Free Software Foundation; either version 2 of the License, or |
  13. | (at your option) any later version. |
  14. | |
  15. | This program is distributed in the hope that it will be useful, |
  16. | but WITHOUT ANY WARRANTY; without even the implied warranty of |
  17. | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
  18. | GNU General Public License for more details. |
  19. | |
  20. | You should have received a copy of the GNU General Public License |
  21. | along with this program; if not, write to the Free Software |
  22. | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
  23. +---------------------------------------------------------------------------+
  24. $Id: UpgradeComponentGroup.php 30820 2009-01-13 19:02:17Z andrew.hill $
  25. */
  26. define('OA_STATUS_PLUGIN_NOT_INSTALLED', -1);
  27. define('OA_STATUS_PLUGIN_CURRENT_VERSION', 0);
  28. define('OA_STATUS_PLUGIN_VERSION_FAILED', 3);
  29. define('OA_STATUS_PLUGIN_DBINTEG_FAILED', 5);
  30. //define('OA_STATUS_PLUGIN_CONFINTEG_FAILED', 6);
  31. define('OA_STATUS_PLUGIN_CAN_UPGRADE', 10);
  32. require_once MAX_PATH.'/lib/OA/Upgrade/Upgrade.php';
  33. // setup oUpgrader, determine whether they are installing or that they can Upgrade
  34. /**
  35. * @package OpenXPlugin
  36. */
  37. class OX_Plugin_UpgradeComponentGroup extends OA_Upgrade
  38. {
  39. var $oComponentGroupManager;
  40. var $aComponentGroup;
  41. function __construct($aComponentGroup, $oComponentGroupManager)
  42. {
  43. parent::__construct();
  44. $this->aComponentGroup = $aComponentGroup;
  45. $this->oComponentGroupManager = $oComponentGroupManager;
  46. $pluginPath = $this->oComponentGroupManager->getPathToComponentGroup($this->aComponentGroup['name']);
  47. $this->upgradePath = $pluginPath.'etc/changes/';
  48. $this->oDBUpgrader->path_changes = $this->upgradePath;
  49. $this->recoveryFile = MAX_PATH.'/var/plugins/recover/'.strtoupper($this->aComponentGroup['name']);
  50. /*$this->nobackupsFile = MAX_PATH.$pluginPath.'NOBACKUPS';
  51. $this->postTaskFile = MAX_PATH.$pluginPath.'TASKS.php';*/
  52. $this->initDatabaseConnection();
  53. if (!file_exists(MAX_PATH.'/var/plugins/log'))
  54. {
  55. @mkdir(MAX_PATH.'/var/plugins/log');
  56. }
  57. $this->oLogger->setLogFile('plugins/log/'.$this->aComponentGroup['name'].'_upgrade.log');
  58. }
  59. function canUpgrade()
  60. {
  61. $strDetected = ' configuration file detected';
  62. $strCanUpgrade = 'This version can be upgraded';
  63. $strNoUpgrade = 'This version cannot be upgraded';
  64. $strProductName = $this->aComponentGroup['name'];
  65. $this->oLogger->logClear();
  66. $this->oLogger->logOnly('Compiling details of currently installed '.$this->aComponentGroup['name']);
  67. $skipIntegrityCheck = true;
  68. if ( !empty($this->aComponentGroup['install']['schema']['mdb2schema']))
  69. {
  70. // if a schema is declared but no previous schema version found
  71. // its a new schema so don't bother with integrity check
  72. if (!$this->oComponentGroupManager->getSchemaInfo($this->aComponentGroup['install']['schema']['mdb2schema']))
  73. {
  74. $skipIntegrityCheck = false;
  75. }
  76. }
  77. $this->detectComponentGroup($skipIntegrityCheck);
  78. switch ($this->existing_installation_status)
  79. {
  80. case OA_STATUS_PLUGIN_NOT_INSTALLED:
  81. $this->oLogger->log($strProductName.' not detected');
  82. $this->oLogger->logError($strNoUpgrade);
  83. return false;
  84. break;
  85. case OA_STATUS_PLUGIN_VERSION_FAILED:
  86. $this->oLogger->log($strProductName.' detected');
  87. $this->oLogger->logError($strNoUpgrade);
  88. return false;
  89. break;
  90. case OA_STATUS_PLUGIN_DBINTEG_FAILED:
  91. $this->oLogger->log($strProductName.' detected');
  92. $this->oLogger->logError($strNoUpgrade);
  93. return false;
  94. case OA_STATUS_PLUGIN_CAN_UPGRADE:
  95. $this->oLogger->log($strProductName.' detected');
  96. $this->oLogger->log($strCanUpgrade);
  97. return true;
  98. case OA_STATUS_CURRENT_VERSION:
  99. $this->oLogger->log($strProductName.' detected');
  100. $this->oLogger->log('This version is up to date.');
  101. return false;
  102. }
  103. return false;
  104. }
  105. function detectComponentGroup($skipIntegrityCheck = false)
  106. {
  107. $this->versionInitialApplication = $this->oVersioner->getApplicationVersion($this->aComponentGroup['name']);
  108. if (!$this->versionInitialApplication)
  109. {
  110. $this->existing_installation_status = OA_STATUS_PLUGIN_NOT_INSTALLED;
  111. return false;
  112. }
  113. $schemaName = $this->aComponentGroup['install']['schema']['mdb2schema'];
  114. $this->versionInitialSchema[$schemaName] = $this->oVersioner->getSchemaVersion($schemaName);
  115. $current = (version_compare($this->versionInitialApplication,$this->aComponentGroup['version'])==0);
  116. $valid = (version_compare($this->versionInitialApplication,$this->aComponentGroup['version'])<0);
  117. if ($valid)
  118. {
  119. $this->aPackageList = $this->getUpgradePackageList($this->versionInitialApplication, $this->_readUpgradePackagesArray($this->upgradePath.$this->aComponentGroup['name'].'_upgrade_array.txt'));
  120. if (!$skipIntegrityCheck && count($this->aPackageList)>0)
  121. {
  122. $aSchema['name'] = $schemaName;
  123. $aSchema['schemaOld'] = MAX_PATH.$this->oComponentGroupManager->pathPackages.$this->aComponentGroup['name'].'/etc/changes/schema_tables_'.$this->aComponentGroup['name'].'_'.$this->versionInitialSchema[$schemaName].'.xml';
  124. if (!$this->_checkDBIntegrity($this->versionInitialSchema[$schemaName], $aSchema))
  125. {
  126. $this->existing_installation_status = OA_STATUS_PLUGIN_DBINTEG_FAILED;
  127. return false;
  128. }
  129. }
  130. $this->existing_installation_status = OA_STATUS_CAN_UPGRADE;
  131. $this->aDsn['database'] = $GLOBALS['_MAX']['CONF']['database'];
  132. $this->aDsn['table'] = $GLOBALS['_MAX']['CONF']['table'];
  133. return true;
  134. }
  135. else if ($current)
  136. {
  137. $this->existing_installation_status = OA_STATUS_CURRENT_VERSION;
  138. $this->aPackageList = array();
  139. return false;
  140. }
  141. $this->existing_installation_status = OA_STATUS_PLUGIN_VERSION_FAILED;
  142. return false;
  143. }
  144. function upgrade($input_file='', $timing='constructive')
  145. {
  146. // initialise database connection if necessary
  147. if (is_null($this->oDbh))
  148. {
  149. $this->initDatabaseConnection();
  150. }
  151. if (!$this->checkPermissionToCreateTable())
  152. {
  153. $this->oLogger->logError('Insufficient database permissions or incorrect database settings');
  154. return false;
  155. }
  156. // first deal with each of the packages in the list
  157. // that was compiled during detection
  158. if (count($this->aPackageList)>0)
  159. {
  160. foreach ($this->aPackageList AS $k => $this->package_file)
  161. {
  162. if (!$this->upgradeExecute($this->package_file))
  163. {
  164. $halt = true;
  165. break;
  166. }
  167. }
  168. }
  169. if ($halt)
  170. {
  171. return false;
  172. }
  173. $version = $this->aComponentGroup['version'];
  174. $this->package_file = $this->aComponentGroup['name'].'_version_stamp_'.$version;
  175. $this->oAuditor->setUpgradeActionId();
  176. $this->oAuditor->setKeyParams(array('upgrade_name'=>$this->package_file,
  177. 'version_to'=>$version,
  178. 'version_from'=>$this->versionInitialApplication,
  179. 'logfile'=>basename($this->oLogger->logFile)
  180. )
  181. );
  182. $this->oAuditor->logAuditAction(array('description'=>'FAILED',
  183. 'action'=>UPGRADE_ACTION_UPGRADE_FAILED,
  184. )
  185. );
  186. if (!$this->_upgradeConfig())
  187. {
  188. $this->oLogger->logError('Failed to upgrade configuration file');
  189. return false;
  190. }
  191. if (!$this->_putDataObjects())
  192. {
  193. $this->oLogger->logError('Failed to upgrade dataobjects');
  194. return false;
  195. }
  196. if ($this->versionInitialApplication != $version)
  197. {
  198. if (!$this->oVersioner->putApplicationVersion($version, $this->aComponentGroup['name']))
  199. {
  200. $this->oLogger->logError('Failed to update '.$this->aComponentGroup['name'].' version to '.$version);
  201. $this->message = 'Failed to update '.$this->aComponentGroup['name'].' version to '.$version;
  202. return false;
  203. }
  204. $this->versionInitialApplication = $this->oVersioner->getApplicationVersion();
  205. $this->oLogger->log($this->aComponentGroup['name'].' version updated to '. $version);
  206. }
  207. $this->oAuditor->updateAuditAction(array('description'=>'UPGRADE COMPLETE',
  208. 'action'=>UPGRADE_ACTION_UPGRADE_SUCCEEDED,
  209. 'confbackup'=>$this->oConfiguration->getConfigBackupName()
  210. )
  211. );
  212. $this->_pickupRecoveryFile();
  213. return true;
  214. }
  215. function _upgradeConfig()
  216. {
  217. return $this->_upgradeSettings() && $this->_upgradePreferences();
  218. }
  219. function _putDataObjects()
  220. {
  221. if ($this->aComponentGroup['install']['schema']['dboschema'])
  222. {
  223. $name = $this->aComponentGroup['name'];
  224. $aSchema = $this->aComponentGroup['install']['schema'];
  225. if (!$this->oComponentGroupManager->_putDataObjects($name, $aSchema))
  226. {
  227. $this->_logError('Failed to implement dataobjects for '.$name);
  228. //$this->_dropTables($name, $aSchema);
  229. return false;
  230. }
  231. if (!$this->oComponentGroupManager->_cacheDataObjects($name, $aSchema))
  232. {
  233. $this->_logError('Failed to cache dataobject schema for '.$name);
  234. //$this->_dropTables($name, $aSchema);
  235. return false;
  236. }
  237. if (!$this->oComponentGroupManager->_verifyDataObjects($name, $aSchema))
  238. {
  239. $this->_logError('Failed to verify dataobjects for '.$name);
  240. //$this->_dropTables($name, $aSchema);
  241. return false;
  242. }
  243. }
  244. return true;
  245. }
  246. function _upgradeSettings()
  247. {
  248. $aConf = $GLOBALS['_MAX']['CONF'][$this->aComponentGroup['name']];
  249. foreach ($this->aComponentGroup['install']['conf']['settings'] AS $k => $aSet)
  250. {
  251. $aSettingsNew[] = array(
  252. 'key' => $aSet['key'],
  253. 'value' => (array_key_exists($aSet['key'], $aConf) ? $aConf[$aSet['key']] : $aSet['value'])
  254. );
  255. }
  256. return $this->oComponentGroupManager->_unregisterSettings($this->aComponentGroup['name'],false)
  257. && $this->oComponentGroupManager->_registerSettings($this->aComponentGroup['name'], $aSettingsNew);
  258. }
  259. function _upgradePreferences()
  260. {
  261. $aPrefsNew = array();
  262. $aPrefsDel = array();
  263. foreach ($this->aComponentGroup['install']['conf']['preferences'] AS $k => $aPref)
  264. {
  265. $aPrefsNew[$aPref['name']] = $aPref;
  266. }
  267. $prefix = $this->aComponentGroup['name'].'_';
  268. $doPreferences = OA_Dal::factoryDO('preferences');
  269. $doPreferences->whereAdd('preference_name LIKE '.$this->oDbh->quote('%'.$prefix.'%'));
  270. $result = $doPreferences->find();
  271. while ($doPreferences->fetch())
  272. {
  273. if (array_key_exists(str_replace($prefix,'',$doPreferences->preference_name), $aPrefsNew))
  274. {
  275. // preference exists, ignore it ( what if the permission or default value has changed? )
  276. unset($aPrefsNew[str_replace($prefix,'',$doPreferences->preference_name)]);
  277. }
  278. else
  279. {
  280. // existing preference not found in new preference array, deprecated so delete
  281. $aPrefsDel[] = array('name'=>str_replace($prefix,'',$doPreferences->preference_name));
  282. }
  283. }
  284. foreach ($aPrefsNew AS $name => $aPref)
  285. {
  286. // insert
  287. $this->oComponentGroupManager->_registerPreferences($this->aComponentGroup['name'], $aPrefsNew);
  288. }
  289. foreach ($aPrefsDel AS $i => $aPrefs)
  290. {
  291. // delete
  292. $this->oComponentGroupManager->_unregisterPreferences($this->aComponentGroup['name'], $aPrefsDel);
  293. }
  294. return true;
  295. }
  296. }
  297. ?>