PageRenderTime 74ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/lib/php/PEAR/PackageFile/Generator/v1.php

https://bitbucket.org/adarshj/convenient_website
PHP | 1284 lines | 1054 code | 38 blank | 192 comment | 216 complexity | 7db4fa3f069fc2ed490464b1efe6d801 MD5 | raw file
Possible License(s): Apache-2.0, MPL-2.0-no-copyleft-exception, LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /**
  3. * package.xml generation class, package.xml version 1.0
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * @category pear
  8. * @package PEAR
  9. * @author Greg Beaver <cellog@php.net>
  10. * @copyright 1997-2009 The Authors
  11. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  12. * @version CVS: $Id: v1.php 313023 2011-07-06 19:17:11Z dufuz $
  13. * @link http://pear.php.net/package/PEAR
  14. * @since File available since Release 1.4.0a1
  15. */
  16. /**
  17. * needed for PEAR_VALIDATE_* constants
  18. */
  19. require_once 'PEAR/Validate.php';
  20. require_once 'System.php';
  21. require_once 'PEAR/PackageFile/v2.php';
  22. /**
  23. * This class converts a PEAR_PackageFile_v1 object into any output format.
  24. *
  25. * Supported output formats include array, XML string, and a PEAR_PackageFile_v2
  26. * object, for converting package.xml 1.0 into package.xml 2.0 with no sweat.
  27. * @category pear
  28. * @package PEAR
  29. * @author Greg Beaver <cellog@php.net>
  30. * @copyright 1997-2009 The Authors
  31. * @license http://opensource.org/licenses/bsd-license.php New BSD License
  32. * @version Release: 1.9.4
  33. * @link http://pear.php.net/package/PEAR
  34. * @since Class available since Release 1.4.0a1
  35. */
  36. class PEAR_PackageFile_Generator_v1
  37. {
  38. /**
  39. * @var PEAR_PackageFile_v1
  40. */
  41. var $_packagefile;
  42. function PEAR_PackageFile_Generator_v1(&$packagefile)
  43. {
  44. $this->_packagefile = &$packagefile;
  45. }
  46. function getPackagerVersion()
  47. {
  48. return '1.9.4';
  49. }
  50. /**
  51. * @param PEAR_Packager
  52. * @param bool if true, a .tgz is written, otherwise a .tar is written
  53. * @param string|null directory in which to save the .tgz
  54. * @return string|PEAR_Error location of package or error object
  55. */
  56. function toTgz(&$packager, $compress = true, $where = null)
  57. {
  58. require_once 'Archive/Tar.php';
  59. if ($where === null) {
  60. if (!($where = System::mktemp(array('-d')))) {
  61. return PEAR::raiseError('PEAR_Packagefile_v1::toTgz: mktemp failed');
  62. }
  63. } elseif (!@System::mkDir(array('-p', $where))) {
  64. return PEAR::raiseError('PEAR_Packagefile_v1::toTgz: "' . $where . '" could' .
  65. ' not be created');
  66. }
  67. if (file_exists($where . DIRECTORY_SEPARATOR . 'package.xml') &&
  68. !is_file($where . DIRECTORY_SEPARATOR . 'package.xml')) {
  69. return PEAR::raiseError('PEAR_Packagefile_v1::toTgz: unable to save package.xml as' .
  70. ' "' . $where . DIRECTORY_SEPARATOR . 'package.xml"');
  71. }
  72. if (!$this->_packagefile->validate(PEAR_VALIDATE_PACKAGING)) {
  73. return PEAR::raiseError('PEAR_Packagefile_v1::toTgz: invalid package file');
  74. }
  75. $pkginfo = $this->_packagefile->getArray();
  76. $ext = $compress ? '.tgz' : '.tar';
  77. $pkgver = $pkginfo['package'] . '-' . $pkginfo['version'];
  78. $dest_package = getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext;
  79. if (file_exists(getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext) &&
  80. !is_file(getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext)) {
  81. return PEAR::raiseError('PEAR_Packagefile_v1::toTgz: cannot create tgz file "' .
  82. getcwd() . DIRECTORY_SEPARATOR . $pkgver . $ext . '"');
  83. }
  84. if ($pkgfile = $this->_packagefile->getPackageFile()) {
  85. $pkgdir = dirname(realpath($pkgfile));
  86. $pkgfile = basename($pkgfile);
  87. } else {
  88. return PEAR::raiseError('PEAR_Packagefile_v1::toTgz: package file object must ' .
  89. 'be created from a real file');
  90. }
  91. // {{{ Create the package file list
  92. $filelist = array();
  93. $i = 0;
  94. foreach ($this->_packagefile->getFilelist() as $fname => $atts) {
  95. $file = $pkgdir . DIRECTORY_SEPARATOR . $fname;
  96. if (!file_exists($file)) {
  97. return PEAR::raiseError("File does not exist: $fname");
  98. } else {
  99. $filelist[$i++] = $file;
  100. if (!isset($atts['md5sum'])) {
  101. $this->_packagefile->setFileAttribute($fname, 'md5sum', md5_file($file));
  102. }
  103. $packager->log(2, "Adding file $fname");
  104. }
  105. }
  106. // }}}
  107. $packagexml = $this->toPackageFile($where, PEAR_VALIDATE_PACKAGING, 'package.xml', true);
  108. if ($packagexml) {
  109. $tar =& new Archive_Tar($dest_package, $compress);
  110. $tar->setErrorHandling(PEAR_ERROR_RETURN); // XXX Don't print errors
  111. // ----- Creates with the package.xml file
  112. $ok = $tar->createModify(array($packagexml), '', $where);
  113. if (PEAR::isError($ok)) {
  114. return $ok;
  115. } elseif (!$ok) {
  116. return PEAR::raiseError('PEAR_Packagefile_v1::toTgz: tarball creation failed');
  117. }
  118. // ----- Add the content of the package
  119. if (!$tar->addModify($filelist, $pkgver, $pkgdir)) {
  120. return PEAR::raiseError('PEAR_Packagefile_v1::toTgz: tarball creation failed');
  121. }
  122. return $dest_package;
  123. }
  124. }
  125. /**
  126. * @param string|null directory to place the package.xml in, or null for a temporary dir
  127. * @param int one of the PEAR_VALIDATE_* constants
  128. * @param string name of the generated file
  129. * @param bool if true, then no analysis will be performed on role="php" files
  130. * @return string|PEAR_Error path to the created file on success
  131. */
  132. function toPackageFile($where = null, $state = PEAR_VALIDATE_NORMAL, $name = 'package.xml',
  133. $nofilechecking = false)
  134. {
  135. if (!$this->_packagefile->validate($state, $nofilechecking)) {
  136. return PEAR::raiseError('PEAR_Packagefile_v1::toPackageFile: invalid package.xml',
  137. null, null, null, $this->_packagefile->getValidationWarnings());
  138. }
  139. if ($where === null) {
  140. if (!($where = System::mktemp(array('-d')))) {
  141. return PEAR::raiseError('PEAR_Packagefile_v1::toPackageFile: mktemp failed');
  142. }
  143. } elseif (!@System::mkDir(array('-p', $where))) {
  144. return PEAR::raiseError('PEAR_Packagefile_v1::toPackageFile: "' . $where . '" could' .
  145. ' not be created');
  146. }
  147. $newpkgfile = $where . DIRECTORY_SEPARATOR . $name;
  148. $np = @fopen($newpkgfile, 'wb');
  149. if (!$np) {
  150. return PEAR::raiseError('PEAR_Packagefile_v1::toPackageFile: unable to save ' .
  151. "$name as $newpkgfile");
  152. }
  153. fwrite($np, $this->toXml($state, true));
  154. fclose($np);
  155. return $newpkgfile;
  156. }
  157. /**
  158. * fix both XML encoding to be UTF8, and replace standard XML entities < > " & '
  159. *
  160. * @param string $string
  161. * @return string
  162. * @access private
  163. */
  164. function _fixXmlEncoding($string)
  165. {
  166. if (version_compare(phpversion(), '5.0.0', 'lt')) {
  167. $string = utf8_encode($string);
  168. }
  169. return strtr($string, array(
  170. '&' => '&amp;',
  171. '>' => '&gt;',
  172. '<' => '&lt;',
  173. '"' => '&quot;',
  174. '\'' => '&apos;' ));
  175. }
  176. /**
  177. * Return an XML document based on the package info (as returned
  178. * by the PEAR_Common::infoFrom* methods).
  179. *
  180. * @return string XML data
  181. */
  182. function toXml($state = PEAR_VALIDATE_NORMAL, $nofilevalidation = false)
  183. {
  184. $this->_packagefile->setDate(date('Y-m-d'));
  185. if (!$this->_packagefile->validate($state, $nofilevalidation)) {
  186. return false;
  187. }
  188. $pkginfo = $this->_packagefile->getArray();
  189. static $maint_map = array(
  190. "handle" => "user",
  191. "name" => "name",
  192. "email" => "email",
  193. "role" => "role",
  194. );
  195. $ret = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n";
  196. $ret .= "<!DOCTYPE package SYSTEM \"http://pear.php.net/dtd/package-1.0\">\n";
  197. $ret .= "<package version=\"1.0\" packagerversion=\"1.9.4\">\n" .
  198. " <name>$pkginfo[package]</name>";
  199. if (isset($pkginfo['extends'])) {
  200. $ret .= "\n<extends>$pkginfo[extends]</extends>";
  201. }
  202. $ret .=
  203. "\n <summary>".$this->_fixXmlEncoding($pkginfo['summary'])."</summary>\n" .
  204. " <description>".trim($this->_fixXmlEncoding($pkginfo['description']))."\n </description>\n" .
  205. " <maintainers>\n";
  206. foreach ($pkginfo['maintainers'] as $maint) {
  207. $ret .= " <maintainer>\n";
  208. foreach ($maint_map as $idx => $elm) {
  209. $ret .= " <$elm>";
  210. $ret .= $this->_fixXmlEncoding($maint[$idx]);
  211. $ret .= "</$elm>\n";
  212. }
  213. $ret .= " </maintainer>\n";
  214. }
  215. $ret .= " </maintainers>\n";
  216. $ret .= $this->_makeReleaseXml($pkginfo, false, $state);
  217. if (isset($pkginfo['changelog']) && count($pkginfo['changelog']) > 0) {
  218. $ret .= " <changelog>\n";
  219. foreach ($pkginfo['changelog'] as $oldrelease) {
  220. $ret .= $this->_makeReleaseXml($oldrelease, true);
  221. }
  222. $ret .= " </changelog>\n";
  223. }
  224. $ret .= "</package>\n";
  225. return $ret;
  226. }
  227. // }}}
  228. // {{{ _makeReleaseXml()
  229. /**
  230. * Generate part of an XML description with release information.
  231. *
  232. * @param array $pkginfo array with release information
  233. * @param bool $changelog whether the result will be in a changelog element
  234. *
  235. * @return string XML data
  236. *
  237. * @access private
  238. */
  239. function _makeReleaseXml($pkginfo, $changelog = false, $state = PEAR_VALIDATE_NORMAL)
  240. {
  241. // XXX QUOTE ENTITIES IN PCDATA, OR EMBED IN CDATA BLOCKS!!
  242. $indent = $changelog ? " " : "";
  243. $ret = "$indent <release>\n";
  244. if (!empty($pkginfo['version'])) {
  245. $ret .= "$indent <version>$pkginfo[version]</version>\n";
  246. }
  247. if (!empty($pkginfo['release_date'])) {
  248. $ret .= "$indent <date>$pkginfo[release_date]</date>\n";
  249. }
  250. if (!empty($pkginfo['release_license'])) {
  251. $ret .= "$indent <license>$pkginfo[release_license]</license>\n";
  252. }
  253. if (!empty($pkginfo['release_state'])) {
  254. $ret .= "$indent <state>$pkginfo[release_state]</state>\n";
  255. }
  256. if (!empty($pkginfo['release_notes'])) {
  257. $ret .= "$indent <notes>".trim($this->_fixXmlEncoding($pkginfo['release_notes']))
  258. ."\n$indent </notes>\n";
  259. }
  260. if (!empty($pkginfo['release_warnings'])) {
  261. $ret .= "$indent <warnings>".$this->_fixXmlEncoding($pkginfo['release_warnings'])."</warnings>\n";
  262. }
  263. if (isset($pkginfo['release_deps']) && sizeof($pkginfo['release_deps']) > 0) {
  264. $ret .= "$indent <deps>\n";
  265. foreach ($pkginfo['release_deps'] as $dep) {
  266. $ret .= "$indent <dep type=\"$dep[type]\" rel=\"$dep[rel]\"";
  267. if (isset($dep['version'])) {
  268. $ret .= " version=\"$dep[version]\"";
  269. }
  270. if (isset($dep['optional'])) {
  271. $ret .= " optional=\"$dep[optional]\"";
  272. }
  273. if (isset($dep['name'])) {
  274. $ret .= ">$dep[name]</dep>\n";
  275. } else {
  276. $ret .= "/>\n";
  277. }
  278. }
  279. $ret .= "$indent </deps>\n";
  280. }
  281. if (isset($pkginfo['configure_options'])) {
  282. $ret .= "$indent <configureoptions>\n";
  283. foreach ($pkginfo['configure_options'] as $c) {
  284. $ret .= "$indent <configureoption name=\"".
  285. $this->_fixXmlEncoding($c['name']) . "\"";
  286. if (isset($c['default'])) {
  287. $ret .= " default=\"" . $this->_fixXmlEncoding($c['default']) . "\"";
  288. }
  289. $ret .= " prompt=\"" . $this->_fixXmlEncoding($c['prompt']) . "\"";
  290. $ret .= "/>\n";
  291. }
  292. $ret .= "$indent </configureoptions>\n";
  293. }
  294. if (isset($pkginfo['provides'])) {
  295. foreach ($pkginfo['provides'] as $key => $what) {
  296. $ret .= "$indent <provides type=\"$what[type]\" ";
  297. $ret .= "name=\"$what[name]\" ";
  298. if (isset($what['extends'])) {
  299. $ret .= "extends=\"$what[extends]\" ";
  300. }
  301. $ret .= "/>\n";
  302. }
  303. }
  304. if (isset($pkginfo['filelist'])) {
  305. $ret .= "$indent <filelist>\n";
  306. if ($state ^ PEAR_VALIDATE_PACKAGING) {
  307. $ret .= $this->recursiveXmlFilelist($pkginfo['filelist']);
  308. } else {
  309. foreach ($pkginfo['filelist'] as $file => $fa) {
  310. if (!isset($fa['role'])) {
  311. $fa['role'] = '';
  312. }
  313. $ret .= "$indent <file role=\"$fa[role]\"";
  314. if (isset($fa['baseinstalldir'])) {
  315. $ret .= ' baseinstalldir="' .
  316. $this->_fixXmlEncoding($fa['baseinstalldir']) . '"';
  317. }
  318. if (isset($fa['md5sum'])) {
  319. $ret .= " md5sum=\"$fa[md5sum]\"";
  320. }
  321. if (isset($fa['platform'])) {
  322. $ret .= " platform=\"$fa[platform]\"";
  323. }
  324. if (!empty($fa['install-as'])) {
  325. $ret .= ' install-as="' .
  326. $this->_fixXmlEncoding($fa['install-as']) . '"';
  327. }
  328. $ret .= ' name="' . $this->_fixXmlEncoding($file) . '"';
  329. if (empty($fa['replacements'])) {
  330. $ret .= "/>\n";
  331. } else {
  332. $ret .= ">\n";
  333. foreach ($fa['replacements'] as $r) {
  334. $ret .= "$indent <replace";
  335. foreach ($r as $k => $v) {
  336. $ret .= " $k=\"" . $this->_fixXmlEncoding($v) .'"';
  337. }
  338. $ret .= "/>\n";
  339. }
  340. $ret .= "$indent </file>\n";
  341. }
  342. }
  343. }
  344. $ret .= "$indent </filelist>\n";
  345. }
  346. $ret .= "$indent </release>\n";
  347. return $ret;
  348. }
  349. /**
  350. * @param array
  351. * @access protected
  352. */
  353. function recursiveXmlFilelist($list)
  354. {
  355. $this->_dirs = array();
  356. foreach ($list as $file => $attributes) {
  357. $this->_addDir($this->_dirs, explode('/', dirname($file)), $file, $attributes);
  358. }
  359. return $this->_formatDir($this->_dirs);
  360. }
  361. /**
  362. * @param array
  363. * @param array
  364. * @param string|null
  365. * @param array|null
  366. * @access private
  367. */
  368. function _addDir(&$dirs, $dir, $file = null, $attributes = null)
  369. {
  370. if ($dir == array() || $dir == array('.')) {
  371. $dirs['files'][basename($file)] = $attributes;
  372. return;
  373. }
  374. $curdir = array_shift($dir);
  375. if (!isset($dirs['dirs'][$curdir])) {
  376. $dirs['dirs'][$curdir] = array();
  377. }
  378. $this->_addDir($dirs['dirs'][$curdir], $dir, $file, $attributes);
  379. }
  380. /**
  381. * @param array
  382. * @param string
  383. * @param string
  384. * @access private
  385. */
  386. function _formatDir($dirs, $indent = '', $curdir = '')
  387. {
  388. $ret = '';
  389. if (!count($dirs)) {
  390. return '';
  391. }
  392. if (isset($dirs['dirs'])) {
  393. uksort($dirs['dirs'], 'strnatcasecmp');
  394. foreach ($dirs['dirs'] as $dir => $contents) {
  395. $usedir = "$curdir/$dir";
  396. $ret .= "$indent <dir name=\"$dir\">\n";
  397. $ret .= $this->_formatDir($contents, "$indent ", $usedir);
  398. $ret .= "$indent </dir> <!-- $usedir -->\n";
  399. }
  400. }
  401. if (isset($dirs['files'])) {
  402. uksort($dirs['files'], 'strnatcasecmp');
  403. foreach ($dirs['files'] as $file => $attribs) {
  404. $ret .= $this->_formatFile($file, $attribs, $indent);
  405. }
  406. }
  407. return $ret;
  408. }
  409. /**
  410. * @param string
  411. * @param array
  412. * @param string
  413. * @access private
  414. */
  415. function _formatFile($file, $attributes, $indent)
  416. {
  417. $ret = "$indent <file role=\"$attributes[role]\"";
  418. if (isset($attributes['baseinstalldir'])) {
  419. $ret .= ' baseinstalldir="' .
  420. $this->_fixXmlEncoding($attributes['baseinstalldir']) . '"';
  421. }
  422. if (isset($attributes['md5sum'])) {
  423. $ret .= " md5sum=\"$attributes[md5sum]\"";
  424. }
  425. if (isset($attributes['platform'])) {
  426. $ret .= " platform=\"$attributes[platform]\"";
  427. }
  428. if (!empty($attributes['install-as'])) {
  429. $ret .= ' install-as="' .
  430. $this->_fixXmlEncoding($attributes['install-as']) . '"';
  431. }
  432. $ret .= ' name="' . $this->_fixXmlEncoding($file) . '"';
  433. if (empty($attributes['replacements'])) {
  434. $ret .= "/>\n";
  435. } else {
  436. $ret .= ">\n";
  437. foreach ($attributes['replacements'] as $r) {
  438. $ret .= "$indent <replace";
  439. foreach ($r as $k => $v) {
  440. $ret .= " $k=\"" . $this->_fixXmlEncoding($v) .'"';
  441. }
  442. $ret .= "/>\n";
  443. }
  444. $ret .= "$indent </file>\n";
  445. }
  446. return $ret;
  447. }
  448. // {{{ _unIndent()
  449. /**
  450. * Unindent given string (?)
  451. *
  452. * @param string $str The string that has to be unindented.
  453. * @return string
  454. * @access private
  455. */
  456. function _unIndent($str)
  457. {
  458. // remove leading newlines
  459. $str = preg_replace('/^[\r\n]+/', '', $str);
  460. // find whitespace at the beginning of the first line
  461. $indent_len = strspn($str, " \t");
  462. $indent = substr($str, 0, $indent_len);
  463. $data = '';
  464. // remove the same amount of whitespace from following lines
  465. foreach (explode("\n", $str) as $line) {
  466. if (substr($line, 0, $indent_len) == $indent) {
  467. $data .= substr($line, $indent_len) . "\n";
  468. }
  469. }
  470. return $data;
  471. }
  472. /**
  473. * @return array
  474. */
  475. function dependenciesToV2()
  476. {
  477. $arr = array();
  478. $this->_convertDependencies2_0($arr);
  479. return $arr['dependencies'];
  480. }
  481. /**
  482. * Convert a package.xml version 1.0 into version 2.0
  483. *
  484. * Note that this does a basic conversion, to allow more advanced
  485. * features like bundles and multiple releases
  486. * @param string the classname to instantiate and return. This must be
  487. * PEAR_PackageFile_v2 or a descendant
  488. * @param boolean if true, only valid, deterministic package.xml 1.0 as defined by the
  489. * strictest parameters will be converted
  490. * @return PEAR_PackageFile_v2|PEAR_Error
  491. */
  492. function &toV2($class = 'PEAR_PackageFile_v2', $strict = false)
  493. {
  494. if ($strict) {
  495. if (!$this->_packagefile->validate()) {
  496. $a = PEAR::raiseError('invalid package.xml version 1.0 cannot be converted' .
  497. ' to version 2.0', null, null, null,
  498. $this->_packagefile->getValidationWarnings(true));
  499. return $a;
  500. }
  501. }
  502. $arr = array(
  503. 'attribs' => array(
  504. 'version' => '2.0',
  505. 'xmlns' => 'http://pear.php.net/dtd/package-2.0',
  506. 'xmlns:tasks' => 'http://pear.php.net/dtd/tasks-1.0',
  507. 'xmlns:xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
  508. 'xsi:schemaLocation' => "http://pear.php.net/dtd/tasks-1.0\n" .
  509. "http://pear.php.net/dtd/tasks-1.0.xsd\n" .
  510. "http://pear.php.net/dtd/package-2.0\n" .
  511. 'http://pear.php.net/dtd/package-2.0.xsd',
  512. ),
  513. 'name' => $this->_packagefile->getPackage(),
  514. 'channel' => 'pear.php.net',
  515. );
  516. $arr['summary'] = $this->_packagefile->getSummary();
  517. $arr['description'] = $this->_packagefile->getDescription();
  518. $maintainers = $this->_packagefile->getMaintainers();
  519. foreach ($maintainers as $maintainer) {
  520. if ($maintainer['role'] != 'lead') {
  521. continue;
  522. }
  523. $new = array(
  524. 'name' => $maintainer['name'],
  525. 'user' => $maintainer['handle'],
  526. 'email' => $maintainer['email'],
  527. 'active' => 'yes',
  528. );
  529. $arr['lead'][] = $new;
  530. }
  531. if (!isset($arr['lead'])) { // some people... you know?
  532. $arr['lead'] = array(
  533. 'name' => 'unknown',
  534. 'user' => 'unknown',
  535. 'email' => 'noleadmaintainer@example.com',
  536. 'active' => 'no',
  537. );
  538. }
  539. if (count($arr['lead']) == 1) {
  540. $arr['lead'] = $arr['lead'][0];
  541. }
  542. foreach ($maintainers as $maintainer) {
  543. if ($maintainer['role'] == 'lead') {
  544. continue;
  545. }
  546. $new = array(
  547. 'name' => $maintainer['name'],
  548. 'user' => $maintainer['handle'],
  549. 'email' => $maintainer['email'],
  550. 'active' => 'yes',
  551. );
  552. $arr[$maintainer['role']][] = $new;
  553. }
  554. if (isset($arr['developer']) && count($arr['developer']) == 1) {
  555. $arr['developer'] = $arr['developer'][0];
  556. }
  557. if (isset($arr['contributor']) && count($arr['contributor']) == 1) {
  558. $arr['contributor'] = $arr['contributor'][0];
  559. }
  560. if (isset($arr['helper']) && count($arr['helper']) == 1) {
  561. $arr['helper'] = $arr['helper'][0];
  562. }
  563. $arr['date'] = $this->_packagefile->getDate();
  564. $arr['version'] =
  565. array(
  566. 'release' => $this->_packagefile->getVersion(),
  567. 'api' => $this->_packagefile->getVersion(),
  568. );
  569. $arr['stability'] =
  570. array(
  571. 'release' => $this->_packagefile->getState(),
  572. 'api' => $this->_packagefile->getState(),
  573. );
  574. $licensemap =
  575. array(
  576. 'php' => 'http://www.php.net/license',
  577. 'php license' => 'http://www.php.net/license',
  578. 'lgpl' => 'http://www.gnu.org/copyleft/lesser.html',
  579. 'bsd' => 'http://www.opensource.org/licenses/bsd-license.php',
  580. 'bsd style' => 'http://www.opensource.org/licenses/bsd-license.php',
  581. 'bsd-style' => 'http://www.opensource.org/licenses/bsd-license.php',
  582. 'mit' => 'http://www.opensource.org/licenses/mit-license.php',
  583. 'gpl' => 'http://www.gnu.org/copyleft/gpl.html',
  584. 'apache' => 'http://www.opensource.org/licenses/apache2.0.php'
  585. );
  586. if (isset($licensemap[strtolower($this->_packagefile->getLicense())])) {
  587. $arr['license'] = array(
  588. 'attribs' => array('uri' =>
  589. $licensemap[strtolower($this->_packagefile->getLicense())]),
  590. '_content' => $this->_packagefile->getLicense()
  591. );
  592. } else {
  593. // don't use bogus uri
  594. $arr['license'] = $this->_packagefile->getLicense();
  595. }
  596. $arr['notes'] = $this->_packagefile->getNotes();
  597. $temp = array();
  598. $arr['contents'] = $this->_convertFilelist2_0($temp);
  599. $this->_convertDependencies2_0($arr);
  600. $release = ($this->_packagefile->getConfigureOptions() || $this->_isExtension) ?
  601. 'extsrcrelease' : 'phprelease';
  602. if ($release == 'extsrcrelease') {
  603. $arr['channel'] = 'pecl.php.net';
  604. $arr['providesextension'] = $arr['name']; // assumption
  605. }
  606. $arr[$release] = array();
  607. if ($this->_packagefile->getConfigureOptions()) {
  608. $arr[$release]['configureoption'] = $this->_packagefile->getConfigureOptions();
  609. foreach ($arr[$release]['configureoption'] as $i => $opt) {
  610. $arr[$release]['configureoption'][$i] = array('attribs' => $opt);
  611. }
  612. if (count($arr[$release]['configureoption']) == 1) {
  613. $arr[$release]['configureoption'] = $arr[$release]['configureoption'][0];
  614. }
  615. }
  616. $this->_convertRelease2_0($arr[$release], $temp);
  617. if ($release == 'extsrcrelease' && count($arr[$release]) > 1) {
  618. // multiple extsrcrelease tags added in PEAR 1.4.1
  619. $arr['dependencies']['required']['pearinstaller']['min'] = '1.4.1';
  620. }
  621. if ($cl = $this->_packagefile->getChangelog()) {
  622. foreach ($cl as $release) {
  623. $rel = array();
  624. $rel['version'] =
  625. array(
  626. 'release' => $release['version'],
  627. 'api' => $release['version'],
  628. );
  629. if (!isset($release['release_state'])) {
  630. $release['release_state'] = 'stable';
  631. }
  632. $rel['stability'] =
  633. array(
  634. 'release' => $release['release_state'],
  635. 'api' => $release['release_state'],
  636. );
  637. if (isset($release['release_date'])) {
  638. $rel['date'] = $release['release_date'];
  639. } else {
  640. $rel['date'] = date('Y-m-d');
  641. }
  642. if (isset($release['release_license'])) {
  643. if (isset($licensemap[strtolower($release['release_license'])])) {
  644. $uri = $licensemap[strtolower($release['release_license'])];
  645. } else {
  646. $uri = 'http://www.example.com';
  647. }
  648. $rel['license'] = array(
  649. 'attribs' => array('uri' => $uri),
  650. '_content' => $release['release_license']
  651. );
  652. } else {
  653. $rel['license'] = $arr['license'];
  654. }
  655. if (!isset($release['release_notes'])) {
  656. $release['release_notes'] = 'no release notes';
  657. }
  658. $rel['notes'] = $release['release_notes'];
  659. $arr['changelog']['release'][] = $rel;
  660. }
  661. }
  662. $ret = new $class;
  663. $ret->setConfig($this->_packagefile->_config);
  664. if (isset($this->_packagefile->_logger) && is_object($this->_packagefile->_logger)) {
  665. $ret->setLogger($this->_packagefile->_logger);
  666. }
  667. $ret->fromArray($arr);
  668. return $ret;
  669. }
  670. /**
  671. * @param array
  672. * @param bool
  673. * @access private
  674. */
  675. function _convertDependencies2_0(&$release, $internal = false)
  676. {
  677. $peardep = array('pearinstaller' =>
  678. array('min' => '1.4.0b1')); // this is a lot safer
  679. $required = $optional = array();
  680. $release['dependencies'] = array('required' => array());
  681. if ($this->_packagefile->hasDeps()) {
  682. foreach ($this->_packagefile->getDeps() as $dep) {
  683. if (!isset($dep['optional']) || $dep['optional'] == 'no') {
  684. $required[] = $dep;
  685. } else {
  686. $optional[] = $dep;
  687. }
  688. }
  689. foreach (array('required', 'optional') as $arr) {
  690. $deps = array();
  691. foreach ($$arr as $dep) {
  692. // organize deps by dependency type and name
  693. if (!isset($deps[$dep['type']])) {
  694. $deps[$dep['type']] = array();
  695. }
  696. if (isset($dep['name'])) {
  697. $deps[$dep['type']][$dep['name']][] = $dep;
  698. } else {
  699. $deps[$dep['type']][] = $dep;
  700. }
  701. }
  702. do {
  703. if (isset($deps['php'])) {
  704. $php = array();
  705. if (count($deps['php']) > 1) {
  706. $php = $this->_processPhpDeps($deps['php']);
  707. } else {
  708. if (!isset($deps['php'][0])) {
  709. list($key, $blah) = each ($deps['php']); // stupid buggy versions
  710. $deps['php'] = array($blah[0]);
  711. }
  712. $php = $this->_processDep($deps['php'][0]);
  713. if (!$php) {
  714. break; // poor mans throw
  715. }
  716. }
  717. $release['dependencies'][$arr]['php'] = $php;
  718. }
  719. } while (false);
  720. do {
  721. if (isset($deps['pkg'])) {
  722. $pkg = array();
  723. $pkg = $this->_processMultipleDepsName($deps['pkg']);
  724. if (!$pkg) {
  725. break; // poor mans throw
  726. }
  727. $release['dependencies'][$arr]['package'] = $pkg;
  728. }
  729. } while (false);
  730. do {
  731. if (isset($deps['ext'])) {
  732. $pkg = array();
  733. $pkg = $this->_processMultipleDepsName($deps['ext']);
  734. $release['dependencies'][$arr]['extension'] = $pkg;
  735. }
  736. } while (false);
  737. // skip sapi - it's not supported so nobody will have used it
  738. // skip os - it's not supported in 1.0
  739. }
  740. }
  741. if (isset($release['dependencies']['required'])) {
  742. $release['dependencies']['required'] =
  743. array_merge($peardep, $release['dependencies']['required']);
  744. } else {
  745. $release['dependencies']['required'] = $peardep;
  746. }
  747. if (!isset($release['dependencies']['required']['php'])) {
  748. $release['dependencies']['required']['php'] =
  749. array('min' => '4.0.0');
  750. }
  751. $order = array();
  752. $bewm = $release['dependencies']['required'];
  753. $order['php'] = $bewm['php'];
  754. $order['pearinstaller'] = $bewm['pearinstaller'];
  755. isset($bewm['package']) ? $order['package'] = $bewm['package'] :0;
  756. isset($bewm['extension']) ? $order['extension'] = $bewm['extension'] :0;
  757. $release['dependencies']['required'] = $order;
  758. }
  759. /**
  760. * @param array
  761. * @access private
  762. */
  763. function _convertFilelist2_0(&$package)
  764. {
  765. $ret = array('dir' =>
  766. array(
  767. 'attribs' => array('name' => '/'),
  768. 'file' => array()
  769. )
  770. );
  771. $package['platform'] =
  772. $package['install-as'] = array();
  773. $this->_isExtension = false;
  774. foreach ($this->_packagefile->getFilelist() as $name => $file) {
  775. $file['name'] = $name;
  776. if (isset($file['role']) && $file['role'] == 'src') {
  777. $this->_isExtension = true;
  778. }
  779. if (isset($file['replacements'])) {
  780. $repl = $file['replacements'];
  781. unset($file['replacements']);
  782. } else {
  783. unset($repl);
  784. }
  785. if (isset($file['install-as'])) {
  786. $package['install-as'][$name] = $file['install-as'];
  787. unset($file['install-as']);
  788. }
  789. if (isset($file['platform'])) {
  790. $package['platform'][$name] = $file['platform'];
  791. unset($file['platform']);
  792. }
  793. $file = array('attribs' => $file);
  794. if (isset($repl)) {
  795. foreach ($repl as $replace ) {
  796. $file['tasks:replace'][] = array('attribs' => $replace);
  797. }
  798. if (count($repl) == 1) {
  799. $file['tasks:replace'] = $file['tasks:replace'][0];
  800. }
  801. }
  802. $ret['dir']['file'][] = $file;
  803. }
  804. return $ret;
  805. }
  806. /**
  807. * Post-process special files with install-as/platform attributes and
  808. * make the release tag.
  809. *
  810. * This complex method follows this work-flow to create the release tags:
  811. *
  812. * <pre>
  813. * - if any install-as/platform exist, create a generic release and fill it with
  814. * o <install as=..> tags for <file name=... install-as=...>
  815. * o <install as=..> tags for <file name=... platform=!... install-as=..>
  816. * o <ignore> tags for <file name=... platform=...>
  817. * o <ignore> tags for <file name=... platform=... install-as=..>
  818. * - create a release for each platform encountered and fill with
  819. * o <install as..> tags for <file name=... install-as=...>
  820. * o <install as..> tags for <file name=... platform=this platform install-as=..>
  821. * o <install as..> tags for <file name=... platform=!other platform install-as=..>
  822. * o <ignore> tags for <file name=... platform=!this platform>
  823. * o <ignore> tags for <file name=... platform=other platform>
  824. * o <ignore> tags for <file name=... platform=other platform install-as=..>
  825. * o <ignore> tags for <file name=... platform=!this platform install-as=..>
  826. * </pre>
  827. *
  828. * It does this by accessing the $package parameter, which contains an array with
  829. * indices:
  830. *
  831. * - platform: mapping of file => OS the file should be installed on
  832. * - install-as: mapping of file => installed name
  833. * - osmap: mapping of OS => list of files that should be installed
  834. * on that OS
  835. * - notosmap: mapping of OS => list of files that should not be
  836. * installed on that OS
  837. *
  838. * @param array
  839. * @param array
  840. * @access private
  841. */
  842. function _convertRelease2_0(&$release, $package)
  843. {
  844. //- if any install-as/platform exist, create a generic release and fill it with
  845. if (count($package['platform']) || count($package['install-as'])) {
  846. $generic = array();
  847. $genericIgnore = array();
  848. foreach ($package['install-as'] as $file => $as) {
  849. //o <install as=..> tags for <file name=... install-as=...>
  850. if (!isset($package['platform'][$file])) {
  851. $generic[] = $file;
  852. continue;
  853. }
  854. //o <install as=..> tags for <file name=... platform=!... install-as=..>
  855. if (isset($package['platform'][$file]) &&
  856. $package['platform'][$file]{0} == '!') {
  857. $generic[] = $file;
  858. continue;
  859. }
  860. //o <ignore> tags for <file name=... platform=... install-as=..>
  861. if (isset($package['platform'][$file]) &&
  862. $package['platform'][$file]{0} != '!') {
  863. $genericIgnore[] = $file;
  864. continue;
  865. }
  866. }
  867. foreach ($package['platform'] as $file => $platform) {
  868. if (isset($package['install-as'][$file])) {
  869. continue;
  870. }
  871. if ($platform{0} != '!') {
  872. //o <ignore> tags for <file name=... platform=...>
  873. $genericIgnore[] = $file;
  874. }
  875. }
  876. if (count($package['platform'])) {
  877. $oses = $notplatform = $platform = array();
  878. foreach ($package['platform'] as $file => $os) {
  879. // get a list of oses
  880. if ($os{0} == '!') {
  881. if (isset($oses[substr($os, 1)])) {
  882. continue;
  883. }
  884. $oses[substr($os, 1)] = count($oses);
  885. } else {
  886. if (isset($oses[$os])) {
  887. continue;
  888. }
  889. $oses[$os] = count($oses);
  890. }
  891. }
  892. //- create a release for each platform encountered and fill with
  893. foreach ($oses as $os => $releaseNum) {
  894. $release[$releaseNum]['installconditions']['os']['name'] = $os;
  895. $release[$releaseNum]['filelist'] = array('install' => array(),
  896. 'ignore' => array());
  897. foreach ($package['install-as'] as $file => $as) {
  898. //o <install as=..> tags for <file name=... install-as=...>
  899. if (!isset($package['platform'][$file])) {
  900. $release[$releaseNum]['filelist']['install'][] =
  901. array(
  902. 'attribs' => array(
  903. 'name' => $file,
  904. 'as' => $as,
  905. ),
  906. );
  907. continue;
  908. }
  909. //o <install as..> tags for
  910. // <file name=... platform=this platform install-as=..>
  911. if (isset($package['platform'][$file]) &&
  912. $package['platform'][$file] == $os) {
  913. $release[$releaseNum]['filelist']['install'][] =
  914. array(
  915. 'attribs' => array(
  916. 'name' => $file,
  917. 'as' => $as,
  918. ),
  919. );
  920. continue;
  921. }
  922. //o <install as..> tags for
  923. // <file name=... platform=!other platform install-as=..>
  924. if (isset($package['platform'][$file]) &&
  925. $package['platform'][$file] != "!$os" &&
  926. $package['platform'][$file]{0} == '!') {
  927. $release[$releaseNum]['filelist']['install'][] =
  928. array(
  929. 'attribs' => array(
  930. 'name' => $file,
  931. 'as' => $as,
  932. ),
  933. );
  934. continue;
  935. }
  936. //o <ignore> tags for
  937. // <file name=... platform=!this platform install-as=..>
  938. if (isset($package['platform'][$file]) &&
  939. $package['platform'][$file] == "!$os") {
  940. $release[$releaseNum]['filelist']['ignore'][] =
  941. array(
  942. 'attribs' => array(
  943. 'name' => $file,
  944. ),
  945. );
  946. continue;
  947. }
  948. //o <ignore> tags for
  949. // <file name=... platform=other platform install-as=..>
  950. if (isset($package['platform'][$file]) &&
  951. $package['platform'][$file]{0} != '!' &&
  952. $package['platform'][$file] != $os) {
  953. $release[$releaseNum]['filelist']['ignore'][] =
  954. array(
  955. 'attribs' => array(
  956. 'name' => $file,
  957. ),
  958. );
  959. continue;
  960. }
  961. }
  962. foreach ($package['platform'] as $file => $platform) {
  963. if (isset($package['install-as'][$file])) {
  964. continue;
  965. }
  966. //o <ignore> tags for <file name=... platform=!this platform>
  967. if ($platform == "!$os") {
  968. $release[$releaseNum]['filelist']['ignore'][] =
  969. array(
  970. 'attribs' => array(
  971. 'name' => $file,
  972. ),
  973. );
  974. continue;
  975. }
  976. //o <ignore> tags for <file name=... platform=other platform>
  977. if ($platform{0} != '!' && $platform != $os) {
  978. $release[$releaseNum]['filelist']['ignore'][] =
  979. array(
  980. 'attribs' => array(
  981. 'name' => $file,
  982. ),
  983. );
  984. }
  985. }
  986. if (!count($release[$releaseNum]['filelist']['install'])) {
  987. unset($release[$releaseNum]['filelist']['install']);
  988. }
  989. if (!count($release[$releaseNum]['filelist']['ignore'])) {
  990. unset($release[$releaseNum]['filelist']['ignore']);
  991. }
  992. }
  993. if (count($generic) || count($genericIgnore)) {
  994. $release[count($oses)] = array();
  995. if (count($generic)) {
  996. foreach ($generic as $file) {
  997. if (isset($package['install-as'][$file])) {
  998. $installas = $package['install-as'][$file];
  999. } else {
  1000. $installas = $file;
  1001. }
  1002. $release[count($oses)]['filelist']['install'][] =
  1003. array(
  1004. 'attribs' => array(
  1005. 'name' => $file,
  1006. 'as' => $installas,
  1007. )
  1008. );
  1009. }
  1010. }
  1011. if (count($genericIgnore)) {
  1012. foreach ($genericIgnore as $file) {
  1013. $release[count($oses)]['filelist']['ignore'][] =
  1014. array(
  1015. 'attribs' => array(
  1016. 'name' => $file,
  1017. )
  1018. );
  1019. }
  1020. }
  1021. }
  1022. // cleanup
  1023. foreach ($release as $i => $rel) {
  1024. if (isset($rel['filelist']['install']) &&
  1025. count($rel['filelist']['install']) == 1) {
  1026. $release[$i]['filelist']['install'] =
  1027. $release[$i]['filelist']['install'][0];
  1028. }
  1029. if (isset($rel['filelist']['ignore']) &&
  1030. count($rel['filelist']['ignore']) == 1) {
  1031. $release[$i]['filelist']['ignore'] =
  1032. $release[$i]['filelist']['ignore'][0];
  1033. }
  1034. }
  1035. if (count($release) == 1) {
  1036. $release = $release[0];
  1037. }
  1038. } else {
  1039. // no platform atts, but some install-as atts
  1040. foreach ($package['install-as'] as $file => $value) {
  1041. $release['filelist']['install'][] =
  1042. array(
  1043. 'attribs' => array(
  1044. 'name' => $file,
  1045. 'as' => $value
  1046. )
  1047. );
  1048. }
  1049. if (count($release['filelist']['install']) == 1) {
  1050. $release['filelist']['install'] = $release['filelist']['install'][0];
  1051. }
  1052. }
  1053. }
  1054. }
  1055. /**
  1056. * @param array
  1057. * @return array
  1058. * @access private
  1059. */
  1060. function _processDep($dep)
  1061. {
  1062. if ($dep['type'] == 'php') {
  1063. if ($dep['rel'] == 'has') {
  1064. // come on - everyone has php!
  1065. return false;
  1066. }
  1067. }
  1068. $php = array();
  1069. if ($dep['type'] != 'php') {
  1070. $php['name'] = $dep['name'];
  1071. if ($dep['type'] == 'pkg') {
  1072. $php['channel'] = 'pear.php.net';
  1073. }
  1074. }
  1075. switch ($dep['rel']) {
  1076. case 'gt' :
  1077. $php['min'] = $dep['version'];
  1078. $php['exclude'] = $dep['version'];
  1079. break;
  1080. case 'ge' :
  1081. if (!isset($dep['version'])) {
  1082. if ($dep['type'] == 'php') {
  1083. if (isset($dep['name'])) {
  1084. $dep['version'] = $dep['name'];
  1085. }
  1086. }
  1087. }
  1088. $php['min'] = $dep['version'];
  1089. break;
  1090. case 'lt' :
  1091. $php['max'] = $dep['version'];
  1092. $php['exclude'] = $dep['version'];
  1093. break;
  1094. case 'le' :
  1095. $php['max'] = $dep['version'];
  1096. break;
  1097. case 'eq' :
  1098. $php['min'] = $dep['version'];
  1099. $php['max'] = $dep['version'];
  1100. break;
  1101. case 'ne' :
  1102. $php['exclude'] = $dep['version'];
  1103. break;
  1104. case 'not' :
  1105. $php['conflicts'] = 'yes';
  1106. break;
  1107. }
  1108. return $php;
  1109. }
  1110. /**
  1111. * @param array
  1112. * @return array
  1113. */
  1114. function _processPhpDeps($deps)
  1115. {
  1116. $test = array();
  1117. foreach ($deps as $dep) {
  1118. $test[] = $this->_processDep($dep);
  1119. }
  1120. $min = array();
  1121. $max = array();
  1122. foreach ($test as $dep) {
  1123. if (!$dep) {
  1124. continue;
  1125. }
  1126. if (isset($dep['min'])) {
  1127. $min[$dep['min']] = count($min);
  1128. }
  1129. if (isset($dep['max'])) {
  1130. $max[$dep['max']] = count($max);
  1131. }
  1132. }
  1133. if (count($min) > 0) {
  1134. uksort($min, 'version_compare');
  1135. }
  1136. if (count($max) > 0) {
  1137. uksort($max, 'version_compare');
  1138. }
  1139. if (count($min)) {
  1140. // get the highest minimum
  1141. $min = array_pop($a = array_flip($min));
  1142. } else {
  1143. $min = false;
  1144. }
  1145. if (count($max)) {
  1146. // get the lowest maximum
  1147. $max = array_shift($a = array_flip($max));
  1148. } else {
  1149. $max = false;
  1150. }
  1151. if ($min) {
  1152. $php['min'] = $min;
  1153. }
  1154. if ($max) {
  1155. $php['max'] = $max;
  1156. }
  1157. $exclude = array();
  1158. foreach ($test as $dep) {
  1159. if (!isset($dep['exclude'])) {
  1160. continue;
  1161. }
  1162. $exclude[] = $dep['exclude'];
  1163. }
  1164. if (count($exclude)) {
  1165. $php['exclude'] = $exclude;
  1166. }
  1167. return $php;
  1168. }
  1169. /**
  1170. * process multiple dependencies that have a name, like package deps
  1171. * @param array
  1172. * @return array
  1173. * @access private
  1174. */
  1175. function _processMultipleDepsName($deps)
  1176. {
  1177. $ret = $tests = array();
  1178. foreach ($deps as $name => $dep) {
  1179. foreach ($dep as $d) {
  1180. $tests[$name][] = $this->_processDep($d);
  1181. }
  1182. }
  1183. foreach ($tests as $name => $test) {
  1184. $max = $min = $php = array();
  1185. $php['name'] = $name;
  1186. foreach ($test as $dep) {
  1187. if (!$dep) {
  1188. continue;
  1189. }
  1190. if (isset($dep['channel'])) {
  1191. $php['channel'] = 'pear.php.net';
  1192. }
  1193. if (isset($dep['conflicts']) && $dep['conflicts'] == 'yes') {
  1194. $php['conflicts'] = 'yes';
  1195. }
  1196. if (isset($dep['min'])) {
  1197. $min[$dep['min']] = count($min);
  1198. }
  1199. if (isset($dep['max'])) {
  1200. $max[$dep['max']] = count($max);
  1201. }
  1202. }
  1203. if (count($min) > 0) {
  1204. uksort($min, 'version_compare');
  1205. }
  1206. if (count($max) > 0) {
  1207. uksort($max, 'version_compare');
  1208. }
  1209. if (count($min)) {
  1210. // get the highest minimum
  1211. $min = array_pop($a = array_flip($min));
  1212. } else {
  1213. $min = false;
  1214. }
  1215. if (count($max)) {
  1216. // get the lowest maximum
  1217. $max = array_shift($a = array_flip($max));
  1218. } else {
  1219. $max = false;
  1220. }
  1221. if ($min) {
  1222. $php['min'] = $min;
  1223. }
  1224. if ($max) {
  1225. $php['max'] = $max

Large files files are truncated, but you can click here to view the full file