3 * PEAR_PackageFile_v1, package.xml version 1.0
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
18 require_once 'PEAR/ErrorStack.php';
21 * Error code if parsing is attempted with no xml extension
23 define('PEAR_PACKAGEFILE_ERROR_NO_XML_EXT', 3);
26 * Error code if creating the xml parser resource fails
28 define('PEAR_PACKAGEFILE_ERROR_CANT_MAKE_PARSER', 4);
31 * Error code used for all sax xml parsing errors
33 define('PEAR_PACKAGEFILE_ERROR_PARSER_ERROR', 5);
36 * Error code used when there is no name
38 define('PEAR_PACKAGEFILE_ERROR_NO_NAME', 6);
41 * Error code when a package name is not valid
43 define('PEAR_PACKAGEFILE_ERROR_INVALID_NAME', 7);
46 * Error code used when no summary is parsed
48 define('PEAR_PACKAGEFILE_ERROR_NO_SUMMARY', 8);
51 * Error code for summaries that are more than 1 line
53 define('PEAR_PACKAGEFILE_ERROR_MULTILINE_SUMMARY', 9);
56 * Error code used when no description is present
58 define('PEAR_PACKAGEFILE_ERROR_NO_DESCRIPTION', 10);
61 * Error code used when no license is present
63 define('PEAR_PACKAGEFILE_ERROR_NO_LICENSE', 11);
66 * Error code used when a <version> version number is not present
68 define('PEAR_PACKAGEFILE_ERROR_NO_VERSION', 12);
71 * Error code used when a <version> version number is invalid
73 define('PEAR_PACKAGEFILE_ERROR_INVALID_VERSION', 13);
76 * Error code when release state is missing
78 define('PEAR_PACKAGEFILE_ERROR_NO_STATE', 14);
81 * Error code when release state is invalid
83 define('PEAR_PACKAGEFILE_ERROR_INVALID_STATE', 15);
86 * Error code when release state is missing
88 define('PEAR_PACKAGEFILE_ERROR_NO_DATE', 16);
91 * Error code when release state is invalid
93 define('PEAR_PACKAGEFILE_ERROR_INVALID_DATE', 17);
96 * Error code when no release notes are found
98 define('PEAR_PACKAGEFILE_ERROR_NO_NOTES', 18);
101 * Error code when no maintainers are found
103 define('PEAR_PACKAGEFILE_ERROR_NO_MAINTAINERS', 19);
106 * Error code when a maintainer has no handle
108 define('PEAR_PACKAGEFILE_ERROR_NO_MAINTHANDLE', 20);
111 * Error code when a maintainer has no handle
113 define('PEAR_PACKAGEFILE_ERROR_NO_MAINTROLE', 21);
116 * Error code when a maintainer has no name
118 define('PEAR_PACKAGEFILE_ERROR_NO_MAINTNAME', 22);
121 * Error code when a maintainer has no email
123 define('PEAR_PACKAGEFILE_ERROR_NO_MAINTEMAIL', 23);
126 * Error code when a maintainer has no handle
128 define('PEAR_PACKAGEFILE_ERROR_INVALID_MAINTROLE', 24);
131 * Error code when a dependency is not a PHP dependency, but has no name
133 define('PEAR_PACKAGEFILE_ERROR_NO_DEPNAME', 25);
136 * Error code when a dependency has no type (pkg, php, etc.)
138 define('PEAR_PACKAGEFILE_ERROR_NO_DEPTYPE', 26);
141 * Error code when a dependency has no relation (lt, ge, has, etc.)
143 define('PEAR_PACKAGEFILE_ERROR_NO_DEPREL', 27);
146 * Error code when a dependency is not a 'has' relation, but has no version
148 define('PEAR_PACKAGEFILE_ERROR_NO_DEPVERSION', 28);
151 * Error code when a dependency has an invalid relation
153 define('PEAR_PACKAGEFILE_ERROR_INVALID_DEPREL', 29);
156 * Error code when a dependency has an invalid type
158 define('PEAR_PACKAGEFILE_ERROR_INVALID_DEPTYPE', 30);
161 * Error code when a dependency has an invalid optional option
163 define('PEAR_PACKAGEFILE_ERROR_INVALID_DEPOPTIONAL', 31);
166 * Error code when a dependency is a pkg dependency, and has an invalid package name
168 define('PEAR_PACKAGEFILE_ERROR_INVALID_DEPNAME', 32);
171 * Error code when a dependency has a channel="foo" attribute, and foo is not a registered channel
173 define('PEAR_PACKAGEFILE_ERROR_UNKNOWN_DEPCHANNEL', 33);
176 * Error code when rel="has" and version attribute is present.
178 define('PEAR_PACKAGEFILE_ERROR_DEPVERSION_IGNORED', 34);
181 * Error code when type="php" and dependency name is present
183 define('PEAR_PACKAGEFILE_ERROR_DEPNAME_IGNORED', 35);
186 * Error code when a configure option has no name
188 define('PEAR_PACKAGEFILE_ERROR_NO_CONFNAME', 36);
191 * Error code when a configure option has no name
193 define('PEAR_PACKAGEFILE_ERROR_NO_CONFPROMPT', 37);
196 * Error code when a file in the filelist has an invalid role
198 define('PEAR_PACKAGEFILE_ERROR_INVALID_FILEROLE', 38);
201 * Error code when a file in the filelist has no role
203 define('PEAR_PACKAGEFILE_ERROR_NO_FILEROLE', 39);
206 * Error code when analyzing a php source file that has parse errors
208 define('PEAR_PACKAGEFILE_ERROR_INVALID_PHPFILE', 40);
211 * Error code when analyzing a php source file reveals a source element
212 * without a package name prefix
214 define('PEAR_PACKAGEFILE_ERROR_NO_PNAME_PREFIX', 41);
217 * Error code when an unknown channel is specified
219 define('PEAR_PACKAGEFILE_ERROR_UNKNOWN_CHANNEL', 42);
222 * Error code when no files are found in the filelist
224 define('PEAR_PACKAGEFILE_ERROR_NO_FILES', 43);
227 * Error code when a file is not valid php according to _analyzeSourceCode()
229 define('PEAR_PACKAGEFILE_ERROR_INVALID_FILE', 44);
232 * Error code when the channel validator returns an error or warning
234 define('PEAR_PACKAGEFILE_ERROR_CHANNELVAL', 45);
237 * Error code when a php5 package is packaged in php4 (analysis doesn't work)
239 define('PEAR_PACKAGEFILE_ERROR_PHP5', 46);
242 * Error code when a file is listed in package.xml but does not exist
244 define('PEAR_PACKAGEFILE_ERROR_FILE_NOTFOUND', 47);
247 * Error code when a <dep type="php" rel="not"... is encountered (use rel="ne")
249 define('PEAR_PACKAGEFILE_PHP_NO_NOT', 48);
252 * Error code when a package.xml contains non-ISO-8859-1 characters
254 define('PEAR_PACKAGEFILE_ERROR_NON_ISO_CHARS', 49);
257 * Error code when a dependency is not a 'has' relation, but has no version
259 define('PEAR_PACKAGEFILE_ERROR_NO_DEPPHPVERSION', 50);
262 * Error code when a package has no lead developer
264 define('PEAR_PACKAGEFILE_ERROR_NO_LEAD', 51);
267 * Error code when a filename begins with "."
269 define('PEAR_PACKAGEFILE_ERROR_INVALID_FILENAME', 52);
271 * package.xml encapsulator
274 * @author Greg Beaver <cellog@php.net>
275 * @copyright 1997-2009 The Authors
276 * @license http://opensource.org/licenses/bsd-license.php New BSD License
277 * @version Release: 1.10.1
278 * @link http://pear.php.net/package/PEAR
279 * @since Class available since Release 1.4.0a1
281 class PEAR_PackageFile_v1
285 * @var PEAR_ErrorStack
291 * A registry object, used to access the package name validation regex for non-standard channels
298 * An object that contains a log method that matches PEAR_Common::log's signature
305 * Parsed package information
312 * path to package.xml
319 * path to package .tgz or false if this is a local/extracted package.xml
332 * Determines whether this packagefile was initialized only with partial package info
334 * If this package file was constructed via parsing REST, it will only contain
342 var $_incomplete = true;
345 * @param bool determines whether to return a PEAR_Error object, or use the PEAR_ErrorStack
346 * @param string Name of Error Stack class to use.
348 function __construct()
350 $this->_stack = new PEAR_ErrorStack('PEAR_PackageFile_v1');
351 $this->_stack->setErrorMessageTemplate($this->_getErrorMessage());
355 function installBinary($installer)
360 function isExtension($name)
365 function setConfig(&$config)
367 $this->_config = &$config;
368 $this->_registry = &$config->getRegistry();
371 function setRequestedGroup()
377 * For saving in the registry.
379 * Set the last version that was installed
382 function setLastInstalledVersion($version)
384 $this->_packageInfo['_lastversion'] = $version;
388 * @return string|false
390 function getLastInstalledVersion()
392 if (isset($this->_packageInfo['_lastversion'])) {
393 return $this->_packageInfo['_lastversion'];
398 function getInstalledBinary()
403 function listPostinstallScripts()
408 function initPostinstallScripts()
413 function setLogger(&$logger)
415 if ($logger && (!is_object($logger) || !method_exists($logger, 'log'))) {
416 return PEAR::raiseError('Logger must be compatible with PEAR_Common::log');
418 $this->_logger = &$logger;
421 function setPackagefile($file, $archive = false)
423 $this->_packageFile = $file;
424 $this->_archiveFile = $archive ? $archive : $file;
427 function getPackageFile()
429 return isset($this->_packageFile) ? $this->_packageFile : false;
432 function getPackageType()
437 function getArchiveFile()
439 return $this->_archiveFile;
442 function packageInfo($field)
444 if (!is_string($field) || empty($field) ||
445 !isset($this->_packageInfo[$field])) {
448 return $this->_packageInfo[$field];
451 function setDirtree($path)
453 if (!isset($this->_packageInfo['dirtree'])) {
454 $this->_packageInfo['dirtree'] = array();
456 $this->_packageInfo['dirtree'][$path] = true;
459 function getDirtree()
461 if (isset($this->_packageInfo['dirtree']) && count($this->_packageInfo['dirtree'])) {
462 return $this->_packageInfo['dirtree'];
467 function resetDirtree()
469 unset($this->_packageInfo['dirtree']);
472 function fromArray($pinfo)
474 $this->_incomplete = false;
475 $this->_packageInfo = $pinfo;
478 function isIncomplete()
480 return $this->_incomplete;
483 function getChannel()
485 return 'pear.php.net';
498 function getExtends()
500 if (isset($this->_packageInfo['extends'])) {
501 return $this->_packageInfo['extends'];
511 if (!$this->validate(PEAR_VALIDATE_NORMAL)) {
514 return $this->getArray();
519 return $this->_packageInfo;
524 return $this->getPackage();
527 function getPackage()
529 if (isset($this->_packageInfo['package'])) {
530 return $this->_packageInfo['package'];
536 * WARNING - don't use this unless you know what you are doing
538 function setRawPackage($package)
540 $this->_packageInfo['package'] = $package;
543 function setPackage($package)
545 $this->_packageInfo['package'] = $package;
546 $this->_isValid = false;
549 function getVersion()
551 if (isset($this->_packageInfo['version'])) {
552 return $this->_packageInfo['version'];
557 function setVersion($version)
559 $this->_packageInfo['version'] = $version;
560 $this->_isValid = false;
563 function clearMaintainers()
565 unset($this->_packageInfo['maintainers']);
568 function getMaintainers()
570 if (isset($this->_packageInfo['maintainers'])) {
571 return $this->_packageInfo['maintainers'];
577 * Adds a new maintainer - no checking of duplicates is performed, use
578 * updatemaintainer for that purpose.
580 function addMaintainer($role, $handle, $name, $email)
582 $this->_packageInfo['maintainers'][] =
583 array('handle' => $handle, 'role' => $role, 'email' => $email, 'name' => $name);
584 $this->_isValid = false;
587 function updateMaintainer($role, $handle, $name, $email)
590 if (!isset($this->_packageInfo['maintainers']) ||
591 !is_array($this->_packageInfo['maintainers'])) {
592 return $this->addMaintainer($role, $handle, $name, $email);
594 foreach ($this->_packageInfo['maintainers'] as $i => $maintainer) {
595 if ($maintainer['handle'] == $handle) {
600 if ($found !== false) {
601 unset($this->_packageInfo['maintainers'][$found]);
602 $this->_packageInfo['maintainers'] =
603 array_values($this->_packageInfo['maintainers']);
605 $this->addMaintainer($role, $handle, $name, $email);
608 function deleteMaintainer($handle)
611 foreach ($this->_packageInfo['maintainers'] as $i => $maintainer) {
612 if ($maintainer['handle'] == $handle) {
617 if ($found !== false) {
618 unset($this->_packageInfo['maintainers'][$found]);
619 $this->_packageInfo['maintainers'] =
620 array_values($this->_packageInfo['maintainers']);
628 if (isset($this->_packageInfo['release_state'])) {
629 return $this->_packageInfo['release_state'];
634 function setRawState($state)
636 $this->_packageInfo['release_state'] = $state;
639 function setState($state)
641 $this->_packageInfo['release_state'] = $state;
642 $this->_isValid = false;
647 if (isset($this->_packageInfo['release_date'])) {
648 return $this->_packageInfo['release_date'];
653 function setDate($date)
655 $this->_packageInfo['release_date'] = $date;
656 $this->_isValid = false;
659 function getLicense()
661 if (isset($this->_packageInfo['release_license'])) {
662 return $this->_packageInfo['release_license'];
667 function setLicense($date)
669 $this->_packageInfo['release_license'] = $date;
670 $this->_isValid = false;
673 function getSummary()
675 if (isset($this->_packageInfo['summary'])) {
676 return $this->_packageInfo['summary'];
681 function setSummary($summary)
683 $this->_packageInfo['summary'] = $summary;
684 $this->_isValid = false;
687 function getDescription()
689 if (isset($this->_packageInfo['description'])) {
690 return $this->_packageInfo['description'];
695 function setDescription($desc)
697 $this->_packageInfo['description'] = $desc;
698 $this->_isValid = false;
703 if (isset($this->_packageInfo['release_notes'])) {
704 return $this->_packageInfo['release_notes'];
709 function setNotes($notes)
711 $this->_packageInfo['release_notes'] = $notes;
712 $this->_isValid = false;
717 if (isset($this->_packageInfo['release_deps'])) {
718 return $this->_packageInfo['release_deps'];
724 * Reset dependencies prior to adding new ones
728 unset($this->_packageInfo['release_deps']);
731 function addPhpDep($version, $rel)
733 $this->_isValid = false;
734 $this->_packageInfo['release_deps'][] =
735 array('type' => 'php',
737 'version' => $version);
740 function addPackageDep($name, $version, $rel, $optional = 'no')
742 $this->_isValid = false;
744 array('type' => 'pkg',
747 'optional' => $optional);
748 if ($rel != 'has' && $rel != 'not') {
749 $dep['version'] = $version;
751 $this->_packageInfo['release_deps'][] = $dep;
754 function addExtensionDep($name, $version, $rel, $optional = 'no')
756 $this->_isValid = false;
757 $this->_packageInfo['release_deps'][] =
758 array('type' => 'ext',
761 'version' => $version,
762 'optional' => $optional);
766 * WARNING - do not use this function directly unless you know what you're doing
768 function setDeps($deps)
770 $this->_packageInfo['release_deps'] = $deps;
775 return isset($this->_packageInfo['release_deps']) &&
776 count($this->_packageInfo['release_deps']);
779 function getDependencyGroup($group)
784 function isCompatible($pf)
789 function isSubpackageOf($p)
791 return $p->isSubpackage($this);
794 function isSubpackage($p)
799 function dependsOn($package, $channel)
801 if (strtolower($channel) != 'pear.php.net') {
804 if (!($deps = $this->getDeps())) {
807 foreach ($deps as $dep) {
808 if ($dep['type'] != 'pkg') {
811 if (strtolower($dep['name']) == strtolower($package)) {
818 function getConfigureOptions()
820 if (isset($this->_packageInfo['configure_options'])) {
821 return $this->_packageInfo['configure_options'];
826 function hasConfigureOptions()
828 return isset($this->_packageInfo['configure_options']) &&
829 count($this->_packageInfo['configure_options']);
832 function addConfigureOption($name, $prompt, $default = false)
834 $o = array('name' => $name, 'prompt' => $prompt);
835 if ($default !== false) {
836 $o['default'] = $default;
838 if (!isset($this->_packageInfo['configure_options'])) {
839 $this->_packageInfo['configure_options'] = array();
841 $this->_packageInfo['configure_options'][] = $o;
844 function clearConfigureOptions()
846 unset($this->_packageInfo['configure_options']);
849 function getProvides()
851 if (isset($this->_packageInfo['provides'])) {
852 return $this->_packageInfo['provides'];
857 function getProvidesExtension()
862 function addFile($dir, $file, $attrs)
864 $dir = preg_replace(array('!\\\\+!', '!/+!'), array('/', '/'), $dir);
865 if ($dir == '/' || $dir == '') {
870 $file = $dir . $file;
871 $file = preg_replace('![\\/]+!', '/', $file);
872 $this->_packageInfo['filelist'][$file] = $attrs;
875 function getInstallationFilelist()
877 return $this->getFilelist();
880 function getFilelist()
882 if (isset($this->_packageInfo['filelist'])) {
883 return $this->_packageInfo['filelist'];
888 function setFileAttribute($file, $attr, $value)
890 $this->_packageInfo['filelist'][$file][$attr] = $value;
893 function resetFilelist()
895 $this->_packageInfo['filelist'] = array();
898 function setInstalledAs($file, $path)
901 return $this->_packageInfo['filelist'][$file]['installed_as'] = $path;
903 unset($this->_packageInfo['filelist'][$file]['installed_as']);
906 function installedFile($file, $atts)
908 if (isset($this->_packageInfo['filelist'][$file])) {
909 $this->_packageInfo['filelist'][$file] =
910 array_merge($this->_packageInfo['filelist'][$file], $atts);
912 $this->_packageInfo['filelist'][$file] = $atts;
916 function getChangelog()
918 if (isset($this->_packageInfo['changelog'])) {
919 return $this->_packageInfo['changelog'];
924 function getPackagexmlVersion()
930 * Wrapper to {@link PEAR_ErrorStack::getErrors()}
931 * @param boolean determines whether to purge the error stack after retrieving
934 function getValidationWarnings($purge = true)
936 return $this->_stack->getErrors($purge);
941 * Validation error. Also marks the object contents as invalid
943 * @param array error information
946 function _validateError($code, $params = array())
948 $this->_stack->push($code, 'error', $params, false, false, debug_backtrace());
949 $this->_isValid = false;
953 * Validation warning. Does not mark the object contents invalid.
955 * @param array error information
958 function _validateWarning($code, $params = array())
960 $this->_stack->push($code, 'warning', $params, false, false, debug_backtrace());
964 * @param integer error code
967 function _getErrorMessage()
970 PEAR_PACKAGEFILE_ERROR_NO_NAME =>
971 'Missing Package Name',
972 PEAR_PACKAGEFILE_ERROR_NO_SUMMARY =>
974 PEAR_PACKAGEFILE_ERROR_MULTILINE_SUMMARY =>
975 'Summary should be on one line',
976 PEAR_PACKAGEFILE_ERROR_NO_DESCRIPTION =>
977 'Missing description',
978 PEAR_PACKAGEFILE_ERROR_NO_LICENSE =>
980 PEAR_PACKAGEFILE_ERROR_NO_VERSION =>
981 'No release version found',
982 PEAR_PACKAGEFILE_ERROR_NO_STATE =>
983 'No release state found',
984 PEAR_PACKAGEFILE_ERROR_NO_DATE =>
985 'No release date found',
986 PEAR_PACKAGEFILE_ERROR_NO_NOTES =>
987 'No release notes found',
988 PEAR_PACKAGEFILE_ERROR_NO_LEAD =>
989 'Package must have at least one lead maintainer',
990 PEAR_PACKAGEFILE_ERROR_NO_MAINTAINERS =>
991 'No maintainers found, at least one must be defined',
992 PEAR_PACKAGEFILE_ERROR_NO_MAINTHANDLE =>
993 'Maintainer %index% has no handle (user ID at channel server)',
994 PEAR_PACKAGEFILE_ERROR_NO_MAINTROLE =>
995 'Maintainer %index% has no role',
996 PEAR_PACKAGEFILE_ERROR_NO_MAINTNAME =>
997 'Maintainer %index% has no name',
998 PEAR_PACKAGEFILE_ERROR_NO_MAINTEMAIL =>
999 'Maintainer %index% has no email',
1000 PEAR_PACKAGEFILE_ERROR_NO_DEPNAME =>
1001 'Dependency %index% is not a php dependency, and has no name',
1002 PEAR_PACKAGEFILE_ERROR_NO_DEPREL =>
1003 'Dependency %index% has no relation (rel)',
1004 PEAR_PACKAGEFILE_ERROR_NO_DEPTYPE =>
1005 'Dependency %index% has no type',
1006 PEAR_PACKAGEFILE_ERROR_DEPNAME_IGNORED =>
1007 'PHP Dependency %index% has a name attribute of "%name%" which will be' .
1009 PEAR_PACKAGEFILE_ERROR_NO_DEPVERSION =>
1010 'Dependency %index% is not a rel="has" or rel="not" dependency, ' .
1011 'and has no version',
1012 PEAR_PACKAGEFILE_ERROR_NO_DEPPHPVERSION =>
1013 'Dependency %index% is a type="php" dependency, ' .
1014 'and has no version',
1015 PEAR_PACKAGEFILE_ERROR_DEPVERSION_IGNORED =>
1016 'Dependency %index% is a rel="%rel%" dependency, versioning is ignored',
1017 PEAR_PACKAGEFILE_ERROR_INVALID_DEPOPTIONAL =>
1018 'Dependency %index% has invalid optional value "%opt%", should be yes or no',
1019 PEAR_PACKAGEFILE_PHP_NO_NOT =>
1020 'Dependency %index%: php dependencies cannot use "not" rel, use "ne"' .
1021 ' to exclude specific versions',
1022 PEAR_PACKAGEFILE_ERROR_NO_CONFNAME =>
1023 'Configure Option %index% has no name',
1024 PEAR_PACKAGEFILE_ERROR_NO_CONFPROMPT =>
1025 'Configure Option %index% has no prompt',
1026 PEAR_PACKAGEFILE_ERROR_NO_FILES =>
1027 'No files in <filelist> section of package.xml',
1028 PEAR_PACKAGEFILE_ERROR_NO_FILEROLE =>
1029 'File "%file%" has no role, expecting one of "%roles%"',
1030 PEAR_PACKAGEFILE_ERROR_INVALID_FILEROLE =>
1031 'File "%file%" has invalid role "%role%", expecting one of "%roles%"',
1032 PEAR_PACKAGEFILE_ERROR_INVALID_FILENAME =>
1033 'File "%file%" cannot start with ".", cannot package or install',
1034 PEAR_PACKAGEFILE_ERROR_INVALID_PHPFILE =>
1035 'Parser error: invalid PHP found in file "%file%"',
1036 PEAR_PACKAGEFILE_ERROR_NO_PNAME_PREFIX =>
1037 'in %file%: %type% "%name%" not prefixed with package name "%package%"',
1038 PEAR_PACKAGEFILE_ERROR_INVALID_FILE =>
1039 'Parser error: invalid PHP file "%file%"',
1040 PEAR_PACKAGEFILE_ERROR_CHANNELVAL =>
1041 'Channel validator error: field "%field%" - %reason%',
1042 PEAR_PACKAGEFILE_ERROR_PHP5 =>
1043 'Error, PHP5 token encountered in %file%, analysis should be in PHP5',
1044 PEAR_PACKAGEFILE_ERROR_FILE_NOTFOUND =>
1045 'File "%file%" in package.xml does not exist',
1046 PEAR_PACKAGEFILE_ERROR_NON_ISO_CHARS =>
1047 'Package.xml contains non-ISO-8859-1 characters, and may not validate',
1052 * Validate XML package definition file.
1057 function validate($state = PEAR_VALIDATE_NORMAL, $nofilechecking = false)
1059 if (($this->_isValid & $state) == $state) {
1062 $this->_isValid = true;
1063 $info = $this->_packageInfo;
1064 if (empty($info['package'])) {
1065 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_NAME);
1066 $this->_packageName = $pn = 'unknown';
1068 $this->_packageName = $pn = $info['package'];
1071 if (empty($info['summary'])) {
1072 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_SUMMARY);
1073 } elseif (strpos(trim($info['summary']), "\n") !== false) {
1074 $this->_validateWarning(PEAR_PACKAGEFILE_ERROR_MULTILINE_SUMMARY,
1075 array('summary' => $info['summary']));
1077 if (empty($info['description'])) {
1078 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_DESCRIPTION);
1080 if (empty($info['release_license'])) {
1081 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_LICENSE);
1083 if (empty($info['version'])) {
1084 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_VERSION);
1086 if (empty($info['release_state'])) {
1087 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_STATE);
1089 if (empty($info['release_date'])) {
1090 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_DATE);
1092 if (empty($info['release_notes'])) {
1093 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_NOTES);
1095 if (empty($info['maintainers'])) {
1096 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_MAINTAINERS);
1100 foreach ($info['maintainers'] as $m) {
1101 if (empty($m['handle'])) {
1102 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_MAINTHANDLE,
1103 array('index' => $i));
1105 if (empty($m['role'])) {
1106 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_MAINTROLE,
1107 array('index' => $i, 'roles' => PEAR_Common::getUserRoles()));
1108 } elseif ($m['role'] == 'lead') {
1111 if (empty($m['name'])) {
1112 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_MAINTNAME,
1113 array('index' => $i));
1115 if (empty($m['email'])) {
1116 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_MAINTEMAIL,
1117 array('index' => $i));
1122 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_LEAD);
1125 if (!empty($info['release_deps'])) {
1127 foreach ($info['release_deps'] as $d) {
1128 if (!isset($d['type']) || empty($d['type'])) {
1129 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_DEPTYPE,
1130 array('index' => $i, 'types' => PEAR_Common::getDependencyTypes()));
1133 if (!isset($d['rel']) || empty($d['rel'])) {
1134 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_DEPREL,
1135 array('index' => $i, 'rels' => PEAR_Common::getDependencyRelations()));
1138 if (!empty($d['optional'])) {
1139 if (!in_array($d['optional'], array('yes', 'no'))) {
1140 $this->_validateError(PEAR_PACKAGEFILE_ERROR_INVALID_DEPOPTIONAL,
1141 array('index' => $i, 'opt' => $d['optional']));
1144 if ($d['rel'] != 'has' && $d['rel'] != 'not' && empty($d['version'])) {
1145 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_DEPVERSION,
1146 array('index' => $i));
1147 } elseif (($d['rel'] == 'has' || $d['rel'] == 'not') && !empty($d['version'])) {
1148 $this->_validateWarning(PEAR_PACKAGEFILE_ERROR_DEPVERSION_IGNORED,
1149 array('index' => $i, 'rel' => $d['rel']));
1151 if ($d['type'] == 'php' && !empty($d['name'])) {
1152 $this->_validateWarning(PEAR_PACKAGEFILE_ERROR_DEPNAME_IGNORED,
1153 array('index' => $i, 'name' => $d['name']));
1154 } elseif ($d['type'] != 'php' && empty($d['name'])) {
1155 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_DEPNAME,
1156 array('index' => $i));
1158 if ($d['type'] == 'php' && empty($d['version'])) {
1159 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_DEPPHPVERSION,
1160 array('index' => $i));
1162 if (($d['rel'] == 'not') && ($d['type'] == 'php')) {
1163 $this->_validateError(PEAR_PACKAGEFILE_PHP_NO_NOT,
1164 array('index' => $i));
1169 if (!empty($info['configure_options'])) {
1171 foreach ($info['configure_options'] as $c) {
1172 if (empty($c['name'])) {
1173 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_CONFNAME,
1174 array('index' => $i));
1176 if (empty($c['prompt'])) {
1177 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_CONFPROMPT,
1178 array('index' => $i));
1183 if (empty($info['filelist'])) {
1184 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_FILES);
1185 $errors[] = 'no files';
1187 foreach ($info['filelist'] as $file => $fa) {
1188 if (empty($fa['role'])) {
1189 $this->_validateError(PEAR_PACKAGEFILE_ERROR_NO_FILEROLE,
1190 array('file' => $file, 'roles' => PEAR_Common::getFileRoles()));
1192 } elseif (!in_array($fa['role'], PEAR_Common::getFileRoles())) {
1193 $this->_validateError(PEAR_PACKAGEFILE_ERROR_INVALID_FILEROLE,
1194 array('file' => $file, 'role' => $fa['role'], 'roles' => PEAR_Common::getFileRoles()));
1196 if (preg_match('~/\.\.?(/|\\z)|^\.\.?/~', str_replace('\\', '/', $file))) {
1197 // file contains .. parent directory or . cur directory references
1198 $this->_validateError(PEAR_PACKAGEFILE_ERROR_INVALID_FILENAME,
1199 array('file' => $file));
1201 if (isset($fa['install-as']) &&
1202 preg_match('~/\.\.?(/|\\z)|^\.\.?/~',
1203 str_replace('\\', '/', $fa['install-as']))) {
1204 // install-as contains .. parent directory or . cur directory references
1205 $this->_validateError(PEAR_PACKAGEFILE_ERROR_INVALID_FILENAME,
1206 array('file' => $file . ' [installed as ' . $fa['install-as'] . ']'));
1208 if (isset($fa['baseinstalldir']) &&
1209 preg_match('~/\.\.?(/|\\z)|^\.\.?/~',
1210 str_replace('\\', '/', $fa['baseinstalldir']))) {
1211 // install-as contains .. parent directory or . cur directory references
1212 $this->_validateError(PEAR_PACKAGEFILE_ERROR_INVALID_FILENAME,
1213 array('file' => $file . ' [baseinstalldir ' . $fa['baseinstalldir'] . ']'));
1217 if (isset($this->_registry) && $this->_isValid) {
1218 $chan = $this->_registry->getChannel('pear.php.net');
1219 if (PEAR::isError($chan)) {
1220 $this->_validateError(PEAR_PACKAGEFILE_ERROR_CHANNELVAL, $chan->getMessage());
1221 return $this->_isValid = 0;
1223 $validator = $chan->getValidationObject();
1224 $validator->setPackageFile($this);
1225 $validator->validate($state);
1226 $failures = $validator->getFailures();
1227 foreach ($failures['errors'] as $error) {
1228 $this->_validateError(PEAR_PACKAGEFILE_ERROR_CHANNELVAL, $error);
1230 foreach ($failures['warnings'] as $warning) {
1231 $this->_validateWarning(PEAR_PACKAGEFILE_ERROR_CHANNELVAL, $warning);
1234 if ($this->_isValid && $state == PEAR_VALIDATE_PACKAGING && !$nofilechecking) {
1235 if ($this->_analyzePhpFiles()) {
1236 $this->_isValid = true;
1239 if ($this->_isValid) {
1240 return $this->_isValid = $state;
1242 return $this->_isValid = 0;
1245 function _analyzePhpFiles()
1247 if (!$this->_isValid) {
1250 if (!isset($this->_packageFile)) {
1253 $dir_prefix = dirname($this->_packageFile);
1254 $common = new PEAR_Common;
1255 $log = isset($this->_logger) ? array(&$this->_logger, 'log') :
1256 array($common, 'log');
1257 $info = $this->getFilelist();
1258 foreach ($info as $file => $fa) {
1259 if (!file_exists($dir_prefix . DIRECTORY_SEPARATOR . $file)) {
1260 $this->_validateError(PEAR_PACKAGEFILE_ERROR_FILE_NOTFOUND,
1261 array('file' => realpath($dir_prefix) . DIRECTORY_SEPARATOR . $file));
1264 if ($fa['role'] == 'php' && $dir_prefix) {
1265 call_user_func_array($log, array(1, "Analyzing $file"));
1266 $srcinfo = $this->_analyzeSourceCode($dir_prefix . DIRECTORY_SEPARATOR . $file);
1268 $this->_buildProvidesArray($srcinfo);
1272 $this->_packageName = $pn = $this->getPackage();
1274 if (isset($this->_packageInfo['provides'])) {
1275 foreach ((array) $this->_packageInfo['provides'] as $key => $what) {
1276 if (isset($what['explicit'])) {
1277 // skip conformance checks if the provides entry is
1278 // specified in the package.xml file
1282 if ($type == 'class') {
1283 if (!strncasecmp($name, $pn, $pnl)) {
1286 $this->_validateWarning(PEAR_PACKAGEFILE_ERROR_NO_PNAME_PREFIX,
1287 array('file' => $file, 'type' => $type, 'name' => $name, 'package' => $pn));
1288 } elseif ($type == 'function') {
1289 if (strstr($name, '::') || !strncasecmp($name, $pn, $pnl)) {
1292 $this->_validateWarning(PEAR_PACKAGEFILE_ERROR_NO_PNAME_PREFIX,
1293 array('file' => $file, 'type' => $type, 'name' => $name, 'package' => $pn));
1297 return $this->_isValid;
1301 * Get the default xml generator object
1303 * @return PEAR_PackageFile_Generator_v1
1305 function &getDefaultGenerator()
1307 if (!class_exists('PEAR_PackageFile_Generator_v1')) {
1308 require_once 'PEAR/PackageFile/Generator/v1.php';
1310 $a = new PEAR_PackageFile_Generator_v1($this);
1315 * Get the contents of a file listed within the package.xml
1319 function getFileContents($file)
1321 if ($this->_archiveFile == $this->_packageFile) { // unpacked
1322 $dir = dirname($this->_packageFile);
1323 $file = $dir . DIRECTORY_SEPARATOR . $file;
1324 $file = str_replace(array('/', '\\'),
1325 array(DIRECTORY_SEPARATOR, DIRECTORY_SEPARATOR), $file);
1326 if (file_exists($file) && is_readable($file)) {
1327 return implode('', file($file));
1330 if (!class_exists('Archive_Tar')) {
1331 require_once 'Archive/Tar.php';
1333 $tar = new Archive_Tar($this->_archiveFile);
1334 $tar->pushErrorHandling(PEAR_ERROR_RETURN);
1335 if ($file != 'package.xml' && $file != 'package2.xml') {
1336 $file = $this->getPackage() . '-' . $this->getVersion() . '/' . $file;
1338 $file = $tar->extractInString($file);
1339 $tar->popErrorHandling();
1340 if (PEAR::isError($file)) {
1341 return PEAR::raiseError("Cannot locate file '$file' in archive");
1347 // {{{ analyzeSourceCode()
1349 * Analyze the source code of the given PHP file
1351 * @param string Filename of the PHP file
1355 function _analyzeSourceCode($file)
1357 if (!function_exists("token_get_all")) {
1360 if (!defined('T_DOC_COMMENT')) {
1361 define('T_DOC_COMMENT', T_COMMENT);
1363 if (!defined('T_INTERFACE')) {
1364 define('T_INTERFACE', -1);
1366 if (!defined('T_IMPLEMENTS')) {
1367 define('T_IMPLEMENTS', -1);
1369 if (!$fp = @fopen($file, "r")) {
1373 $contents = file_get_contents($file);
1374 $tokens = token_get_all($contents);
1376 for ($i = 0; $i < sizeof($tokens); $i++) {
1377 @list($token, $data) = $tokens[$i];
1378 if (is_string($token)) {
1381 print token_name($token) . ' ';
1382 var_dump(rtrim($data));
1391 $current_class = '';
1392 $current_interface = '';
1393 $current_class_level = -1;
1394 $current_function = '';
1395 $current_function_level = -1;
1396 $declared_classes = array();
1397 $declared_interfaces = array();
1398 $declared_functions = array();
1399 $declared_methods = array();
1400 $used_classes = array();
1401 $used_functions = array();
1403 $implements = array();
1407 for ($i = 0; $i < sizeof($tokens); $i++) {
1408 if (is_array($tokens[$i])) {
1409 list($token, $data) = $tokens[$i];
1411 $token = $tokens[$i];
1415 if ($token != '"' && $token != T_END_HEREDOC) {
1427 $current_function = '';
1428 $current_function_level = -1;
1432 case T_START_HEREDOC:
1436 case T_DOLLAR_OPEN_CURLY_BRACES:
1437 case '{': $brace_level++; continue 2;
1440 if ($current_class_level == $brace_level) {
1441 $current_class = '';
1442 $current_class_level = -1;
1444 if ($current_function_level == $brace_level) {
1445 $current_function = '';
1446 $current_function_level = -1;
1449 case '[': $bracket_level++; continue 2;
1450 case ']': $bracket_level--; continue 2;
1451 case '(': $paren_level++; continue 2;
1452 case ')': $paren_level--; continue 2;
1456 if (($current_class_level != -1) || ($current_function_level != -1)) {
1457 $this->_validateError(PEAR_PACKAGEFILE_ERROR_INVALID_PHPFILE,
1458 array('file' => $file));
1468 if ($look_for == T_CLASS) {
1469 $current_class = $data;
1470 $current_class_level = $brace_level;
1471 $declared_classes[] = $current_class;
1472 } elseif ($look_for == T_INTERFACE) {
1473 $current_interface = $data;
1474 $current_class_level = $brace_level;
1475 $declared_interfaces[] = $current_interface;
1476 } elseif ($look_for == T_IMPLEMENTS) {
1477 $implements[$current_class] = $data;
1478 } elseif ($look_for == T_EXTENDS) {
1479 $extends[$current_class] = $data;
1480 } elseif ($look_for == T_FUNCTION) {
1481 if ($current_class) {
1482 $current_function = "$current_class::$data";
1483 $declared_methods[$current_class][] = $data;
1484 } elseif ($current_interface) {
1485 $current_function = "$current_interface::$data";
1486 $declared_methods[$current_interface][] = $data;
1488 $current_function = $data;
1489 $declared_functions[] = $current_function;
1491 $current_function_level = $brace_level;
1493 } elseif ($look_for == T_NEW) {
1494 $used_classes[$data] = true;
1503 if (preg_match('!^/\*\*\s!', $data)) {
1504 $lastphpdoc = $data;
1505 if (preg_match_all('/@nodep\s+(\S+)/', $lastphpdoc, $m)) {
1506 $nodeps = array_merge($nodeps, $m[1]);
1510 case T_DOUBLE_COLON:
1511 if (!($tokens[$i - 1][0] == T_WHITESPACE || $tokens[$i - 1][0] == T_STRING)) {
1512 $this->_validateError(PEAR_PACKAGEFILE_ERROR_INVALID_PHPFILE,
1513 array('file' => $file));
1516 $class = $tokens[$i - 1][1];
1517 if (strtolower($class) != 'parent') {
1518 $used_classes[$class] = true;
1524 "source_file" => $file,
1525 "declared_classes" => $declared_classes,
1526 "declared_interfaces" => $declared_interfaces,
1527 "declared_methods" => $declared_methods,
1528 "declared_functions" => $declared_functions,
1529 "used_classes" => array_diff(array_keys($used_classes), $nodeps),
1530 "inheritance" => $extends,
1531 "implements" => $implements,
1536 * Build a "provides" array from data returned by
1537 * analyzeSourceCode(). The format of the built array is like
1541 * 'class;MyClass' => 'array('type' => 'class', 'name' => 'MyClass'),
1546 * @param array $srcinfo array with information about a source file
1547 * as returned by the analyzeSourceCode() method.
1554 function _buildProvidesArray($srcinfo)
1556 if (!$this->_isValid) {
1559 $file = basename($srcinfo['source_file']);
1560 $pn = $this->getPackage();
1562 foreach ($srcinfo['declared_classes'] as $class) {
1563 $key = "class;$class";
1564 if (isset($this->_packageInfo['provides'][$key])) {
1567 $this->_packageInfo['provides'][$key] =
1568 array('file'=> $file, 'type' => 'class', 'name' => $class);
1569 if (isset($srcinfo['inheritance'][$class])) {
1570 $this->_packageInfo['provides'][$key]['extends'] =
1571 $srcinfo['inheritance'][$class];
1574 foreach ($srcinfo['declared_methods'] as $class => $methods) {
1575 foreach ($methods as $method) {
1576 $function = "$class::$method";
1577 $key = "function;$function";
1578 if ($method{0} == '_' || !strcasecmp($method, $class) ||
1579 isset($this->_packageInfo['provides'][$key])) {
1582 $this->_packageInfo['provides'][$key] =
1583 array('file'=> $file, 'type' => 'function', 'name' => $function);
1587 foreach ($srcinfo['declared_functions'] as $function) {
1588 $key = "function;$function";
1589 if ($function{0} == '_' || isset($this->_packageInfo['provides'][$key])) {
1592 if (!strstr($function, '::') && strncasecmp($function, $pn, $pnl)) {
1593 $warnings[] = "in1 " . $file . ": function \"$function\" not prefixed with package name \"$pn\"";
1595 $this->_packageInfo['provides'][$key] =
1596 array('file'=> $file, 'type' => 'function', 'name' => $function);