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