# SPDX-License-Identifier: AGPL-3.0-or-later """Configure MediaWiki.""" import os import pathlib import shutil import subprocess import tempfile from plinth.actions import privileged, secret_str from plinth.utils import generate_password MAINTENANCE_SCRIPTS_DIR = "/usr/share/mediawiki/maintenance" CONF_FILE = '/etc/mediawiki/FreedomBoxSettings.php' LOCAL_SETTINGS_CONF = '/etc/mediawiki/LocalSettings.php' def get_php_command(): """Return the PHP command that should be used on CLI. This is workaround for /usr/bin/php pointing to a different version than what php-defaults (and php-mbstring, php-xml) point to. See: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=959742 """ version = '' try: process = subprocess.run(['dpkg', '-s', 'php'], stdout=subprocess.PIPE, check=True) for line in process.stdout.decode().splitlines(): if line.startswith('Version:'): version = line.split(':')[-1].split('+')[0].strip() except subprocess.CalledProcessError: pass return f'php{version}' @privileged def setup(): """Run the installer script to create database and configuration file.""" data_dir = '/var/lib/mediawiki-db/' if not os.path.exists(data_dir): os.mkdir(data_dir) if not os.path.exists(os.path.join(data_dir, 'my_wiki.sqlite')) or \ not os.path.exists(LOCAL_SETTINGS_CONF): install_script = os.path.join(MAINTENANCE_SCRIPTS_DIR, 'install.php') password = generate_password() with tempfile.NamedTemporaryFile() as password_file_handle: password_file_handle.write(password.encode()) password_file_handle.flush() subprocess.check_call([ get_php_command(), install_script, '--confpath=/etc/mediawiki', '--dbtype=sqlite', '--dbpath=' + data_dir, '--scriptpath=/mediawiki', '--passfile', password_file_handle.name, 'Wiki', 'admin' ]) subprocess.run(['chmod', '-R', 'o-rwx', data_dir], check=True) subprocess.run(['chown', '-R', 'www-data:www-data', data_dir], check=True) conf_file = pathlib.Path(CONF_FILE) if not conf_file.exists(): conf_file.write_text('