Skip to content

Commit 296bb7c

Browse files
authored
Add page_num to the commons() Twig function
* Add an optional `page_num` argument to the `commons()` Twig function. * Switch from teatimeguest/setup-texlive-action to zauguin/install-texlive as the former no longer exists.
1 parent 5810682 commit 296bb7c

4 files changed

Lines changed: 21 additions & 11 deletions

File tree

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
extensions: fileinfo, json, pdo, mbstring, gd, ast, sqlite, pdo_sqlite
2929

3030
- name: Set up TeXLive
31-
uses: teatimeguest/setup-texlive-action@v3
31+
uses: zauguin/install-texlive@v4
3232
with:
3333
packages: scheme-basic latexmk listings float
3434

docs/content/templates.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ these are explained on this page.
2222

2323
## Functions
2424

25-
1. `commons(file_name)` – Get information about a [Wikimedia Commons](https://commons.wikimedia.org/) file.
25+
1. `commons(file_name, [page_num])` – Get information about a [Wikimedia Commons](https://commons.wikimedia.org/) file.
26+
The optional `page_num` parameter is used to get the thumnail URLs of a PDF's page.
2627
2. `flickr(photo_id)` – Get information about a [Flickr](https://www.flickr.com/) photo.
2728
To use this, you need to set the `flickr.api_key` and `flickr.api_secret` values
2829
in your site's `basildon.local.yaml` file.

example/templates/recent.html.twig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,9 @@
2323
{% endfor %}
2424
</ol>
2525

26+
<p>
27+
Page 3 of a PDF with <code>commons('example.pdf', 3).imageinfo.0.thumburl</code>:
28+
<img src="{{ commons('example.pdf', 3).imageinfo.0.thumburl }}">
29+
</p>
30+
2631
{% endblock %}

src/Twig.php

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ public function functionFlickr(string $photoId): array
357357
/**
358358
* @return mixed[]
359359
*/
360-
public function functionCommons(string $filename): array
360+
public function functionCommons(string $filename, ?int $pageNum = null): array
361361
{
362362
if (isset(self::$data['commons'][$filename])) {
363363
return self::$data['commons'][$filename];
@@ -368,14 +368,18 @@ public function functionCommons(string $filename): array
368368
return $cacheItem->get();
369369
}
370370
$api = $this->site->getMediawikiApi('https://commons.wikimedia.org/w/api.php');
371-
$fileInfoResponse = $api->request(ActionRequest::simpleGet('query')
372-
->addParams([
373-
'prop' => 'imageinfo',
374-
'iiprop' => 'url',
375-
'iiurlwidth' => $this->site->getConfig()->embedWidth ?? 800,
376-
'titles' => 'File:' . $filename,
377-
'redirects' => true,
378-
]));
371+
$urlWidth = $this->site->getConfig()->embedWidth ?? 800;
372+
$params = [
373+
'prop' => 'imageinfo',
374+
'iiprop' => 'url',
375+
'iiurlwidth' => $urlWidth,
376+
'titles' => 'File:' . $filename,
377+
'redirects' => true,
378+
];
379+
if ($pageNum) {
380+
$params['iiurlparam'] = "page$pageNum-{$urlWidth}px";
381+
}
382+
$fileInfoResponse = $api->request(ActionRequest::simpleGet('query', $params));
379383
$fileInfo = array_shift($fileInfoResponse['query']['pages']);
380384
if (!isset($fileInfo['pageid'])) {
381385
throw new Exception('Commons file does not exist: ' . $filename);

0 commit comments

Comments
 (0)