utf8 related cleanup in glossary import and export code - please test

This commit is contained in:
skodak 2006-03-03 19:49:08 +00:00
parent dcd51df25e
commit 212039c0d8
5 changed files with 106 additions and 143 deletions

View file

@ -92,12 +92,11 @@ $string['entryusedynalink'] = 'This entry should be automatically linked';
$string['explainaddentry'] = 'Add a new entry to the current glossary.<br />Concept and definition are mandatory fields.'; $string['explainaddentry'] = 'Add a new entry to the current glossary.<br />Concept and definition are mandatory fields.';
$string['explainall'] = 'Shows ALL entries on one page'; $string['explainall'] = 'Shows ALL entries on one page';
$string['explainalphabet'] = 'Browse the glossary using this index'; $string['explainalphabet'] = 'Browse the glossary using this index';
$string['explainexport'] = 'A file has been generated.<br />Download it and keep it safe. You can import it anytime you wish in this or other course.'; $string['explainexport'] = 'Click on the button below to export glossary entries.<br />You can import it anytime you wish in this or other course.<p>Please note that attachments (e.g. images) and authors are not exported.</p>';
$string['explainimport'] = 'You must specify the file to import and define the criteria of the process.<p>Submit your request and review the results.</p>'; $string['explainimport'] = 'You must specify the file to import and define the criteria of the process.<p>Submit your request and review the results.</p>';
$string['explainspecial'] = 'Shows entries that do not begin with a letter'; $string['explainspecial'] = 'Shows entries that do not begin with a letter';
$string['exportedentry'] = 'Exported entry';
$string['exportedfile'] = 'Exported file';
$string['exportentries'] = 'Export entries'; $string['exportentries'] = 'Export entries';
$string['exportfile'] = 'Export entries to file';
$string['exportglossary'] = 'Export glossary'; $string['exportglossary'] = 'Export glossary';
$string['exporttomainglossary'] = 'Export to main glossary'; $string['exporttomainglossary'] = 'Export to main glossary';
$string['filetoimport'] = 'File to import'; $string['filetoimport'] = 'File to import';
@ -107,7 +106,6 @@ $string['fullmatch'] = 'Match whole words only';
$string['globalglossary'] = 'Global glossary'; $string['globalglossary'] = 'Global glossary';
$string['glossaryleveldefaultsettings'] = 'Glossary Level Default Settings'; $string['glossaryleveldefaultsettings'] = 'Glossary Level Default Settings';
$string['glossarytype'] = 'Glossary Type'; $string['glossarytype'] = 'Glossary Type';
$string['glosssaryexported'] = 'Glossary exported.';
$string['importcategories'] = 'Import categories'; $string['importcategories'] = 'Import categories';
$string['importedcategories'] = 'Imported categories'; $string['importedcategories'] = 'Imported categories';
$string['importedentries'] = 'Imported entries'; $string['importedentries'] = 'Imported entries';

View file

@ -2,7 +2,6 @@
require_once("../../config.php"); require_once("../../config.php");
require_once("lib.php"); require_once("lib.php");
global $CFG, $USER;
$id = required_param('id', PARAM_INT); // Course Module ID $id = required_param('id', PARAM_INT); // Course Module ID
@ -34,6 +33,7 @@
$strsearchconcept = get_string("searchconcept", "glossary"); $strsearchconcept = get_string("searchconcept", "glossary");
$strsearchindefinition = get_string("searchindefinition", "glossary"); $strsearchindefinition = get_string("searchindefinition", "glossary");
$strsearch = get_string("search"); $strsearch = get_string("search");
$strexportfile = get_string("exportfile", "glossary");
$navigation = ""; $navigation = "";
if ($course->category) { if ($course->category) {
@ -56,21 +56,20 @@
} }
/// Tabbed browsing sections /// Tabbed browsing sections
$lastl = $l;
$lastcat = $cat;
$tab = GLOSSARY_EXPORT_VIEW; $tab = GLOSSARY_EXPORT_VIEW;
include("tabs.html"); include("tabs.html");
glossary_generate_export_file($glossary,$lastl,$lastcat); ?>
print_string("glosssaryexported","glossary"); <form action="exportfile.php" method="post">
<table border="0" cellpadding="6" cellspacing="6" width="100%"><tr>
$ffurl = "/$course->id/glossary/" . clean_filename(strip_tags(format_string($glossary->name,true))) ."/glossary.xml"; <tr><td align="center">
if ($CFG->slasharguments) { <input type="submit" value="<?php p($strexportfile)?>">
$ffurl = "../../file.php$ffurl" ; </td></tr></table>
} else { <input type="hidden" name="id" value="<?php p($id)?>" />
$ffurl = "../../file.php?file=$ffurl"; <input type="hidden" name="l" value="<?php p($l)?>" />
} <input type="hidden" name="cat" value="<?php p($cat)?>" />
echo '<p align="center"><a href="' . $ffurl . '" target="_blank">' . get_string("exportedfile","glossary") . '</a></p>'; </form>
<?php
echo '</center>'; echo '</center>';
glossary_print_tabbed_table_end(); glossary_print_tabbed_table_end();

