/**
  * Returns all available commands.
  * 
  * @return	array<\wcf\system\cli\command\ICLICommand>
  */
 public static function getCommands()
 {
     if (empty(self::$commands)) {
         $directory = DirectoryUtil::getInstance(WCF_DIR . 'lib/system/cli/command/');
         $commands = $directory->getFiles(SORT_ASC, new Regex('Command\\.class\\.php$'));
         foreach ($commands as $command) {
             $class = 'wcf\\system\\cli\\command\\' . basename($command, '.class.php');
             if (!class_exists($class) && !interface_exists($class)) {
                 Log::info('Invalid command file: ', $command);
                 continue;
             }
             if (!class_exists($class)) {
                 continue;
             }
             $object = new $class();
             if (!$object instanceof ICLICommand) {
                 Log::info('Invalid command file: ', $command);
                 continue;
             }
             if (!$object->canAccess()) {
                 continue;
             }
             self::$commands[strtolower(basename($command, 'CLICommand.class.php'))] = $object;
         }
     }
     return self::$commands;
 }
 /**
  * Installs the specified package.
  * 
  * @param	string	$file
  */
 private function install($file)
 {
     // PackageStartInstallForm::validateDownloadPackage()
     if (FileUtil::isURL($file)) {
         // download package
         $archive = new PackageArchive($file, null);
         try {
             if (VERBOSITY >= 1) {
                 Log::info("Downloading '" . $file . "'");
             }
             $file = $archive->downloadArchive();
         } catch (SystemException $e) {
             $this->error('notFound', array('file' => $file));
         }
     } else {
         // probably local path
         if (!file_exists($file)) {
             $this->error('notFound', array('file' => $file));
         }
         $archive = new PackageArchive($file, null);
     }
     // PackageStartInstallForm::validateArchive()
     // try to open the archive
     try {
         // TODO: Exceptions thrown within openArchive() are discarded, resulting in
         // the meaningless message 'not a valid package'
         $archive->openArchive();
     } catch (SystemException $e) {
         $this->error('noValidPackage');
     }
     $errors = PackageInstallationDispatcher::validatePHPRequirements($archive->getPhpRequirements());
     if (!empty($errors)) {
         // TODO: Nice output
         $this->error('phpRequirements', array('errors' => $errors));
     }
     // try to find existing package
     $sql = "SELECT\t*\n\t\t\tFROM\twcf" . WCF_N . "_package\n\t\t\tWHERE\tpackage = ?";
     $statement = CLIWCF::getDB()->prepareStatement($sql);
     $statement->execute(array($archive->getPackageInfo('name')));
     $row = $statement->fetchArray();
     $package = null;
     if ($row !== false) {
         $package = new Package(null, $row);
     }
     // check update or install support
     if ($package !== null) {
         CLIWCF::getSession()->checkPermissions(array('admin.system.package.canUpdatePackage'));
         $archive->setPackage($package);
         if (!$archive->isValidUpdate()) {
             $this->error('noValidUpdate');
         }
     } else {
         CLIWCF::getSession()->checkPermissions(array('admin.system.package.canInstallPackage'));
         if (!$archive->isValidInstall()) {
             $this->error('noValidInstall');
         } else {
             if ($archive->getPackageInfo('isApplication')) {
                 // applications cannot be installed via CLI
                 $this->error('cli.installIsApplication');
             } else {
                 if ($archive->isAlreadyInstalled()) {
                     $this->error('uniqueAlreadyInstalled');
                 } else {
                     if ($archive->getPackageInfo('isApplication') && $this->archive->hasUniqueAbbreviation()) {
                         $this->error('noUniqueAbbrevation');
                     }
                 }
             }
         }
     }
     // PackageStartInstallForm::save()
     $processNo = PackageInstallationQueue::getNewProcessNo();
     // insert queue
     $queue = PackageInstallationQueueEditor::create(array('processNo' => $processNo, 'userID' => CLIWCF::getUser()->userID, 'package' => $archive->getPackageInfo('name'), 'packageName' => $archive->getLocalizedPackageInfo('packageName'), 'packageID' => $package !== null ? $package->packageID : null, 'archive' => $file, 'action' => $package !== null ? 'update' : 'install'));
     // PackageInstallationDispatcher::openQueue()
     $parentQueueID = 0;
     $conditions = new PreparedStatementConditionBuilder();
     $conditions->add("userID = ?", array(CLIWCF::getUser()->userID));
     $conditions->add("parentQueueID = ?", array($parentQueueID));
     if ($processNo != 0) {
         $conditions->add("processNo = ?", array($processNo));
     }
     $conditions->add("done = ?", array(0));
     $sql = "SELECT\t\t*\n\t\t\tFROM\t\twcf" . WCF_N . "_package_installation_queue\n\t\t\t" . $conditions . "\n\t\t\tORDER BY\tqueueID ASC";
     $statement = CLIWCF::getDB()->prepareStatement($sql);
     $statement->execute($conditions->getParameters());
     $packageInstallation = $statement->fetchArray();
     if (!isset($packageInstallation['queueID'])) {
         $this->error('internalOpenQueue');
         return;
     } else {
         $queueID = $packageInstallation['queueID'];
     }
     // PackageInstallationConfirmPage::readParameters()
     $queue = new PackageInstallationQueue($queueID);
     if (!$queue->queueID || $queue->done) {
         $this->error('internalReadParameters');
         return;
     }
     // PackageInstallationConfirmPage::readData()
     $missingPackages = 0;
     $packageInstallationDispatcher = new PackageInstallationDispatcher($queue);
     // get requirements
     $requirements = $packageInstallationDispatcher->getArchive()->getRequirements();
     $openRequirements = $packageInstallationDispatcher->getArchive()->getOpenRequirements();
     foreach ($requirements as &$requirement) {
         if (isset($openRequirements[$requirement['name']])) {
             $requirement['status'] = 'missing';
             $requirement['action'] = $openRequirements[$requirement['name']]['action'];
             if (!isset($requirement['file'])) {
                 if ($requirement['action'] === 'update') {
                     $requirement['status'] = 'missingVersion';
                     $requirement['existingVersion'] = $openRequirements[$requirement['name']]['existingVersion'];
                 }
                 $missingPackages++;
             } else {
                 $requirement['status'] = 'delivered';
                 $packageArchive = new PackageArchive($packageInstallationDispatcher->getArchive()->extractTar($requirement['file']));
                 $packageArchive->openArchive();
                 // make sure that the delivered package is correct
                 if ($requirement['name'] != $packageArchive->getPackageInfo('name')) {
                     $requirement['status'] = 'invalidDeliveredPackage';
                     $requirement['deliveredPackage'] = $packageArchive->getPackageInfo('name');
                     $missingPackages++;
                 } else {
                     if (isset($requirement['minversion'])) {
                         // make sure that the delivered version is sufficient
                         if (Package::compareVersion($requirement['minversion'], $packageArchive->getPackageInfo('version')) > 0) {
                             $requirement['deliveredVersion'] = $packageArchive->getPackageInfo('version');
                             $requirement['status'] = 'missingVersion';
                             $missingPackages++;
                         }
                     }
                 }
             }
         } else {
             $requirement['status'] = 'installed';
         }
     }
     unset($requirement);
     // PackageInstallationConfirmPage::assignVariables/show()
     $excludingPackages = $packageInstallationDispatcher->getArchive()->getConflictedExcludingPackages();
     $excludedPackages = $packageInstallationDispatcher->getArchive()->getConflictedExcludedPackages();
     if (!($missingPackages == 0 && count($excludingPackages) == 0 && count($excludedPackages) == 0)) {
         $this->error('missingPackagesOrExclude', array('requirements' => $requirements, 'excludingPackages' => $excludingPackages, 'excludedPackages' => $excludedPackages));
         return;
     }
     // AbstractDialogAction::readParameters()
     $step = 'prepare';
     $queueID = $queue->queueID;
     $node = '';
     // initialize progressbar
     $progressbar = new ProgressBar(new ConsoleProgressBar(array('width' => CLIWCF::getTerminal()->getWidth(), 'elements' => array(ConsoleProgressBar::ELEMENT_PERCENT, ConsoleProgressBar::ELEMENT_BAR, ConsoleProgressBar::ELEMENT_TEXT), 'textWidth' => min(floor(CLIWCF::getTerminal()->getWidth() / 2), 50))));
     // InstallPackageAction::readParameters()
     $finished = false;
     while (!$finished) {
         $queue = new PackageInstallationQueue($queueID);
         if (!$queue->queueID) {
             // todo: what to output?
             echo "InstallPackageAction::readParameters()";
             return;
         }
         $installation = new PackageInstallationDispatcher($queue);
         switch ($step) {
             case 'prepare':
                 // InstallPackageAction::stepPrepare()
                 // update package information
                 $installation->updatePackage();
                 // clean-up previously created nodes
                 $installation->nodeBuilder->purgeNodes();
                 // create node tree
                 $installation->nodeBuilder->buildNodes();
                 $node = $installation->nodeBuilder->getNextNode();
                 $queueID = $installation->nodeBuilder->getQueueByNode($installation->queue->processNo, $node);
                 $step = 'install';
                 $progress = 0;
                 $currentAction = $installation->nodeBuilder->getPackageNameByQueue($queueID);
                 break;
             case 'install':
                 // InstallPackageAction::stepInstall()
                 $step_ = $installation->install($node);
                 $queueID = $installation->nodeBuilder->getQueueByNode($installation->queue->processNo, $step_->getNode());
                 if ($step_->hasDocument()) {
                     $innerTemplate = $step_->getTemplate();
                     $progress = $installation->nodeBuilder->calculateProgress($node);
                     $node = $step_->getNode();
                     $currentAction = $installation->nodeBuilder->getPackageNameByQueue($queueID);
                 } else {
                     if ($step_->getNode() == '') {
                         // perform final actions
                         $installation->completeSetup();
                         // InstallPackageAction::finalize()
                         CacheHandler::getInstance()->flushAll();
                         // /InstallPackageAction::finalize()
                         // show success
                         $progress = 100;
                         $currentAction = CLIWCF::getLanguage()->get('wcf.acp.package.installation.step.install.success');
                         $finished = true;
                         continue;
                     } else {
                         // continue with next node
                         $progress = $installation->nodeBuilder->calculateProgress($node);
                         $node = $step_->getNode();
                         $currentAction = $installation->nodeBuilder->getPackageNameByQueue($queueID);
                     }
                 }
                 break;
         }
         $progressbar->update($progress, $currentAction);
     }
     $progressbar->getAdapter()->finish();
 }
 /**
  * Returns an array with the list of all available workers.
  * 
  * @return	array
  */
 public function generateList()
 {
     $directory = DirectoryUtil::getInstance(WCF_DIR . 'lib/system/worker/');
     $workerList = $directory->getFiles(SORT_ASC, new Regex('Worker\\.class\\.php$'));
     $table = array(array('Class', 'Description'));
     foreach ($workerList as $worker) {
         $class = 'wcf\\system\\worker\\' . basename($worker, '.class.php');
         if (!class_exists($class) && !interface_exists($class)) {
             Log::info('Invalid worker file: ', $worker);
             continue;
         }
         $reflection = new \ReflectionClass($class);
         if (!$reflection->isInstantiable()) {
             continue;
         }
         if (!ClassUtil::isInstanceOf($class, 'wcf\\system\\worker\\IWorker')) {
             Log::info('Invalid worker file: ', $worker);
             continue;
         }
         $docComment = explode("\n", StringUtil::unifyNewlines($reflection->getDocComment()));
         foreach ($docComment as $commentLine) {
             if (Regex::compile('[a-z]', Regex::CASE_INSENSITIVE)->match($commentLine)) {
                 $comment = Regex::compile('^[^a-z]+', Regex::CASE_INSENSITIVE)->replace($commentLine, '');
                 break;
             }
         }
         $table[] = array(basename($worker, '.class.php'), $comment);
     }
     return $table;
 }