|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Usage: |
| 4 | + * php cli.php installer update check |
| 5 | + * php cli.php installer update <slug> |
| 6 | + * php cli.php installer update everything |
| 7 | + */ |
| 8 | +class installerUpdateCli extends waCliController |
| 9 | +{ |
| 10 | + protected $stderr; |
| 11 | + |
| 12 | + protected function preExecute() |
| 13 | + { |
| 14 | + $this->stderr = fopen('php://stderr', 'w'); |
| 15 | + $this->setupInstallationDomain(); |
| 16 | + } |
| 17 | + |
| 18 | + protected function postExecute() |
| 19 | + { |
| 20 | + fclose($this->stderr); |
| 21 | + } |
| 22 | + |
| 23 | + public function execute() |
| 24 | + { |
| 25 | + try { |
| 26 | + $this->unsafeExecute(); |
| 27 | + } catch (Throwable $e) { |
| 28 | + fwrite($this->stderr, "ERROR\n".((string)$e)); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + public function unsafeExecute() |
| 33 | + { |
| 34 | + $update_check = $update_everything = false; |
| 35 | + $update_list = waRequest::param(); |
| 36 | + switch (ifset($update_list, 0, '')) { |
| 37 | + case 'everything': |
| 38 | + $update_everything = true; |
| 39 | + $update_list = []; |
| 40 | + break; |
| 41 | + case 'list': |
| 42 | + array_shift($update_list); |
| 43 | + if (!$update_list) { |
| 44 | + return $this->usage(); |
| 45 | + } |
| 46 | + break; |
| 47 | + case 'check': |
| 48 | + $update_check = true; |
| 49 | + $update_list = []; |
| 50 | + break; |
| 51 | + default: |
| 52 | + return $this->usage(); |
| 53 | + } |
| 54 | + |
| 55 | + $items = installerHelper::getUpdates(); |
| 56 | + list($items, $inapplicable_count, $applicable_slugs) = $this->filterInapplicable($items); |
| 57 | + if ($update_check) { |
| 58 | + echo join(PHP_EOL, $applicable_slugs); |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + $urls = $this->prepareInstallationUrls($items); |
| 63 | + |
| 64 | + $this->executeUpdate($urls); |
| 65 | + |
| 66 | + $items = installerHelper::getUpdates(); |
| 67 | + echo "PENDING ".$inapplicable_count."\n"; |
| 68 | + } |
| 69 | + |
| 70 | + protected function usage() |
| 71 | + { |
| 72 | + |
| 73 | + fwrite($this->stderr, "Usage: |
| 74 | +> php cli.php installer update check |
| 75 | +team |
| 76 | +shop |
| 77 | +shop/plugin/brands |
| 78 | +shop/theme/hypermarket |
| 79 | +
|
| 80 | +> php cli.php installer update shop shop/plugin/brands shop/theme/hypermarket |
| 81 | +UPDATED 3 |
| 82 | +PENDING 1 |
| 83 | +
|
| 84 | +> php cli.php installer update everything |
| 85 | +UPDATED 1 |
| 86 | +PENDING 0 |
| 87 | +"); |
| 88 | + } |
| 89 | + |
| 90 | + protected function setupInstallationDomain() |
| 91 | + { |
| 92 | + $domain = getenv('WA_INSTALLATION_DOMAIN'); |
| 93 | + if (!$domain) { |
| 94 | + $sql = " |
| 95 | + SELECT cs.value |
| 96 | + FROM wa_contact_settings AS cs |
| 97 | + JOIN wa_contact AS c |
| 98 | + ON c.id=cs.contact_id |
| 99 | + WHERE c.is_user >= 1 |
| 100 | + AND cs.app_id='webasyst' |
| 101 | + AND cs.name='backend_url' |
| 102 | + ORDER BY c.last_datetime DESC |
| 103 | + LIMIT 1 |
| 104 | + "; |
| 105 | + $backend_url = (new waModel())->query($sql)->fetchField(); |
| 106 | + if ($backend_url) { |
| 107 | + $domain = parse_url($backend_url, PHP_URL_HOST); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + $_SERVER['SERVER_NAME'] = $_SERVER['HTTP_HOST'] = $domain; |
| 112 | + } |
| 113 | + |
| 114 | + protected function filterInapplicable($items) |
| 115 | + { |
| 116 | + $execute_actions = array( |
| 117 | + waInstallerApps::ACTION_INSTALL, // update may be ACTION_INSTALL if vendor changed or licence type changed (normal -> premium) |
| 118 | + waInstallerApps::ACTION_CRITICAL_UPDATE, |
| 119 | + waInstallerApps::ACTION_UPDATE, |
| 120 | + ); |
| 121 | + |
| 122 | + $applicable_slugs = []; |
| 123 | + $inapplicable_count = 0; |
| 124 | + $checkItem = function(&$info) use ($execute_actions, &$applicable_slugs, &$inapplicable_count) { |
| 125 | + if (!in_array($info['action'], $execute_actions) |
| 126 | + || empty($info['applicable']) |
| 127 | + || (!empty($info['commercial']) && empty($info['purchased'])) |
| 128 | + ) { |
| 129 | + $inapplicable_count++; |
| 130 | + return false; |
| 131 | + } else { |
| 132 | + $applicable_slugs[] = $info['slug']; |
| 133 | + return true; |
| 134 | + } |
| 135 | + }; |
| 136 | + |
| 137 | + foreach ($items as $app_id => &$info) { |
| 138 | + if (!empty($info['download_url'])) { |
| 139 | + if (!$checkItem($info)) { |
| 140 | + unset($info['download_url']); |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + foreach (array('themes', 'plugins', 'widgets') as $type) { |
| 145 | + if (empty($info[$type]) || !is_array($info[$type])) { |
| 146 | + unset($info[$type]); |
| 147 | + continue; |
| 148 | + } |
| 149 | + |
| 150 | + foreach ($info[$type] as $extra_id => $extras_info) { |
| 151 | + if (empty($extras_info['download_url']) || !$checkItem($extras_info)) { |
| 152 | + unset($info[$type][$extra_id]); |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + } |
| 157 | + unset($info); |
| 158 | + |
| 159 | + return [$items, $inapplicable_count, $applicable_slugs]; |
| 160 | + } |
| 161 | + |
| 162 | + protected function prepareInstallationUrls($items) |
| 163 | + { |
| 164 | + $urls = []; |
| 165 | + $add = function($target, $info, $item_id = null) use (&$urls) { |
| 166 | + $urls[$target] = array( |
| 167 | + 'source' => $info['download_url'], |
| 168 | + 'target' => $target, |
| 169 | + 'slug' => $target, |
| 170 | + 'real_slug' => $info['slug'], |
| 171 | + 'md5' => !empty($info['md5']) ? $info['md5'] : null, |
| 172 | + ); |
| 173 | + |
| 174 | + if ($item_id) { |
| 175 | + $urls[$target] = array_merge($urls[$target], array( |
| 176 | + 'slug' => $item_id, |
| 177 | + 'real_slug' => $info['slug'], |
| 178 | + 'pass' => false, |
| 179 | + 'name' => $info['name'], |
| 180 | + 'icon' => $info['icon'], |
| 181 | + 'update' => !empty($info['installed']), |
| 182 | + 'subject' => empty($info['subject']) ? 'system' : $info['subject'], |
| 183 | + 'edition' => empty($info['edition']) ? true : $info['edition'], |
| 184 | + )); |
| 185 | + } |
| 186 | + }; |
| 187 | + |
| 188 | + foreach ($items as $app_id => $info) { |
| 189 | + if (!empty($info['download_url'])) { |
| 190 | + $info['subject'] = 'app'; |
| 191 | + if ($app_id == 'installer') { |
| 192 | + foreach ($info['download_url'] as $target => $url) { |
| 193 | + $_info = $info; |
| 194 | + $_info['download_url'] = $url; |
| 195 | + $_info['name'] = _w('Webasyst framework').' ('.$target.')'; |
| 196 | + $add($target, $_info); |
| 197 | + unset($_info); |
| 198 | + } |
| 199 | + } else { |
| 200 | + $target = 'wa-apps/'.$app_id; |
| 201 | + $add($target, $info, $app_id); |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 205 | + foreach (array('themes', 'plugins', 'widgets') as $type) { |
| 206 | + foreach (ifset($info, $type, []) as $extra_id => $extras_info) { |
| 207 | + $extras_info['subject'] = 'app_'.$type; |
| 208 | + if ($type == 'themes' && is_array($extras_info['download_url'])) { |
| 209 | + foreach ($extras_info['download_url'] as $target => $url) { |
| 210 | + $__info = $extras_info; |
| 211 | + $__info['download_url'] = $url; |
| 212 | + $__info['slug'] = preg_replace('@^wa-apps/@', '', $target); |
| 213 | + $__info['app'] = preg_replace('@^wa-apps/([^/]+)/.+$@', '$1', $target); |
| 214 | + |
| 215 | + if (!isset($urls[$target])) { |
| 216 | + if (($__info['app'] == $app_id) || empty($items[$__info['app']]['themes'][$extra_id])) { |
| 217 | + if (!empty($items[$__info['app']]['themes'][$extra_id]['name'])) { |
| 218 | + $__info['name'] .= " ({$info['name']})"; |
| 219 | + } elseif ( ( $app_info = wa()->getAppInfo($__info['app']))) { |
| 220 | + $__info['name'] .= " ({$app_info['name']})"; |
| 221 | + } else { |
| 222 | + $__info['name'] .= " ({$__info['app']})"; |
| 223 | + } |
| 224 | + $add($target, $__info); |
| 225 | + } |
| 226 | + } |
| 227 | + } |
| 228 | + } else { |
| 229 | + if (!empty($info['name'])) { |
| 230 | + $extras_info['name'] .= " ({$info['name']})"; |
| 231 | + } |
| 232 | + if (strpos($app_id, '/')) { |
| 233 | + //system plugins |
| 234 | + $target = $app_id.'/'.$extra_id; |
| 235 | + } elseif (($app_id == 'webasyst') && ($type == 'widgets')) { |
| 236 | + $target = 'wa-widgets/'.$extra_id; |
| 237 | + } elseif (($app_id == 'wa-widgets')) { |
| 238 | + $target = 'wa-widgets/'.$extra_id; |
| 239 | + } else { |
| 240 | + $target = 'wa-apps/'.$app_id.'/'.$type.'/'.$extra_id; |
| 241 | + } |
| 242 | + $add($target, $extras_info, $target); |
| 243 | + } |
| 244 | + } |
| 245 | + } |
| 246 | + unset($info); |
| 247 | + } |
| 248 | + |
| 249 | + return $urls; |
| 250 | + } |
| 251 | + |
| 252 | + protected function logError($message) |
| 253 | + { |
| 254 | + waLog::log($message, 'installer.cli.update.log'); |
| 255 | + } |
| 256 | + |
| 257 | + protected function executeUpdate($urls) |
| 258 | + { |
| 259 | + $updater = new waInstaller(waInstaller::LOG_WARNING); |
| 260 | + $app_settings_model = new waAppSettingsModel(); |
| 261 | + $updater->init(); |
| 262 | + $app_settings_model->ping(); |
| 263 | + |
| 264 | + $urls = $updater->update($urls); |
| 265 | + $app_settings_model->ping(); |
| 266 | + |
| 267 | + $updated_urls = []; |
| 268 | + $updated_slugs = []; |
| 269 | + foreach ($urls as $item) { |
| 270 | + if (empty($item['skipped'])) { |
| 271 | + $updated_urls[] = $item; |
| 272 | + if (!empty($item['real_slug'])) { |
| 273 | + $updated_slugs[] = $item['real_slug']; |
| 274 | + } |
| 275 | + } |
| 276 | + } |
| 277 | + echo "UPDATED ".count($updated_urls)."\n"; |
| 278 | + if (empty($updated_urls)) { |
| 279 | + return; |
| 280 | + } |
| 281 | + if (!empty($updated_slugs)) { |
| 282 | + $sender = new installerUpdateFact(installerUpdateFact::ACTION_ADD, $updated_slugs); |
| 283 | + $sender->query(); |
| 284 | + } |
| 285 | + |
| 286 | + //update themes |
| 287 | + foreach ($updated_urls as $url) { |
| 288 | + if (preg_match('@(wa-apps/)?(.+)/themes/(.+)@', $url['slug'], $matches)) { |
| 289 | + try { |
| 290 | + $theme = new waTheme($matches[3], $matches[2]); |
| 291 | + $theme->update(); |
| 292 | + } catch (Exception $ex) { |
| 293 | + $this->logError(sprintf('Error during theme %s@%s update: %s', $matches[3], $matches[2], $ex->getMessage())."\n".$e->getTraceAsString()); |
| 294 | + } |
| 295 | + } |
| 296 | + } |
| 297 | + |
| 298 | + $app_settings_model->ping(); |
| 299 | + installerHelper::flushCache(); |
| 300 | + $app_settings_model->ping(); |
| 301 | + |
| 302 | + wa('installer')->event('end_installation', $urls); |
| 303 | + } |
| 304 | + |
| 305 | +} |
0 commit comments