View file

@ -0,0 +1,32 @@
<?php // $Id$
require_once("../../config.php");
require_once("lib.php");
$id = required_param('id', PARAM_INT); // Course Module ID
$l = optional_param('l','', PARAM_ALPHANUM);
$cat = optional_param('cat',0, PARAM_ALPHANUM);
if (! $cm = get_record("course_modules", "id", $id)) {
error("Course Module ID was incorrect");
}
if (! $course = get_record("course", "id", $cm->course)) {
error("Course is misconfigured");
}
if (! $glossary = get_record("glossary", "id", $cm->instance)) {
error("Course module is incorrect");
}
require_login($course->id, false);
if (!isteacher($course->id)) {
error("You must be a teacher to use this page.");
}
$filename = clean_filename(strip_tags(format_string($glossary->name,true))).'.xml';
$content = glossary_generate_export_file($glossary,$l,$cat);
send_file($content, $filename, 0, 0, true, true);
?>

View file

@ -97,10 +97,10 @@
if ( $xmlglossary['NAME'][0]['#'] ) { if ( $xmlglossary['NAME'][0]['#'] ) {
unset($glossary); unset($glossary);
$glossary->name = addslashes(utf8_decode($xmlglossary['NAME'][0]['#'])); $glossary->name = addslashes($xmlglossary['NAME'][0]['#']);
$glossary->course = $course->id; $glossary->course = $course->id;
$glossary->globalglossary = addslashes($xmlglossary['GLOBALGLOSSARY'][0]['#']); $glossary->globalglossary = addslashes($xmlglossary['GLOBALGLOSSARY'][0]['#']);
$glossary->intro = addslashes(utf8_decode($xmlglossary['INTRO'][0]['#'])); $glossary->intro = addslashes($xmlglossary['INTRO'][0]['#']);
$glossary->showspecial = addslashes($xmlglossary['SHOWSPECIAL'][0]['#']); $glossary->showspecial = addslashes($xmlglossary['SHOWSPECIAL'][0]['#']);
$glossary->showalphabet = addslashes($xmlglossary['SHOWALPHABET'][0]['#']); $glossary->showalphabet = addslashes($xmlglossary['SHOWALPHABET'][0]['#']);
$glossary->showall = addslashes($xmlglossary['SHOWALL'][0]['#']); $glossary->showall = addslashes($xmlglossary['SHOWALL'][0]['#']);
@ -210,8 +210,8 @@
// Inserting the entries // Inserting the entries
$xmlentry = $xmlentries[$i]; $xmlentry = $xmlentries[$i];
unset($newentry); unset($newentry);
$newentry->concept = addslashes(trim(utf8_decode($xmlentry['#']['CONCEPT'][0]['#']))); $newentry->concept = trim(addslashes($xmlentry['#']['CONCEPT'][0]['#']));
$newentry->definition = addslashes(utf8_decode($xmlentry['#']['DEFINITION'][0]['#'])); $newentry->definition = addslashes($xmlentry['#']['DEFINITION'][0]['#']);
if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) { if ( isset($xmlentry['#']['CASESENSITIVE'][0]['#']) ) {
$newentry->casesensitive = addslashes($xmlentry['#']['CASESENSITIVE'][0]['#']); $newentry->casesensitive = addslashes($xmlentry['#']['CASESENSITIVE'][0]['#']);
} else { } else {
@ -272,7 +272,7 @@
if (!empty($aliasname)) { if (!empty($aliasname)) {
unset($newalias); unset($newalias);
$newalias->entryid = $newentry->id; $newalias->entryid = $newentry->id;
$newalias->alias = addslashes(trim(utf8_decode($aliasname))); $newalias->alias = trim(addslashes($aliasname));
$newalias->id = insert_record("glossary_alias",$newalias); $newalias->id = insert_record("glossary_alias",$newalias);
} }
} }
@ -284,7 +284,7 @@
$xmlcat = $xmlcats[$k]; $xmlcat = $xmlcats[$k];
unset($newcat); unset($newcat);
$newcat->name = addslashes(utf8_decode($xmlcat['#']['NAME'][0]['#'])); $newcat->name = addslashes($xmlcat['#']['NAME'][0]['#']);
$newcat->usedynalink = addslashes($xmlcat['#']['USEDYNALINK'][0]['#']); $newcat->usedynalink = addslashes($xmlcat['#']['USEDYNALINK'][0]['#']);
if ( !$category = get_record("glossary_categories","glossaryid",$glossary->id,"name",$newcat->name) ) { if ( !$category = get_record("glossary_categories","glossaryid",$glossary->id,"name",$newcat->name) ) {
// Create the category if it does not exist // Create the category if it does not exist

View file

@ -1627,29 +1627,27 @@ function glossary_print_dynaentry($courseid, $entries, $displayformat = -1) {
function glossary_generate_export_file($glossary, $hook = "", $hook = 0) { function glossary_generate_export_file($glossary, $hook = "", $hook = 0) {
global $CFG; global $CFG;
glossary_check_moddata_dir($glossary);
if (!$h = glossary_open_xml($glossary)) { $co = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
error("An error occurred while opening a file to write to.");
}
$status = fwrite ($h,glossary_start_tag("INFO",1,true)); $co .= glossary_start_tag("GLOSSARY",0,true);
fwrite ($h,glossary_full_tag("NAME",2,false,$glossary->name)); $co .= glossary_start_tag("INFO",1,true);
fwrite ($h,glossary_full_tag("INTRO",2,false,$glossary->intro)); $co .= glossary_full_tag("NAME",2,false,$glossary->name);
fwrite ($h,glossary_full_tag("STUDENTCANPOST",2,false,$glossary->studentcanpost)); $co .= glossary_full_tag("INTRO",2,false,$glossary->intro);
fwrite ($h,glossary_full_tag("ALLOWDUPLICATEDENTRIES",2,false,$glossary->allowduplicatedentries)); $co .= glossary_full_tag("STUDENTCANPOST",2,false,$glossary->studentcanpost);
fwrite ($h,glossary_full_tag("DISPLAYFORMAT",2,false,$glossary->displayformat)); $co .= glossary_full_tag("ALLOWDUPLICATEDENTRIES",2,false,$glossary->allowduplicatedentries);
fwrite ($h,glossary_full_tag("SHOWSPECIAL",2,false,$glossary->showspecial)); $co .= glossary_full_tag("DISPLAYFORMAT",2,false,$glossary->displayformat);
fwrite ($h,glossary_full_tag("SHOWALPHABET",2,false,$glossary->showalphabet)); $co .= glossary_full_tag("SHOWSPECIAL",2,false,$glossary->showspecial);
fwrite ($h,glossary_full_tag("SHOWALL",2,false,$glossary->showall)); $co .= glossary_full_tag("SHOWALPHABET",2,false,$glossary->showalphabet);
fwrite ($h,glossary_full_tag("ALLOWCOMMENTS",2,false,$glossary->allowcomments)); $co .= glossary_full_tag("SHOWALL",2,false,$glossary->showall);
fwrite ($h,glossary_full_tag("USEDYNALINK",2,false,$glossary->usedynalink)); $co .= glossary_full_tag("ALLOWCOMMENTS",2,false,$glossary->allowcomments);
fwrite ($h,glossary_full_tag("DEFAULTAPPROVAL",2,false,$glossary->defaultapproval)); $co .= glossary_full_tag("USEDYNALINK",2,false,$glossary->usedynalink);
fwrite ($h,glossary_full_tag("GLOBALGLOSSARY",2,false,$glossary->globalglossary)); $co .= glossary_full_tag("DEFAULTAPPROVAL",2,false,$glossary->defaultapproval);
fwrite ($h,glossary_full_tag("ENTBYPAGE",2,false,$glossary->entbypage)); $co .= glossary_full_tag("GLOBALGLOSSARY",2,false,$glossary->globalglossary);
$co .= glossary_full_tag("ENTBYPAGE",2,false,$glossary->entbypage);
if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) { if ( $entries = get_records("glossary_entries","glossaryid",$glossary->id) ) {
$status = fwrite ($h,glossary_start_tag("ENTRIES",2,true)); $co .= glossary_start_tag("ENTRIES",2,true);
foreach ($entries as $entry) { foreach ($entries as $entry) {
$permissiongranted = 1; $permissiongranted = 1;
if ( $hook ) { if ( $hook ) {
@ -1675,81 +1673,54 @@ global $CFG;
} }
} }
if ( $entry->approved and $permissiongranted ) { if ( $entry->approved and $permissiongranted ) {
$status = fwrite($h,glossary_start_tag("ENTRY",3,true)); $co .= glossary_start_tag("ENTRY",3,true);
fwrite($h,glossary_full_tag("CONCEPT",4,false,trim($entry->concept))); $co .= glossary_full_tag("CONCEPT",4,false,trim($entry->concept));
fwrite($h,glossary_full_tag("DEFINITION",4,false,$entry->definition)); $co .= glossary_full_tag("DEFINITION",4,false,$entry->definition);
fwrite($h,glossary_full_tag("FORMAT",4,false,$entry->format)); $co .= glossary_full_tag("FORMAT",4,false,$entry->format);
fwrite($h,glossary_full_tag("USEDYNALINK",4,false,$entry->usedynalink)); $co .= glossary_full_tag("USEDYNALINK",4,false,$entry->usedynalink);
fwrite($h,glossary_full_tag("CASESENSITIVE",4,false,$entry->casesensitive)); $co .= glossary_full_tag("CASESENSITIVE",4,false,$entry->casesensitive);
fwrite($h,glossary_full_tag("FULLMATCH",4,false,$entry->fullmatch)); $co .= glossary_full_tag("FULLMATCH",4,false,$entry->fullmatch);
fwrite($h,glossary_full_tag("TEACHERENTRY",4,false,$entry->teacherentry)); $co .= glossary_full_tag("TEACHERENTRY",4,false,$entry->teacherentry);
if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) { if ( $aliases = get_records("glossary_alias","entryid",$entry->id) ) {
$status = fwrite ($h,glossary_start_tag("ALIASES",4,true)); $co .= glossary_start_tag("ALIASES",4,true);
foreach ($aliases as $alias) { foreach ($aliases as $alias) {
$status = fwrite ($h,glossary_start_tag("ALIAS",5,true)); $co .= glossary_start_tag("ALIAS",5,true);
fwrite($h,glossary_full_tag("NAME",6,false,trim($alias->alias))); $co .= glossary_full_tag("NAME",6,false,trim($alias->alias));
$status = fwrite($h,glossary_end_tag("ALIAS",5,true)); $co .= glossary_end_tag("ALIAS",5,true);
} }
$status = fwrite($h,glossary_end_tag("ALIASES",4,true)); $co .= glossary_end_tag("ALIASES",4,true);
} }
if ( $catentries = get_records("glossary_entries_categories","entryid",$entry->id) ) { if ( $catentries = get_records("glossary_entries_categories","entryid",$entry->id) ) {
$status = fwrite ($h,glossary_start_tag("CATEGORIES",4,true)); $co .= glossary_start_tag("CATEGORIES",4,true);
foreach ($catentries as $catentry) { foreach ($catentries as $catentry) {
$category = get_record("glossary_categories","id",$catentry->categoryid); $category = get_record("glossary_categories","id",$catentry->categoryid);
$status = fwrite ($h,glossary_start_tag("CATEGORY",5,true)); $co .= glossary_start_tag("CATEGORY",5,true);
fwrite($h,glossary_full_tag("NAME",6,false,$category->name)); $co .= glossary_full_tag("NAME",6,false,$category->name);
fwrite($h,glossary_full_tag("USEDYNALINK",6,false,$category->usedynalink)); $co .= glossary_full_tag("USEDYNALINK",6,false,$category->usedynalink);
$status = fwrite($h,glossary_end_tag("CATEGORY",5,true)); $co .= glossary_end_tag("CATEGORY",5,true);
} }
$status = fwrite($h,glossary_end_tag("CATEGORIES",4,true)); $co .= glossary_end_tag("CATEGORIES",4,true);
} }
$status =fwrite($h,glossary_end_tag("ENTRY",3,true)); $co .= glossary_end_tag("ENTRY",3,true);
} }
} }
$status =fwrite ($h,glossary_end_tag("ENTRIES",2,true)); $co .= glossary_end_tag("ENTRIES",2,true);
} }
$status =fwrite ($h,glossary_end_tag("INFO",1,true)); $co .= glossary_end_tag("INFO",1,true);
$co .= glossary_end_tag("GLOSSARY",0,true);
$h = glossary_close_xml($h); return $co;
}
// Functions designed by Eloy Lafuente
//
//Function to create, open and write header of the xml file
function glossary_open_xml($glossary) {
global $CFG;
$status = true;
//Open for writing
$glossaryname = clean_filename(strip_tags(format_string($glossary->name,true)));
$pathname = make_upload_directory("$glossary->course/glossary/$glossaryname");
$filename = "$pathname/glossary.xml";
if (!$h = fopen($filename,"w")) {
notify("Error opening '$filename'");
return false;
}
//Writes the header
$status = fwrite ($h,"<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
if ($status) {
$status = fwrite ($h,glossary_start_tag("GLOSSARY",0,true));
}
if ($status) {
return $h;
} else {
return false;
}
} }
/// Functions designed by Eloy Lafuente
/// Functions to create, open and write header of the xml file
// Read import file and convert to current charset
function glossary_read_imported_file($file) { function glossary_read_imported_file($file) {
require_once "../../lib/xmlize.php"; require_once "../../lib/xmlize.php";
$h = fopen($file,"r"); $h = fopen($file,"r");
@ -1761,12 +1732,11 @@ require_once "../../lib/xmlize.php";
} }
fclose($h); fclose($h);
} }
return xmlize($line, 0); if (current_charset() != 'UTF-8') {
$textlib = textlib_get_instance();
$line = $textlib->convert($line, 'UTF-8', current_charset());
} }
//Close the file return xmlize($line, 0);
function glossary_close_xml($h) {
$status = fwrite ($h,glossary_end_tag("GLOSSARY",0,true));
return fclose($h);
} }
//Return the xml start tag //Return the xml start tag
@ -1790,53 +1760,17 @@ function glossary_end_tag($tag,$level=0,$endline=true) {
} }
//Return the start tag, the contents and the end tag //Return the start tag, the contents and the end tag
function glossary_full_tag($tag,$level=0,$endline=true,$content,$to_utf=true) { function glossary_full_tag($tag,$level=0,$endline=true,$content) {
$st = glossary_start_tag($tag,$level,$endline); $st = glossary_start_tag($tag,$level,$endline);
$co=""; if (current_charset() != 'UFT-8') {
if ($to_utf) { $textlib = textlib_get_instance();
$co = preg_replace("/\r\n|\r/", "\n", utf8_encode(htmlspecialchars($content))); $content = $textlib->convert($content, current_charset(), 'UTF-8');
} else {
$co = preg_replace("/\r\n|\r/", "\n", htmlspecialchars($content));
} }
$co = preg_replace("/\r\n|\r/", "\n", s($content));
$et = glossary_end_tag($tag,0,true); $et = glossary_end_tag($tag,0,true);
return $st.$co.$et; return $st.$co.$et;
} }
//Function to check and create the needed moddata dir to
//save all the mod backup files. We always name it moddata
//to be able to restore it, but in restore we check for
//$CFG->moddata !!
function glossary_check_moddata_dir($glossary) {
global $CFG;
$status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course",true);
if ( $status ) {
$status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary",true);
if ( $status ) {
$status = glossary_check_dir_exists($CFG->dataroot."/$glossary->course/glossary/". clean_filename(strip_tags(format_string($glossary->name,true))),true);
}
}
return $status;
}
//Function to check if a directory exists
//and, optionally, create it
function glossary_check_dir_exists($dir,$create=false) {
global $CFG;
$status = true;
if(!is_dir($dir)) {
if (!$create) {
$status = false;
} else {
umask(0000);
$status = mkdir ($dir,$CFG->directorypermissions);
}
}
return $status;
}
/* /*
* Adding grading functions * Adding grading functions
*/ */