MadelineProto/src/danog/MadelineProto/DocsBuilder/Constructors.php

292 lines
14 KiB
PHP
Raw Normal View History

2017-08-18 18:10:30 +02:00
<?php
2017-08-18 18:10:30 +02:00
/*
Copyright 2016-2018 Daniil Gentili
2017-08-18 18:10:30 +02:00
(https://daniil.it)
This file is part of MadelineProto.
MadelineProto is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
MadelineProto is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU Affero General Public License for more details.
You should have received a copy of the GNU General Public License along with MadelineProto.
If not, see <http://www.gnu.org/licenses/>.
*/
2018-02-24 17:54:39 +01:00
2017-08-18 18:10:30 +02:00
namespace danog\MadelineProto\DocsBuilder;
trait Constructors
{
2017-08-18 18:11:04 +02:00
public function mk_constructors()
{
2018-02-24 17:54:39 +01:00
foreach (glob('constructors/'.$this->any) as $unlink) {
2017-08-18 18:10:30 +02:00
unlink($unlink);
}
if (file_exists('constructors')) {
rmdir('constructors');
}
mkdir('constructors');
$this->docs_constructors = [];
2018-04-08 19:53:30 +02:00
$this->logger->logger('Generating constructors documentation...', \danog\MadelineProto\Logger::NOTICE);
2017-08-18 18:10:30 +02:00
$got = [];
foreach ($this->constructors->by_predicate_and_layer as $predicate => $id) {
$data = $this->constructors->by_id[$id];
if (isset($got[$id])) {
$data['layer'] = '';
}
$got[$id] = '';
/*
if (preg_match('/%/', $type)) {
$type = $this->constructors->find_by_type(str_replace('%', '', $type))['predicate'];
}*/
2018-02-24 17:54:39 +01:00
$layer = isset($data['layer']) && $data['layer'] !== '' ? '_'.$data['layer'] : '';
2017-08-18 18:10:30 +02:00
$type = str_replace(['.', '<', '>'], ['_', '_of_', ''], $data['type']);
$php_type = preg_replace('/.*_of_/', '', $type);
$constructor = str_replace(['.', '<', '>'], ['_', '_of_', ''], $data['predicate']);
$php_constructor = preg_replace('/.*_of_/', '', $constructor);
if (!isset($this->types[$php_type])) {
$this->types[$php_type] = ['constructors' => [], 'methods' => []];
}
if (!in_array($data, $this->types[$php_type]['constructors'])) {
$this->types[$php_type]['constructors'][] = $data;
}
$params = '';
foreach ($data['params'] as $param) {
if (in_array($param['name'], ['flags', 'random_id', 'random_bytes'])) {
continue;
}
2017-09-28 19:38:43 +02:00
if ($type === 'EncryptedMessage' && $param['name'] === 'bytes' && !isset($this->settings['td'])) {
2017-08-18 18:10:30 +02:00
$param['name'] = 'decrypted_message';
$param['type'] = 'DecryptedMessage';
}
$type_or_subtype = isset($param['subtype']) ? 'subtype' : 'type';
$type_or_bare_type = ctype_upper($this->end(explode('.', $param[$type_or_subtype]))[0]) || in_array($param[$type_or_subtype], ['!X', 'X', 'bytes', 'true', 'false', 'double', 'string', 'Bool', 'int53', 'int', 'long', 'int128', 'int256', 'int512']) ? 'types' : 'constructors';
2017-08-18 18:10:30 +02:00
$param[$type_or_subtype] = str_replace(['.', 'true', 'false'], ['_', 'Bool', 'Bool'], $param[$type_or_subtype]);
if (preg_match('/%/', $param[$type_or_subtype])) {
$param[$type_or_subtype] = $this->constructors->find_by_type(str_replace('%', '', $param[$type_or_subtype]))['predicate'];
}
2017-09-29 15:50:59 +02:00
if (substr($param[$type_or_subtype], -1) === '>') {
2017-09-29 16:09:05 +02:00
$param[$type_or_subtype] = substr($param[$type_or_subtype], 0, -1);
2017-09-29 15:50:59 +02:00
}
2018-02-24 17:54:39 +01:00
$params .= "'".$param['name']."' => ";
$param[$type_or_subtype] = '['.$this->escape($param[$type_or_subtype]).'](../'.$type_or_bare_type.'/'.$param[$type_or_subtype].'.md)';
$params .= (isset($param['subtype']) ? '\\['.$param[$type_or_subtype].'\\]' : $param[$type_or_subtype]).', ';
2017-08-18 18:10:30 +02:00
}
2018-02-24 17:54:39 +01:00
$md_constructor = str_replace('_', '\\_', $constructor.$layer);
$this->docs_constructors[$constructor] = '[$'.$md_constructor.'](../constructors/'.$php_constructor.$layer.'.md) = \\['.$params.'\\];<a name="'.$constructor.$layer.'"></a>
2017-08-18 18:10:30 +02:00
';
$table = empty($data['params']) ? '' : '### Attributes:
| Name | Type | Required |
2017-08-20 11:05:56 +02:00
|----------|---------------|----------|
2017-08-18 18:10:30 +02:00
';
if (!isset($this->td_descriptions['constructors'][$data['predicate']])) {
$this->add_to_lang('object_'.$data['predicate']);
if (\danog\MadelineProto\Lang::$lang['en']['object_'.$data['predicate']] !== '') {
$this->td_descriptions['constructors'][$data['predicate']]['description'] = \danog\MadelineProto\Lang::$lang['en']['object_'.$data['predicate']];
}
}
2018-03-20 19:39:53 +01:00
if (isset($this->td_descriptions['constructors'][$data['predicate']]) && !empty($data['params'])) {
2017-08-18 18:10:30 +02:00
$table = '### Attributes:
| Name | Type | Required | Description |
2017-08-20 11:05:56 +02:00
|----------|---------------|----------|-------------|
2017-08-18 18:10:30 +02:00
';
}
$params = '';
$lua_params = '';
$pwr_params = '';
$hasreplymarkup = false;
$hasentities = false;
foreach ($data['params'] as $param) {
if (in_array($param['name'], ['flags', 'random_id', 'random_bytes'])) {
continue;
}
2017-09-28 19:38:43 +02:00
if ($type === 'EncryptedMessage' && $param['name'] === 'bytes' && !isset($this->settings['td'])) {
2017-08-18 18:10:30 +02:00
$param['name'] = 'decrypted_message';
$param['type'] = 'DecryptedMessage';
}
if ($type === 'DecryptedMessageMedia' && in_array($param['name'], ['key', 'iv'])) {
unset(\danog\MadelineProto\Lang::$lang['en']['object_'.$data['predicate'].'_param_'.$param['name'].'_type_'.$param['type']]);
continue;
}
2017-08-18 18:10:30 +02:00
$ptype = str_replace('.', '_', $param[isset($param['subtype']) ? 'subtype' : 'type']);
2017-09-29 15:50:59 +02:00
//$type_or_bare_type = 'types';
/*if (isset($param['subtype'])) {
if ($param['type'] === 'vector') {
$type_or_bare_type = 'constructors';
}
}*/
2017-08-18 18:10:30 +02:00
if (preg_match('/%/', $ptype)) {
$ptype = $this->constructors->find_by_type(str_replace('%', '', $ptype))['predicate'];
}
$type_or_bare_type = (ctype_upper($this->end(explode('_', $ptype))[0]) || in_array($ptype, ['!X', 'X', 'bytes', 'true', 'false', 'double', 'string', 'Bool', 'int53', 'int', 'long', 'int128', 'int256', 'int512'])) && $ptype !== 'MTmessage' ? 'types' : 'constructors';
2017-09-29 15:50:59 +02:00
if (substr($ptype, -1) === '>') {
2017-09-29 16:09:05 +02:00
$ptype = substr($ptype, 0, -1);
2017-09-29 15:50:59 +02:00
}
2017-08-18 18:10:30 +02:00
switch ($ptype) {
case 'true':
case 'false':
$ptype = 'Bool';
}
$human_ptype = $ptype;
if (strpos($type, 'Input') === 0 && in_array($ptype, ['User', 'InputUser', 'Chat', 'InputChannel', 'Peer', 'InputDialogPeer', 'DialogPeer', 'NotifyPeer', 'InputNotifyPeer', 'InputPeer']) && !isset($this->settings['td'])) {
$human_ptype = 'Username, chat ID, Update, Message or '.$ptype;
}
2018-03-23 16:39:58 +01:00
if (strpos($type, 'Input') === 0 && in_array($ptype, ['InputMedia', 'InputDocument', 'InputPhoto']) && !isset($this->settings['td'])) {
$human_ptype = 'MessageMedia, Message, Update or '.$ptype;
}
2018-03-23 16:39:58 +01:00
if (in_array($ptype, ['InputMessage']) && !isset($this->settings['td'])) {
$human_ptype = 'Message ID or '.$ptype;
}
2018-03-23 16:39:58 +01:00
if (in_array($ptype, ['InputEncryptedChat']) && !isset($this->settings['td'])) {
$human_ptype = 'Secret chat ID, Update, EncryptedMessage or '.$ptype;
}
2018-03-23 16:39:58 +01:00
if (in_array($ptype, ['InputFile']) && !isset($this->settings['td'])) {
$human_ptype = 'File path or '.$ptype;
}
2018-03-23 16:39:58 +01:00
if (in_array($ptype, ['InputEncryptedFile']) && !isset($this->settings['td'])) {
$human_ptype = 'File path or '.$ptype;
}
$table .= '|'.str_replace('_', '\\_', $param['name']).'|'.(isset($param['subtype']) ? 'Array of ' : '').'['.str_replace('_', '\\_', $human_ptype).'](../'.$type_or_bare_type.'/'.$ptype.'.md) | '.(isset($param['pow']) || $this->constructors->find_by_predicate(lcfirst($param['type']).'Empty') || ($data['type'] === 'InputMedia' && $param['name'] === 'mime_type') || ($data['type'] === 'DocumentAttribute' && in_array($param['name'], ['w', 'h', 'duration'])) ? 'Optional' : 'Yes').'|';
if (!isset($this->td_descriptions['constructors'][$data['predicate']]['params'][$param['name']])) {
$this->add_to_lang('object_'.$data['predicate'].'_param_'.$param['name'].'_type_'.$param['type']);
if (isset($this->td_descriptions['constructors'][$data['predicate']]['description'])) {
$this->td_descriptions['constructors'][$data['predicate']]['params'][$param['name']] = \danog\MadelineProto\Lang::$lang['en']['object_'.$data['predicate'].'_param_'.$param['name'].'_type_'.$param['type']];
}
}
2017-08-18 18:10:30 +02:00
if (isset($this->td_descriptions['constructors'][$data['predicate']]['params'][$param['name']])) {
2018-02-24 17:54:39 +01:00
$table .= $this->td_descriptions['constructors'][$data['predicate']]['params'][$param['name']].'|';
2017-08-18 18:10:30 +02:00
}
$table .= PHP_EOL;
2018-02-24 17:54:39 +01:00
$pptype = in_array($ptype, ['string', 'bytes']) ? "'".$ptype."'" : $ptype;
2018-04-19 19:56:52 +02:00
$ppptype = in_array($ptype, ['string']) ? '"'.$ptype.'"' : $ptype;
$ppptype = in_array($ptype, ['bytes']) ? '{"_": "bytes", "bytes":"base64 encoded '.$ptype.'"}' : $ppptype;
2018-02-24 17:54:39 +01:00
$params .= ", '".$param['name']."' => ";
$params .= isset($param['subtype']) ? '['.$pptype.', '.$pptype.']' : $pptype;
2018-02-24 17:54:39 +01:00
$lua_params .= ', '.$param['name'].'=';
$lua_params .= isset($param['subtype']) ? '{'.$pptype.'}' : $pptype;
$pwr_params .= ', "'.$param['name'].'": '.(isset($param['subtype']) ? '['.$ppptype.']' : $ppptype);
2017-08-18 18:10:30 +02:00
if ($param['name'] === 'reply_markup') {
$hasreplymarkup = true;
}
}
2018-02-24 17:54:39 +01:00
$params = "['_' => '".$data['predicate']."'".$params.']';
$lua_params = "{_='".$data['predicate']."'".$lua_params.'}';
$pwr_params = '{"_": "'.$data['predicate'].'"'.$pwr_params.'}';
$description = isset($this->td_descriptions['constructors'][$data['predicate']]) ? $this->td_descriptions['constructors'][$data['predicate']]['description'] : $constructor.' attributes, type and example';
2017-08-18 18:10:30 +02:00
$header = '---
2018-02-24 17:54:39 +01:00
title: '.$data['predicate'].'
description: '.$description.'
2018-04-04 19:52:41 +02:00
image: https://docs.madelineproto.xyz/favicons/android-chrome-256x256.png
2017-08-18 18:10:30 +02:00
---
2018-04-04 20:11:59 +02:00
# Constructor: '.str_replace('_', '\\_', $data['predicate'].$layer).'
2017-08-18 18:10:30 +02:00
[Back to constructors index](index.md)
';
$table .= '
';
if (isset($this->td_descriptions['constructors'][$data['predicate']])) {
2018-02-24 17:54:39 +01:00
$header .= $this->td_descriptions['constructors'][$data['predicate']]['description'].PHP_EOL.PHP_EOL;
2017-08-18 18:10:30 +02:00
}
2018-02-24 17:54:39 +01:00
$type = '### Type: ['.str_replace('_', '\\_', $php_type).'](../types/'.$php_type.'.md)
2017-08-18 18:10:30 +02:00
';
2017-08-25 14:43:29 +02:00
$example = '';
if (!isset($this->settings['td'])) {
2017-08-25 14:43:50 +02:00
$example = '### Example:
2017-08-18 18:10:30 +02:00
```php
2018-02-24 17:54:39 +01:00
$'.$constructor.$layer.' = '.$params.';
2017-08-18 18:10:30 +02:00
```
[PWRTelegram](https://pwrtelegram.xyz) json-encoded version:
```
2018-02-24 17:54:39 +01:00
'.$pwr_params.'
2017-08-18 18:10:30 +02:00
```
Or, if you\'re into Lua:
2017-08-18 18:10:30 +02:00
```lua
2018-02-24 17:54:39 +01:00
'.$constructor.$layer.'='.$lua_params.'
2017-08-18 18:10:30 +02:00
```
';
2017-08-25 14:43:50 +02:00
if ($hasreplymarkup) {
$example .= '
2017-08-18 18:10:30 +02:00
## Usage of reply_markup
You can provide bot API reply_markup objects here.
';
2017-08-25 14:43:50 +02:00
}
if ($hasentities) {
$example .= '
2017-08-18 18:10:30 +02:00
## Usage of parse_mode:
Set parse_mode to html to enable HTML parsing of the message.
Set parse_mode to Markdown to enable markown AND html parsing of the message.
The following tags are currently supported:
```html
2017-08-18 18:10:30 +02:00
<br>a newline
<b><i>bold works ok, internal tags are stripped</i> </b>
<strong>bold</strong>
<em>italic</em>
<i>italic</i>
<code>inline fixed-width code</code>
<pre>pre-formatted fixed-width code block</pre>
<a href="https://github.com">URL</a>
<a href="mention:@danogentili">Mention by username</a>
<a href="mention:186785362">Mention by user id</a>
<pre language="json">Pre tags can have a language attribute</pre>
```
You can also use normal markdown, note that to create mentions you must use the `mention:` syntax like in html:
```markdown
2017-08-18 18:10:30 +02:00
[Mention by username](mention:@danogentili)
[Mention by user id](mention:186785362)
```
MadelineProto supports all html entities supported by [html_entity_decode](http://php.net/manual/en/function.html-entity-decode.php).
';
2017-08-25 14:43:50 +02:00
}
}
2018-02-24 17:54:39 +01:00
file_put_contents('constructors/'.$constructor.$layer.'.md', $header.$table.$type.$example);
2017-08-18 18:10:30 +02:00
}
2018-04-08 19:53:30 +02:00
$this->logger->logger('Generating constructors index...', \danog\MadelineProto\Logger::NOTICE);
2017-08-18 18:10:30 +02:00
ksort($this->docs_constructors);
$last_namespace = '';
foreach ($this->docs_constructors as $constructor => &$value) {
$new_namespace = preg_replace('/_.*/', '', $constructor);
$br = $new_namespace != $last_namespace ? '***
<br><br>' : '';
2018-02-24 17:54:39 +01:00
$value = $br.$value;
2017-08-18 18:10:30 +02:00
$last_namespace = $new_namespace;
}
2018-02-24 17:54:39 +01:00
file_put_contents('constructors/'.$this->index, '---
2017-08-18 18:10:30 +02:00
title: Constructors
description: List of constructors
2018-04-04 19:52:41 +02:00
image: https://docs.madelineproto.xyz/favicons/android-chrome-256x256.png
2017-08-18 18:10:30 +02:00
---
# Constructors
[Back to API documentation index](..)
2018-02-24 17:54:39 +01:00
'.implode('', $this->docs_constructors));
2017-08-18 18:10:30 +02:00
}
2018-02-24 17:54:39 +01:00
}