Skip to content

Commit

Permalink
Add get_json Twig function
Browse files Browse the repository at this point in the history
Also fix a bug in the wikipedia function by switching it to use
the new json function.
  • Loading branch information
samwilson committed Oct 23, 2023
1 parent 15d234c commit ab7e42c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
2 changes: 2 additions & 0 deletions docs/content/templates.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ these are explained on this page.
See the example in [/example/templates/tag.html.twig](https://github.com/samwilson/basildon/blob/main/example/templates/tag.html.twig)
6. `wikipedia(lang, title)` — Returns an HTML extract of the given article.
For example: `{{wikipedia('en', 'Tag (metadata)')|raw}}`
7. `get_json(url)` — Fetch JSON data from any URL.
For example: `{{get_json('https://api.wikitree.com/api.php?action=getProfile&key=Hall-22337').0.profile.LongName}}`

## Filters and escapers

Expand Down
6 changes: 6 additions & 0 deletions example/templates/recent.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@

{{ page.body|md2html|raw }}

{% set example_data = get_json('https://microsoftedge.github.io/Demos/json-dummy-data/64KB.json') %}

<p>Example description: {{example_data.3.bio}}</p>

<p>Another JSON example: {{get_json('https://api.wikitree.com/api.php?action=getProfile&key=Hall-22337').0.profile.LongName}}</p>

<ol>
{% for p in database.query('SELECT * FROM pages WHERE date IS NOT NULL ORDER BY date DESC LIMIT 10') %}
<li>
Expand Down
32 changes: 22 additions & 10 deletions src/Twig.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ final class Twig extends AbstractExtension
'commons' => [],
'wikidata' => [],
'flickr' => [],
'json' => [],
];

public function __construct(Database $db, Site $site, Page $page)
Expand Down Expand Up @@ -83,6 +84,7 @@ public function getFunctions(): array
new TwigFunction('date_create', [$this, 'functionDateCreate']),
new TwigFunction('strtotime', 'strtotime'),
new TwigFunction('json_decode', 'json_decode'),
new TwigFunction('get_json', [$this, 'functionGetJson']),
new TwigFunction('tex_url', [$this, 'functionTexUrl']),
new TwigFunction('wikidata', [$this, 'functionWikidata']),
new TwigFunction('commons', [$this, 'functionCommons']),
Expand Down Expand Up @@ -305,25 +307,35 @@ public function functionCommons(string $filename): array

public function functionWikipedia(string $lang, string $articleTitle): string
{
if (isset(self::$data['wikipedia'][$articleTitle])) {
return self::$data['wikipedia'][$articleTitle];
$url = "https://$lang.wikipedia.org/api/rest_v1/page/summary/" . str_replace(' ', '_', $articleTitle);
$response = $this->functionGetJson($url);
if (!isset($response['extract_html'])) {
throw new Exception("Unable to get extract of Wikipedia article: $articleTitle");
}
return $response['extract_html'];
}

public function functionGetJson(string $url): mixed
{
$cacheKey = md5($url);
if (isset(self::$data['json'][$cacheKey])) {
return self::$data['json'][$cacheKey];
}
$cache = $this->getCachePool('wikipedia');
$cacheItem = $cache->getItem('wikipedia' . $articleTitle);
$cache = $this->getCachePool('json_' . parse_url($url, PHP_URL_HOST));
$cacheItem = $cache->getItem($cacheKey);
if ($cacheItem->isHit()) {
return $cacheItem->get();
}
CommandBase::writeln("Wikipedia fetch extract: $articleTitle");
$url = "https://$lang.wikipedia.org/api/rest_v1/page/summary/" . str_replace(' ', '_', $articleTitle);
CommandBase::writeln("Get JSON: $url");
$json = (new Client())->get($url)->getBody()->getContents();
$response = json_decode($json, true);
if (!$response) {
throw new Exception("Unable to get extract of Wikipedia article: $articleTitle");
throw new Exception("Unable to get JSON from URL: $url");
}
self::$data['commons'][$articleTitle] = $response['extract_html'];
$cacheItem->set(self::$data['commons'][$articleTitle]);
self::$data['json'][$cacheKey] = $response;
$cacheItem->set(self::$data['json'][$cacheKey]);
$cache->save($cacheItem);
return self::$data['commons'][$articleTitle];
return self::$data['json'][$cacheKey];
}

/**
Expand Down

0 comments on commit ab7e42c

Please sign in to comment.