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