first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-09-30 18:11:26 -04:00
commit e592ca6823
27270 changed files with 5002257 additions and 0 deletions
@@ -0,0 +1,68 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Creates Formular for customlang file export
*
* @package tool_customlang
* @copyright 2020 Thomas Wedekind <Thomas.Wedekind@univie.ac.at>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_customlang\form;
use tool_customlang_utils;
/**
* Formular for customlang file export
*
* @copyright 2020 Thomas Wedekind <Thomas.Wedekind@univie.ac.at>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class export extends \moodleform {
/**
* Add elements to form
*/
public function definition() {
$lng = $this->_customdata['lng'];
$mform = $this->_form;
$langdir = tool_customlang_utils::get_localpack_location($lng);
// The export button only appears if a local lang is present.
if (!check_dir_exists($langdir) || !count(glob("$langdir/*"))) {
throw new \moodle_exception('nolocallang', 'tool_customlang');
}
$langfiles = scandir($langdir);
$fileoptions = [];
foreach ($langfiles as $file) {
if (substr($file, 0, 1) != '.') {
$fileoptions[$file] = $file;
}
}
$mform->addElement('hidden', 'lng', $lng);
$mform->setType('lng', PARAM_LANG);
$select = $mform->addElement('select', 'files', get_string('exportfilter', 'tool_customlang'), $fileoptions);
$select->setMultiple(true);
$mform->addRule('files', get_string('required'), 'required', null, 'client');
$mform->setDefault('files', $fileoptions);
$this->add_action_buttons(true, get_string('export', 'tool_customlang'));
}
}
@@ -0,0 +1,68 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Upload a zip of custom lang php files.
*
* @package tool_customlang
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_customlang\form;
use tool_customlang\local\importer;
/**
* Upload a zip/php of custom lang php files.
*
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class import extends \moodleform {
/**
* Form definition.
*/
public function definition() {
$mform = $this->_form;
$mform->addElement('header', 'settingsheader', get_string('import', 'tool_customlang'));
$mform->addElement('hidden', 'lng');
$mform->setType('lng', PARAM_LANG);
$mform->setDefault('lng', $this->_customdata['lng']);
$filemanageroptions = array(
'accepted_types' => array('.php', '.zip'),
'maxbytes' => 0,
'maxfiles' => 1,
'subdirs' => 0
);
$mform->addElement('filepicker', 'pack', get_string('langpack', 'tool_customlang'),
null, $filemanageroptions);
$mform->addRule('pack', null, 'required');
$modes = [
importer::IMPORTALL => get_string('import_all', 'tool_customlang'),
importer::IMPORTUPDATE => get_string('import_update', 'tool_customlang'),
importer::IMPORTNEW => get_string('import_new', 'tool_customlang'),
];
$mform->addElement('select', 'importmode', get_string('import_mode', 'tool_customlang'), $modes);
$mform->addElement('submit', 'importcustomstrings', get_string('importfile', 'tool_customlang'));
}
}
@@ -0,0 +1,268 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Custom lang importer.
*
* @package tool_customlang
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_customlang\local;
use tool_customlang\local\mlang\phpparser;
use tool_customlang\local\mlang\logstatus;
use tool_customlang\local\mlang\langstring;
use core\output\notification;
use stored_file;
use coding_exception;
use moodle_exception;
use core_component;
use stdClass;
/**
* Class containing tha custom lang importer
*
* @package tool_customlang
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class importer {
/** @var int imports will only create new customizations */
public const IMPORTNEW = 1;
/** @var int imports will only update the current customizations */
public const IMPORTUPDATE = 2;
/** @var int imports all strings */
public const IMPORTALL = 3;
/**
* @var string the language name
*/
protected $lng;
/**
* @var int the importation mode (new, update, all)
*/
protected $importmode;
/**
* @var string request folder path
*/
private $folder;
/**
* @var array import log messages
*/
private $log;
/**
* Constructor for the importer class.
*
* @param string $lng the current language to import.
* @param int $importmode the import method (IMPORTALL, IMPORTNEW, IMPORTUPDATE).
*/
public function __construct(string $lng, int $importmode = self::IMPORTALL) {
$this->lng = $lng;
$this->importmode = $importmode;
$this->log = [];
}
/**
* Returns the last parse log.
*
* @return logstatus[] mlang logstatus with the messages
*/
public function get_log(): array {
return $this->log;
}
/**
* Import customlang files.
*
* @param stored_file[] $files array of files to import
*/
public function import(array $files): void {
// Create a temporal folder to store the files.
$this->folder = make_request_directory(false);
$langfiles = $this->deploy_files($files);
$this->process_files($langfiles);
}
/**
* Deploy all files into a request folder.
*
* @param stored_file[] $files array of files to deploy
* @return string[] of file paths
*/
private function deploy_files(array $files): array {
$result = [];
// Desploy all files.
foreach ($files as $file) {
if ($file->get_mimetype() == 'application/zip') {
$result = array_merge($result, $this->unzip_file($file));
} else {
$path = $this->folder.'/'.$file->get_filename();
$file->copy_content_to($path);
$result = array_merge($result, [$path]);
}
}
return $result;
}
/**
* Unzip a file into the request folder.
*
* @param stored_file $file the zip file to unzip
* @return string[] of zip content paths
*/
private function unzip_file(stored_file $file): array {
$fp = get_file_packer('application/zip');
$zipcontents = $fp->extract_to_pathname($file, $this->folder);
if (!$zipcontents) {
throw new moodle_exception("Error Unzipping file", 1);
}
$result = [];
foreach ($zipcontents as $contentname => $success) {
if ($success) {
$result[] = $this->folder.'/'.$contentname;
}
}
return $result;
}
/**
* Import strings from a list of langfiles.
*
* @param string[] $langfiles an array with file paths
*/
private function process_files(array $langfiles): void {
$parser = phpparser::get_instance();
foreach ($langfiles as $filepath) {
$component = $this->component_from_filepath($filepath);
if ($component) {
$strings = $parser->parse(file_get_contents($filepath));
$this->import_strings($strings, $component);
}
}
}
/**
* Try to get the component from a filepath.
*
* @param string $filepath the filepath
* @return stdCalss|null the DB record of that component
*/
private function component_from_filepath(string $filepath) {
global $DB;
// Get component from filename.
$pathparts = pathinfo($filepath);
if (empty($pathparts['filename'])) {
throw new coding_exception("Cannot get filename from $filepath", 1);
}
$filename = $pathparts['filename'];
$normalized = core_component::normalize_component($filename);
if (count($normalized) == 1 || empty($normalized[1])) {
$componentname = $normalized[0];
} else {
$componentname = implode('_', $normalized);
}
$result = $DB->get_record('tool_customlang_components', ['name' => $componentname]);
if (!$result) {
$this->log[] = new logstatus('notice_missingcomponent', notification::NOTIFY_ERROR, null, $componentname);
return null;
}
return $result;
}
/**
* Import an array of strings into the customlang tables.
*
* @param langstring[] $strings the langstring to set
* @param stdClass $component the target component
*/
private function import_strings(array $strings, stdClass $component): void {
global $DB;
foreach ($strings as $newstring) {
// Check current DB entry.
$customlang = $DB->get_record('tool_customlang', [
'componentid' => $component->id,
'stringid' => $newstring->id,
'lang' => $this->lng,
]);
if (!$customlang) {
$customlang = null;
}
if ($this->can_save_string($customlang, $newstring, $component)) {
$customlang->local = $newstring->text;
$customlang->timecustomized = $newstring->timemodified;
$customlang->outdated = 0;
$customlang->modified = 1;
$DB->update_record('tool_customlang', $customlang);
}
}
}
/**
* Determine if a specific string can be saved based on the current importmode.
*
* @param stdClass $customlang customlang original record
* @param langstring $newstring the new strign to store
* @param stdClass $component the component target
* @return bool if the string can be stored
*/
private function can_save_string(?stdClass $customlang, langstring $newstring, stdClass $component): bool {
$result = false;
$message = 'notice_success';
if (empty($customlang)) {
$message = 'notice_inexitentstring';
$this->log[] = new logstatus($message, notification::NOTIFY_ERROR, null, $component->name, $newstring);
return $result;
}
switch ($this->importmode) {
case self::IMPORTNEW:
$result = empty($customlang->local);
$warningmessage = 'notice_ignoreupdate';
break;
case self::IMPORTUPDATE:
$result = !empty($customlang->local);
$warningmessage = 'notice_ignorenew';
break;
case self::IMPORTALL:
$result = true;
break;
}
if ($result) {
$errorlevel = notification::NOTIFY_SUCCESS;
} else {
$errorlevel = notification::NOTIFY_ERROR;
$message = $warningmessage;
}
$this->log[] = new logstatus($message, $errorlevel, null, $component->name, $newstring);
return $result;
}
}
@@ -0,0 +1,177 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Language string based on David Mudrak langstring from local_amos.
*
* @package tool_customlang
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_customlang\local\mlang;
use moodle_exception;
use stdclass;
/**
* Class containing a lang string cleaned.
*
* @package tool_customlang
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Represents a single string
*/
class langstring {
/** @var string identifier */
public $id = null;
/** @var string */
public $text = '';
/** @var int the time stamp when this string was saved */
public $timemodified = null;
/** @var bool is deleted */
public $deleted = false;
/** @var stdclass extra information about the string */
public $extra = null;
/**
* Class constructor.
*
* @param string $id string identifier
* @param string $text string text
* @param int $timemodified
* @param int $deleted
* @param stdclass $extra
*/
public function __construct(string $id, string $text = '', int $timemodified = null,
int $deleted = 0, stdclass $extra = null) {
if (is_null($timemodified)) {
$timemodified = time();
}
$this->id = $id;
$this->text = $text;
$this->timemodified = $timemodified;
$this->deleted = $deleted;
$this->extra = $extra;
}
/**
* Given a string text, returns it being formatted properly for storing in AMOS repository.
*
* Note: This method is taken directly from local_amos as it is highly tested and robust.
* The Moodle 1.x part is keep on puspose to make it easier the copy paste from both codes.
* This could change in the future when AMOS stop suporting the 1.x langstrings.
*
* We need to know for what branch the string should be prepared due to internal changes in
* format required by get_string()
* - for get_string() in Moodle 1.6 - 1.9 use $format == 1
* - for get_string() in Moodle 2.0 and higher use $format == 2
*
* Typical usages of this methods:
* $t = langstring::fix_syntax($t); // sanity new translations of 2.x strings
* $t = langstring::fix_syntax($t, 1); // sanity legacy 1.x strings
* $t = langstring::fix_syntax($t, 2, 1); // convert format of 1.x strings into 2.x
*
* Backward converting 2.x format into 1.x is not supported
*
* @param string $text string text to be fixed
* @param int $format target get_string() format version
* @param int $from which format version does the text come from, defaults to the same as $format
* @return string
*/
public static function fix_syntax(string $text, int $format = 2, ?int $from = null): string {
if (is_null($from)) {
$from = $format;
}
// Common filter.
$clean = trim($text);
$search = [
// Remove \r if it is part of \r\n.
'/\r(?=\n)/',
// Control characters to be replaced with \n
// LINE TABULATION, FORM FEED, CARRIAGE RETURN, END OF TRANSMISSION BLOCK,
// END OF MEDIUM, SUBSTITUTE, BREAK PERMITTED HERE, NEXT LINE, START OF STRING,
// STRING TERMINATOR and Unicode character categorys Zl and Zp.
'/[\x{0B}-\r\x{17}\x{19}\x{1A}\x{82}\x{85}\x{98}\x{9C}\p{Zl}\p{Zp}]/u',
// Control characters to be removed
// NULL, ENQUIRY, ACKNOWLEDGE, BELL, SHIFT {OUT,IN}, DATA LINK ESCAPE,
// DEVICE CONTROL {ONE,TWO,THREE,FOUR}, NEGATIVE ACKNOWLEDGE, SYNCHRONOUS IDLE, ESCAPE,
// DELETE, PADDING CHARACTER, HIGH OCTET PRESET, NO BREAK HERE, INDEX,
// {START,END} OF SELECTED AREA, CHARACTER TABULATION {SET,WITH JUSTIFICATION},
// LINE TABULATION SET, PARTIAL LINE {FORWARD,BACKWARD}, REVERSE LINE FEED,
// SINGLE SHIFT {TWO,THREE}, DEVICE CONTROL STRING, PRIVATE USE {ONE,TWO},
// SET TRANSMIT STATE, MESSAGE WAITING, {START,END} OF GUARDED AREA,
// {SINGLE {GRAPHIC,} CHARACTER,CONTROL SEQUENCE} INTRODUCER, OPERATING SYSTEM COMMAND,
// PRIVACY MESSAGE, APPLICATION PROGRAM COMMAND, ZERO WIDTH {,NO-BREAK} SPACE,
// REPLACEMENT CHARACTER.
'/[\0\x{05}-\x{07}\x{0E}-\x{16}\x{1B}\x{7F}\x{80}\x{81}\x{83}\x{84}\x{86}-\x{93}\x{95}-\x{97}\x{99}-\x{9B}\x{9D}-\x{9F}\x{200B}\x{FEFF}\x{FFFD}]++/u',
// Remove trailing whitespace at the end of lines in a multiline string.
'/[ \t]+(?=\n)/',
];
$replace = [
'',
"\n",
'',
'',
];
$clean = preg_replace($search, $replace, $clean);
if (($format === 2) && ($from === 2)) {
// Sanity translations of 2.x strings.
$clean = preg_replace("/\n{3,}/", "\n\n\n", $clean); // Collapse runs of blank lines.
} else if (($format === 2) && ($from === 1)) {
// Convert 1.x string into 2.x format.
$clean = preg_replace("/\n{3,}/", "\n\n\n", $clean); // Collapse runs of blank lines.
$clean = preg_replace('/%+/', '%', $clean); // Collapse % characters.
$clean = str_replace('\$', '@@@___XXX_ESCAPED_DOLLAR__@@@', $clean); // Remember for later.
$clean = str_replace("\\", '', $clean); // Delete all slashes.
$clean = preg_replace('/(^|[^{])\$a\b(\->[a-zA-Z0-9_]+)?/', '\\1{$a\\2}', $clean); // Wrap placeholders.
$clean = str_replace('@@@___XXX_ESCAPED_DOLLAR__@@@', '$', $clean);
$clean = str_replace('&#36;', '$', $clean);
} else if (($format === 1) && ($from === 1)) {
// Sanity legacy 1.x strings.
$clean = preg_replace("/\n{3,}/", "\n\n", $clean); // Collapse runs of blank lines.
$clean = str_replace('\$', '@@@___XXX_ESCAPED_DOLLAR__@@@', $clean);
$clean = str_replace("\\", '', $clean); // Delete all slashes.
$clean = str_replace('$', '\$', $clean); // Escape all embedded variables.
// Unescape placeholders: only $a and $a->something are allowed. All other $variables are left escaped.
$clean = preg_replace('/\\\\\$a\b(\->[a-zA-Z0-9_]+)?/', '$a\\1', $clean); // Unescape placeholders.
$clean = str_replace('@@@___XXX_ESCAPED_DOLLAR__@@@', '\$', $clean);
$clean = str_replace('"', "\\\"", $clean); // Add slashes for ".
$clean = preg_replace('/%+/', '%', $clean); // Collapse % characters.
$clean = str_replace('%', '%%', $clean); // Duplicate %.
} else {
throw new moodle_exception('Unknown get_string() format version');
}
return $clean;
}
}
@@ -0,0 +1,92 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Language string based on David Mudrak langstring from local_amos.
*
* @package tool_customlang
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_customlang\local\mlang;
use moodle_exception;
use stdclass;
/**
* Class containing a lang string cleaned.
*
* @package tool_customlang
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* Represents a single string
*/
class logstatus {
/** @var langstring the current string */
public $langstring = null;
/** @var string the component */
public $component = null;
/** @var string the string ID */
public $stringid = null;
/** @var string the original filename */
public $filename = null;
/** @var int the error level */
public $errorlevel = null;
/** @var string the message identifier */
private $message;
/**
* Class creator.
*
* @param string $message the message identifier to display
* @param string $errorlevel the notice level
* @param string|null $filename the filename of this log
* @param string|null $component the component of this log
* @param langstring|null $langstring the langstring of this log
*/
public function __construct(string $message, string $errorlevel, ?string $filename = null,
?string $component = null, ?langstring $langstring = null) {
$this->filename = $filename;
$this->component = $component;
$this->langstring = $langstring;
$this->message = $message;
$this->errorlevel = $errorlevel;
if ($langstring) {
$this->stringid = $langstring->id;
}
}
/**
* Get the log message.
*
* @return string the log message.
*/
public function get_message(): string {
return get_string($this->message, 'tool_customlang', $this);
}
}
@@ -0,0 +1,260 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Mlang PHP based on David Mudrak phpparser for local_amos.
*
* @package tool_customlang
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_customlang\local\mlang;
use coding_exception;
use moodle_exception;
/**
* Parser of Moodle strings defined as associative array.
*
* Moodle core just includes this file format directly as normal PHP code. However
* for security reasons, we must not do this for files uploaded by anonymous users.
* This parser reconstructs the associative $string array without actually including
* the file.
*/
class phpparser {
/** @var holds the singleton instance of self */
private static $instance = null;
/**
* Prevents direct creation of object
*/
private function __construct() {
}
/**
* Prevent from cloning the instance
*/
public function __clone() {
throw new coding_exception('Cloning os singleton is not allowed');
}
/**
* Get the singleton instance fo this class
*
* @return phpparser singleton instance of phpparser
*/
public static function get_instance(): phpparser {
if (is_null(self::$instance)) {
self::$instance = new phpparser();
}
return self::$instance;
}
/**
* Parses the given data in Moodle PHP string format
*
* Note: This method is adapted from local_amos as it is highly tested and robust.
* The priority is keeping it similar to the original one to make it easier to mantain.
*
* @param string $data definition of the associative array
* @param int $format the data format on the input, defaults to the one used since 2.0
* @return langstring[] array of langstrings of this file
*/
public function parse(string $data, int $format = 2): array {
$result = [];
$strings = $this->extract_strings($data);
foreach ($strings as $id => $text) {
$cleaned = clean_param($id, PARAM_STRINGID);
if ($cleaned !== $id) {
continue;
}
$text = langstring::fix_syntax($text, 2, $format);
$result[] = new langstring($id, $text);
}
return $result;
}
/**
* Low level parsing method
*
* Note: This method is adapted from local_amos as it is highly tested and robust.
* The priority is keeping it similar to the original one to make it easier to mantain.
*
* @param string $data
* @return string[] the data strings
*/
protected function extract_strings(string $data): array {
$strings = []; // To be returned.
if (empty($data)) {
return $strings;
}
// Tokenize data - we expect valid PHP code.
$tokens = token_get_all($data);
// Get rid of all non-relevant tokens.
$rubbish = [T_WHITESPACE, T_INLINE_HTML, T_COMMENT, T_DOC_COMMENT, T_OPEN_TAG, T_CLOSE_TAG];
foreach ($tokens as $i => $token) {
if (is_array($token)) {
if (in_array($token[0], $rubbish)) {
unset($tokens[$i]);
}
}
}
$id = null;
$text = null;
$line = 0;
$expect = 'STRING_VAR'; // The first expected token is '$string'.
// Iterate over tokens and look for valid $string array assignment patterns.
foreach ($tokens as $token) {
$foundtype = null;
$founddata = null;
if (is_array($token)) {
$foundtype = $token[0];
$founddata = $token[1];
if (!empty($token[2])) {
$line = $token[2];
}
} else {
$foundtype = 'char';
$founddata = $token;
}
if ($expect == 'STRING_VAR') {
if ($foundtype === T_VARIABLE and $founddata === '$string') {
$expect = 'LEFT_BRACKET';
continue;
} else {
// Allow other code at the global level.
continue;
}
}
if ($expect == 'LEFT_BRACKET') {
if ($foundtype === 'char' and $founddata === '[') {
$expect = 'STRING_ID';
continue;
} else {
throw new moodle_exception('Parsing error. Expected character [ at line '.$line);
}
}
if ($expect == 'STRING_ID') {
if ($foundtype === T_CONSTANT_ENCAPSED_STRING) {
$id = $this->decapsulate($founddata);
$expect = 'RIGHT_BRACKET';
continue;
} else {
throw new moodle_exception('Parsing error. Expected T_CONSTANT_ENCAPSED_STRING array key at line '.$line);
}
}
if ($expect == 'RIGHT_BRACKET') {
if ($foundtype === 'char' and $founddata === ']') {
$expect = 'ASSIGNMENT';
continue;
} else {
throw new moodle_exception('Parsing error. Expected character ] at line '.$line);
}
}
if ($expect == 'ASSIGNMENT') {
if ($foundtype === 'char' and $founddata === '=') {
$expect = 'STRING_TEXT';
continue;
} else {
throw new moodle_exception('Parsing error. Expected character = at line '.$line);
}
}
if ($expect == 'STRING_TEXT') {
if ($foundtype === T_CONSTANT_ENCAPSED_STRING) {
$text = $this->decapsulate($founddata);
$expect = 'SEMICOLON';
continue;
} else {
throw new moodle_exception(
'Parsing error. Expected T_CONSTANT_ENCAPSED_STRING array item value at line '.$line
);
}
}
if ($expect == 'SEMICOLON') {
if (is_null($id) or is_null($text)) {
throw new moodle_exception('Parsing error. NULL string id or value at line '.$line);
}
if ($foundtype === 'char' and $founddata === ';') {
if (!empty($id)) {
$strings[$id] = $text;
}
$id = null;
$text = null;
$expect = 'STRING_VAR';
continue;
} else {
throw new moodle_exception('Parsing error. Expected character ; at line '.$line);
}
}
}
return $strings;
}
/**
* Given one T_CONSTANT_ENCAPSED_STRING, return its value without quotes
*
* Also processes escaped quotes inside the text.
*
* Note: This method is taken directly from local_amos as it is highly tested and robust.
*
* @param string $text value obtained by token_get_all()
* @return string value without quotes
*/
protected function decapsulate(string $text): string {
if (strlen($text) < 2) {
throw new moodle_exception('Parsing error. Expected T_CONSTANT_ENCAPSED_STRING in decapsulate()');
}
if (substr($text, 0, 1) == "'" and substr($text, -1) == "'") {
// Single quoted string.
$text = trim($text, "'");
$text = str_replace("\'", "'", $text);
$text = str_replace('\\\\', '\\', $text);
return $text;
} else if (substr($text, 0, 1) == '"' and substr($text, -1) == '"') {
// Double quoted string.
$text = trim($text, '"');
$text = str_replace('\"', '"', $text);
$text = str_replace('\\\\', '\\', $text);
return $text;
} else {
throw new moodle_exception(
'Parsing error. Unexpected quotation in T_CONSTANT_ENCAPSED_STRING in decapsulate(): '.$text
);
}
}
}
@@ -0,0 +1,65 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Renderer class for tool customlang
*
* @package tool_customlang
* @category output
* @copyright 2019 Bas Brands <bas@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_customlang\output;
defined('MOODLE_INTERNAL') || die();
/**
* Renderer for the customlang tool.
*
* @copyright 2019 Bas Brands <bas@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class renderer extends \plugin_renderer_base {
/**
* Defer to template.
*
* @param tool_customlang_translator $translator
* @return string Html for the translator
*/
protected function render_tool_customlang_translator(\tool_customlang_translator $translator) {
$renderabletranslator = new translator($translator);
$templatevars = $renderabletranslator->export_for_template($this);
return $this->render_from_template('tool_customlang/translator', $templatevars);
}
/**
* Defer to template.
*
* @param tool_customlang_menu $menu
* @return string html the customlang menu buttons
*/
protected function render_tool_customlang_menu(\tool_customlang_menu $menu) {
$output = '';
foreach ($menu->get_items() as $item) {
$button = $this->single_button($item->url, $item->title, $item->method);
$output .= $this->box($button, 'menu');
}
return $output;
}
}
@@ -0,0 +1,93 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* customlang specific renderers.
*
* @package tool_customlang
* @copyright 2019 Moodle
* @author Bas Brands
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_customlang\output;
defined('MOODLE_INTERNAL') || die();
use renderable;
use templatable;
use renderer_base;
use stdClass;
/**
* Class containing data for customlang translator page
*
* @copyright 2019 Bas Brands
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class translator implements renderable, templatable {
/**
* @var tool_customlang_translator $translator object.
*/
private $translator;
/**
* Construct this renderable.
*
* @param tool_customlang_translator $translator The translator object.
*/
public function __construct(\tool_customlang_translator $translator) {
$this->translator = $translator;
}
/**
* Export the data.
*
* @param renderer_base $output
* @return stdClass
*/
public function export_for_template(renderer_base $output) {
$data = new stdClass();
$data->nostrings = $output->notification(get_string('nostringsfound', 'tool_customlang'));
$data->formurl = $this->translator->handler;
$data->currentpage = $this->translator->currentpage;
$data->sesskey = sesskey();
$data->strings = [];
if (!empty($this->translator->strings)) {
$data->hasstrings = true;
foreach ($this->translator->strings as $string) {
// Find strings that use placeholders.
if (preg_match('/\{\$a(->.+)?\}/', $string->master)) {
$string->placeholderhelp = $output->help_icon('placeholder', 'tool_customlang',
get_string('placeholderwarning', 'tool_customlang'));
}
if (!is_null($string->local) and $string->outdated) {
$string->outdatedhelp = $output->help_icon('markinguptodate', 'tool_customlang');
$string->checkupdated = true;
}
if ($string->original !== $string->master) {
$string->showoriginalvsmaster = true;
}
$string->local = s($string->local);
$data->strings[] = $string;
}
}
return $data;
}
}
@@ -0,0 +1,46 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Privacy Subsystem implementation for tool_customlang.
*
* @package tool_customlang
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_customlang\privacy;
defined('MOODLE_INTERNAL') || die();
/**
* Privacy Subsystem for tool_customlang implementing null_provider.
*
* @copyright 2018 Zig Tan <zig@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class provider implements \core_privacy\local\metadata\null_provider {
/**
* Get the language string identifier with the component's language
* file to explain why this plugin stores no data.
*
* @return string
*/
public static function get_reason(): string {
return 'privacy:metadata';
}
}
+136
View File
@@ -0,0 +1,136 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Export custom language strings to zip files.
*
* @package tool_customlang
* @subpackage customlang
* @copyright 2020 Thomas Wedekind <thomas.wedekind@univie.ac.at>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('CLI_SCRIPT', true);
require(__DIR__ . '/../../../../config.php');
require_once("$CFG->libdir/clilib.php");
require_once("$CFG->dirroot/$CFG->admin/tool/customlang/locallib.php");
$usage = <<<EOF
"Export custom language files to a target folder.
Useful for uploading custom langstrings to AMOS or importing or syncing them to other moodle instances.
Options:
-l, --lang Comma seperated language ids to export, default: all
-c, --components Comma seperated components to export, default: all
-t, --target Target directory to copy the zip files to, default: $CFG->tempdir/customlang
-o, --overwrite Overwrite existing files in the target folder.
Note: If the target is not set, the files are always overwritten!
-h, --help Print out this help
Examples:
Export all custom language files to the default folder:
\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/export.php
Export just the english files of moodle core and the activity 'quiz' in a subfolder in my home folder:
\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/export.php --lang='en' --components='moodle,quiz' --target='~/customdir'
EOF;
$dafaulttarget = "$CFG->tempdir/customlang/";
// Now get cli options.
list($options, $unrecognized) = cli_get_params(
[
'lang' => '',
'components' => '',
'target' => $dafaulttarget,
'overwrite' => false,
'help' => false,
],
['h' => 'help', 'c' => 'components', 't' => 'target', 'o' => 'overwrite']
);
if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}
if ($options['help']) {
echo $usage;
die;
}
// Ensure target directory exists.
$options['target'] = rtrim($options['target'], '/') . '/';
check_dir_exists($options['target']);
cli_writeln(get_string('cliexportheading', 'tool_customlang'));
$langs = [];
if ($options['lang']) {
$langs = explode(',', $options['lang']);
} else {
// No language set. We export all installed languages.
$langs = array_keys(get_string_manager()->get_list_of_translations(true));
}
foreach ($langs as $lang) {
$filename = $options['target'] . "customlang_{$lang}.zip";
// If the file exists and we are not using the temp folder it requires an ovewrite.
if ($options['target'] != $dafaulttarget && file_exists($filename) && !$options['overwrite']) {
cli_problem(get_string('cliexportfileexists', 'tool_customlang', ['lang' => $lang]));
continue;
}
cli_heading(get_string('cliexportstartexport', 'tool_customlang', $lang));
$langdir = tool_customlang_utils::get_localpack_location($lang);
if (!file_exists($langdir)) {
// No custom files set for this language set.
cli_writeln(get_string('cliexportnofilefoundforlang', 'tool_customlang', ['lang' => $lang]));
continue;
}
$zipper = get_file_packer();
$tempzip = tempnam($CFG->tempdir . '/', 'tool_customlang_export');
$filelist = [];
if ($options['components']) {
$components = explode(',', $options['components']);
foreach ($components as $component) {
$filepath = "$langdir/$component.php";
if (file_exists($filepath)) {
$filelist["$component.php"] = $filepath;
} else {
cli_problem(
get_string('cliexportfilenotfoundforcomponent', 'tool_customlang', ['lang' => $lang, 'file' => $filepath])
);
}
}
} else {
$langfiles = scandir($langdir);
foreach ($langfiles as $file) {
if (substr($file, 0, 1) != '.') {
$filelist[$file] = "$langdir/$file";
}
}
}
if (empty($filelist)) {
cli_problem(get_string('cliexportnofilefoundforlang', 'tool_customlang', ['lang' => $lang]));
continue;
}
if ($zipper->archive_to_pathname($filelist, $filename)) {
cli_writeln(get_string('cliexportzipdone', 'tool_customlang', $filename));
} else {
cli_problem(get_string('cliexportzipfail', 'tool_customlang', $filename));
}
}
+214
View File
@@ -0,0 +1,214 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* CLI customlang import tool.
*
* @package tool_customlang
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_customlang\local\importer;
use core\output\notification;
define('CLI_SCRIPT', true);
require(__DIR__ . '/../../../../config.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/customlang/locallib.php');
require_once("$CFG->libdir/clilib.php");
$usage =
"Import lang customization.
It can get a single file or a folder.
If no lang is provided it will try to infere from the filename
Options:
--lang The target language (will get from filename if not provided)
--source=path File or folder of the custom lang files (zip or php files)
--mode What string should be imported. Options are:
- all: all string will be imported (default)
- new: only string with no previous customisation
- update: only strings already modified
--checkin Save strings to the language pack
-h, --help Print out this help
Examples:
\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/import.php --lang=en --source=customlangs.zip
\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/import.php --source=/tmp/customlangs --checkin
\$ sudo -u www-data /usr/bin/php admin/tool/customlang/cli/import.php --lang=en --source=/tmp/customlangs
";
list($options, $unrecognized) = cli_get_params(
[
'help' => false,
'lang' => false,
'source' => false,
'mode' => 'all',
'checkin' => false,
],
['h' => 'help']
);
if ($unrecognized) {
$unrecognized = implode("\n ", $unrecognized);
cli_error(get_string('cliunknowoption', 'admin', $unrecognized));
}
if ($options['help']) {
cli_write($usage);
exit(0);
}
$source = $options['source'] ?? null;
$lang = $options['lang'] ?? null;
$modeparam = $options['mode'] ?? 'all';
$checkin = $options['checkin'] ?? false;
$modes = [
'all' => importer::IMPORTALL,
'update' => importer::IMPORTUPDATE,
'new' => importer::IMPORTNEW,
];
if (!isset($modes[$modeparam])) {
cli_error(get_string('climissingmode', 'tool_customlang'));
}
$mode = $modes[$modeparam];
if (empty($source)) {
$source = $CFG->dataroot.'/temp/customlang';
}
if (!file_exists($source)) {
cli_error(get_string('climissingsource', 'tool_customlang'));
}
// Emulate normal session - we use admin account by default.
\core\cron::setup_user();
// Get the file list.
$files = [];
$langfiles = [];
if (is_file($source)) {
$files[] = $source;
}
if (is_dir($source)) {
$filelist = glob("$source/*");
foreach ($filelist as $filename) {
$files[] = "$filename";
}
}
$countfiles = 0;
foreach ($files as $filepath) {
// Try to get the lang.
$filelang = $lang;
// Get component from filename.
$pathparts = pathinfo($filepath);
$filename = $pathparts['filename'];
$extension = $pathparts['extension'];
if ($extension == 'zip') {
if (!$filelang) {
// Try to get the lang from the filename.
if (strrpos($filename, 'customlang_') === 0) {
$parts = explode('_', $filename);
if (!empty($parts[1])) {
$filelang = $parts[1];
}
}
}
} else if ($extension != 'php') {
// Ignore any other file extension.
continue;
}
if (empty($filelang)) {
cli_error(get_string('climissinglang', 'tool_customlang'));
}
if (!isset($langfiles[$filelang])) {
$langfiles[$filelang] = [];
}
$langfiles[$filelang][] = $filepath;
$countfiles ++;
}
if (!$countfiles) {
cli_error(get_string('climissingfiles', 'tool_customlang'));
}
foreach ($langfiles as $lng => $files) {
$importer = new importer($lng, $mode);
$storedfiles = [];
$fs = get_file_storage();
cli_heading(get_string('clifiles', 'tool_customlang', $lng));
// If we intend to check in any changes, we must first check them out.
if ($checkin) {
cli_writeln(get_string('checkout', 'tool_customlang'));
$progressbar = new progress_bar();
$progressbar->create();
tool_customlang_utils::checkout($lng, $progressbar);
}
foreach ($files as $file) {
// Generate a valid stored_file from this file.
$record = (object)[
'filearea' => 'draft',
'component' => 'user',
'filepath' => '/',
'itemid' => file_get_unused_draft_itemid(),
'license' => $CFG->sitedefaultlicense,
'author' => '',
'filename' => clean_param(basename($file), PARAM_FILE),
'contextid' => \context_user::instance($USER->id)->id,
'userid' => $USER->id,
];
cli_writeln($file);
$storedfiles[] = $fs->create_file_from_pathname($record, $file);
}
cli_writeln("");
// Import files.
cli_heading(get_string('cliimporting', 'tool_customlang', $modeparam));
$importer->import($storedfiles);
// Display logs.
$log = $importer->get_log();
if (empty($log)) {
cli_problem(get_string('clinolog', 'tool_customlang', $lng));
}
foreach ($log as $message) {
if ($message->errorlevel == notification::NOTIFY_ERROR) {
cli_problem($message->get_message());
} else {
cli_writeln($message->get_message());
}
}
// Do the checkin if necessary.
if ($checkin) {
tool_customlang_utils::checkin($lng);
cli_writeln(get_string('savecheckin', 'tool_customlang'));
}
cli_writeln("");
}
exit(0);
+59
View File
@@ -0,0 +1,59 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Defines the capabilities used by the Language customization admin tool
*
* @package tool_customlang
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$capabilities = array(
/* allows the user to view the current language customization */
'tool/customlang:view' => array(
'riskbitmask' => RISK_CONFIG,
'captype' => 'read',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'manager' => CAP_ALLOW
),
),
/* allows the user to edit the current language customization */
'tool/customlang:edit' => array(
'riskbitmask' => RISK_CONFIG | RISK_XSS,
'captype' => 'write',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'manager' => CAP_ALLOW
),
),
/* allows the user to export the current language customization */
'tool/customlang:export' => array(
'riskbitmask' => RISK_CONFIG,
'captype' => 'read',
'contextlevel' => CONTEXT_SYSTEM,
'archetypes' => array(
'manager' => CAP_ALLOW
),
),
);
+33
View File
@@ -0,0 +1,33 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Post installation and migration code.
*
* @package tool
* @subpackage customlang
* @copyright 2011 Petr Skoda {@link http://skodak.org}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die;
function xmldb_tool_customlang_install() {
global $CFG, $OUTPUT, $DB;
}
+40
View File
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="admin/tool/customlang/db" VERSION="20120122" COMMENT="XMLDB file for Moodle admin/tool/customlang"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../../lib/xmldb/xmldb.xsd"
>
<TABLES>
<TABLE NAME="tool_customlang" COMMENT="Contains the working checkout of all strings and their customization">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="lang" TYPE="char" LENGTH="20" NOTNULL="true" SEQUENCE="false" COMMENT="The code of the language this string belongs to. Like en, cs or es"/>
<FIELD NAME="componentid" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="The id of the component"/>
<FIELD NAME="stringid" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="The identifier of the string"/>
<FIELD NAME="original" TYPE="text" NOTNULL="true" SEQUENCE="false" COMMENT="English original of the string"/>
<FIELD NAME="master" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Master translation of the string as is distributed in the official lang pack, null if not translated"/>
<FIELD NAME="local" TYPE="text" NOTNULL="false" SEQUENCE="false" COMMENT="Local customization of the string, null if not customized"/>
<FIELD NAME="timemodified" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="false" COMMENT="The timestamp of when the original or master was recently modified"/>
<FIELD NAME="timecustomized" TYPE="int" LENGTH="10" NOTNULL="false" SEQUENCE="false" COMMENT="The timestamp of when the value of the local translation was recently modified, null if not customized yet"/>
<FIELD NAME="outdated" TYPE="int" LENGTH="3" NOTNULL="false" DEFAULT="0" SEQUENCE="false" COMMENT="Either the English original or the master translation changed and the customization may be outdated"/>
<FIELD NAME="modified" TYPE="int" LENGTH="3" NOTNULL="false" DEFAULT="0" SEQUENCE="false" COMMENT="Has the string been modified via the translator?"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
<KEY NAME="fk_component" TYPE="foreign" FIELDS="componentid" REFTABLE="tool_customlang_components" REFFIELDS="id"/>
</KEYS>
<INDEXES>
<INDEX NAME="uq_lang_component_string" UNIQUE="true" FIELDS="lang, componentid, stringid" COMMENT="For a given language and component, string identifiers must be unique"/>
</INDEXES>
</TABLE>
<TABLE NAME="tool_customlang_components" COMMENT="Contains the list of all installed plugins that provide their own language pack">
<FIELDS>
<FIELD NAME="id" TYPE="int" LENGTH="10" NOTNULL="true" SEQUENCE="true"/>
<FIELD NAME="name" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" COMMENT="The normalized name of the plugin"/>
<FIELD NAME="version" TYPE="char" LENGTH="255" NOTNULL="false" SEQUENCE="false" COMMENT="The checked out version of the plugin, null if the version is unknown"/>
</FIELDS>
<KEYS>
<KEY NAME="primary" TYPE="primary" FIELDS="id"/>
</KEYS>
</TABLE>
</TABLES>
</XMLDB>
+40
View File
@@ -0,0 +1,40 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Language customization report upgrades
*
* @package tool
* @subpackage customlang
* @copyright 2010 David Mudrak <david.mudrak@gmail.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
function xmldb_tool_customlang_upgrade($oldversion) {
// Automatically generated Moodle v4.1.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.2.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.3.0 release upgrade line.
// Put any upgrade step following this.
// Automatically generated Moodle v4.4.0 release upgrade line.
// Put any upgrade step following this.
return true;
}
+130
View File
@@ -0,0 +1,130 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package tool
* @subpackage customlang
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/customlang/locallib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/customlang/filter_form.php');
require_once($CFG->libdir.'/adminlib.php');
require_login(SITEID, false);
require_capability('tool/customlang:edit', context_system::instance());
$lng = required_param('lng', PARAM_LANG);
$currentpage = optional_param('p', 0, PARAM_INT);
$translatorsubmitted = optional_param('translatorsubmitted', 0, PARAM_BOOL);
admin_externalpage_setup('toolcustomlang', '', null,
new moodle_url('/admin/tool/customlang/edit.php', array('lng' => $lng)),
array('pagelayout' => 'report')); // Hack: allows for wide page contents.
$PAGE->requires->js_init_call('M.tool_customlang.init_editor', array(), true);
$PAGE->set_context(context_system::instance());
$PAGE->set_secondary_active_tab('siteadminnode');
$PAGE->set_primary_active_tab('siteadminnode');
$PAGE->navbar->add(get_string('editlangpack', 'tool_customlang'), $PAGE->url);
if (empty($lng)) {
// PARAM_LANG validation failed
throw new \moodle_exception('missingparameter');
}
// pre-output processing
$filter = new tool_customlang_filter_form($PAGE->url, null, 'post', '', array('class'=>'filterform'));
$filterdata = tool_customlang_utils::load_filter($USER);
$filter->set_data($filterdata);
if ($filter->is_cancelled()) {
redirect($PAGE->url);
} elseif ($submitted = $filter->get_data()) {
tool_customlang_utils::save_filter($submitted, $USER);
redirect(new moodle_url($PAGE->url, array('p'=>0)));
}
if ($translatorsubmitted) {
$strings = optional_param_array('cust', array(), PARAM_RAW);
$updates = optional_param_array('updates', array(), PARAM_INT);
$checkin = optional_param('savecheckin', false, PARAM_RAW);
if ($checkin === false) {
$nexturl = new moodle_url($PAGE->url, array('p' => $currentpage));
} else {
$nexturl = new moodle_url('/admin/tool/customlang/index.php', array('action'=>'checkin', 'lng' => $lng, 'sesskey'=>sesskey()));
}
if (!is_array($strings)) {
$strings = array();
}
$current = $DB->get_records_list('tool_customlang', 'id', array_keys($strings));
$now = time();
foreach ($strings as $recordid => $customization) {
$customization = trim($customization);
if (empty($customization) and !is_null($current[$recordid]->local)) {
$current[$recordid]->local = null;
$current[$recordid]->modified = 1;
$current[$recordid]->outdated = 0;
$current[$recordid]->timecustomized = null;
$DB->update_record('tool_customlang', $current[$recordid]);
continue;
}
if (empty($customization)) {
continue;
}
if ($customization !== $current[$recordid]->local) {
$current[$recordid]->local = $customization;
$current[$recordid]->modified = 1;
$current[$recordid]->outdated = 0;
$current[$recordid]->timecustomized = $now;
$DB->update_record('tool_customlang', $current[$recordid]);
continue;
}
}
if (!is_array($updates)) {
$updates = array();
}
if (!empty($updates)) {
list($sql, $params) = $DB->get_in_or_equal($updates);
$DB->set_field_select('tool_customlang', 'outdated', 0, "local IS NOT NULL AND id $sql", $params);
}
redirect($nexturl);
}
$translator = new tool_customlang_translator($PAGE->url, $lng, $filterdata, $currentpage);
// output starts here
$output = $PAGE->get_renderer('tool_customlang');
$paginator = $output->paging_bar($translator->numofrows, $currentpage, tool_customlang_translator::PERPAGE, $PAGE->url, 'p');
echo $output->header();
$filter->display();
echo $paginator;
echo $output->render($translator);
echo $paginator;
echo $output->footer();
+68
View File
@@ -0,0 +1,68 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Performs the custom lang export.
*
* @package tool_customlang
* @subpackage customlang
* @copyright 2020 Thomas Wedekind <thomas.wedekind@univie.ac.at>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
require(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/customlang/locallib.php');
require_once($CFG->libdir.'/adminlib.php');
global $PAGE, $CFG;
require_login(SITEID, false);
require_capability('tool/customlang:export', context_system::instance());
$lng = required_param('lng', PARAM_LANG);
admin_externalpage_setup('toolcustomlang', '', null,
new moodle_url('/admin/tool/customlang/import.php', ['lng' => $lng]));
$form = new \tool_customlang\form\export(null, ['lng' => $lng]);
if ($form->is_cancelled()) {
redirect('index.php');
die();
} else if ($formdata = $form->get_data()) {
$tempzip = tempnam($CFG->tempdir . '/', 'tool_customlang_export');
$filelist = [];
foreach ($formdata->files as $file) {
$filepath = tool_customlang_utils::get_localpack_location($lng). '/' . $file;
if (file_exists($filepath)) {
$filelist[$file] = $filepath;
}
}
$zipper = new zip_packer();
if (!empty($filelist) && $zipper->archive_to_pathname($filelist, $tempzip)) {
// Filename include the lang name so the file can be imported with automatic language detection.
send_temp_file($tempzip, "customlang_$lng.zip");
die();
}
}
$output = $PAGE->get_renderer('tool_customlang');
echo $output->header();
echo $output->heading(get_string('pluginname', 'tool_customlang'));
$form->display();
echo $OUTPUT->footer();
+77
View File
@@ -0,0 +1,77 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package tool
* @subpackage customlang
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->dirroot . '/lib/formslib.php');
/**
* Form for filtering the strings to customize
*/
class tool_customlang_filter_form extends moodleform {
function definition() {
$mform = $this->_form;
$mform->addElement('header', 'filtersettings', get_string('filter', 'tool_customlang'));
// Component
$options = array();
foreach (tool_customlang_utils::list_components() as $component => $normalized) {
list($type, $plugin) = core_component::normalize_component($normalized);
if ($type == 'core' and is_null($plugin)) {
$plugin = 'moodle';
}
$options[$type][$normalized] = $component.'.php';
}
$mform->addElement('selectgroups', 'component', get_string('filtercomponent', 'tool_customlang'), $options,
array('multiple'=>'multiple', 'size'=>7));
// Customized only
$mform->addElement('advcheckbox', 'customized', get_string('filtercustomized', 'tool_customlang'));
$mform->setType('customized', PARAM_BOOL);
$mform->setDefault('customized', 0);
// Only helps
$mform->addElement('advcheckbox', 'helps', get_string('filteronlyhelps', 'tool_customlang'));
$mform->setType('helps', PARAM_BOOL);
$mform->setDefault('helps', 0);
// Modified only
$mform->addElement('advcheckbox', 'modified', get_string('filtermodified', 'tool_customlang'));
$mform->setType('modified', PARAM_BOOL);
$mform->setDefault('modified', 0);
// Substring
$mform->addElement('text', 'substring', get_string('filtersubstring', 'tool_customlang'));
$mform->setType('substring', PARAM_RAW);
// String identifier
$mform->addElement('text', 'stringid', get_string('filterstringid', 'tool_customlang'));
$mform->setType('stringid', PARAM_STRINGID);
// Show strings - submit button
$mform->addElement('submit', 'submit', get_string('filtershowstrings', 'tool_customlang'));
}
}
+80
View File
@@ -0,0 +1,80 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Import custom lang files.
*
* @package tool_customlang
* @subpackage customlang
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
use tool_customlang\form\import;
use tool_customlang\local\importer;
require(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/customlang/locallib.php');
require_once($CFG->libdir.'/adminlib.php');
require_login(SITEID, false);
require_capability('tool/customlang:edit', context_system::instance());
$lng = required_param('lng', PARAM_LANG);
admin_externalpage_setup('toolcustomlang', '', null,
new moodle_url('/admin/tool/customlang/import.php', ['lng' => $lng]));
$PAGE->set_context(context_system::instance());
$PAGE->set_secondary_active_tab('siteadminnode');
$PAGE->set_primary_active_tab('siteadminnode');
$PAGE->navbar->add(get_string('import', 'tool_customlang'), $PAGE->url);
$output = $PAGE->get_renderer('tool_customlang');
$form = new import(null, ['lng' => $lng]);
if ($data = $form->get_data()) {
require_sesskey();
// Get the file from the users draft area.
$usercontext = context_user::instance($USER->id);
$fs = get_file_storage();
$files = $fs->get_area_files($usercontext->id, 'user', 'draft', $data->pack, 'id',
false);
// Send files to the importer.
$importer = new importer($data->lng, $data->importmode);
$importer->import($files);
echo $output->header();
// Display logs.
$log = $importer->get_log();
foreach ($log as $message) {
echo $output->notification($message->get_message(), $message->errorlevel);
}
// Show continue button.
echo $output->continue_button(new moodle_url('index.php', array('lng' => $lng)));
} else {
echo $output->header();
$form->display();
}
echo $OUTPUT->footer();
+159
View File
@@ -0,0 +1,159 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Performs checkout of the strings into the translation table
*
* @package tool
* @subpackage customlang
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
define('NO_OUTPUT_BUFFERING', true); // progress bar is used here
require(__DIR__ . '/../../../config.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/tool/customlang/locallib.php');
require_once($CFG->libdir.'/adminlib.php');
require_login(null, false);
require_capability('tool/customlang:view', context_system::instance());
$action = optional_param('action', '', PARAM_ALPHA);
$confirm = optional_param('confirm', false, PARAM_BOOL);
$lng = optional_param('lng', '', PARAM_LANG);
$next = optional_param('next', 'edit', PARAM_ALPHA);
admin_externalpage_setup('toolcustomlang');
$langs = get_string_manager()->get_list_of_translations();
$PAGE->set_primary_active_tab('siteadminnode');
// pre-output actions
if ($action === 'checkout') {
require_sesskey();
require_capability('tool/customlang:edit', context_system::instance());
if (empty($lng)) {
throw new \moodle_exception('missingparameter');
}
$PAGE->set_cacheable(false); // progress bar is used here
$output = $PAGE->get_renderer('tool_customlang');
echo $output->header();
echo $output->heading(get_string('pluginname', 'tool_customlang'));
$progressbar = new progress_bar();
$progressbar->create(); // prints the HTML code of the progress bar
echo $output->continue_button(new moodle_url("/admin/tool/customlang/{$next}.php", array('lng' => $lng)), 'get');
echo $output->footer();
\core\session\manager::write_close();
echo $OUTPUT->select_element_for_append();
// We may need a bit of extra execution time and memory here.
core_php_time_limit::raise(HOURSECS);
raise_memory_limit(MEMORY_EXTRA);
tool_customlang_utils::checkout($lng, $progressbar);
exit;
}
if ($action === 'checkin') {
require_sesskey();
require_capability('tool/customlang:edit', context_system::instance());
if (empty($lng)) {
throw new \moodle_exception('missingparameter');
}
if (!$confirm) {
$output = $PAGE->get_renderer('tool_customlang');
echo $output->header();
echo $output->heading(get_string('pluginname', 'tool_customlang'));
echo $output->heading($langs[$lng], 3);
$numofmodified = tool_customlang_utils::get_count_of_modified($lng);
if ($numofmodified != 0) {
echo $output->heading(get_string('modifiednum', 'tool_customlang', $numofmodified), 3);
echo $output->confirm(get_string('confirmcheckin', 'tool_customlang'),
new moodle_url($PAGE->url, array('action'=>'checkin', 'lng'=>$lng, 'confirm'=>1)),
new moodle_url($PAGE->url, array('lng'=>$lng)));
} else {
echo $output->heading(get_string('modifiedno', 'tool_customlang', $numofmodified), 3);
echo $output->continue_button(new moodle_url($PAGE->url, array('lng' => $lng)));
}
echo $output->footer();
die();
} else {
tool_customlang_utils::checkin($lng);
redirect($PAGE->url);
}
}
$output = $PAGE->get_renderer('tool_customlang');
// output starts here
echo $output->header();
echo $output->heading(get_string('pluginname', 'tool_customlang'));
if (empty($lng)) {
$s = new single_select($PAGE->url, 'lng', $langs);
$s->label = get_accesshide(get_string('language'));
$s->class = 'langselector';
echo $output->box($OUTPUT->render($s), 'langselectorbox');
echo $OUTPUT->footer();
exit;
}
echo $output->heading($langs[$lng], 3);
$numofmodified = tool_customlang_utils::get_count_of_modified($lng);
if ($numofmodified != 0) {
echo $output->heading(get_string('modifiednum', 'tool_customlang', $numofmodified), 3);
}
$menu = array();
if (has_capability('tool/customlang:edit', context_system::instance())) {
$menu['checkout'] = array(
'title' => get_string('checkout', 'tool_customlang'),
'url' => new moodle_url($PAGE->url, array('action' => 'checkout', 'lng' => $lng)),
'method' => 'post',
);
if ($numofmodified != 0) {
$menu['checkin'] = array(
'title' => get_string('checkin', 'tool_customlang'),
'url' => new moodle_url($PAGE->url, array('action' => 'checkin', 'lng' => $lng)),
'method' => 'post',
);
}
$menu['import'] = array(
'title' => get_string('import', 'tool_customlang'),
'url' => new moodle_url($PAGE->url, ['action' => 'checkout', 'lng' => $lng, 'next' => 'import']),
'method' => 'post',
);
}
if (has_capability('tool/customlang:export', context_system::instance())) {
$langdir = tool_customlang_utils::get_localpack_location($lng);
if (check_dir_exists(dirname($langdir)) && count(glob("$langdir/*"))) {
$menu['export'] = [
'title' => get_string('export', 'tool_customlang'),
'url' => new moodle_url("/admin/tool/customlang/export.php", ['lng' => $lng]),
'method' => 'post',
];
}
}
echo $output->render(new tool_customlang_menu($menu));
echo $output->footer();
@@ -0,0 +1 @@
exportzipfilename,tool_customlang
@@ -0,0 +1,96 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Strings for Language customisation admin tool
*
* @package tool
* @subpackage customlang
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$string['checkin'] = 'Save strings to language pack';
$string['checkout'] = 'Open language pack for editing';
$string['checkoutdone'] = 'Language pack loaded';
$string['checkoutinprogress'] = 'Loading language pack';
$string['cliexportfileexists'] = 'File for {$a->lang} already exists, skipping. If you want to overwrite add the --override=true option.';
$string['cliexportheading'] = 'Starting to export lang files.';
$string['cliexportnofilefoundforlang'] = 'No file found to export. Skipping export for this language.';
$string['cliexportfilenotfoundforcomponent'] = 'File {$a->filepath} not found for language {$a->lang}. Skipping this file.';
$string['cliexportstartexport'] = 'Exporting language {$a}';
$string['cliexportzipdone'] = 'Zip created: {$a}';
$string['cliexportzipfail'] = 'Cannot create zip {$a}';
$string['clifiles'] = 'Files to import into {$a}';
$string['cliimporting'] = 'Import files string (mode {$a})';
$string['clinolog'] = 'Nothing to import into {$a}';
$string['climissinglang'] = 'Missing language';
$string['climissingfiles'] = 'Missing valid files';
$string['climissingmode'] = 'Missing or invalid mode (valid is all, new or update)';
$string['climissingsource'] = 'Missing file or folder';
$string['confirmcheckin'] = 'You are about to save modifications to your local language pack. This will export the customised strings from the translator into your site data directory and your site will start using the modified strings. Press \'Continue\' to proceed with saving.';
$string['customlang:edit'] = 'Edit local translation';
$string['customlang:export'] = 'Export local translation';
$string['customlang:view'] = 'View local translation';
$string['export'] = 'Export custom strings';
$string['exportfilter'] = 'Select component(s) to export';
$string['editlangpack'] = 'Edit language pack';
$string['filter'] = 'Filter strings';
$string['filtercomponent'] = 'Show strings of these components';
$string['filtercustomized'] = 'Customised only';
$string['filtermodified'] = 'Modified in this session only';
$string['filteronlyhelps'] = 'Help only';
$string['filtershowstrings'] = 'Show strings';
$string['filterstringid'] = 'String identifier';
$string['filtersubstring'] = 'Only strings containing';
$string['headingcomponent'] = 'Component';
$string['headinglocal'] = 'Local customisation';
$string['headingstandard'] = 'Standard text';
$string['headingstringid'] = 'String';
$string['import'] = 'Import custom strings';
$string['import_mode'] = 'Import mode';
$string['import_new'] = 'Create only strings without local customisation';
$string['import_update'] = 'Update only strings with local customisation';
$string['import_all'] = 'Create or update all strings from the component(s)';
$string['importfile'] = 'Import file';
$string['langpack'] = 'Language component(s)';
$string['markinguptodate'] = 'Marking the customisation as up-to-date';
$string['markinguptodate_help'] = 'The customised translation may get outdated if either the English original or the master translation has modified since the string was customised on your site. Review the customised translation. If you find it up-to-date, click the checkbox. Edit it otherwise.';
$string['markuptodate'] = 'mark as up-to-date';
$string['modifiedno'] = 'There are no modified strings to save.';
$string['modifiednum'] = 'There are {$a} modified strings. Do you wish to save these changes to your local language pack?';
$string['nolocallang'] = 'No local strings found.';
$string['notice_ignorenew'] = 'Ignoring string {$a->component}/{$a->stringid} because it is not customised.';
$string['notice_ignoreupdate'] = 'Ignoring string {$a->component}/{$a->stringid} because it is already defined.';
$string['notice_inexitentstring'] = 'String {$a->component}/{$a->stringid} not found.';
$string['notice_missingcomponent'] = 'Missing component {$a->component}.';
$string['notice_success'] = 'String {$a->component}/{$a->stringid} updated successfully.';
$string['nostringsfound'] = 'No strings found, please modify the filter settings';
$string['placeholder'] = 'Placeholders';
$string['placeholder_help'] = 'Placeholders are special statements like `{$a}` or `{$a->something}` within the string. They are replaced with a value when the string is actually printed.
It is important to copy them exactly as they are in the original string. Do not translate them nor change their left-to-right orientation.';
$string['placeholderwarning'] = 'string contains a placeholder';
$string['pluginname'] = 'Language customisation';
$string['savecheckin'] = 'Save changes to the language pack';
$string['savecontinue'] = 'Apply changes and continue editing';
$string['privacy:metadata'] = 'The Language customisation plugin does not store any personal data.';
// Deprecated since Moodle 4.2.
$string['exportzipfilename'] = 'customlang-export-{$a->lang}.zip';
+24
View File
@@ -0,0 +1,24 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package tool
* @subpackage customlang
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
+553
View File
@@ -0,0 +1,553 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Definition of classes used by language customization admin tool
*
* @package tool
* @subpackage customlang
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/**
* Provides various utilities to be used by the plugin
*
* All the public methods here are static ones, this class can not be instantiated
*/
class tool_customlang_utils {
/**
* Rough number of strings that are being processed during a full checkout.
* This is used to estimate the progress of the checkout.
*/
const ROUGH_NUMBER_OF_STRINGS = 32000;
/** @var array cache of {@link self::list_components()} results */
private static $components = null;
/**
* This class can not be instantiated
*/
private function __construct() {
}
/**
* Returns a list of all components installed on the server
*
* @return array (string)legacyname => (string)frankenstylename
*/
public static function list_components() {
if (self::$components === null) {
$list['moodle'] = 'core';
$coresubsystems = core_component::get_core_subsystems();
ksort($coresubsystems); // Should be but just in case.
foreach ($coresubsystems as $name => $location) {
$list[$name] = 'core_' . $name;
}
$plugintypes = core_component::get_plugin_types();
foreach ($plugintypes as $type => $location) {
$pluginlist = core_component::get_plugin_list($type);
foreach ($pluginlist as $name => $ununsed) {
if ($type == 'mod') {
// Plugin names are now automatically validated.
$list[$name] = $type . '_' . $name;
} else {
$list[$type . '_' . $name] = $type . '_' . $name;
}
}
}
self::$components = $list;
}
return self::$components;
}
/**
* Updates the translator database with the strings from files
*
* This should be executed each time before going to the translation page
*
* @param string $lang language code to checkout
* @param progress_bar $progressbar optionally, the given progress bar can be updated
*/
public static function checkout($lang, progress_bar $progressbar = null) {
global $DB, $CFG;
require_once("{$CFG->libdir}/adminlib.php");
// For behat executions we are going to load only a few components in the
// language customisation structures. Using the whole "en" langpack is
// too much slow (leads to Selenium 30s timeouts, especially on slow
// environments) and we don't really need the whole thing for tests. So,
// apart from escaping from the timeouts, we are also saving some good minutes
// in tests. See MDL-70014 and linked issues for more info.
$behatneeded = ['core', 'core_langconfig', 'tool_customlang'];
// make sure that all components are registered
$current = $DB->get_records('tool_customlang_components', null, 'name', 'name,version,id');
foreach (self::list_components() as $component) {
// Filter out unwanted components when running behat.
if (defined('BEHAT_SITE_RUNNING') && !in_array($component, $behatneeded)) {
continue;
}
if (empty($current[$component])) {
$record = new stdclass();
$record->name = $component;
if (!$version = get_component_version($component)) {
$record->version = null;
} else {
$record->version = $version;
}
$DB->insert_record('tool_customlang_components', $record);
} else if ($version = get_component_version($component)) {
if (is_null($current[$component]->version) or ($version > $current[$component]->version)) {
$DB->set_field('tool_customlang_components', 'version', $version, array('id' => $current[$component]->id));
}
}
}
unset($current);
// initialize the progress counter - stores the number of processed strings
$done = 0;
$strinprogress = get_string('checkoutinprogress', 'tool_customlang');
// reload components and fetch their strings
$stringman = get_string_manager();
$components = $DB->get_records('tool_customlang_components');
foreach ($components as $component) {
$sql = "SELECT stringid, id, lang, componentid, original, master, local, timemodified, timecustomized, outdated, modified
FROM {tool_customlang} s
WHERE lang = ? AND componentid = ?
ORDER BY stringid";
$current = $DB->get_records_sql($sql, array($lang, $component->id));
$english = $stringman->load_component_strings($component->name, 'en', true, true);
if ($lang == 'en') {
$master =& $english;
} else {
$master = $stringman->load_component_strings($component->name, $lang, true, true);
}
$local = $stringman->load_component_strings($component->name, $lang, true, false);
foreach ($english as $stringid => $stringoriginal) {
$stringmaster = isset($master[$stringid]) ? $master[$stringid] : null;
$stringlocal = isset($local[$stringid]) ? $local[$stringid] : null;
$now = time();
if (!is_null($progressbar)) {
$done++;
$donepercent = floor(min($done, self::ROUGH_NUMBER_OF_STRINGS) / self::ROUGH_NUMBER_OF_STRINGS * 100);
$progressbar->update_full($donepercent, $strinprogress);
}
if (isset($current[$stringid])) {
$needsupdate = false;
$currentoriginal = $current[$stringid]->original;
$currentmaster = $current[$stringid]->master;
$currentlocal = $current[$stringid]->local;
if ($currentoriginal !== $stringoriginal or $currentmaster !== $stringmaster) {
$needsupdate = true;
$current[$stringid]->original = $stringoriginal;
$current[$stringid]->master = $stringmaster;
$current[$stringid]->timemodified = $now;
$current[$stringid]->outdated = 1;
}
if ($stringmaster !== $stringlocal) {
$needsupdate = true;
$current[$stringid]->local = $stringlocal;
$current[$stringid]->timecustomized = $now;
} else if (isset($currentlocal) && $stringlocal !== $currentlocal) {
// If local string has been removed, we need to remove also the old local value from DB.
$needsupdate = true;
$current[$stringid]->local = null;
$current[$stringid]->timecustomized = $now;
}
if ($needsupdate) {
$DB->update_record('tool_customlang', $current[$stringid]);
continue;
}
} else {
$record = new stdclass();
$record->lang = $lang;
$record->componentid = $component->id;
$record->stringid = $stringid;
$record->original = $stringoriginal;
$record->master = $stringmaster;
$record->timemodified = $now;
$record->outdated = 0;
if ($stringmaster !== $stringlocal) {
$record->local = $stringlocal;
$record->timecustomized = $now;
} else {
$record->local = null;
$record->timecustomized = null;
}
$DB->insert_record('tool_customlang', $record);
}
}
}
if (!is_null($progressbar)) {
$progressbar->update_full(100, get_string('checkoutdone', 'tool_customlang'));
}
}
/**
* Exports the translator database into disk files
*
* @param mixed $lang language code
*/
public static function checkin($lang) {
global $DB, $USER, $CFG;
require_once($CFG->libdir.'/filelib.php');
if ($lang !== clean_param($lang, PARAM_LANG)) {
return false;
}
list($insql, $inparams) = $DB->get_in_or_equal(self::list_components());
// Get all customized strings from updated valid components.
$sql = "SELECT s.*, c.name AS component
FROM {tool_customlang} s
JOIN {tool_customlang_components} c ON s.componentid = c.id
WHERE s.lang = ?
AND (s.local IS NOT NULL OR s.modified = 1)
AND c.name $insql
ORDER BY componentid, stringid";
array_unshift($inparams, $lang);
$strings = $DB->get_records_sql($sql, $inparams);
$files = array();
foreach ($strings as $string) {
if (!is_null($string->local)) {
$files[$string->component][$string->stringid] = $string->local;
}
}
fulldelete(self::get_localpack_location($lang));
foreach ($files as $component => $strings) {
self::dump_strings($lang, $component, $strings);
}
$DB->set_field_select('tool_customlang', 'modified', 0, 'lang = ?', array($lang));
$sm = get_string_manager();
$sm->reset_caches();
}
/**
* Returns full path to the directory where local packs are dumped into
*
* @param string $lang language code
* @return string full path
*/
public static function get_localpack_location($lang) {
global $CFG;
return $CFG->langlocalroot.'/'.$lang.'_local';
}
/**
* Writes strings into a local language pack file
*
* @param string $component the name of the component
* @param array $strings
* @return void
*/
protected static function dump_strings($lang, $component, $strings) {
global $CFG;
if ($lang !== clean_param($lang, PARAM_LANG)) {
throw new moodle_exception('Unable to dump local strings for non-installed language pack .'.s($lang));
}
if ($component !== clean_param($component, PARAM_COMPONENT)) {
throw new coding_exception('Incorrect component name');
}
if (!$filename = self::get_component_filename($component)) {
throw new moodle_exception('Unable to find the filename for the component '.s($component));
}
if ($filename !== clean_param($filename, PARAM_FILE)) {
throw new coding_exception('Incorrect file name '.s($filename));
}
list($package, $subpackage) = core_component::normalize_component($component);
$packageinfo = " * @package $package";
if (!is_null($subpackage)) {
$packageinfo .= "\n * @subpackage $subpackage";
}
$filepath = self::get_localpack_location($lang);
$filepath = $filepath.'/'.$filename;
if (!is_dir(dirname($filepath))) {
check_dir_exists(dirname($filepath));
}
if (!$f = fopen($filepath, 'w')) {
throw new moodle_exception('Unable to write '.s($filepath));
}
fwrite($f, <<<EOF
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Local language pack from $CFG->wwwroot
*
$packageinfo
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
EOF
);
foreach ($strings as $stringid => $text) {
if ($stringid !== clean_param($stringid, PARAM_STRINGID)) {
debugging('Invalid string identifier '.s($stringid));
continue;
}
fwrite($f, '$string[\'' . $stringid . '\'] = ');
fwrite($f, var_export($text, true));
fwrite($f, ";\n");
}
fclose($f);
@chmod($filepath, $CFG->filepermissions);
}
/**
* Returns the name of the file where the component's local strings should be exported into
*
* @param string $component normalized name of the component, eg 'core' or 'mod_workshop'
* @return string|boolean filename eg 'moodle.php' or 'workshop.php', false if not found
*/
protected static function get_component_filename($component) {
$return = false;
foreach (self::list_components() as $legacy => $normalized) {
if ($component === $normalized) {
$return = $legacy.'.php';
break;
}
}
return $return;
}
/**
* Returns the number of modified strings checked out in the translator
*
* @param string $lang language code
* @return int
*/
public static function get_count_of_modified($lang) {
global $DB;
return $DB->count_records('tool_customlang', array('lang'=>$lang, 'modified'=>1));
}
/**
* Saves filter data into a persistant storage such as user session
*
* @see self::load_filter()
* @param stdclass $data filter values
* @param stdclass $persistant storage object
*/
public static function save_filter(stdclass $data, stdclass $persistant) {
if (!isset($persistant->tool_customlang_filter)) {
$persistant->tool_customlang_filter = array();
}
foreach ($data as $key => $value) {
if ($key !== 'submit') {
$persistant->tool_customlang_filter[$key] = serialize($value);
}
}
}
/**
* Loads the previously saved filter settings from a persistent storage
*
* @see self::save_filter()
* @param stdclass $persistant storage object
* @return stdclass filter data
*/
public static function load_filter(stdclass $persistant) {
$data = new stdclass();
if (isset($persistant->tool_customlang_filter)) {
foreach ($persistant->tool_customlang_filter as $key => $value) {
$data->{$key} = unserialize($value);
}
}
return $data;
}
}
/**
* Represents the action menu of the tool
*/
class tool_customlang_menu implements renderable {
/** @var menu items */
protected $items = array();
public function __construct(array $items = array()) {
global $CFG;
foreach ($items as $itemkey => $item) {
$this->add_item($itemkey, $item['title'], $item['url'], empty($item['method']) ? 'post' : $item['method']);
}
}
/**
* Returns the menu items
*
* @return array (string)key => (object)[->(string)title ->(moodle_url)url ->(string)method]
*/
public function get_items() {
return $this->items;
}
/**
* Adds item into the menu
*
* @param string $key item identifier
* @param string $title localized action title
* @param moodle_url $url action handler
* @param string $method form method
*/
public function add_item($key, $title, moodle_url $url, $method) {
if (isset($this->items[$key])) {
throw new coding_exception('Menu item already exists');
}
if (empty($title) or empty($key)) {
throw new coding_exception('Empty title or item key not allowed');
}
$item = new stdclass();
$item->title = $title;
$item->url = $url;
$item->method = $method;
$this->items[$key] = $item;
}
}
/**
* Represents the translation tool
*/
class tool_customlang_translator implements renderable {
/** @var int number of rows per page */
const PERPAGE = 100;
/** @var int total number of the rows int the table */
public $numofrows = 0;
/** @var moodle_url */
public $handler;
/** @var string language code */
public $lang;
/** @var int page to display, starting with page 0 */
public $currentpage = 0;
/** @var array of stdclass strings to display */
public $strings = array();
/** @var stdclass */
protected $filter;
public function __construct(moodle_url $handler, $lang, $filter, $currentpage = 0) {
global $DB;
$this->handler = $handler;
$this->lang = $lang;
$this->filter = $filter;
$this->currentpage = $currentpage;
if (empty($filter) or empty($filter->component)) {
// nothing to do
$this->currentpage = 1;
return;
}
list($insql, $inparams) = $DB->get_in_or_equal($filter->component, SQL_PARAMS_NAMED);
$csql = "SELECT COUNT(*)";
$fsql = "SELECT s.*, c.name AS component";
$sql = " FROM {tool_customlang_components} c
JOIN {tool_customlang} s ON s.componentid = c.id
WHERE s.lang = :lang
AND c.name $insql";
$params = array_merge(array('lang' => $lang), $inparams);
if (!empty($filter->customized)) {
$sql .= " AND s.local IS NOT NULL";
}
if (!empty($filter->modified)) {
$sql .= " AND s.modified = 1";
}
if (!empty($filter->stringid)) {
$sql .= " AND s.stringid = :stringid";
$params['stringid'] = $filter->stringid;
}
if (!empty($filter->substring)) {
$sql .= " AND (".$DB->sql_like('s.original', ':substringoriginal', false)." OR
".$DB->sql_like('s.master', ':substringmaster', false)." OR
".$DB->sql_like('s.local', ':substringlocal', false).")";
$params['substringoriginal'] = '%'.$filter->substring.'%';
$params['substringmaster'] = '%'.$filter->substring.'%';
$params['substringlocal'] = '%'.$filter->substring.'%';
}
if (!empty($filter->helps)) {
$sql .= " AND ".$DB->sql_like('s.stringid', ':help', false); //ILIKE
$params['help'] = '%\_help';
} else {
$sql .= " AND ".$DB->sql_like('s.stringid', ':link', false, true, true); //NOT ILIKE
$params['link'] = '%\_link';
}
$osql = " ORDER BY c.name, s.stringid";
$this->numofrows = $DB->count_records_sql($csql.$sql, $params);
$this->strings = $DB->get_records_sql($fsql.$sql.$osql, $params, ($this->currentpage) * self::PERPAGE, self::PERPAGE);
}
}
+45
View File
@@ -0,0 +1,45 @@
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package tool
* @subpackage customlang
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
/**
* @namespace
*/
M.tool_customlang = M.tool_customlang || {};
/**
* YUI instance holder
*/
M.tool_customlang.Y = {};
/**
* Initialize JS support for the edit.php
*
* @param {Object} Y YUI instance
*/
M.tool_customlang.init_editor = function(Y) {
M.tool_customlang.Y = Y;
Y.all('#translator .local textarea').each(function (textarea) {
var cell = textarea.get('parentNode');
textarea.setStyle('height', cell.getComputedStyle('height'));
});
}
+26
View File
@@ -0,0 +1,26 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* @package tool
* @subpackage customlang
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$ADMIN->add('language', new admin_externalpage('toolcustomlang', get_string('pluginname', 'tool_customlang'), "$CFG->wwwroot/$CFG->admin/tool/customlang/index.php", 'tool/customlang:view'));
@@ -0,0 +1,159 @@
{{!
This file is part of Moodle - http://moodle.org/
Moodle is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Moodle is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Moodle. If not, see <http://www.gnu.org/licenses/>.
}}
{{!
@template tool_customlang/translator
Template for the custom language translator page.
Classes required for JS:
-
Data attributes required for JS:
-
Context variables required for this template:
* strings
Example context (json):
{
"hasstrings": true,
"formurl": "admin/tool/customlang/edit.php?lng=en",
"currentpage": 0,
"sesskey" : "AZyeeQgmcs",
"strings": [
{
"id": 11,
"component": "core",
"componentid": 1,
"stringid": "course",
"original": "Course",
"master": "Cursus",
"local": "Hoofdstuk",
"outdated": 0,
"modified": 1
}
]
}
}}
{{^hasstrings}}
{{{ nostrings }}}
{{/hasstrings}}
{{#hasstrings}}
<form method="post" action="{{{formurl}}}">
<input type="hidden" name="translatorsubmitted" value="1">
<input type="hidden" name="sesskey" value="{{{ sesskey }}}">
<input type="hidden" name="p" value="{{ currentpage }}">
<fieldset class="m-3">
<button type="submit" name="savecontinue" class="btn btn-secondary">
{{#str}}savecontinue, tool_customlang{{/str}}
</button>
<button type="submit" name="savecheckin" class="btn btn-secondary">
{{#str}}savecheckin, tool_customlang{{/str}}
</button>
</fieldset>
<div class="list-group">
<div class="container-fluid d-none d-md-block list-group-item border-bottom-0">
<div class="row">
<div class="col-sm-4 col-md-2">
<strong>{{#str}}headingcomponent, tool_customlang{{/str}}</strong>
</div>
<div class="col-sm-4 col-md-2">
<strong>{{#str}}headingstringid, tool_customlang{{/str}}</strong>
</div>
<div class="col-sm-4 col-md-2">
<strong>{{#str}}headingstandard, tool_customlang{{/str}}</strong>
</div>
<div class="col-sm-12 col-md-6">
<span class="pl-3">
<strong>{{#str}}headinglocal, tool_customlang{{/str}}</strong>
</span>
</div>
</div>
</div>
</div>
<div class="list-group">
{{#strings}}
<div class="container-fluid list-group-item
{{#local}}list-group-item-info{{/local}}
{{#outdated}}list-group-item-warning{{/outdated}}
{{#modified}}list-group-item-info{{/modified}}"
>
<div class="row">
<div class="col-sm-4 col-md-2 text-break">
<div class="d-md-none">
<strong>{{#str}}headingcomponent, tool_customlang{{/str}}</strong>
</div>
{{{ component }}}
</div>
<div class="col-sm-4 col-md-2 text-break">
<div class="d-md-none">
<strong>{{#str}}headingstringid, tool_customlang{{/str}}</strong>
</div>
{{{ stringid }}}
</div>
<div class="col-sm-4 col-md-2">
<div class="d-md-none">
<strong>{{#str}}headingstandard, tool_customlang{{/str}}</strong>
</div>
{{ master }}
<div class="info">
{{{ placeholderhelp }}}
{{{ outdatedhelp}}}
</div>
{{#showoriginalvsmaster}}
<div class="alert-secondary mt-3 mt-1">
{{ original }}
</div>
{{/showoriginalvsmaster}}
</div>
<div class="col-sm-12 col-md-6 mt-sm-3 mt-md-0">
<div class="d-md-none">
<strong>{{#str}}headinglocal, tool_customlang{{/str}}</strong>
</div>
<div class="py-2 py-md-0 px-md-3">
<label for="{{id}}" class="sr-only sr-only-focusable">{{{ component }}}/{{{ stringid }}}</label>
<textarea class="form-control w-100 border-box" id="{{id}}" name="cust[{{id}}]" cols="40" rows="3">{{{ local }}}</textarea>
{{#checkupdated}}
<div class="uptodatewrapper">
<div class="form-check">
<input id="update_{{id}}" class="form-check-input" name="updates[]" type="checkbox" value="{{id}}">
<label for="update_{{id}}" class="form-check-label">{{#str}}markuptodate, tool_customlang{{/str}}</label>
{{{ outdatedhelp }}}
</div>
</div>
{{/checkupdated}}
</div>
</div>
</div>
</div>
{{/strings}}
</div>
<fieldset class="m-3">
<button type="submit" name="savecontinue" class="btn btn-secondary">
{{#str}}savecontinue, tool_customlang{{/str}}
</button>
<button type="submit" name="savecheckin" class="btn btn-secondary">
{{#str}}savecheckin, tool_customlang{{/str}}
</button>
</fieldset>
</form>
{{/hasstrings}}
@@ -0,0 +1,33 @@
@tool @tool_customlang
Feature: Within a moodle instance, an administrator should be able to modify langstrings for the entire Moodle installation.
In order to change langstrings in the adminsettings of the instance,
As an admin
I need to be able to access and change values in the the language customisation of the language pack.
Background:
# This is a very slow running test and on slow databases can take minutes to complete.
Given I mark this test as slow setting a timeout factor of 4
And I log in as "admin"
And I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
And I press "Open language pack for editing"
And I press "Continue"
And I set the field "Show strings of these components" to "moodle.php"
And I set the field "String identifier" to "moodledocslink"
And I press "Show strings"
And I set the field "core/moodledocslink" to "moodle documents"
@javascript
Scenario: Edit an string but don't save it to lang pack.
When I press "Apply changes and continue editing"
Then I should not see "moodle documents" in the "page-footer" "region"
And I should see "Documentation for this page" in the "page-footer" "region"
@javascript
Scenario: Customize an string as admin and save it to lang pack.
Given I press "Save changes to the language pack"
And I should see "There are 1 modified strings."
When I click on "Continue" "button"
Then I should not see "Documentation for this page" in the "page-footer" "region"
And I should see "moodle documents" in the "page-footer" "region"
@@ -0,0 +1,53 @@
@tool @tool_customlang
Feature: Within a moodle instance, an administrator should be able to export modified langstrings.
In order to export modified langstrings in the adminsettings of the instance,
As an admin
I need to be able to export the php-files of the language customisation of a language.
Background:
# This is a very slow running feature and on slow databases can take minutes to complete.
Given I mark this test as slow setting a timeout factor of 4
@javascript
Scenario: Export button should not appear if no customization is made
Given I log in as "admin"
And I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
Then I should see "Open language pack for editing"
And I should not see "Export custom strings"
@javascript
Scenario: Export button should not appear if no customization is saved into langpack
Given I log in as "admin"
And I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
And I press "Open language pack for editing"
And I press "Continue"
And I set the field "Show strings of these components" to "moodle.php"
And I set the field "String identifier" to "accept"
And I press "Show strings"
And I set the field "core/accept" to "Accept-custom_export"
When I press "Apply changes and continue editing"
And I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
Then I should see "Open language pack for editing"
And I should see "There are 1 modified strings."
And I should not see "Export custom strings"
@javascript
Scenario: Export the php-file including a customised langstring.
Given I log in as "admin"
And I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
And I press "Open language pack for editing"
And I press "Continue"
And I set the field "Show strings of these components" to "moodle.php"
And I set the field "String identifier" to "accept"
And I press "Show strings"
And I set the field "core/accept" to "Accept-custom_export"
When I press "Save changes to the language pack"
And I should see "There are 1 modified strings."
And I click on "Continue" "button"
Then I set the field "lng" to "en"
And I click on "Export custom strings" "button"
And I set the field "Select component(s) to export" to "moodle.php"
@@ -0,0 +1,51 @@
@tool @tool_customlang @_file_upload
Feature: Within a moodle instance, an administrator should be able to import modified langstrings.
In order to import modified langstrings in the adminsettings from one to another instance,
As an admin
I need to be able to import the zips and php files of the language customisation of a language.
Background:
# This is a very slow running test and on slow databases can take minutes to complete.
Given I mark this test as slow setting a timeout factor of 4
And I log in as "admin"
And I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
And I click on "Import custom strings" "button"
And I press "Continue"
@javascript
Scenario: Import a PHP file to add a new core lang customization
When I upload "admin/tool/customlang/tests/fixtures/tool_customlang.php" file to "Language component(s)" filemanager
And I press "Import file"
Then I should see "String tool_customlang/pluginname updated successfully."
And I should see "String tool_customlang/nonexistentinvetedstring not found."
And I click on "Continue" "button"
And I should see "There are 1 modified strings."
And I click on "Save strings to language pack" "button"
And I click on "Continue" "button"
And I should see "An amazing import feature"
@javascript
Scenario: Try to import a PHP file from a non existent component
When I upload "admin/tool/customlang/tests/fixtures/mod_fakecomponent.php" file to "Language component(s)" filemanager
And I press "Import file"
Then I should see "Missing component mod_fakecomponent."
@javascript
Scenario: Import a zip file with some PHP files in it.
When I upload "admin/tool/customlang/tests/fixtures/customlang.zip" file to "Language component(s)" filemanager
And I press "Import file"
Then I should see "String core/administrationsite updated successfully."
And I should see "String core/language updated successfully."
And I should see "String core/nonexistentinvetedstring not found."
And I should see "String tool_customlang/pluginname updated successfully."
And I should see "String tool_customlang/nonexistentinvetedstring not found."
And I should see "Missing component mod_fakecomponent."
And I click on "Continue" "button"
And I should see "There are 3 modified strings."
And I click on "Save strings to language pack" "button"
And I click on "Continue" "button"
And I should see "Uploaded custom string"
And I should see "Another Uploaded string"
And I should see "An amazing import feature"
@@ -0,0 +1,84 @@
@tool @tool_customlang @_file_upload
Feature: Within a moodle instance, an administrator should be able to import langstrings with several modes.
In order to import modified langstrings in the adminsettings from one to another instance,
As an admin
I need to be able to import only some language customisation strings depending on some conditions.
Background:
# This is a very slow running feature and on slow databases can take minutes to complete.
Given I mark this test as slow setting a timeout factor of 4
# Add one customization.
And I log in as "admin"
And I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
And I press "Open language pack for editing"
And I press "Continue"
And I set the field "Show strings of these components" to "moodle.php"
And I set the field "String identifier" to "administrationsite"
And I press "Show strings"
And I set the field "core/administrationsite" to "Custom string example"
And I press "Save changes to the language pack"
And I should see "There are 1 modified strings."
And I click on "Continue" "button"
And I should see "Custom string example"
@javascript
Scenario: Update only customized strings
When I set the field "lng" to "en"
And I click on "Import custom strings" "button"
And I press "Continue"
And I upload "admin/tool/customlang/tests/fixtures/moodle.php" file to "Language component(s)" filemanager
And I set the field "Import mode" to "Update only strings with local customisation"
And I press "Import file"
Then I should see "String core/administrationsite updated successfully."
And I should see "Ignoring string core/language because it is not customised."
And I should see "String core/nonexistentinvetedstring not found."
And I click on "Continue" "button"
And I should see "There are 1 modified strings."
And I should not see "Uploaded custom string"
And I click on "Save strings to language pack" "button"
And I click on "Continue" "button"
And I should not see "Custom string example"
And I should see "Uploaded custom string"
And I should not see "Another Uploaded string"
@javascript
Scenario: Create only new strings
When I set the field "lng" to "en"
And I click on "Import custom strings" "button"
And I press "Continue"
And I upload "admin/tool/customlang/tests/fixtures/moodle.php" file to "Language component(s)" filemanager
And I set the field "Import mode" to "Create only strings without local customisation"
And I press "Import file"
Then I should see "Ignoring string core/administrationsite because it is already defined."
And I should see "String core/language updated successfully."
And I should see "String core/nonexistentinvetedstring not found."
And I click on "Continue" "button"
And I should see "There are 1 modified strings."
And I should not see "Uploaded custom string"
And I click on "Save strings to language pack" "button"
And I click on "Continue" "button"
And I should see "Custom string example"
And I should not see "Uploaded custom string"
And I should see "Another Uploaded string"
@javascript
Scenario: Import all strings
When I set the field "lng" to "en"
And I click on "Import custom strings" "button"
And I press "Continue"
And I upload "admin/tool/customlang/tests/fixtures/moodle.php" file to "Language component(s)" filemanager
And I set the field "Import mode" to "Create or update all strings from the component(s)"
And I press "Import file"
Then I should see "String core/administrationsite updated successfully."
And I should see "String core/language updated successfully."
And I should see "String core/nonexistentinvetedstring not found."
And I click on "Continue" "button"
And I should see "There are 2 modified strings."
And I should not see "Uploaded custom string"
And I click on "Save strings to language pack" "button"
And I click on "Continue" "button"
And I should not see "Custom string example"
And I should see "Uploaded custom string"
And I should see "Another Uploaded string"
@@ -0,0 +1,21 @@
@tool @tool_customlang @javascript
Feature: Verify the breadcrumbs in language customisation site administration pages
Whenever I navigate to language customisation page in site administration
As an admin
The breadcrumbs should be visible
Background:
Given I log in as "admin"
Scenario: Verify the breadcrumbs in language customisation page by visiting open language pack for editing and import custom strings pages
Given I navigate to "Language > Language customisation" in site administration
And I set the field "lng" to "en"
When I click on "Open language pack for editing" "button"
And I wait until the page is ready
And "Language customisation" "text" should exist in the ".breadcrumb" "css_element"
And "Language" "link" should exist in the ".breadcrumb" "css_element"
And I press the "back" button in the browser
And I click on "Import custom strings" "button"
And I wait until the page is ready
And "Language customisation" "text" should exist in the ".breadcrumb" "css_element"
And "Language" "link" should exist in the ".breadcrumb" "css_element"
Binary file not shown.
@@ -0,0 +1,29 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Local language pack.
*
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$string['administrationsite'] = 'Uploaded custom string';
$string['language'] = 'Another Uploaded string';
$string['nonexistentinvetedstring'] = 'This should not be imported';
+28
View File
@@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Local language pack.
*
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$string['administrationsite'] = 'Uploaded custom string';
$string['language'] = 'Another Uploaded string';
$string['nonexistentinvetedstring'] = 'This should not be imported';
@@ -0,0 +1,28 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Local language pack from http://localhost/m/MDL-69583
*
* @copyright 2020 Ferran Recio <ferran@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$string['pluginname'] = 'An amazing import feature';
$string['nonexistentinvetedstring'] = 'This string should not be imported';
File diff suppressed because it is too large Load Diff
@@ -0,0 +1,207 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* PHP lang parser test.
*
* @package tool_customlang
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_customlang\local\mlang;
use advanced_testcase;
use moodle_exception;
/**
* PHP lang parser test class.
*
* @package tool_customlang
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class phpparser_test extends advanced_testcase {
/**
* Test get instance static method.
*
*/
public function test_get_instance(): void {
$instance = phpparser::get_instance();
$this->assertInstanceOf('tool_customlang\local\mlang\phpparser', $instance);
$this->assertEquals($instance, phpparser::get_instance());
}
/**
* Test get instance parse method.
*
* @dataProvider parse_provider
* @param string $phpcode PHP code to test
* @param array $expected Expected result
* @param bool $exception if an exception is expected
*/
public function test_parse(string $phpcode, array $expected, bool $exception): void {
$instance = phpparser::get_instance();
if ($exception) {
$this->expectException(moodle_exception::class);
}
$strings = $instance->parse($phpcode);
$this->assertEquals(count($expected), count($strings));
foreach ($strings as $key => $langstring) {
$this->assertEquals($expected[$key][0], $langstring->id);
$this->assertEquals($expected[$key][1], $langstring->text);
}
}
/**
* Data provider for the test_parse.
*
* @return array
*/
public function parse_provider(): array {
return [
'Invalid PHP code' => [
'No PHP code', [], false
],
'No PHP open tag' => [
"\$string['example'] = 'text';\n", [], false
],
'One string code' => [
"<?php \$string['example'] = 'text';\n", [['example', 'text']], false
],
'Extra spaces' => [
"<?php \$string['example'] = 'text';\n", [['example', 'text']], false
],
'Extra tabs' => [
"<?php \$string['example']\t=\t'text';\n", [['example', 'text']], false
],
'Double quote string' => [
"<?php
\$string['example'] = \"text\";
\$string[\"example2\"] = 'text2';
\$string[\"example3\"] = \"text3\";
", [
['example', 'text'],
['example2', 'text2'],
['example3', 'text3'],
], false
],
'Multiple lines strings' => [
"<?php
\$string['example'] = 'First line\nsecondline';
\$string['example2'] = \"First line\nsecondline2\";
", [
['example', "First line\nsecondline"],
['example2', "First line\nsecondline2"],
], false
],
'Two strings code' => [
"<?php
\$string['example'] = 'text';
\$string['example2'] = 'text2';
", [
['example', 'text'],
['example2', 'text2'],
], false
],
'Scaped characters' => [
"<?php
\$string['example'] = 'Thos are \\' quotes \" 1';
\$string['example2'] = \"Thos are ' quotes \\\" 2\";
", [
['example', "Thos are ' quotes \" 1"],
['example2', "Thos are ' quotes \" 2"],
], false
],
'PHP with single line comments' => [
"<?php
// This is a comment.
\$string['example'] = 'text';
// This is another commment.
", [
['example', 'text'],
], false
],
'PHP with block comments' => [
"<?php
/* This is a block comment. */
\$string['example'] = 'text';
/* This is another
block comment. */
", [
['example', 'text'],
], false
],
'Wrong variable name' => [
"<?php
\$stringwrong['example'] = 'text';
\$wringstring['example'] = 'text';
", [], false
],
'Single line commented valid line' => [
"<?php
// \$string['example'] = 'text';
", [], false
],
'Block commented valid line' => [
"<?php
/*
\$string['example'] = 'text';
*/
", [], false
],
'Syntax error 1 (double assignation)' => [
"<?php
\$string['example'] = 'text' = 'wrong';
", [], true
],
'Syntax error 2 (no closing string)' => [
"<?php
\$string['example'] = 'wrong;
", [], true
],
'Syntax error 3 (Array without key)' => [
"<?php
\$string[] = 'wrong';
", [], true
],
'Syntax error 4 (Array not open)' => [
"<?php
\$string'example'] = 'wrong';
", [], true
],
'Syntax error 5 (Array not closed)' => [
"<?php
\$string['example' = 'wrong';
", [], true
],
'Syntax error 6 (Missing assignment)' => [
"<?php
\$string['example'] 'wrong';
", [], true
],
];
}
}
+30
View File
@@ -0,0 +1,30 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Version information
*
* @package tool
* @subpackage customlang
* @copyright 2010 David Mudrak <david@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
$plugin->version = 2024042200;
$plugin->requires = 2024041600;
$plugin->component = 'tool_customlang'; // Full name of the plugin (used for diagnostics)