4 * This script enables Moodle translators to edit /docs and /help language
5 * files directly via WWW interface.
7 * Author: mudrd8mz@uxit.pedf.cuni.cz (http://moodle.cz)
8 * Based on: lang.php in 1.4.3+ release
9 * Thanks: Jaime Villate for important bug fixing, koen roggemans for his job and all moodlers
10 * for intensive testing of this my first contribution
12 require_once('../config.php');
13 require_once($CFG->libdir
.'/adminlib.php');
14 admin_externalpage_setup('langedit');
17 // Some local configuration
19 $fileeditorrows = 12; // number of textareas' rows
20 $fileeditorcols = 100; // dtto cols
21 $fileeditorinline = 1; // shall be textareas put in one row?
22 $filemissingmark = ' (***)'; // mark to add to non-existing filenames in selection form
23 $fileoldmark = ' (old?)'; // mark to add to filenames in selection form id english version is newer
24 // or to filenames with filesize() == 0
25 $filetemplate = ''; // template for new files, i.e. '$Id$';
27 $currentfile = optional_param('currentfile', 'docs/README.txt', PARAM_PATH
);
29 $strlanguage = get_string("language");
30 $strcurrentlanguage = get_string("currentlanguage");
31 $strthislanguage = get_string("thislanguage");
32 $stredithelpdocs = get_string('edithelpdocs', 'admin');
34 admin_externalpage_print_header();
36 $currentlang = current_language();
37 $langdir = "$CFG->dataroot/lang/$currentlang";
38 $enlangdir = "$CFG->dirroot/lang/en_utf8";
41 if (!file_exists($langdir)) {
42 error ('to edit this language pack, you need to put it in '.$CFG->dataroot
.'/lang');
44 // Shall I save POSTed data?
46 if (isset($_POST['currentfile'])) {
47 if (confirm_sesskey()) {
48 if (langdoc_save_file($langdir, $currentfile, $_POST['filedata'])) {
49 notify(get_string("changessaved")." ($langdir/$currentfile)", "green");
51 error("Could not save the file '$currentfile'!", "langdoc.php?currentfile=$currentfile&sesskey=$USER->sesskey");
56 error_reporting(0); // Error reporting turned off due to non-existing files
58 // Generate selection for all help and documentation files
60 // Get all files from /docs directory
62 if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/docs", "CVS")) {
63 error("Could not find English language docs files!");
68 foreach ($files as $filekey => $file) { // check all the docs files.
69 $options["docs/$file"] = "docs/$file";
70 // add mark if file doesn't exist or is empty
71 if (( !file_exists("$langdir/docs/$file")) ||
(filesize("$langdir/docs/$file") == 0)) {
72 $options["docs/$file"] .= "$filemissingmark";
74 if (filemtime("$langdir/docs/$file") < filemtime("$CFG->dirroot/lang/en_utf8/docs/$file")) {
75 $options["docs/$file"] .= "$fileoldmark";
80 // Get all files from /help directory
82 if (! $files = get_directory_list("$CFG->dirroot/lang/en_utf8/help", "CVS")) {
83 error("Could not find English language help files!");
86 foreach ($files as $filekey => $file) { // check all the help files.
87 $options["help/$file"] = "help/$file";
88 if (( !file_exists("$langdir/help/$file")) ||
(filesize("$CFG->dirroot/lang/en_utf8/help/$file") == 0)) {
89 $options["help/$file"] .= "$filemissingmark";
91 if (filemtime("$langdir/help/$file") < filemtime("$langdir/help/$file")) {
92 $options["help/$file"] .= "$fileoldmark";
97 echo "<table align=\"center\"><tr><td align=\"center\">";
98 echo popup_form ("$CFG->wwwroot/$CFG->admin/langdoc.php?sesskey=$USER->sesskey&currentfile=", $options, "choosefile", $currentfile, "", "", "", true);
99 echo "</td></tr></table>";
101 // Generate textareas
103 if (!empty($currentfile)) {
105 if (!file_exists("$langdir/$currentfile")) {
106 //check if directory exist
107 $pathparts = explode('/',$currentfile);
108 $checkpath = $langdir;
109 for ($a=0; $a < count($pathparts)-1 ; $a++
) {
110 $checkpath .= "/".$pathparts[$a];
111 if(!file_exists($checkpath)){
112 if(!mkdir($checkpath)){
113 echo ("Cannot create directory: $checkpath");
118 // file doesn't exist - let's check webserver's permission to create it
120 if (!touch("$langdir/$currentfile")) {
122 // webserver is unable to create new file
124 echo "<p align=\"center\"><font color=red>".get_string("filemissing", "", "
125 $langdir/$currentfile")."</font></p>";
129 // webserver can create new file - we can delete it now and let
130 // the langdoc_save_file() create it again if its filesize() > 0
133 unlink("$langdir/$currentfile");
135 } elseif ($f = fopen("$langdir/$currentfile","r+")) {
137 // file exists and is writeable - good for you, translator ;-)
143 // file exists but it is not writeable by web server process :-(
146 echo "<p><font size=1>".get_string("makeeditable", "", "$langdir/$currentfile")
150 //en_utf8 in dataroot is not editable
151 if ($currentlang == 'en_utf8') {
155 echo "<table align=\"center\"><tr valign=\"center\"><td align=\"center\">\n";
156 echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"\">\n";
157 echo htmlspecialchars(file_get_contents("$enlangdir/$currentfile"));
158 echo "</textarea>\n";
159 //link_to_popup_window("/lang/en_utf8/$currentfile", "popup", get_string("preview"));
160 $preview_url = langdoc_preview_url($currentfile);
162 link_to_popup_window($preview_url.'&forcelang=en_utf8', 'popup', get_string('preview'));
165 if ($fileeditorinline == 1) {
166 echo "</tr>\n<tr valign=\"center\">\n";
168 echo "<td align=\"center\">\n";
171 echo "<form id=\"$currentfile\" action=\"langdoc.php\" method=\"post\">";
172 echo '<input type="hidden" name="sesskey" value="'.$USER->sesskey
.'" />';
173 echo '<input type="hidden" name="currentfile" value="'.$currentfile.'" />';
175 echo "<textarea rows=\"$fileeditorrows\" cols=\"$fileeditorcols\" name=\"filedata\">\n";
176 if (file_exists("$langdir/$currentfile")) {
177 echo htmlspecialchars(file_get_contents("$langdir/$currentfile"));
179 echo ($filetemplate);
181 echo "</textarea>\n";
182 $preview_url = langdoc_preview_url($currentfile);
184 link_to_popup_window($preview_url, 'popup', get_string('preview'));
186 echo '<div align="center"><input type="submit" value="'.get_string('savechanges').': lang/'.$currentlang.'/'.$currentfile.'" /></div>';
190 echo "</td>\n</tr>\n</table>";
193 error_reporting($CFG->debug
);
196 admin_externalpage_print_footer();
198 //////////////////////////////////////////////////////////////////////
200 function langdoc_save_file($path, $file, $content) {
202 // $path is a full pathname to the file
203 // $file is the file to overwrite.
204 // $content are data to write
210 if (!$f = fopen("$path/$file","w")) {
211 error_reporting($CFG->debug
);
215 error_reporting($CFG->debug
);
217 $content = str_replace("\r", "",$content); // Remove linefeed characters
218 $content = preg_replace("/\n{3,}/", "\n\n", $content); // Collapse runs of blank lines
219 $content = trim($content); // Delete leading/trailing whitespace
221 fwrite($f, stripslashes($content));
225 // Remove file if its empty
227 if (filesize("$path/$file") == 0) {
228 unlink("$path/$file");
235 * Return a preview URL for the file, if available.
237 * Documentation will be moved into moodle.org wiki and current version 1.6 does not
238 * seem to be able to display local documentation. Thus, return empty URL for doc files.
239 * See lib/moodlelib.php document_file() - it still relies on old pre-UTF8 lang/ location.
241 function langdoc_preview_url($currentfile) {
242 if (substr($currentfile, 0, 5) == 'help/') {
243 $currentfile = substr($currentfile, 5);
244 $currentpathexp = explode('/', $currentfile);
245 if (count($currentpathexp) > 1) {
246 $url = '/help.php?module='.$currentpathexp[0].'&file='.$currentpathexp[1];
248 $url = '/help.php?module=moodle&file='.$currentfile;