MadelineProto/build_docs.php

516 lines
14 KiB
PHP
Raw Normal View History

#!/usr/bin/env php
<?php
/*
Copyright 2016 Daniil Gentili
(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/>.
*/
require_once 'vendor/autoload.php';
$mode = 3;
\danog\MadelineProto\Logger::constructor($mode);
$TL = new \danog\MadelineProto\MTProto(['tl' => [
//'mtproto' => __DIR__.'/src/danog/MadelineProto/TL_mtproto_v1.json', // mtproto TL scheme
'telegram' => __DIR__.'/src/danog/MadelineProto/TL_telegram_v57.json', // telegram TL scheme
]]);
2016-12-24 17:20:45 +01:00
$types = [];
\danog\MadelineProto\Logger::log('Copying readme...');
2016-12-20 13:15:22 +01:00
file_put_contents('docs/index.md', '---
title: MadelineProto documentation
2016-12-20 13:32:11 +01:00
description: PHP implementation of telegram\'s MTProto protocol
2016-12-20 13:15:22 +01:00
---
'.file_get_contents('README.md'));
chdir(__DIR__.'/docs/API_docs');
\danog\MadelineProto\Logger::log('Generating documentation index...');
2016-12-20 13:15:22 +01:00
file_put_contents('index.md', '---
title: MadelineProto API documentation (layer 57)
2016-12-20 13:32:11 +01:00
description: MadelineProto API documentation (layer 57)
2016-12-20 13:15:22 +01:00
---
# MadelineProto API documentation (layer 57)
[Methods](methods/)
[Constructors](constructors/)
[Types](types/)
2016-12-20 13:15:22 +01:00
[Back to main documentation](..)
');
foreach (glob('methods/*') as $unlink) {
unlink($unlink);
}
if (file_exists('methods')) {
rmdir('methods');
}
mkdir('methods');
$methods = [];
\danog\MadelineProto\Logger::log('Generating methods documentation...');
foreach ($TL->methods->method as $key => $method) {
2016-12-19 19:20:07 +01:00
$method = str_replace('.', '_', $method);
$type = str_replace(['.', '<', '>'], ['_', '_of_', ''], $TL->methods->type[$key]);
$real_type = preg_replace('/.*_of_/', '', $type);
2016-12-24 17:20:45 +01:00
if (!isset($types[$real_type])) {
$types[$real_type] = ['constructors' => [], 'methods' => []];
}
if (!in_array($key, $types[$real_type]['methods'])) {
$types[$real_type]['methods'][] = $key;
}
$params = '';
foreach ($TL->methods->params[$key] as $param) {
if (in_array($param['name'], ['flags', 'random_id'])) {
2016-12-19 18:58:09 +01:00
continue;
}
$stype = 'type';
$link_type = 'types';
if (isset($param['subtype'])) {
$stype = 'subtype';
if ($param['type'] == 'vector') {
$link_type = 'constructors';
}
}
$ptype = str_replace('.', '_', $param[$stype]);
switch ($ptype) {
case 'true':
case 'false':
$ptype = 'Bool';
}
2016-12-19 18:58:09 +01:00
$params .= "'".$param['name']."' => ";
2016-12-19 20:35:57 +01:00
$ptype =
2016-12-19 18:35:27 +01:00
'['.
str_replace('_', '\_', $ptype).
'](../'.$link_type.'/'.$ptype.'.md)';
2016-12-19 20:35:57 +01:00
$params .= (isset($param['subtype']) ? '\['.$ptype.'\]' : $ptype).', ';
}
2016-12-19 19:37:29 +01:00
$md_method = '['.str_replace('_', '->', $method).']('.$method.'.md)';
2016-12-20 13:15:22 +01:00
$methods[$method] = '$MadelineProto->'.$md_method.'(\['.$params.'\]) == [$'.str_replace('_', '\_', $type).'](../types/'.$real_type.'.md)<a name="'.$method.'"></a>
2016-12-19 18:35:27 +01:00
';
$params = '';
2016-12-19 17:48:27 +01:00
$table = empty($TL->methods->params[$key]) ? '' : '### Parameters:
| Name | Type | Required |
|----------|:-------------:|---------:|
';
foreach ($TL->methods->params[$key] as $param) {
if (in_array($param['name'], ['flags', 'random_id'])) {
2016-12-19 18:58:09 +01:00
continue;
}
$ptype = str_replace('.', '_', $param[isset($param['subtype']) ? 'subtype' : 'type']);
switch ($ptype) {
case 'true':
case 'false':
$ptype = 'Bool';
}
2016-12-19 18:35:27 +01:00
$table .= '|'.str_replace('_', '\_', $param['name']).'|'.(isset($param['subtype']) ? 'Array of ' : '').'['.str_replace('_', '\_', $ptype).'](../types/'.$ptype.'.md) | '.($param['flag'] ? 'Optional' : 'Required').'|
';
2016-12-19 18:58:09 +01:00
$params .= "'".$param['name']."' => ";
$params .= (isset($param['subtype']) ? '['.$ptype.']' : $ptype).', ';
}
2016-12-20 13:15:22 +01:00
$header = '---
title: '.$method.'
2016-12-20 13:32:11 +01:00
description: '.$method.' parameters, return type and example
2016-12-20 13:15:22 +01:00
---
## Method: '.str_replace('_', '\_', $method).'
[Back to methods index](index.md)
2016-12-19 18:35:27 +01:00
2016-12-19 19:37:29 +01:00
';
2016-12-19 18:35:27 +01:00
$table .= '
';
$return = '### Return type: ['.str_replace('_', '\_', $type).'](../types/'.$real_type.'.md)
';
2016-12-19 18:56:05 +01:00
$example = str_replace('[]', '', '### Example:
```
$MadelineProto = new \danog\MadelineProto\API();
if (isset($token)) {
$this->bot_login($token);
}
if (isset($number)) {
$sentCode = $MadelineProto->phone_login($number);
echo \'Enter the code you received: \';
$code = \'\';
for ($x = 0; $x < $sentCode[\'type\'][\'length\']; $x++) {
$code .= fgetc(STDIN);
}
$MadelineProto->complete_phone_login($code);
}
2016-12-21 10:40:30 +01:00
$'.$type.' = $MadelineProto->'.str_replace('_', '->', $method).'(['.$params.']);
```');
2016-12-19 18:35:27 +01:00
file_put_contents('methods/'.$method.'.md', $header.$table.$return.$example);
}
\danog\MadelineProto\Logger::log('Generating methods index...');
ksort($methods);
2016-12-20 13:15:22 +01:00
$last_namespace = '';
foreach ($methods as $method => &$value) {
$new_namespace = preg_replace('/_.*/', '', $method);
$br = $new_namespace != $last_namespace ? '***
<br><br>' : '';
$value = $br.$value;
$last_namespace = $new_namespace;
}
file_put_contents('methods/index.md', '---
title: Methods
2016-12-20 13:32:11 +01:00
description: List of methods
2016-12-20 13:15:22 +01:00
---
# Methods
[Back to API documentation index](..)
'.implode('', $methods));
foreach (glob('constructors/*') as $unlink) {
unlink($unlink);
}
if (file_exists('constructors')) {
rmdir('constructors');
}
mkdir('constructors');
$constructors = [];
\danog\MadelineProto\Logger::log('Generating constructors documentation...');
foreach ($TL->constructors->predicate as $key => $constructor) {
$type = str_replace(['.', '<', '>'], ['_', '_of_', ''], $TL->constructors->type[$key]);
$real_type = preg_replace('/.*_of_/', '', $type);
2016-12-19 18:58:09 +01:00
2016-12-20 13:15:22 +01:00
$constructor = str_replace(['.', '<', '>'], ['_', '_of_', ''], $constructor);
$real_constructor = preg_replace('/.*_of_/', '', $constructor);
$params = '';
foreach ($TL->constructors->params[$key] as $param) {
if (in_array($param['name'], ['flags', 'random_id'])) {
2016-12-19 18:58:09 +01:00
continue;
}
$stype = 'type';
$link_type = 'types';
if (isset($param['subtype'])) {
$stype = 'subtype';
if ($param['type'] == 'vector') {
$link_type = 'constructors';
}
}
$ptype = str_replace('.', '_', $param[$stype]);
switch ($ptype) {
case 'true':
case 'false':
$ptype = 'Bool';
}
2016-12-19 20:35:57 +01:00
2016-12-19 18:58:09 +01:00
$params .= "'".$param['name']."' => ";
2016-12-19 20:35:57 +01:00
$ptype =
2016-12-19 18:35:27 +01:00
'['.
str_replace('_', '\_', $ptype).
'](../'.$link_type.'/'.$ptype.'.md)';
2016-12-19 20:35:57 +01:00
$params .= (isset($param['subtype']) ? '\['.$ptype.'\]' : $ptype).', ';
}
2016-12-20 13:15:22 +01:00
$md_constructor = str_replace('_', '\_', $constructor);
2016-12-20 13:15:22 +01:00
$constructors[$constructor] = '[$'.$md_constructor.'](../constructors/'.$real_constructor.'.md) = \['.$params.'\];<a name="'.$constructor.'"></a>
2016-12-19 18:35:27 +01:00
';
if (!isset($types[$real_type])) {
2016-12-24 17:20:45 +01:00
$types[$real_type] = ['constructors' => [], 'methods' => []];
}
2016-12-24 17:20:45 +01:00
if (!in_array($key, $types[$real_type]['constructors'])) {
$types[$real_type]['constructors'][] = $key;
}
2016-12-19 17:48:27 +01:00
$table = empty($TL->constructors->params[$key]) ? '' : '### Attributes:
2016-12-19 17:48:27 +01:00
| Name | Type | Required |
|----------|:-------------:|---------:|
';
2016-12-19 17:48:27 +01:00
$params = '';
foreach ($TL->constructors->params[$key] as $param) {
if (in_array($param['name'], ['flags', 'random_id'])) {
2016-12-19 18:58:09 +01:00
continue;
}
$ptype = str_replace('.', '_', $param[isset($param['subtype']) ? 'subtype' : 'type']);
2016-12-19 18:58:09 +01:00
$link_type = 'types';
if (isset($param['subtype'])) {
if ($param['type'] == 'vector') {
$link_type = 'constructors';
}
}
switch ($ptype) {
case 'true':
case 'false':
$ptype = 'Bool';
}
2016-12-19 18:35:27 +01:00
$table .= '|'.str_replace('_', '\_', $param['name']).'|'.(isset($param['subtype']) ? 'Array of ' : '').'['.str_replace('_', '\_', $ptype).'](../'.$link_type.'/'.$ptype.'.md) | '.($param['flag'] ? 'Optional' : 'Required').'|
';
2016-12-19 18:58:09 +01:00
$params .= "'".$param['name']."' => ";
2016-12-19 17:48:27 +01:00
$params .= (isset($param['subtype']) ? '['.$param['type'].']' : $param['type']).', ';
}
2016-12-30 16:36:44 +01:00
$params = "['_' => ".$constructor.', '.$params.']';
2016-12-19 17:48:27 +01:00
2016-12-20 13:15:22 +01:00
$header = '---
title: '.$constructor.'
2016-12-20 13:32:11 +01:00
description: '.$constructor.' attributes, type and example
2016-12-20 13:15:22 +01:00
---
## Constructor: '.str_replace('_', '\_', $constructor).'
[Back to constructors index](index.md)
2016-12-19 19:37:29 +01:00
';
2016-12-19 18:56:05 +01:00
$table .= '
';
$type = '### Type: ['.str_replace('_', '\_', $real_type).'](../types/'.$real_type.'.md)
2016-12-19 18:35:27 +01:00
';
$example = '### Example:
2016-12-19 18:35:27 +01:00
```
$'.$constructor.' = '.$params.';
```';
file_put_contents('constructors/'.$constructor.'.md', $header.$table.$type.$example);
}
\danog\MadelineProto\Logger::log('Generating constructors index...');
ksort($constructors);
2016-12-20 13:15:22 +01:00
$last_namespace = '';
foreach ($constructors as $method => &$value) {
$new_namespace = preg_replace('/_.*/', '', $method);
$br = $new_namespace != $last_namespace ? '***
<br><br>' : '';
$value = $br.$value;
$last_namespace = $new_namespace;
}
2016-12-20 13:32:11 +01:00
file_put_contents('constructors/index.md', '---
2016-12-23 12:19:13 +01:00
title: Constructors
2016-12-20 13:32:11 +01:00
description: List of constructors
---
# Constructors
2016-12-20 13:15:22 +01:00
[Back to API documentation index](..)
'.implode('', $constructors));
foreach (glob('types/*') as $unlink) {
unlink($unlink);
}
if (file_exists('types')) {
rmdir('types');
}
mkdir('types');
ksort($types);
$index = '';
\danog\MadelineProto\Logger::log('Generating types documentation...');
2016-12-20 13:15:22 +01:00
$old_namespace = '';
foreach ($types as $type => $keys) {
2016-12-20 13:15:22 +01:00
$new_namespace = preg_replace('/_.*/', '', $method);
$br = $new_namespace != $last_namespace ? '***
<br><br>' : '';
2016-12-19 19:20:07 +01:00
$type = str_replace('.', '_', $type);
2016-12-20 13:15:22 +01:00
$index .= $br.'['.str_replace('_', '\_', $type).']('.$type.'.md)<a name="'.$type.'"></a>
';
$constructors = '';
2016-12-24 17:20:45 +01:00
foreach ($keys['constructors'] as $key) {
$predicate = str_replace('.', '_', $TL->constructors->predicate[$key]);
2016-12-20 13:15:22 +01:00
$md_predicate = str_replace('_', '\_', $predicate);
$constructors .= '['.$md_predicate.'](../constructors/'.$predicate.'.md)
';
}
2016-12-24 17:20:45 +01:00
$methods = '';
foreach ($keys['methods'] as $key) {
$name = str_replace('.', '_', $TL->methods->method[$key]);
$md_name = str_replace('_', '->', $name);
$methods .= '[$MadelineProto->'.$md_name.'](../methods/'.$name.'.md)
';
}
2016-12-20 13:15:22 +01:00
$header = '---
title: '.$type.'
2016-12-24 17:20:45 +01:00
description: constructors and methods of type '.$type.'
2016-12-20 13:15:22 +01:00
---
## Type: '.str_replace('_', '\_', $type).'
[Back to types index](index.md)
2016-12-24 17:20:45 +01:00
';
$constructors = '### Possible values (constructors):
'.$constructors.'
';
$methods = '### Methods that return an object of this type (methods):
'.$methods.'
2016-12-19 19:37:29 +01:00
';
2016-12-24 17:20:45 +01:00
file_put_contents('types/'.$type.'.md', $header.$constructors.$methods);
2016-12-20 13:15:22 +01:00
$last_namespace = $new_namespace;
}
2016-12-19 18:56:05 +01:00
\danog\MadelineProto\Logger::log('Generating types index...');
2016-12-20 13:15:22 +01:00
file_put_contents('types/index.md', '---
title: Types
2016-12-20 13:32:11 +01:00
description: List of types
2016-12-20 13:15:22 +01:00
---
# Types
[Back to API documentation index](..)
2016-12-19 18:56:05 +01:00
'.$index);
\danog\MadelineProto\Logger::log('Generating additional types...');
2016-12-20 13:15:22 +01:00
file_put_contents('types/string.md', '---
title: string
2016-12-20 13:32:11 +01:00
description: A string of variable length
2016-12-20 13:15:22 +01:00
---
## Type: string
[Back to constructor index](index.md)
A string of variable length.');
2016-12-20 13:15:22 +01:00
file_put_contents('types/bytes.md', '---
title: bytes
2016-12-20 13:32:11 +01:00
description: A string of variable length
2016-12-20 13:15:22 +01:00
---
## Type: bytes
[Back to constructor index](index.md)
A string of variable length.');
2016-12-20 13:15:22 +01:00
file_put_contents('types/int.md', '---
title: integer
2016-12-20 13:32:11 +01:00
description: A 32 bit signed integer ranging from -2147483647 to 2147483647
2016-12-20 13:15:22 +01:00
---
## Type: int
[Back to constructor index](index.md)
2016-12-20 13:15:22 +01:00
A 32 bit signed integer ranging from `-2147483647` to `2147483647`.');
2016-12-20 13:15:22 +01:00
file_put_contents('types/long.md', '---
title: long
2016-12-20 13:32:11 +01:00
description: A 32 bit signed integer ranging from -9223372036854775807 to 9223372036854775807
2016-12-20 13:15:22 +01:00
---
## Type: long
[Back to constructor index](index.md)
2016-12-20 13:15:22 +01:00
A 64 bit signed integer ranging from `-9223372036854775807` to `9223372036854775807`.');
2016-12-20 13:15:22 +01:00
file_put_contents('types/double.md', '---
title: double
2016-12-20 13:32:11 +01:00
description: A double precision floating point number
2016-12-20 13:15:22 +01:00
---
## Type: double
[Back to constructor index](index.md)
2016-12-20 13:15:22 +01:00
A double precision floating point number, single precision can also be used (float).');
2016-12-20 13:15:22 +01:00
file_put_contents('types/!X.md', '---
title: !X
2016-12-20 13:32:11 +01:00
description: Represents a TL serialized payload
2016-12-20 13:15:22 +01:00
---
## Type: !X
[Back to constructor index](index.md)
Represents a TL serialized payload.');
2016-12-20 13:15:22 +01:00
file_put_contents('types/X.md', '---
title: X
2016-12-20 13:32:11 +01:00
description: Represents a TL serialized payload
2016-12-20 13:15:22 +01:00
---
## Type: X
[Back to constructor index](index.md)
Represents a TL serialized payload.');
2016-12-20 13:15:22 +01:00
file_put_contents('constructors/boolFalse.md', '---
title: boolFalse
2016-12-20 13:32:11 +01:00
description: Represents a boolean with value equal to false
2016-12-20 13:15:22 +01:00
---
# boolFalse
[Back to constructor index](index.md)
Represents a boolean with value equal to `false`.');
file_put_contents('constructors/boolTrue.md', '---
title: boolTrue
2016-12-20 13:32:11 +01:00
description: Represents a boolean with value equal to true
2016-12-20 13:15:22 +01:00
---
# boolTrue
[Back to constructor index](index.md)
2016-12-19 17:48:27 +01:00
2016-12-20 13:15:22 +01:00
Represents a boolean with value equal to `true`.');
2016-12-19 17:48:27 +01:00
2016-12-24 17:20:45 +01:00
file_put_contents('constructors/null.md', '---
title: null
description: Represents a null value
---
# null
[Back to constructor index](index.md)
Represents a `null` value.');
2016-12-20 13:15:22 +01:00
file_put_contents('types/Bool.md', '---
title: Bool
2016-12-20 13:32:11 +01:00
description: Represents a boolean.
2016-12-20 13:15:22 +01:00
---
# Bool
[Back to types index](index.md)
2016-12-19 17:48:27 +01:00
2016-12-20 13:15:22 +01:00
Represents a boolean.');
2016-12-19 17:48:27 +01:00
\danog\MadelineProto\Logger::log('Done!');