]> git.mxchange.org Git - friendica.git/blob - src/Module/Debug/Babel.php
Add Twitter source debug to Debug\Babel
[friendica.git] / src / Module / Debug / Babel.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\Module\Debug;
23
24 use Friendica\BaseModule;
25 use Friendica\Content\PageInfo;
26 use Friendica\Content\Text;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\Renderer;
29 use Friendica\DI;
30 use Friendica\Model\Conversation;
31 use Friendica\Model\Item;
32 use Friendica\Protocol\Activity;
33 use Friendica\Model\Tag;
34 use Friendica\Util\XML;
35
36 /**
37  * Translates input text into different formats (HTML, BBCode, Markdown)
38  */
39 class Babel extends BaseModule
40 {
41         public static function content(array $parameters = [])
42         {
43                 function visible_whitespace($s)
44                 {
45                         return '<pre>' . htmlspecialchars($s) . '</pre>';
46                 }
47
48                 $results = [];
49                 if (!empty($_REQUEST['text'])) {
50                         switch (($_REQUEST['type'] ?? '') ?: 'bbcode') {
51                                 case 'bbcode':
52                                         $bbcode = trim($_REQUEST['text']);
53                                         $results[] = [
54                                                 'title'   => DI::l10n()->t('Source input'),
55                                                 'content' => visible_whitespace($bbcode)
56                                         ];
57
58                                         $plain = Text\BBCode::toPlaintext($bbcode, false);
59                                         $results[] = [
60                                                 'title'   => DI::l10n()->t('BBCode::toPlaintext'),
61                                                 'content' => visible_whitespace($plain)
62                                         ];
63
64                                         $html = Text\BBCode::convert($bbcode);
65                                         $results[] = [
66                                                 'title'   => DI::l10n()->t('BBCode::convert (raw HTML)'),
67                                                 'content' => visible_whitespace($html)
68                                         ];
69
70                                         $results[] = [
71                                                 'title'   => DI::l10n()->t('BBCode::convert'),
72                                                 'content' => $html
73                                         ];
74
75                                         $bbcode2 = Text\HTML::toBBCode($html);
76                                         $results[] = [
77                                                 'title'   => DI::l10n()->t('BBCode::convert => HTML::toBBCode'),
78                                                 'content' => visible_whitespace($bbcode2)
79                                         ];
80
81                                         $markdown = Text\BBCode::toMarkdown($bbcode);
82                                         $results[] = [
83                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown'),
84                                                 'content' => visible_whitespace($markdown)
85                                         ];
86
87                                         $html2 = Text\Markdown::convert($markdown);
88                                         $results[] = [
89                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert (raw HTML)'),
90                                                 'content' => visible_whitespace($html2)
91                                         ];
92                                         $results[] = [
93                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::convert'),
94                                                 'content' => $html2
95                                         ];
96
97                                         $bbcode3 = Text\Markdown::toBBCode($markdown);
98                                         $results[] = [
99                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown => Markdown::toBBCode'),
100                                                 'content' => visible_whitespace($bbcode3)
101                                         ];
102
103                                         $bbcode4 = Text\HTML::toBBCode($html2);
104                                         $results[] = [
105                                                 'title'   => DI::l10n()->t('BBCode::toMarkdown =>  Markdown::convert => HTML::toBBCode'),
106                                                 'content' => visible_whitespace($bbcode4)
107                                         ];
108
109                                         $tags = Text\BBCode::getTags($bbcode);
110
111                                         $body = Item::setHashtags($bbcode);
112                                         $results[] = [
113                                                 'title'   => DI::l10n()->t('Item Body'),
114                                                 'content' => visible_whitespace($body)
115                                         ];
116                                         $results[] = [
117                                                 'title'   => DI::l10n()->t('Item Tags'),
118                                                 'content' => visible_whitespace(var_export($tags, true)),
119                                         ];
120
121                                         $body2 = PageInfo::appendToBody($bbcode, true);
122                                         $results[] = [
123                                                 'title'   => DI::l10n()->t('PageInfo::appendToBody'),
124                                                 'content' => visible_whitespace($body2)
125                                         ];
126                                         $html3 = Text\BBCode::convert($body2);
127                                         $results[] = [
128                                                 'title'   => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert (raw HTML)'),
129                                                 'content' => visible_whitespace($html3)
130                                         ];
131                                         $results[] = [
132                                                 'title'   => DI::l10n()->t('PageInfo::appendToBody => BBCode::convert'),
133                                                 'content' => $html3
134                                         ];
135                                         break;
136                                 case 'diaspora':
137                                         $diaspora = trim($_REQUEST['text']);
138                                         $results[] = [
139                                                 'title'   => DI::l10n()->t('Source input (Diaspora format)'),
140                                                 'content' => visible_whitespace($diaspora),
141                                         ];
142
143                                         $markdown = XML::unescape($diaspora);
144                                 case 'markdown':
145                                         $markdown = $markdown ?? trim($_REQUEST['text']);
146
147                                         $results[] = [
148                                                 'title'   => DI::l10n()->t('Source input (Markdown)'),
149                                                 'content' => visible_whitespace($markdown),
150                                         ];
151
152                                         $html = Text\Markdown::convert($markdown);
153                                         $results[] = [
154                                                 'title'   => DI::l10n()->t('Markdown::convert (raw HTML)'),
155                                                 'content' => visible_whitespace($html),
156                                         ];
157
158                                         $results[] = [
159                                                 'title'   => DI::l10n()->t('Markdown::convert'),
160                                                 'content' => $html
161                                         ];
162
163                                         $bbcode = Text\Markdown::toBBCode($markdown);
164                                         $results[] = [
165                                                 'title'   => DI::l10n()->t('Markdown::toBBCode'),
166                                                 'content' => visible_whitespace($bbcode),
167                                         ];
168                                         break;
169                                 case 'html' :
170                                         $html = trim($_REQUEST['text']);
171                                         $results[] = [
172                                                 'title'   => DI::l10n()->t('Raw HTML input'),
173                                                 'content' => visible_whitespace($html),
174                                         ];
175
176                                         $results[] = [
177                                                 'title'   => DI::l10n()->t('HTML Input'),
178                                                 'content' => $html
179                                         ];
180
181                                         $bbcode = Text\HTML::toBBCode($html);
182                                         $results[] = [
183                                                 'title'   => DI::l10n()->t('HTML::toBBCode'),
184                                                 'content' => visible_whitespace($bbcode)
185                                         ];
186
187                                         $html2 = Text\BBCode::convert($bbcode);
188                                         $results[] = [
189                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert'),
190                                                 'content' => $html2
191                                         ];
192
193                                         $results[] = [
194                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::convert (raw HTML)'),
195                                                 'content' => htmlspecialchars($html2)
196                                         ];
197
198                                         $bbcode2plain = Text\BBCode::toPlaintext($bbcode);
199                                         $results[] = [
200                                                 'title'   => DI::l10n()->t('HTML::toBBCode => BBCode::toPlaintext'),
201                                                 'content' => visible_whitespace($bbcode2plain),
202                                         ];
203
204                                         $markdown = Text\HTML::toMarkdown($html);
205                                         $results[] = [
206                                                 'title'   => DI::l10n()->t('HTML::toMarkdown'),
207                                                 'content' => visible_whitespace($markdown)
208                                         ];
209
210                                         $text = Text\HTML::toPlaintext($html, 0);
211                                         $results[] = [
212                                                 'title'   => DI::l10n()->t('HTML::toPlaintext'),
213                                                 'content' => visible_whitespace($text),
214                                         ];
215
216                                         $text = Text\HTML::toPlaintext($html, 0, true);
217                                         $results[] = [
218                                                 'title'   => DI::l10n()->t('HTML::toPlaintext (compact)'),
219                                                 'content' => visible_whitespace($text),
220                                         ];
221                                         break;
222                                 case 'twitter':
223                                         $json = trim($_REQUEST['text']);
224
225                                         $status = json_decode($json);
226
227                                         $results[] = [
228                                                 'title'   => DI::l10n()->t('Decoded post'),
229                                                 'content' => visible_whitespace(var_export($status, true)),
230                                         ];
231
232                                         $postarray = [];
233                                         $postarray['object-type'] = Activity\ObjectType::NOTE;
234
235                                         if (!empty($status->full_text)) {
236                                                 $postarray['body'] = $status->full_text;
237                                         } else {
238                                                 $postarray['body'] = $status->text;
239                                         }
240
241                                         // When the post contains links then use the correct object type
242                                         if (count($status->entities->urls) > 0) {
243                                                 $postarray['object-type'] = Activity\ObjectType::BOOKMARK;
244                                         }
245
246                                         if (file_exists('addon/twitter/twitter.php')) {
247                                                 require_once 'addon/twitter/twitter.php';
248
249                                                 $picture = \twitter_media_entities($status, $postarray);
250
251                                                 $results[] = [
252                                                         'title'   => DI::l10n()->t('Post array before expand entities'),
253                                                         'content' => visible_whitespace(var_export($postarray, true)),
254                                                 ];
255
256                                                 $converted = \twitter_expand_entities($postarray['body'], $status, $picture);
257
258                                                 $results[] = [
259                                                         'title'   => DI::l10n()->t('Post converted'),
260                                                         'content' => visible_whitespace(var_export($converted, true)),
261                                                 ];
262
263                                                 $results[] = [
264                                                         'title'   => DI::l10n()->t('Converted body'),
265                                                         'content' => visible_whitespace($converted['body']),
266                                                 ];
267                                         } else {
268                                                 $results[] = [
269                                                         'title'   => DI::l10n()->t('Error'),
270                                                         'content' => DI::l10n()->t('Twitter addon is absent from the addon/ folder.'),
271                                                 ];
272                                         }
273
274                                         break;
275                         }
276                 }
277
278                 $tpl = Renderer::getMarkupTemplate('babel.tpl');
279                 $o = Renderer::replaceMacros($tpl, [
280                         '$text'          => ['text', DI::l10n()->t('Source text'), $_REQUEST['text'] ?? '', ''],
281                         '$type_bbcode'   => ['type', DI::l10n()->t('BBCode'), 'bbcode', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'bbcode'],
282                         '$type_diaspora' => ['type', DI::l10n()->t('Diaspora'), 'diaspora', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'diaspora'],
283                         '$type_markdown' => ['type', DI::l10n()->t('Markdown'), 'markdown', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'markdown'],
284                         '$type_html'     => ['type', DI::l10n()->t('HTML'), 'html', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'html'],
285                         '$flag_twitter'  => file_exists('addon/twitter/twitter.php'),
286                         '$type_twitter'  => ['type', DI::l10n()->t('Twitter Source'), 'twitter', '', (($_REQUEST['type'] ?? '') ?: 'bbcode') == 'twitter'],
287                         '$results'       => $results
288                 ]);
289
290                 return $o;
291         }
292 }