]> git.mxchange.org Git - friendica.git/blob - src/Core/Console.php
Merge remote-tracking branch 'upstream/develop' into media-handling
[friendica.git] / src / Core / Console.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, the Friendica project
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         addon                  Addon management
48         cache                  Manage node cache
49         config                 Edit site config
50         createdoxygen          Generate Doxygen headers
51         dbstructure            Do database updates
52         docbloxerrorchecker    Check the file tree for DocBlox errors
53         extract                Generate translation string file for the Friendica project (deprecated)
54         globalcommunityblock   Block remote profile from interacting with this node
55         globalcommunitysilence Silence a profile from the global community page
56         archivecontact         Archive a contact when you know that it isn't existing anymore
57         help                   Show help about a command, e.g (bin/console help config)
58         autoinstall            Starts automatic installation of friendica based on values from htconfig.php
59         lock                   Edit site locks
60         maintenance            Set maintenance mode for this node
61         user                   User management
62         php2po                 Generate a messages.po file from a strings.php file
63         po2php                 Generate a strings.php file from a messages.po file
64         typo                   Checks for parse errors in Friendica files
65         postupdate             Execute pending post update scripts (can last days)
66         serverblock            Manage blocked servers
67         storage                Manage storage backend
68         relay                  Manage ActivityPub relay servers
69
70 Options:
71         -h|--help|-? Show help information
72         -v           Show more debug information.
73 HELP;
74                 return $help;
75         }
76
77         protected $subConsoles = [
78                 'addon'                  => Friendica\Console\Addon::class,
79                 'cache'                  => Friendica\Console\Cache::class,
80                 'config'                 => Friendica\Console\Config::class,
81                 'createdoxygen'          => Friendica\Console\CreateDoxygen::class,
82                 'docbloxerrorchecker'    => Friendica\Console\DocBloxErrorChecker::class,
83                 'dbstructure'            => Friendica\Console\DatabaseStructure::class,
84                 'extract'                => Friendica\Console\Extract::class,
85                 'globalcommunityblock'   => Friendica\Console\GlobalCommunityBlock::class,
86                 'globalcommunitysilence' => Friendica\Console\GlobalCommunitySilence::class,
87                 'archivecontact'         => Friendica\Console\ArchiveContact::class,
88                 'autoinstall'            => Friendica\Console\AutomaticInstallation::class,
89                 'lock'                   => Friendica\Console\Lock::class,
90                 'maintenance'            => Friendica\Console\Maintenance::class,
91                 'user'                   => Friendica\Console\User::class,
92                 'php2po'                 => Friendica\Console\PhpToPo::class,
93                 'po2php'                 => Friendica\Console\PoToPhp::class,
94                 'typo'                   => Friendica\Console\Typo::class,
95                 'postupdate'             => Friendica\Console\PostUpdate::class,
96                 'serverblock'            => Friendica\Console\ServerBlock::class,
97                 'storage'                => Friendica\Console\Storage::class,
98                 'relay'                  => Friendica\Console\Relay::class,
99                 'fixapdeliveryworkertaskparameters' => Friendica\Console\FixAPDeliveryWorkerTaskParameters::class,
100         ];
101
102         /**
103          * CliInput Friendica constructor.
104          *
105          * @param Dice $dice The DI library
106          * @param array $argv
107          */
108         public function __construct(Dice $dice, array $argv = null)
109         {
110                 parent::__construct($argv);
111
112                 $this->dice = $dice;
113         }
114
115         protected function doExecute()
116         {
117                 if ($this->getOption('v')) {
118                         $this->out('Executable: ' . $this->executable);
119                         $this->out('Arguments: ' . var_export($this->args, true));
120                         $this->out('Options: ' . var_export($this->options, true));
121                 }
122
123                 $subHelp = false;
124                 $command = null;
125
126                 if ($this->getOption('version')) {
127                         $this->out('Friendica Console version ' . FRIENDICA_VERSION);
128
129                         return 0;
130                 } elseif ((count($this->options) === 0 || $this->getOption($this->customHelpOptions) === true || $this->getOption($this->customHelpOptions) === 1) && count($this->args) === 0
131                 ) {
132                 } elseif (count($this->args) >= 2 && $this->getArgument(0) == 'help') {
133                         $command = $this->getArgument(1);
134                         $subHelp = true;
135                         array_shift($this->args);
136                         array_shift($this->args);
137                 } elseif (count($this->args) >= 1) {
138                         $command = $this->getArgument(0);
139                         array_shift($this->args);
140                 }
141
142                 if (is_null($command)) {
143                         $this->out($this->getHelp());
144                         return 0;
145                 }
146
147                 $console = $this->getSubConsole($command);
148
149                 if ($subHelp) {
150                         $console->setOption($this->customHelpOptions, true);
151                 }
152
153                 return $console->execute();
154         }
155
156         private function getSubConsole($command)
157         {
158                 if ($this->getOption('v')) {
159                         $this->out('Command: ' . $command);
160                 }
161
162                 if (!isset($this->subConsoles[$command])) {
163                         throw new \Asika\SimpleConsole\CommandArgsException('Command ' . $command . ' doesn\'t exist');
164                 }
165
166                 $subargs = $this->args;
167                 array_unshift($subargs, $this->executable);
168
169                 $className = $this->subConsoles[$command];
170
171                 Friendica\DI::init($this->dice);
172
173                 /** @var Console $subconsole */
174                 $subconsole = $this->dice->create($className, [$subargs]);
175
176                 foreach ($this->options as $name => $value) {
177                         $subconsole->setOption($name, $value);
178                 }
179
180                 return $subconsole;
181         }
182
183 }