getRequest(); // FIXME: Write and use a CLIRouter here (see classdoc) $router = new PageRouter(); $router->setApplication($application); $request->setRouter($router); // Initialize the locale and load generic plugins. PluginRegistry::loadCategory('generic'); $this->argv = isset($argv) && is_array($argv) ? $argv : []; if (isset($_SERVER['SERVER_NAME'])) { exit('This script can only be executed from the command-line'); } $this->scriptName = isset($this->argv[0]) ? array_shift($this->argv) : ''; if (Config::getVar('general', 'installed')) $this->checkArgsForUsername(); if (isset($this->argv[0]) && $this->argv[0] == '-h') { $this->exitWithUsageMessage(); } } public function usage() { } private function checkArgsForUsername() { $usernameKeyPos = array_search('--user_name', $this->argv); if (!$usernameKeyPos) { $usernameKeyPos = array_search('-u', $this->argv); } if ($usernameKeyPos) { $usernamePos = $usernameKeyPos + 1; if (count($this->argv) >= $usernamePos + 1) { $this->username = $this->argv[$usernamePos]; unset($this->argv[$usernamePos]); } unset($this->argv[$usernameKeyPos]); } if ($this->username) { $user = Repo::user()->getByUsername($this->username, true); $this->setUser($user); } if (!$this->user) { $adminGroups = Repo::userGroup()->getArrayIdByRoleId(Role::ROLE_ID_SITE_ADMIN); if (count($adminGroups)) { $groupUsers = Repo::user()->getCollector() ->filterByUserGroupIds([$adminGroups[0]]) ->getMany(); if ($groupUsers->isNotEmpty()) { $this->setUser($groupUsers->first()); } else { $this->exitWithUsageMessage(); } } } } /** * Sets the user for the CLI Tool * * @param \PKP\user\User $user The user to set as the execution user of this CLI command */ public function setUser($user) { $registeredUser = Registry::get('user', true, null); if (!isset($registeredUser)) { /** * This is used in order to reconcile with possible $request->getUser() * used inside import processes, when the import is done by CLI tool. */ if ($user) { Registry::set('user', $user); $this->user = $user; } } else { $this->user = $registeredUser; } } /** * Exit the CLI tool if an error occurs */ public function exitWithUsageMessage() { $this->usage(); exit(0); } } if (!PKP_STRICT_MODE) { class_alias('\PKP\cliTool\CommandLineTool', '\CommandLineTool'); }