2015-04-07 19:58:49 +02:00
|
|
|
<?php
|
2016-05-20 12:27:31 +02:00
|
|
|
/**
|
|
|
|
* Help.php
|
2020-01-28 08:46:01 +01:00
|
|
|
* Copyright (c) 2019 james@firefly-iii.org
|
2016-05-20 12:27:31 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
2016-10-05 06:52:15 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program 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.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
2017-10-21 08:40:00 +02:00
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2019-10-02 06:37:26 +02:00
|
|
|
* GNU Affero General Public License for more details.
|
2017-10-21 08:40:00 +02:00
|
|
|
*
|
2019-10-02 06:37:26 +02:00
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2016-05-20 12:27:31 +02:00
|
|
|
*/
|
2017-03-24 15:15:12 +01:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2015-04-07 19:58:49 +02:00
|
|
|
namespace FireflyIII\Helpers\Help;
|
|
|
|
|
|
|
|
use Cache;
|
2018-04-02 14:42:07 +02:00
|
|
|
use Exception;
|
2018-06-06 21:20:21 +02:00
|
|
|
use GuzzleHttp\Client;
|
2015-04-07 19:58:49 +02:00
|
|
|
use League\CommonMark\CommonMarkConverter;
|
2016-10-31 18:31:52 +01:00
|
|
|
use Log;
|
2015-04-07 19:58:49 +02:00
|
|
|
use Route;
|
|
|
|
|
|
|
|
/**
|
2017-11-15 12:25:49 +01:00
|
|
|
* Class Help.
|
2015-04-07 19:58:49 +02:00
|
|
|
*/
|
|
|
|
class Help implements HelpInterface
|
|
|
|
{
|
2018-07-07 23:14:16 +02:00
|
|
|
/** @var string The cache key */
|
2018-03-28 19:37:59 +02:00
|
|
|
public const CACHEKEY = 'help_%s_%s';
|
2018-07-07 23:14:16 +02:00
|
|
|
/** @var string The user agent. */
|
2019-06-21 19:10:02 +02:00
|
|
|
protected $userAgent = 'Firefly III v%s';
|
2015-04-07 19:58:49 +02:00
|
|
|
|
|
|
|
/**
|
2018-09-06 12:29:32 +02:00
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public function __construct()
|
|
|
|
{
|
2019-06-21 19:10:02 +02:00
|
|
|
$this->userAgent = sprintf($this->userAgent, config('firefly.version'));
|
2018-09-06 12:29:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-07 23:14:16 +02:00
|
|
|
* Get from cache.
|
|
|
|
*
|
2018-08-29 10:57:42 +02:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2016-10-23 17:33:53 +02:00
|
|
|
* @param string $route
|
|
|
|
* @param string $language
|
2015-04-07 19:58:49 +02:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2016-10-23 17:33:53 +02:00
|
|
|
public function getFromCache(string $route, string $language): string
|
2015-04-07 19:58:49 +02:00
|
|
|
{
|
2017-07-23 07:30:05 +02:00
|
|
|
$line = sprintf(self::CACHEKEY, $route, $language);
|
2016-11-02 07:04:14 +01:00
|
|
|
|
|
|
|
return Cache::get($line);
|
2015-04-07 19:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-07 23:14:16 +02:00
|
|
|
* Get text from GitHub.
|
|
|
|
*
|
2016-02-05 09:25:15 +01:00
|
|
|
* @param string $route
|
2017-03-19 17:54:21 +01:00
|
|
|
* @param string $language
|
2015-04-07 19:58:49 +02:00
|
|
|
*
|
2016-10-23 17:33:53 +02:00
|
|
|
* @return string
|
2015-04-07 19:58:49 +02:00
|
|
|
*/
|
2018-07-07 23:14:16 +02:00
|
|
|
public function getFromGitHub(string $route, string $language): string
|
2015-04-07 19:58:49 +02:00
|
|
|
{
|
2020-06-29 06:43:53 +02:00
|
|
|
$uri = sprintf('https://raw.githubusercontent.com/firefly-iii/help/main/%s/%s.md', $language, $route);
|
2016-10-31 18:31:52 +01:00
|
|
|
Log::debug(sprintf('Trying to get %s...', $uri));
|
2018-07-07 21:17:46 +02:00
|
|
|
$opt = ['headers' => ['User-Agent' => $this->userAgent]];
|
|
|
|
$content = '';
|
|
|
|
$statusCode = 500;
|
2018-08-29 10:57:42 +02:00
|
|
|
$client = app(Client::class);
|
2016-10-31 18:31:52 +01:00
|
|
|
try {
|
2018-07-07 21:17:46 +02:00
|
|
|
$res = $client->request('GET', $uri, $opt);
|
|
|
|
$statusCode = $res->getStatusCode();
|
|
|
|
$content = trim($res->getBody()->getContents());
|
2019-02-13 17:38:41 +01:00
|
|
|
} catch (Exception $e) {
|
2018-08-09 17:46:14 +02:00
|
|
|
Log::info($e->getMessage());
|
|
|
|
Log::info($e->getTraceAsString());
|
2015-04-07 19:58:49 +02:00
|
|
|
}
|
2016-02-23 16:12:59 +01:00
|
|
|
|
2018-07-07 21:17:46 +02:00
|
|
|
Log::debug(sprintf('Status code is %d', $statusCode));
|
2017-07-23 07:30:05 +02:00
|
|
|
|
2019-02-13 17:38:41 +01:00
|
|
|
if ('' !== $content) {
|
2016-10-31 18:31:52 +01:00
|
|
|
Log::debug('Content is longer than zero. Expect something.');
|
|
|
|
$converter = new CommonMarkConverter();
|
|
|
|
$content = $converter->convertToHtml($content);
|
2015-04-07 19:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-07 23:14:16 +02:00
|
|
|
* Do we have the route?
|
|
|
|
*
|
2018-08-29 10:57:42 +02:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2016-02-05 09:25:15 +01:00
|
|
|
* @param string $route
|
2015-05-03 12:58:55 +02:00
|
|
|
*
|
|
|
|
* @return bool
|
2015-04-07 19:58:49 +02:00
|
|
|
*/
|
2016-12-14 18:59:12 +01:00
|
|
|
public function hasRoute(string $route): bool
|
2015-04-07 19:58:49 +02:00
|
|
|
{
|
|
|
|
return Route::has($route);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-07 23:14:16 +02:00
|
|
|
* Is in cache?
|
|
|
|
*
|
2018-08-29 10:57:42 +02:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2016-02-05 09:25:15 +01:00
|
|
|
* @param string $route
|
2016-10-23 17:33:53 +02:00
|
|
|
* @param string $language
|
2015-04-07 19:58:49 +02:00
|
|
|
*
|
2015-06-29 15:23:50 +02:00
|
|
|
* @return bool
|
2015-04-07 19:58:49 +02:00
|
|
|
*/
|
2016-12-14 18:59:12 +01:00
|
|
|
public function inCache(string $route, string $language): bool
|
2015-04-07 19:58:49 +02:00
|
|
|
{
|
2017-07-23 07:30:05 +02:00
|
|
|
$line = sprintf(self::CACHEKEY, $route, $language);
|
2016-11-02 07:04:14 +01:00
|
|
|
$result = Cache::has($line);
|
2016-10-31 18:31:52 +01:00
|
|
|
if ($result) {
|
|
|
|
Log::debug(sprintf('Cache has this entry: %s', 'help.' . $route . '.' . $language));
|
|
|
|
}
|
|
|
|
if (!$result) {
|
|
|
|
Log::debug(sprintf('Cache does not have this entry: %s', 'help.' . $route . '.' . $language));
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2015-04-07 19:58:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-07-07 23:14:16 +02:00
|
|
|
* Put help text in cache.
|
|
|
|
*
|
2018-08-29 10:57:42 +02:00
|
|
|
* @codeCoverageIgnore
|
|
|
|
*
|
2016-02-05 09:25:15 +01:00
|
|
|
* @param string $route
|
2016-04-09 09:27:04 +02:00
|
|
|
* @param string $language
|
2016-10-23 17:33:53 +02:00
|
|
|
* @param string $content
|
2015-04-07 19:58:49 +02:00
|
|
|
*/
|
2018-07-07 21:17:46 +02:00
|
|
|
public function putInCache(string $route, string $language, string $content): void
|
2015-04-07 19:58:49 +02:00
|
|
|
{
|
2017-07-23 07:30:05 +02:00
|
|
|
$key = sprintf(self::CACHEKEY, $route, $language);
|
2019-02-13 17:38:41 +01:00
|
|
|
if ('' !== $content) {
|
2016-11-02 14:33:57 +01:00
|
|
|
Log::debug(sprintf('Will store entry in cache: %s', $key));
|
|
|
|
Cache::put($key, $content, 10080); // a week.
|
2017-03-24 15:15:12 +01:00
|
|
|
|
2016-11-02 14:33:57 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log::info(sprintf('Will not cache %s because content is empty.', $key));
|
2015-04-07 19:58:49 +02:00
|
|
|
}
|
2015-05-05 12:51:57 +02:00
|
|
|
}
|