Template:Multilingual link/Syntax helper
This is Syntax helper of the template {{multilingual link}}
. If you know nothing about the template {{multilingual link}}
, read about it firstly.
Description of the Syntax helper
[edit]The wikitext to call the template {{Multilingual link}}
can be long, writing such text is a tedious task. This Perl script creates such wikitext for you, provided that the topic is covered in Wikidata which lists the wikilinks to all articles in Wikipedia editions, and if a Wikipedia article is given to the script, it has been associated with a relevant Wikidata element.
For example, if you type ./syntaxhelper.perl en:Saint Petersburg State Agrarian University
or ./syntaxhelper.perl Q1628690
, the script returns the wikitext:
{{Multilingual link
|be = Санкт-Пецярбургскі дзяржаўны аграрны ўніверсітэт
|en = Saint Petersburg State Agrarian University
|eo = Sankt-Peterburga Ŝtata Agrikultura Universitato
|et = Peterburi Riiklik Agraarülikool
|fr = Université agraire d'État de Saint-Pétersbourg
|ru = Санкт-Петербургский государственный аграрный университет
}}
and you just need to copy and paste that wikitext.
Code of the Syntax helper
[edit]Do not copy the code from here. Download it as described below.
#!/usr/bin/perl
#### (C) Gamliel Fishkin, 2024. CC-BY-SA 4.0.
#### https://commons.wikimedia.org/wiki/Template:Multilingual_link/Syntax_helper
use strict;
use warnings 'all';
my @text;
if ($ARGV[0]) {
my $url = 'https://www.wikidata.org/wiki/';
if (
$ARGV[0] =~ m/^[qQ]?([1-9][0-9]*)$/
|| $ARGV[0] =~ m%^$url(?:Special:EntityPage/|)Q([1-9][0-9]*)(?:$|#)%
) {
$url .= 'Q' . $1;
}
else {
my $wiki;
{
my $arg = join ' ', @ARGV;
my $w = '.wikipedia.org/w';
if (
$arg =~ m%^(https://[a-z-]+?${w}iki/.+?)(?:$|\?|#)%
|| $arg =~ m%^(https://[a-z-]+?$w/(?:index.php|)\?title=.+?)(?:$|&|#)%
) {
$wiki = '"' . $1 . '"';
}
elsif (
$arg =~ m/^ \{\{ (w|W)\|(?:1=|)(.+?)(?:\|.*?|) \}\} $/x
|| $arg =~ m/^ \{\{.+?\| (?:1=|)\{\{ (w|W)\|(?:1=|)(.+?)(?:\|.*?|) \}\}\}\} $/x
|| $arg =~ m/^ ([wW]:[a-z-]+|[a-zADEIKPW-]+) :(.+?) $/x
|| $arg =~ m/^ \[\[:? ([wW]:[a-z-]+|[a-zADEIKPW-]+) :(.+?)(?:\|.*?|) \]\] $/x
|| $arg =~ m/^ \{\{.+?\| (?:1=|)\[\[:? ([wW]:[a-z-]+|[a-zADEIKPW-]+) :(.+?)
(?:\|.*?|) \]\]\}\} $/x
) {
my ($lng, $title) = ($1, $2);
if ($lng =~ m/^w(?:ikipedia|)$/i) {
$lng = 'en';
}
elsif ($lng =~ m/^w:([a-z-]+)$/) {
$lng = $1;
}
$title =~ s/ /_/g;
$wiki = '"https://' . $lng . $w . 'iki/' . $title . '"';
}
else {
&d ('Unreadable data.');
}
}
$wiki = qx(curl $wiki -sLm10) || &d ('Cannot get Wikipedia page.');
if ($wiki =~ m%${url}Special:EntityPage/(Q[1-9][0-9]*)#sitelinks-wikipedia%) {
$url .= $1;
}
else {
&d ('This Wikipedia page does not contain the expected link to Wikidata.');
}
}
my $text = qx(curl $url -sLm10) || &d ('Cannot get Wikidata page.');
my $w = 'wikibase-sitelinkview';
@text = ($text =~ m%<li class=\"$w $w-.+?wiki\".+?>(.+?)</li>%gs);
}
else {
&d ('No data.');
}
my (@lng, %title, %other, @title);
{
my ($lng, $title);
my $w = 'wikibase-sitelinkview-siteid';
foreach (@text) {
if (m%<span class=\"$w $w-.+?wiki.+?>(.+?)wiki</span>.+?<a href=.+?>(.+?)</a>%s) {
($lng, $title) = ($1, $2);
if (!grep {$lng eq $_} qw(commons mediawiki meta sources species wikidata)) {
$lng =~ s/_/-/g;
push @lng, $lng;
$title =~ s/\342\200\214/‌/g;
$title{$lng} = $title;
if ($title =~ m/^(.+?) ?\(.+?\)$/) {
$other{$lng} = $1;
push @title, $1;
}
else {
push @title, $title;
}
}
}
}
}
@text = ();
if (0 == $#lng) {
&d (
'This Wikipedia article has only one language',
'version. It would be great to write an article',
'on the same topic in another language (or to',
'translate this article), is not it? '
);
}
elsif (-1 == $#lng) {
&d ('This Wikidata item does not contain links to Wikipedia.');
}
my $other = '';
if ((!grep {$title[0] ne $_} @title) && %other > 1) {
$other = '|title=' . $title[0] . "\n";
%other = ();
}
@title = ();
my $lines = '{{multilingual link';
foreach (@lng) {
$lines .= "\n" . '|' . $_ . '=' . $title{$_};
$lines .= '|title ' . $_ . '=' . $other{$_} if $other{$_};
}
$lines .= "\n" . '|default=' if !$title{en};
print $lines . "\n" . $other . '}}' . "\n";
sub d {
my $l = "\n" . '#' x (length($_[0]) + 6) . "\n";
my $d = join ' ##' . "\n" . '## ', @_;
my $s = 'https://commons.wikimedia.org/wiki/Template:Multilingual_link/Syntax_helper';
die $l . '## ' . $d . ' ##' . $l . "\n" . $s . "\n";
}
Installation of the Syntax helper
[edit]System requirements
[edit]The Syntax helper script is written and successfully tested under Debian. The script is also successfully tested under CentOS, NetBSD, OpenBSD and Ubuntu. Theorethically, the script should work also under other operating systems (including non-Unix and not Unix-like), but it is not tested.
The script ultimately needs Perl and curl. (If Perl itself is inavailable for your operating system, you can install ActivePerl).
The Syntax helper is a script for a terminal (aka console). Do not try to upload the Syntax helper to a web server as a CGI script; it would not not work in such way.
Preparations
[edit]Unix and Unix-like operating systems
[edit]Perl
[edit]Type which perl
and if the reply is /usr/bin/perl
, go to the next section (curl).
If the reply is empty, it means that Perl is not installed. Install perl: type as root apt-get install perl
, or yum install perl
, or similarly, depending on your operating system (obviously, you have to know how to install software in your operating system). Type which perl
.
If the reply to which perl
is not empty, but not /usr/bin/perl
(/usr/local/bin/perl
, /usr/pkg/bin/perl
or another string), type ls -l /usr/bin/perl
; if the reply is not empty, it is okay. If the reply to ls -l /usr/bin/perl
is empty, type as root:
- under GNU/Linux:
ln -Ts $(which perl) /usr/bin/perl
- under *BSD:
ln -s $(which perl) /usr/bin/perl
and after it the reply to ls -l /usr/bin/perl
will be not empty; so, go to the next section (curl).
curl
[edit]Type which curl
. If the reply is empty, install curl. If the reply is not empty, preparations are finished.
Microsoft Windows
[edit]There is no certainty that the Syntax helper will work under Microsoft Windows, but you can try.
Check if ActivePerl and curl are installed. If not, install them. Get sure that the file extension .perl is associated with ActivePerl.
Downloading the Syntax helper
[edit]Type in a terminal:
curl https://commons.wikimedia.org/w/index.php?title=Template:Multilingual_link/Syntax_helper/code\&oldid=847024748\&action=raw -m 15 -o syntaxhelper.perl &&
chmod +x syntaxhelper.perl
(you can highlight the above code to copy it and then paste it into a terminal).
Usage
[edit]Use the Syntax helper in the terminal emulator inside graphical interface, because it permits to copy text from terminal.
Calling the Syntax helper
[edit]Type ./syntaxhelper.perl parameter
, where parameter
can be any in one of these forms:
- the identifier number of a Wikidata item (with or without the initial Q) (this is the recommended and simplest option, because it is independent of the text encoding, see the next subsection);
- the full page name (with the language code prefix, separated by a colon from the article name) of a Wikipedia article;
- an URL of a Wikidata item page (starting by "http://" or "https://", you can copy it from the address bar of a web browser when viewing that page);
- an URL of a Wikipedia article (same remark);
- a wikilink (many variations of their syntax are working, but the syntax of the parameter on the command line may be tricky with some embedded characters).
For example, the following strings have absolutely the same effect (this list is not exhaustive, and you can use any one ot them, just one at the same time):
12345
Q12345
q12345
en:Count von Count
w:en:Count von Count
w:Count von Count
de:Graf Zahl
w:de:Graf Zahl
https://www.wikidata.org/wiki/Q12345
https://www.wikidata.org/wiki/Special:EntityPage/Q12345
https://en.wikipedia.org/wiki/Count_von_Count
https://en.wikipedia.org/w/index.php?title=Count_von_Count
https://de.wikipedia.org/wiki/Graf_Zahl
https://de.wikipedia.org/w/index.php?title=Graf_Zahl
[[w:Count von Count|foo bar]]
[[w:en:Count von Count|foo bar]]
[[:de:Graf Zahl|foo bar]]
[[w:de:Graf Zahl|foo bar]]
"[[:en:Count von Count]]"
"[[:w:Count von Count]]"
"[[:w:en:Count von Count]]"
"[[en:Count von Count]]"
"[[en:Count von Count|foo bar]]"
"[[w:Count von Count]]"
"[[w:Count von Count|foo bar]]"
"[[w:en:Count von Count]]"
"[[de:Graf Zahl]]"
"[[w:de:Graf Zahl]]"
"{{w|Count von Count}}"
"{{w|1=Count von Count}}"
"{{en|[[en:Count von Count]]}}"
"{{en|[[w:Count von Count]]}}"
"{{en|[[w:en:Count von Count]]}}"
"{{de|[[de:Graf Zahl]]}}"
"{{de|[[w:de:Graf Zahl]]}}"
"{{en|{{w|Count von Count}}}}"
"{{en|1=[[en:Count von Count]]}}"
"{{en|1=[[w:Count von Count]]}}"
"{{en|1=[[w:en:Count von Count]]}}"
"{{en|1={{w|Count von Count}}}}"
"{{en|1={{w|1=Count von Count}}}}"
"{{de|1=[[de:Graf Zahl]]}}"
"{{de|1=[[w:de:Graf Zahl]]}}"
Note, that some Wikipedia articles have over two hundred language versions and so the output can be very long. It is better to see the output in the less command line tool.
For example, at 25 January 2024, the article en:Earth has 299 language versions and so the output of ./syntaxhelper.perl en:Earth
contains 301 lines, exceeding the visible space in the terminal window.
So, it is recommended to type not just: ./syntaxhelper.perl en:Earth
, but instead: ./syntaxhelper.perl en:Earth | less
if you want to read the full output in the terminal window (when the terminal does not have a scrollable history).
Alternatively, you can just redirect the output of the Syntax helper to a custom storage file (for example myfile.txt) like this: ./syntaxhelper.perl en:Earth > myfile.txt
, that you can then view using any text editor (and that you can and edit easily as needed to fit your needs before copy-pasting its content into a wiki page).
Special characters in the input and ouput
[edit]Use of non-Latin alphabets is normally not problem, provided that you use a terminal configured with an Unicode-compatible text encoding (UTF-8) supporting all non-Latin characters needed for many languages present in the command line or in the generated output (and not a legacy 8-bit encoding, which may suitable only to render correctly your current locale language, but which may transform, alter, or not render completely all texts in many other languages, written in various scripts). This configured text encoding must be the same one used across all the different programs communicating any text data (including the content of storage files, the terminal itself when you type in the window, or copy-paste text from/to the terminal).
- On Unix and Linux, make sure to configure your locale with one ending with ".UTF-8" in your user environment, for example (see https://stackoverflow.com/questions/28477852/setting-shell-script-to-utf8 for more details) with one of these shell commands (the first one configures the current user environment to use the default basic English language of the system, the second one configures it for French; the chosen language and country preferences does not matter here, only the text encoding setting matters here):
export LANG=C.UTF-8
export LANG=fr-FR.UTF-8
- On Windows, the command line shell and the terminal may also need to be configured as well to replace the legacy OEM 8-bit code page used by default, by the 65001 code page (for the UTF-8 encoding), so that input and output characters are not transformed in a destructive way (by approximate characters, or by question marks, or simply dropped silently). For example (see https://stackoverflow.com/questions/57131654/using-utf-8-encoding-chcp-65001-in-command-prompt-windows-powershell-window for more details):
chcp 65001
Within the subset of Basic Latin characters (ASCII), the regular spaces, underscores, commas, colons, full stops, plus and minus-hyphens signs, foreword slashes, decimal digits and Latin letters that are present in the input or in the generated output are not a problem. For example, ./syntaxhelper.perl en:Carriage return
works fine.
However:
- If the parameter contains any one of these two characters:
"
\
- you must prefix every such character with the
\
sign to escape it for the shell syntax.
- you must prefix every such character with the
- Then if the parameter contains any one of these punctuation and brackets characters (with special meaning in the shell syntax):
<
{
[
(
|
'
`
$
*
?
)
]
}
>
- you need to do any of the two things (NB: do just one, not both):
- prefix every such character with the
\
sign to escape it for the shell syntax, or - enclose the entire parameter in
"
double quotes"
.
- prefix every such character with the
- you need to do any of the two things (NB: do just one, not both):
Few examples:
./syntaxhelper.perl it:Angelo d\'Arrigo
or
./syntaxhelper.perl "it:Angelo d'Arrigo"
./syntaxhelper.perl eo:Gantopupo \(Interreto\) | less
or
./syntaxhelper.perl "eo:Gantopupo (Interreto)" | less
./syntaxhelper.perl \{\{ru\|\[\[ru:Возврат каретки\|возврат каретки\]\]\}\} | less
or
./syntaxhelper.perl "{{ru|[[ru:Возврат каретки|возврат каретки]]}}" | less
Treating the output
[edit]If the entire output fits inside one screen, highlight the output, copy it (⇧ Shift+Ctrl+C) and paste it to the editing textarea (Ctrl+V). If the output is longer than one screen, than highlight, copy and paste the top part of the output, scroll down at one screen, highlight, copy and paste the next part, etc; copy and paste the entire output in such a way.
Alternatively, just use an external text editor to open the custom output file (e.g. myfile.txt, as shown above), select all the edited text and copy that selection (usually Ctrl+A then (Ctrl+C, in many modern text editors), then paste it to the editing textarea of the wiki editor (Ctrl+V).
See over the wikitext generated by the Syntax helper. Very probably, some language versions will be provided with titles; possibly, you will want to edit some of the titles.
Especially important: when it is needed (notably if there's no value in English from Wikidata available, where the Syntax helper would generate it in a line after |en =
), the Syntax helper adds the parameter |default =
but does not fill it. You have to fill that parameter with ISO code of one of the languages available in the generated wikitext. For example, if the generated wikitext is:
{{Multilingual link
|bg = Флатер
|de = Flattern (Luftfahrt) |title de = Flattern
|eo = Flirta oscilado (aviado) |title eo = Flirta oscilado
|fr = Flottement
|hu = Flatter (repülés) |title hu = Flatter
|ja = フラッター現象
|ru = Флаттер (авиация) |title ru = Флаттер
|uk = Флатер (авіація) |title uk = Флатер
|default =
}}
where you have to fill in the parameter |default =
with one of the eight available codes (bg
, de
, eo
, fr
, hu
, ja
, ru
, uk
) by your choice; if you will choose eo
, the result will be:
{{Multilingual link
|bg = Флатер
|de = Flattern (Luftfahrt) |title de = Flattern
|eo = Flirta oscilado (aviado) |title eo = Flirta oscilado
|fr = Flottement
|hu = Flatter (repülés) |title hu = Flatter
|ja = フラッター現象
|ru = Флаттер (авиация) |title ru = Флаттер
|uk = Флатер (авіація) |title uk = Флатер
|default = eo
}}
When the all is done, click the button Publish changes.
Userbox
[edit]Syntax helper | This user works with the Syntax helper of the template {{multilingual link}} |
The Syntax helper needs your moral support. You can place this userbox on your user page with the code {{User of Syntax helper}}
. This userbox on your user page is your vote for the usefulness of the Syntax helper.