PageRenderTime 47ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/downloader/lib/Mage/Connect/Command/Registry.php

https://bitbucket.org/claudiu_marginean/magento-hg-mirror
PHP | 364 lines | 264 code | 32 blank | 68 comment | 64 complexity | b496e7e78728655797c10a28ef57ae92 MD5 | raw file
Possible License(s): CC-BY-SA-3.0, LGPL-2.1, GPL-2.0, WTFPL
  1. <?php
  2. /**
  3. * Magento
  4. *
  5. * NOTICE OF LICENSE
  6. *
  7. * This source file is subject to the Open Software License (OSL 3.0)
  8. * that is bundled with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://opensource.org/licenses/osl-3.0.php
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@magentocommerce.com so we can send you a copy immediately.
  14. *
  15. * DISCLAIMER
  16. *
  17. * Do not edit or add to this file if you wish to upgrade Magento to newer
  18. * versions in the future. If you wish to customize Magento for your
  19. * needs please refer to http://www.magentocommerce.com for more information.
  20. *
  21. * @category Mage
  22. * @package Mage_Connect
  23. * @copyright Copyright (c) 2010 Magento Inc. (http://www.magentocommerce.com)
  24. * @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
  25. */
  26. final class Mage_Connect_Command_Registry
  27. extends Mage_Connect_Command
  28. {
  29. const PACKAGE_PEAR_DIR = 'pearlib/php/.registry';
  30. /**
  31. * List-installed callback
  32. * @param string $command
  33. * @param array $options
  34. * @param array $params
  35. * @return void
  36. */
  37. public function doList($command, $options, $params)
  38. {
  39. $this->cleanupParams($params);
  40. try {
  41. $packager = $this->getPackager();
  42. $ftp = empty($options['ftp']) ? false : $options['ftp'];
  43. if($ftp) {
  44. list($cache, $ftpObj) = $packager->getRemoteCache($ftp);
  45. } else {
  46. $cache = $this->getSconfig();
  47. }
  48. if(!empty($params[0])) {
  49. $chanName = $conf->chanName($params[0]);
  50. $data = $cache->getInstalledPackages($chanName);
  51. } else {
  52. $data = $cache->getInstalledPackages();
  53. }
  54. if($ftp) {
  55. @unlink($cache->getFilename());
  56. }
  57. $this->ui()->output(array($command=>array('data'=>$data, 'channel-title'=>"Installed package for channel '%s' :")));
  58. } catch (Exception $e) {
  59. if($ftp) {
  60. @unlink($cache->getFilename());
  61. }
  62. $this->doError($command, $e->getMessage());
  63. }
  64. }
  65. /**
  66. * list-files callback
  67. * @param string $command
  68. * @param array $options
  69. * @param array $params
  70. * @return void
  71. */
  72. public function doFileList($command, $options, $params)
  73. {
  74. $this->cleanupParams($params);
  75. //$this->splitPackageArgs($params);
  76. try {
  77. $channel = false;
  78. if(count($params) < 2) {
  79. throw new Exception("Argument count should be = 2");
  80. }
  81. $channel = $params[0];
  82. $package = $params[1];
  83. $packager = $this->getPackager();
  84. $ftp = empty($options['ftp']) ? false : $options['ftp'];
  85. if($ftp) {
  86. list($cache, $config, $ftpObj) = $packager->getRemoteConf($ftp);
  87. } else {
  88. $cache = $this->getSconfig();
  89. $confif = $this->config();
  90. }
  91. if(!$cache->hasPackage($channel, $package)) {
  92. return $this->ui()->output("No package found: {$channel}/{$package}");
  93. }
  94. $p = $cache->getPackageObject($channel, $package);
  95. $contents = $p->getContents();
  96. if($ftp) {
  97. $ftpObj->close();
  98. }
  99. if(!count($contents)) {
  100. return $this->ui()->output("No contents for package {$package}");
  101. }
  102. $title = ("Contents of '{$package}': ");
  103. if($ftp) {
  104. @unlink($config->getFilename());
  105. @unlink($cache->getFilename());
  106. }
  107. $this->ui()->output(array($command=>array('data'=>$contents, 'title'=>$title)));
  108. } catch (Exception $e) {
  109. if($ftp) {
  110. @unlink($config->getFilename());
  111. @unlink($cache->getFilename());
  112. }
  113. $this->doError($command, $e->getMessage());
  114. }
  115. }
  116. /**
  117. * Installed package info
  118. * info command callback
  119. * @param string $command
  120. * @param array $options
  121. * @param array $params
  122. * @return
  123. */
  124. public function doInfo($command, $options, $params)
  125. {
  126. $this->cleanupParams($params);
  127. //$this->splitPackageArgs($params);
  128. $cache = null;
  129. $ftp = empty($options['ftp']) ? false : $options['ftp'];
  130. try {
  131. $channel = false;
  132. if(count($params) < 2) {
  133. throw new Exception("Argument count should be = 2");
  134. }
  135. $channel = $params[0];
  136. $package = $params[1];
  137. $packager = $this->getPackager();
  138. if($ftp) {
  139. list($cache, $ftpObj) = $packager->getRemoteCache($ftp);
  140. } else {
  141. $cache = $this->getSconfig();
  142. }
  143. if(!$cache->isChannel($channel)) {
  144. throw new Exception("'{$channel}' is not a valid installed channel name/uri");
  145. }
  146. $channelUri = $cache->chanUrl($channel);
  147. $rest = $this->rest();
  148. $rest->setChannel($channelUri);
  149. $releases = $rest->getReleases($package);
  150. if(false === $releases) {
  151. throw new Exception("No information found about {$channel}/{$package}");
  152. }
  153. $data = array($command => array('releases'=>$releases));
  154. if($ftp) {
  155. @unlink($cache->getFilename());
  156. }
  157. $this->ui()->output($data);
  158. } catch (Exception $e) {
  159. if ($ftp && isset($cache)) {
  160. @unlink($cache->getFilename());
  161. }
  162. $this->doError($command, $e->getMessage());
  163. }
  164. }
  165. /**
  166. * Synchronize manually installed package info with local cache
  167. *
  168. * @param string $command
  169. * @param array $options
  170. * @param array $params
  171. */
  172. public function doSync($command, $options, $params)
  173. {
  174. $this->cleanupParams($params);
  175. try {
  176. $packager = $this->getPackager();
  177. $cache = null;
  178. $config = null;
  179. $ftpObj = null;
  180. $ftp = empty($options['ftp']) ? false : $options['ftp'];
  181. if($ftp) {
  182. list($cache, $config, $ftpObj) = $packager->getRemoteConf($ftp);
  183. } else {
  184. $config = $this->config();
  185. $cache = $this->getSconfig();
  186. }
  187. if ($this->_checkPearData($config)) {
  188. $this->doSyncPear($command, $options, $params);
  189. }
  190. $packageDir = $config->magento_root . DS . Mage_Connect_Package::PACKAGE_XML_DIR;
  191. if (is_dir($packageDir)) {
  192. $entries = scandir($packageDir);
  193. foreach ((array)$entries as $entry) {
  194. $path = $packageDir. DS .$entry;
  195. $info = pathinfo($path);
  196. if ($entry == '.' || $entry == '..' || is_dir($path) || $info['extension'] != 'xml') {
  197. continue;
  198. }
  199. if (is_readable($path)) {
  200. $data = file_get_contents($path);
  201. if ($data === false) {
  202. continue;
  203. }
  204. $package = new Mage_Connect_Package($data);
  205. $name = $package->getName();
  206. $channel = $package->getChannel();
  207. $version = $package->getVersion();
  208. if (!$cache->isChannel($channel) && $channel == $config->root_channel) {
  209. $cache->addChannel($channel, $config->root_channel_uri);
  210. }
  211. if (!$cache->hasPackage($channel, $name, $version, $version)) {
  212. $cache->addPackage($package);
  213. $this->ui()->output("Successfully added: {$channel}/{$name}-{$version}");
  214. }
  215. }
  216. }
  217. if ($ftp) {
  218. $packager->writeToRemoteCache($cache, $ftpObj);
  219. }
  220. }
  221. } catch (Exception $e) {
  222. $this->doError($command, $e->getMessage());
  223. }
  224. }
  225. /**
  226. * Synchronize packages installed earlier (by pear installer) with local cache
  227. *
  228. * @param string $command
  229. * @param array $options
  230. * @param array $params
  231. */
  232. public function doSyncPear($command, $options, $params)
  233. {
  234. $this->cleanupParams($params);
  235. try {
  236. $packager = $this->getPackager();
  237. $cache = null;
  238. $config = null;
  239. $ftpObj = null;
  240. $ftp = empty($options['ftp']) ? false : $options['ftp'];
  241. if($ftp) {
  242. list($cache, $config, $ftpObj) = $packager->getRemoteConf($ftp);
  243. } else {
  244. $config = $this->config();
  245. $cache = $this->getSconfig();
  246. }
  247. $pkglist = array();
  248. if (!$this->_checkPearData($config)) {
  249. return $pkglist;
  250. }
  251. $pearStorage = $config->magento_root . DS . $config->downloader_path . DS . self::PACKAGE_PEAR_DIR;
  252. $channels = array(
  253. '.channel.connect.magentocommerce.com_community',
  254. '.channel.connect.magentocommerce.com_core'
  255. );
  256. foreach ($channels as $channel) {
  257. $channelDirectory = $pearStorage . DS . $channel;
  258. if (!file_exists($channelDirectory) || !is_dir($channelDirectory)) {
  259. continue;
  260. }
  261. $dp = opendir($channelDirectory);
  262. if (!$dp) {
  263. continue;
  264. }
  265. while ($ent = readdir($dp)) {
  266. if ($ent{0} == '.' || substr($ent, -4) != '.reg') {
  267. continue;
  268. }
  269. $pkglist[] = array('file'=>$ent, 'channel'=>$channel);
  270. }
  271. closedir($dp);
  272. }
  273. $package = new Mage_Connect_Package();
  274. foreach ($pkglist as $pkg) {
  275. $pkgFilename = $pearStorage . DS . $pkg['channel'] . DS . $pkg['file'];
  276. if (!file_exists($pkgFilename)) {
  277. continue;
  278. }
  279. $data = file_get_contents($pkgFilename);
  280. $data = unserialize($data);
  281. $package->importDataV1x($data);
  282. $name = $package->getName();
  283. $channel = $package->getChannel();
  284. $version = $package->getVersion();
  285. if (!$cache->isChannel($channel) && $channel == $config->root_channel) {
  286. $cache->addChannel($channel, $config->root_channel_uri);
  287. }
  288. if (!$cache->hasPackage($channel, $name, $version, $version)) {
  289. $cache->addPackage($package);
  290. if($ftp) {
  291. $localXml = tempnam(sys_get_temp_dir(),'package');
  292. @file_put_contents($localXml, $package->getPackageXml());
  293. if (is_file($localXml)) {
  294. $ftpDir = $ftpObj->getcwd();
  295. $remoteXmlPath = $ftpDir . '/' . Mage_Connect_Package::PACKAGE_XML_DIR;
  296. $remoteXml = $package->getReleaseFilename() . '.xml';
  297. $ftpObj->mkdirRecursive($remoteXmlPath);
  298. $ftpObj->upload($remoteXml, $localXml, 0777, 0666);
  299. $ftpObj->chdir($ftpDir);
  300. }
  301. } else {
  302. $destDir = rtrim($config->magento_root, "\\/") . DS . Mage_Connect_Package::PACKAGE_XML_DIR;
  303. $destFile = $package->getReleaseFilename() . '.xml';
  304. $dest = $destDir . DS . $destFile;
  305. @mkdir($destDir, 0777, true);
  306. @file_put_contents($dest, $package->getPackageXml());
  307. @chmod($dest, 0666);
  308. }
  309. $this->ui()->output("Successfully added: {$channel}/{$name}-{$version}");
  310. }
  311. }
  312. $config->sync_pear = true;
  313. if($ftp) {
  314. $packager->writeToRemoteCache($cache, $ftpObj);
  315. @unlink($config->getFilename());
  316. }
  317. } catch (Exception $e) {
  318. $this->doError($command, $e->getMessage());
  319. }
  320. return true;
  321. }
  322. /**
  323. * Check is need to sync old pear data
  324. *
  325. * @param Mage_Connect_Config $config
  326. * @return boolean
  327. */
  328. protected function _checkPearData($config) {
  329. $pearStorage = $config->magento_root . DS . $config->downloader_path . DS . self::PACKAGE_PEAR_DIR;
  330. return (!$config->sync_pear) && file_exists($pearStorage) && is_dir($pearStorage);
  331. }
  332. }