mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-06-10 11:00:22 +00:00
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:
parent
dcd8ef9634
commit
f3621a274d
@ -3,13 +3,17 @@
|
||||
FreedomBox app for configuring MediaWiki.
|
||||
"""
|
||||
|
||||
import json
|
||||
import logging
|
||||
import pathlib
|
||||
import re
|
||||
import subprocess
|
||||
|
||||
from django import forms
|
||||
from django.core import validators
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from . import privileged
|
||||
|
||||
|
||||
def get_skins():
|
||||
"""Return a list of available skins as choice field values."""
|
||||
@ -31,12 +35,19 @@ def get_languages():
|
||||
if not names_old.exists():
|
||||
names_file = names_new
|
||||
|
||||
with open(names_file, 'r') as lang_file:
|
||||
content = lang_file.read()
|
||||
matches = re.findall(r"'([a-z_-]+)' => '(.+)', # .+", content)
|
||||
language_choices = [(code, name) for code, name in matches]
|
||||
|
||||
return language_choices
|
||||
script = rf'''<?php
|
||||
require_once('{names_file}');
|
||||
print(json_encode(Mediawiki\Languages\Data\Names::$names));'''
|
||||
try:
|
||||
process = subprocess.run([privileged.get_php_command()],
|
||||
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
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user