3 * PEAR_PackageFile, package.xml parsing utility class
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 * @link http://pear.php.net/package/PEAR
13 * @since File available since Release 1.4.0a1
17 * needed for PEAR_VALIDATE_* constants
19 require_once 'PEAR/Validate.php';
21 * Error code if the package.xml <package> tag does not contain a valid version
23 define('PEAR_PACKAGEFILE_ERROR_NO_PACKAGEVERSION', 1);
25 * Error code if the package.xml <package> tag version is not supported (version 1.0 and 1.1 are the only supported versions,
28 define('PEAR_PACKAGEFILE_ERROR_INVALID_PACKAGEVERSION', 2);
30 * Abstraction for the package.xml package description file
34 * @author Greg Beaver <cellog@php.net>
35 * @copyright 1997-2009 The Authors
36 * @license http://opensource.org/licenses/bsd-license.php New BSD License
37 * @version Release: 1.10.1
38 * @link http://pear.php.net/package/PEAR
39 * @since Class available since Release 1.4.0a1
41 class PEAR_PackageFile
53 var $_rawReturn = false;
56 * helper for extracting Archive_Tar errors
60 var $_extractErrors = array();
64 * @param PEAR_Config $config
66 * @param string @tmpdir Optional temporary directory for uncompressing
69 function __construct(&$config, $debug = false)
71 $this->_config = $config;
72 $this->_debug = $debug;
76 * Turn off validation - return a parsed package.xml without checking it
78 * This is used by the package-validate command
82 $this->_rawReturn = true;
85 function setLogger(&$l)
91 * Create a PEAR_PackageFile_Parser_v* of a given version.
93 * @return PEAR_PackageFile_Parser_v1|PEAR_PackageFile_Parser_v1
95 function &parserFactory($version)
97 if (!in_array($version{0}, array('1', '2'))) {
102 include_once 'PEAR/PackageFile/Parser/v' . $version{0} . '.php';
103 $version = $version{0};
104 $class = "PEAR_PackageFile_Parser_v$version";
110 * For simpler unit-testing
113 function getClassPrefix()
115 return 'PEAR_PackageFile_v';
119 * Create a PEAR_PackageFile_v* of a given version.
120 * @param int $version
121 * @return PEAR_PackageFile_v1|PEAR_PackageFile_v1
123 function &factory($version)
125 if (!in_array($version{0}, array('1', '2'))) {
130 include_once 'PEAR/PackageFile/v' . $version{0} . '.php';
131 $version = $version{0};
132 $class = $this->getClassPrefix() . $version;
138 * Create a PEAR_PackageFile_v* from its toArray() method
140 * WARNING: no validation is performed, the array is assumed to be valid,
141 * always parse from xml if you want validation.
143 * @return PEAR_PackageFileManager_v1|PEAR_PackageFileManager_v2
144 * @uses factory() to construct the returned object.
146 function &fromArray($arr)
148 if (isset($arr['xsdversion'])) {
149 $obj = &$this->factory($arr['xsdversion']);
150 if ($this->_logger) {
151 $obj->setLogger($this->_logger);
154 $obj->setConfig($this->_config);
155 $obj->fromArray($arr);
159 if (isset($arr['package']['attribs']['version'])) {
160 $obj = &$this->factory($arr['package']['attribs']['version']);
162 $obj = &$this->factory('1.0');
165 if ($this->_logger) {
166 $obj->setLogger($this->_logger);
169 $obj->setConfig($this->_config);
170 $obj->fromArray($arr);
175 * Create a PEAR_PackageFile_v* from an XML string.
177 * @param string $data contents of package.xml file
178 * @param int $state package state (one of PEAR_VALIDATE_* constants)
179 * @param string $file full path to the package.xml file (and the files
181 * @param string $archive optional name of the archive that the XML was
182 * extracted from, if any
183 * @return PEAR_PackageFile_v1|PEAR_PackageFile_v2
184 * @uses parserFactory() to construct a parser to load the package.
186 function &fromXmlString($data, $state, $file, $archive = false)
188 if (preg_match('/<package[^>]+version=[\'"]([0-9]+\.[0-9]+)[\'"]/', $data, $packageversion)) {
189 if (!in_array($packageversion[1], array('1.0', '2.0', '2.1'))) {
190 return PEAR::raiseError('package.xml version "' . $packageversion[1] .
191 '" is not supported, only 1.0, 2.0, and 2.1 are supported.');
194 $object = &$this->parserFactory($packageversion[1]);
195 if ($this->_logger) {
196 $object->setLogger($this->_logger);
199 $object->setConfig($this->_config);
200 $pf = $object->parse($data, $file, $archive);
201 if (PEAR::isError($pf)) {
205 if ($this->_rawReturn) {
209 if (!$pf->validate($state)) {;
210 if ($this->_config->get('verbose') > 0
211 && $this->_logger && $pf->getValidationWarnings(false)
213 foreach ($pf->getValidationWarnings(false) as $warning) {
214 $this->_logger->log(0, 'ERROR: ' . $warning['message']);
218 $a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed',
219 2, null, null, $pf->getValidationWarnings());
223 if ($this->_logger && $pf->getValidationWarnings(false)) {
224 foreach ($pf->getValidationWarnings() as $warning) {
225 $this->_logger->log(0, 'WARNING: ' . $warning['message']);
229 if (method_exists($pf, 'flattenFilelist')) {
230 $pf->flattenFilelist(); // for v2
234 } elseif (preg_match('/<package[^>]+version=[\'"]([^"\']+)[\'"]/', $data, $packageversion)) {
235 $a = PEAR::raiseError('package.xml file "' . $file .
236 '" has unsupported package.xml <package> version "' . $packageversion[1] . '"');
239 if (!class_exists('PEAR_ErrorStack')) {
240 require_once 'PEAR/ErrorStack.php';
243 PEAR_ErrorStack::staticPush('PEAR_PackageFile',
244 PEAR_PACKAGEFILE_ERROR_NO_PACKAGEVERSION,
245 'warning', array('xml' => $data), 'package.xml "' . $file .
246 '" has no package.xml <package> version');
247 $object = &$this->parserFactory('1.0');
248 $object->setConfig($this->_config);
249 $pf = $object->parse($data, $file, $archive);
250 if (PEAR::isError($pf)) {
254 if ($this->_rawReturn) {
258 if (!$pf->validate($state)) {
259 $a = PEAR::raiseError('Parsing of package.xml from file "' . $file . '" failed',
260 2, null, null, $pf->getValidationWarnings());
264 if ($this->_logger && $pf->getValidationWarnings(false)) {
265 foreach ($pf->getValidationWarnings() as $warning) {
266 $this->_logger->log(0, 'WARNING: ' . $warning['message']);
270 if (method_exists($pf, 'flattenFilelist')) {
271 $pf->flattenFilelist(); // for v2
279 * Register a temporary file or directory. When the destructor is
280 * executed, all registered temporary files and directories are
283 * @param string $file name of file or directory
286 function addTempFile($file)
288 $GLOBALS['_PEAR_Common_tempfiles'][] = $file;
292 * Create a PEAR_PackageFile_v* from a compresed Tar or Tgz file.
294 * @param string contents of package.xml file
295 * @param int package state (one of PEAR_VALIDATE_* constants)
296 * @return PEAR_PackageFile_v1|PEAR_PackageFile_v2
297 * @using Archive_Tar to extract the files
298 * @using fromPackageFile() to load the package after the package.xml
301 function &fromTgzFile($file, $state)
303 if (!class_exists('Archive_Tar')) {
304 require_once 'Archive/Tar.php';
307 $tar = new Archive_Tar($file);
308 if ($this->_debug <= 1) {
309 $tar->pushErrorHandling(PEAR_ERROR_RETURN);
312 $content = $tar->listContent();
313 if ($this->_debug <= 1) {
314 $tar->popErrorHandling();
317 if (!is_array($content)) {
318 if (is_string($file) && strlen($file < 255) &&
319 (!file_exists($file) || !@is_file($file))) {
320 $ret = PEAR::raiseError("could not open file \"$file\"");
324 $file = realpath($file);
325 $ret = PEAR::raiseError("Could not get contents of package \"$file\"".
326 '. Invalid tgz file.');
330 if (!count($content) && !@is_file($file)) {
331 $ret = PEAR::raiseError("could not open file \"$file\"");
337 foreach ($content as $file) {
338 $name = $file['filename'];
339 if ($name == 'package2.xml') { // allow a .tgz to distribute both versions
344 if ($name == 'package.xml') {
347 } elseif (preg_match('/package.xml$/', $name, $match)) {
353 $tmpdir = System::mktemp('-t "' . $this->_config->get('temp_dir') . '" -d pear');
354 if ($tmpdir === false) {
355 $ret = PEAR::raiseError("there was a problem with getting the configured temp directory");
359 PEAR_PackageFile::addTempFile($tmpdir);
361 $this->_extractErrors();
362 PEAR::staticPushErrorHandling(PEAR_ERROR_CALLBACK, array($this, '_extractErrors'));
364 if (!$xml || !$tar->extractList(array($xml), $tmpdir)) {
365 $extra = implode("\n", $this->_extractErrors());
367 $extra = ' ' . $extra;
370 PEAR::staticPopErrorHandling();
371 $ret = PEAR::raiseError('could not extract the package.xml file from "' .
372 $origfile . '"' . $extra);
376 PEAR::staticPopErrorHandling();
377 $ret = &PEAR_PackageFile::fromPackageFile("$tmpdir/$xml", $state, $origfile);
382 * helper callback for extracting Archive_Tar errors
384 * @param PEAR_Error|null $err
388 function _extractErrors($err = null)
390 static $errors = array();
396 $errors[] = $err->getMessage();
400 * Create a PEAR_PackageFile_v* from a package.xml file.
403 * @param string $descfile name of package xml file
404 * @param int $state package state (one of PEAR_VALIDATE_* constants)
405 * @param string|false $archive name of the archive this package.xml came
407 * @return PEAR_PackageFile_v1|PEAR_PackageFile_v2
408 * @uses PEAR_PackageFile::fromXmlString to create the oject after the
409 * XML is loaded from the package.xml file.
411 function &fromPackageFile($descfile, $state, $archive = false)
414 if (is_string($descfile) && strlen($descfile) < 255 &&
416 !file_exists($descfile) || !is_file($descfile) || !is_readable($descfile)
417 || (!$fp = @fopen($descfile, 'r'))
420 $a = PEAR::raiseError("Unable to open $descfile");
424 // read the whole thing so we only get one cdata callback
425 // for each block of cdata
427 $data = file_get_contents($descfile);
428 $ret = &PEAR_PackageFile::fromXmlString($data, $state, $descfile, $archive);
433 * Create a PEAR_PackageFile_v* from a .tgz archive or package.xml file.
435 * This method is able to extract information about a package from a .tgz
436 * archive or from a XML package definition file.
439 * @param string $info file name
440 * @param int $state package state (one of PEAR_VALIDATE_* constants)
441 * @return PEAR_PackageFile_v1|PEAR_PackageFile_v2
442 * @uses fromPackageFile() if the file appears to be XML
443 * @uses fromTgzFile() to load all non-XML files
445 function &fromAnyFile($info, $state)
448 $dir_name = realpath($info);
449 if (file_exists($dir_name . '/package.xml')) {
450 $info = PEAR_PackageFile::fromPackageFile($dir_name . '/package.xml', $state);
451 } elseif (file_exists($dir_name . '/package2.xml')) {
452 $info = PEAR_PackageFile::fromPackageFile($dir_name . '/package2.xml', $state);
454 $info = PEAR::raiseError("No package definition found in '$info' directory");
461 if (is_string($info) && strlen($info) < 255 &&
462 (file_exists($info) || ($fp = @fopen($info, 'r')))
469 $tmp = substr($info, -4);
470 if ($tmp == '.xml') {
471 $info = &PEAR_PackageFile::fromPackageFile($info, $state);
472 } elseif ($tmp == '.tar' || $tmp == '.tgz') {
473 $info = &PEAR_PackageFile::fromTgzFile($info, $state);
475 $fp = fopen($info, 'r');
476 $test = fread($fp, 5);
478 if ($test == '<?xml') {
479 $info = &PEAR_PackageFile::fromPackageFile($info, $state);
481 $info = &PEAR_PackageFile::fromTgzFile($info, $state);
488 $info = PEAR::raiseError("Cannot open '$info' for parsing");