3 * <tasks:postinstallscript>
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/Task/Common.php';
20 * Implements the postinstallscript file task.
22 * Note that post-install scripts are handled separately from installation, by the
23 * "pear run-scripts" command
27 * @author Greg Beaver <cellog@php.net>
28 * @copyright 1997-2009 The Authors
29 * @license http://opensource.org/licenses/bsd-license.php New BSD License
30 * @version Release: 1.10.1
31 * @link http://pear.php.net/package/PEAR
32 * @since Class available since Release 1.4.0a1
34 class PEAR_Task_Postinstallscript extends PEAR_Task_Common
36 public $type = 'script';
42 * @var PEAR_PackageFile_v2
46 public $phase = PEAR_TASK_INSTALL;
49 * Validate the raw xml at parsing-time.
51 * This also attempts to validate the script to make sure it meets the criteria
52 * for a post-install script
54 * @param PEAR_PackageFile_v2
55 * @param array The XML contents of the <postinstallscript> tag
57 * @param array the entire parsed <file> tag
59 public static function validateXml($pkg, $xml, $config, $fileXml)
61 if ($fileXml['role'] != 'php') {
62 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
63 $fileXml['name'].'" must be role="php"', );
65 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
66 $file = $pkg->getFileContents($fileXml['name']);
67 if (PEAR::isError($file)) {
68 PEAR::popErrorHandling();
70 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
71 $fileXml['name'].'" is not valid: '.
72 $file->getMessage(), );
73 } elseif ($file === null) {
74 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
75 $fileXml['name'].'" could not be retrieved for processing!', );
77 $analysis = $pkg->analyzeSourceCode($file, true);
79 PEAR::popErrorHandling();
81 foreach ($pkg->getValidationWarnings() as $warn) {
82 $warnings .= $warn['message']."\n";
85 return array(PEAR_TASK_ERROR_INVALID, 'Analysis of post-install script "'.
86 $fileXml['name'].'" failed: '.$warnings, );
88 if (count($analysis['declared_classes']) != 1) {
89 PEAR::popErrorHandling();
91 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
92 $fileXml['name'].'" must declare exactly 1 class', );
94 $class = $analysis['declared_classes'][0];
95 if ($class != str_replace(
96 array('/', '.php'), array('_', ''),
99 PEAR::popErrorHandling();
101 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
102 $fileXml['name'].'" class "'.$class.'" must be named "'.
104 array('/', '.php'), array('_', ''),
106 ).'_postinstall"', );
108 if (!isset($analysis['declared_methods'][$class])) {
109 PEAR::popErrorHandling();
111 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
112 $fileXml['name'].'" must declare methods init() and run()', );
114 $methods = array('init' => 0, 'run' => 1);
115 foreach ($analysis['declared_methods'][$class] as $method) {
116 if (isset($methods[$method])) {
117 unset($methods[$method]);
120 if (count($methods)) {
121 PEAR::popErrorHandling();
123 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
124 $fileXml['name'].'" must declare methods init() and run()', );
127 PEAR::popErrorHandling();
128 $definedparams = array();
129 $tasksNamespace = $pkg->getTasksNs().':';
130 if (!isset($xml[$tasksNamespace.'paramgroup']) && isset($xml['paramgroup'])) {
131 // in order to support the older betas, which did not expect internal tags
132 // to also use the namespace
133 $tasksNamespace = '';
135 if (isset($xml[$tasksNamespace.'paramgroup'])) {
136 $params = $xml[$tasksNamespace.'paramgroup'];
137 if (!is_array($params) || !isset($params[0])) {
138 $params = array($params);
140 foreach ($params as $param) {
141 if (!isset($param[$tasksNamespace.'id'])) {
142 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
143 $fileXml['name'].'" <paramgroup> must have '.
144 'an '.$tasksNamespace.'id> tag', );
146 if (isset($param[$tasksNamespace.'name'])) {
147 if (!in_array($param[$tasksNamespace.'name'], $definedparams)) {
148 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
149 $fileXml['name'].'" '.$tasksNamespace.
150 'paramgroup> id "'.$param[$tasksNamespace.'id'].
151 '" parameter "'.$param[$tasksNamespace.'name'].
152 '" has not been previously defined', );
154 if (!isset($param[$tasksNamespace.'conditiontype'])) {
155 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
156 $fileXml['name'].'" '.$tasksNamespace.
157 'paramgroup> id "'.$param[$tasksNamespace.'id'].
158 '" must have a '.$tasksNamespace.
159 'conditiontype> tag containing either "=", '.
160 '"!=", or "preg_match"', );
163 $param[$tasksNamespace.'conditiontype'],
164 array('=', '!=', 'preg_match')
166 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
167 $fileXml['name'].'" '.$tasksNamespace.
168 'paramgroup> id "'.$param[$tasksNamespace.'id'].
169 '" must have a '.$tasksNamespace.
170 'conditiontype> tag containing either "=", '.
171 '"!=", or "preg_match"', );
173 if (!isset($param[$tasksNamespace.'value'])) {
174 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
175 $fileXml['name'].'" '.$tasksNamespace.
176 'paramgroup> id "'.$param[$tasksNamespace.'id'].
177 '" must have a '.$tasksNamespace.
178 'value> tag containing expected parameter value', );
181 if (isset($param[$tasksNamespace.'instructions'])) {
182 if (!is_string($param[$tasksNamespace.'instructions'])) {
183 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
184 $fileXml['name'].'" '.$tasksNamespace.
185 'paramgroup> id "'.$param[$tasksNamespace.'id'].
186 '" '.$tasksNamespace.'instructions> must be simple text', );
189 if (!isset($param[$tasksNamespace.'param'])) {
190 continue; // <param> is no longer required
192 $subparams = $param[$tasksNamespace.'param'];
193 if (!is_array($subparams) || !isset($subparams[0])) {
194 $subparams = array($subparams);
196 foreach ($subparams as $subparam) {
197 if (!isset($subparam[$tasksNamespace.'name'])) {
198 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
199 $fileXml['name'].'" parameter for '.
200 $tasksNamespace.'paramgroup> id "'.
201 $param[$tasksNamespace.'id'].'" must have '.
202 'a '.$tasksNamespace.'name> tag', );
206 $subparam[$tasksNamespace.'name']
208 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
209 $fileXml['name'].'" parameter "'.
210 $subparam[$tasksNamespace.'name'].
211 '" for '.$tasksNamespace.'paramgroup> id "'.
212 $param[$tasksNamespace.'id'].
213 '" is not a valid name. Must contain only alphanumeric characters', );
215 if (!isset($subparam[$tasksNamespace.'prompt'])) {
216 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
217 $fileXml['name'].'" parameter "'.
218 $subparam[$tasksNamespace.'name'].
219 '" for '.$tasksNamespace.'paramgroup> id "'.
220 $param[$tasksNamespace.'id'].
221 '" must have a '.$tasksNamespace.'prompt> tag', );
223 if (!isset($subparam[$tasksNamespace.'type'])) {
224 return array(PEAR_TASK_ERROR_INVALID, 'Post-install script "'.
225 $fileXml['name'].'" parameter "'.
226 $subparam[$tasksNamespace.'name'].
227 '" for '.$tasksNamespace.'paramgroup> id "'.
228 $param[$tasksNamespace.'id'].
229 '" must have a '.$tasksNamespace.'type> tag', );
231 $definedparams[] = $param[$tasksNamespace.'id'].'::'.
232 $subparam[$tasksNamespace.'name'];
241 * Initialize a task instance with the parameters
242 * @param array $xml raw, parsed xml
243 * @param array $fileattribs attributes from the <file> tag containing
245 * @param string|null $lastversion last installed version of this package,
246 * if any (useful for upgrades)
248 public function init($xml, $fileattribs, $lastversion)
250 $this->_class = str_replace('/', '_', $fileattribs['name']);
251 $this->_filename = $fileattribs['name'];
252 $this->_class = str_replace('.php', '', $this->_class).'_postinstall';
253 $this->_params = $xml;
254 $this->_lastversion = $lastversion;
258 * Strip the tasks: namespace from internal params
262 public function _stripNamespace($params = null)
264 if ($params === null) {
266 if (!is_array($this->_params)) {
269 foreach ($this->_params as $i => $param) {
270 if (is_array($param)) {
271 $param = $this->_stripNamespace($param);
273 $params[str_replace($this->_pkg->getTasksNs().':', '', $i)] = $param;
275 $this->_params = $params;
277 $newparams = array();
278 foreach ($params as $i => $param) {
279 if (is_array($param)) {
280 $param = $this->_stripNamespace($param);
282 $newparams[str_replace($this->_pkg->getTasksNs().':', '', $i)] = $param;
290 * Unlike other tasks, the installed file name is passed in instead of the
291 * file contents, because this task is handled post-installation
293 * @param mixed $pkg PEAR_PackageFile_v1|PEAR_PackageFile_v2
294 * @param string $contents file name
296 * @return bool|PEAR_Error false to skip this file, PEAR_Error to fail
297 * (use $this->throwError)
299 public function startSession($pkg, $contents)
301 if ($this->installphase != PEAR_TASK_INSTALL) {
304 // remove the tasks: namespace if present
306 $this->_stripNamespace();
308 0, 'Including external post-installation script "'.
309 $contents.'" - any errors are in this script'
311 include_once $contents;
312 if (class_exists($this->_class)) {
313 $this->logger->log(0, 'Inclusion succeeded');
315 return $this->throwError(
316 'init of post-install script class "'.$this->_class
320 $this->_obj = new $this->_class();
321 $this->logger->log(1, 'running post-install script "'.$this->_class.'->init()"');
322 PEAR::pushErrorHandling(PEAR_ERROR_RETURN);
323 $res = $this->_obj->init($this->config, $pkg, $this->_lastversion);
324 PEAR::popErrorHandling();
326 $this->logger->log(0, 'init succeeded');
328 return $this->throwError(
329 'init of post-install script "'.$this->_class.
333 $this->_contents = $contents;
341 * @see PEAR_PackageFile_v2::runPostinstallScripts()
342 * @param array an array of tasks
343 * @param string install or upgrade
346 public static function run()