mediawiki: Make retrieving list of supported languages robust

- Invoke PHP command to retrieve the list instead of parsing PHP file. This
fixes issue with regex not being generic enough to retrieve languages list using
double quotes. Also make is much more robust to future formatting changes.

- If there is an error in retrieving, which may happen due to future code
changes, fall back to showing a safe list of languages instead of making the app
unusable.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2023-04-09 15:58:13 +05:30 committed by James Valleroy
parent dcd8ef9634
commit f3621a274d
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808

View File

@ -3,13 +3,17 @@
FreedomBox app for configuring MediaWiki. FreedomBox app for configuring MediaWiki.
""" """
import json
import logging
import pathlib import pathlib
import re import subprocess
from django import forms from django import forms
from django.core import validators from django.core import validators
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
from . import privileged
def get_skins(): def get_skins():
"""Return a list of available skins as choice field values.""" """Return a list of available skins as choice field values."""
@ -31,12 +35,19 @@ def get_languages():
if not names_old.exists(): if not names_old.exists():
names_file = names_new names_file = names_new
with open(names_file, 'r') as lang_file: script = rf'''<?php
content = lang_file.read() require_once('{names_file}');
matches = re.findall(r"'([a-z_-]+)' => '(.+)', # .+", content) print(json_encode(Mediawiki\Languages\Data\Names::$names));'''
language_choices = [(code, name) for code, name in matches] try:
process = subprocess.run([privileged.get_php_command()],
return language_choices input=script.encode(), stdout=subprocess.PIPE,
check=True)
languages_dict = json.loads(process.stdout.decode())
return list(languages_dict.items())
except Exception as exception:
logging.exception('Unable read list of Mediawiki languages.',
exception)
return [('en', 'English')]
class MediaWikiForm(forms.Form): # pylint: disable=W0232 class MediaWikiForm(forms.Form): # pylint: disable=W0232