]> git.mxchange.org Git - friendica.git/blob - src/Core/Console.php
f43b89e9e67cc1832090d00ad10598a6b40e5bcf
[friendica.git] / src / Core / Console.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Core;
23
24 use Dice\Dice;
25 use Friendica;
26
27 /**
28  * Description of Console
29  */
30 class Console extends \Asika\SimpleConsole\Console
31 {
32         // Disables the default help handling
33         protected $helpOptions = [];
34         protected $customHelpOptions = ['h', 'help', '?'];
35
36         /**
37          * @var Dice The DI library
38          */
39         protected $dice;
40
41         protected function getHelp()
42         {
43                 $help = <<<HELP
44 Usage: bin/console [--version] [-h|--help|-?] <command> [<args>] [-v]
45
46 Commands:
47         cache                  Manage node cache
48         config                 Edit site config
49         createdoxygen          Generate Doxygen headers
50         dbstructure            Do database updates
51         docbloxerrorchecker    Check the file tree for DocBlox errors
52         extract                Generate translation string file for the Friendica project (deprecated)
53         globalcommunityblock   Block remote profile from interacting with this node
54         globalcommunitysilence Silence a profile from the global community page
55         archivecontact         Archive a contact when you know that it isn't existing anymore
56         help                   Show help about a command, e.g (bin/console help config)
57         autoinstall            Starts automatic installation of friendica based on values from htconfig.php
58         lock                   Edit site locks
59         maintenance            Set maintenance mode for this node
60         user                   User management
61         php2po                 Generate a messages.po file from a strings.php file
62         po2php                 Generate a strings.php file from a messages.po file
63         typo                   Checks for parse errors in Friendica files
64         postupdate             Execute pending post update scripts (can last days)
65         serverblock            Manage blocked servers
66         storage                Manage storage backend
67         relay                  Manage ActivityPub relay servers
68
69 Options:
70         -h|--help|-? Show help information
71         -v           Show more debug information.
72 HELP;
73                 return $help;
74         }
75
76         protected $subConsoles = [
77                 'cache'                  => Friendica\Console\Cache::class,
78                 'config'                 => Friendica\Console\Config::class,
79                 'createdoxygen'          => Friendica\Console\CreateDoxygen::class,
80                 'docbloxerrorchecker'    => Friendica\Console\DocBloxErrorChecker::class,
81                 'dbstructure'            => Friendica\Console\DatabaseStructure::class,
82                 'extract'                => Friendica\Console\Extract::class,
83                 'globalcommunityblock'   => Friendica\Console\GlobalCommunityBlock::class,
84                 'globalcommunitysilence' => Friendica\Console\GlobalCommunitySilence::class,
85                 'archivecontact'         => Friendica\Console\ArchiveContact::class,
86                 'autoinstall'            => Friendica\Console\AutomaticInstallation::class,
87                 'lock'                   => Friendica\Console\Lock::class,
88                 'maintenance'            => Friendica\Console\Maintenance::class,
89                 'user'                   => Friendica\Console\User::class,
90                 'php2po'                 => Friendica\Console\PhpToPo::class,
91                 'po2php'                 => Friendica\Console\PoToPhp::class,
92                 'typo'                   => Friendica\Console\Typo::class,
93                 'postupdate'             => Friendica\Console\PostUpdate::class,
94                 'serverblock'            => Friendica\Console\ServerBlock::class,
95                 'storage'                => Friendica\Console\Storage::class,
96                 'relay'                  => Friendica\Console\Relay::class,
97                 'fixapdeliveryworkertaskparameters' => Friendica\Console\FixAPDeliveryWorkerTaskParameters::class,
98         ];
99
100         /**
101          * CliInput Friendica constructor.
102          *
103          * @param Dice $dice The DI library
104          * @param array $argv
105          */
106         public function __construct(Dice $dice, array $argv = null)
107         {
108                 parent::__construct($argv);
109
110                 $this->dice = $dice;
111         }
112
113         protected function doExecute()
114         {
115                 if ($this->getOption('v')) {
116                         $this->out('Executable: ' . $this->executable);
117                         $this->out('Arguments: ' . var_export($this->args, true));
118                         $this->out('Options: ' . var_export($this->options, true));
119                 }
120
121                 $subHelp = false;
122                 $command = null;
123
124                 if ($this->getOption('version')) {
125                         $this->out('Friendica Console version ' . FRIENDICA_VERSION);
126
127                         return 0;
128                 } elseif ((count($this->options) === 0 || $this->getOption($this->customHelpOptions) === true || $this->getOption($this->customHelpOptions) === 1) && count($this->args) === 0
129                 ) {
130                 } elseif (count($this->args) >= 2 && $this->getArgument(0) == 'help') {
131                         $command = $this->getArgument(1);
132                         $subHelp = true;
133                         array_shift($this->args);
134                         array_shift($this->args);
135                 } elseif (count($this->args) >= 1) {
136                         $command = $this->getArgument(0);
137                         array_shift($this->args);
138                 }
139
140                 if (is_null($command)) {
141                         $this->out($this->getHelp());
142                         return 0;
143                 }
144
145                 $console = $this->getSubConsole($command);
146
147                 if ($subHelp) {
148                         $console->setOption($this->customHelpOptions, true);
149                 }
150
151                 return $console->execute();
152         }
153
154         private function getSubConsole($command)
155         {
156                 if ($this->getOption('v')) {
157                         $this->out('Command: ' . $command);
158                 }
159
160                 if (!isset($this->subConsoles[$command])) {
161                         throw new \Asika\SimpleConsole\CommandArgsException('Command ' . $command . ' doesn\'t exist');
162                 }
163
164                 $subargs = $this->args;
165                 array_unshift($subargs, $this->executable);
166
167                 $className = $this->subConsoles[$command];
168
169                 Friendica\DI::init($this->dice);
170
171                 /** @var Console $subconsole */
172                 $subconsole = $this->dice->create($className, [$subargs]);
173
174                 foreach ($this->options as $name => $value) {
175                         $subconsole->setOption($name, $value);
176                 }
177
178                 return $subconsole;
179         }
180
181 }