Merge pull request #2523 from aynsix/PHRAS-1316-port-1205-double-extension

PHRAS-1316 port-1205-double-extension-issue
This commit is contained in:
Nicolas Maillat
2018-03-19 14:27:43 +01:00
committed by GitHub
3 changed files with 229 additions and 227 deletions

View File

@@ -386,15 +386,16 @@ class set_export extends set_abstract
/** /**
* @param User $user * @param User $user
* @param Filesystem $filesystem * @param Filesystem $filesystem
* @param array $subdefs * @param array $wantedSubdefs
* @param bool $rename_title * @param bool $rename_title
* @param bool $includeBusinessFields * @param bool $includeBusinessFields
* @param $stampChoice
* @return array * @return array
* @throws Exception * @throws Exception
*/ */
public function prepare_export(User $user, Filesystem $filesystem, array $subdefs, $rename_title, $includeBusinessFields) public function prepare_export(User $user, Filesystem $filesystem, Array $wantedSubdefs, $rename_title, $includeBusinessFields, $stampChoice = null)
{ {
if (!is_array($subdefs)) { if (!is_array($wantedSubdefs)) {
throw new Exception('No subdefs given'); throw new Exception('No subdefs given');
} }
@@ -403,18 +404,19 @@ class set_export extends set_abstract
$n_files = 0; $n_files = 0;
$file_names = []; $file_names = [];
$size = 0; $size = 0;
$unicode = new \unicode(); $unicode = $this->app['unicode'];
/** @var record_exportElement $download_element */ /** @var record_exportElement $download_element */
foreach ($this->get_elements() as $download_element) { foreach ($this->elements as $download_element) {
$id = count($files); $id = count($files);
$files[$id] = [ $files[$id] = [
'base_id' => $download_element->getBaseId(), 'base_id' => $download_element->getBaseId(),
'record_id' => $download_element->getRecordId(), 'record_id' => $download_element->getRecordId(),
'original_name' => '', 'original_name' => '',
'export_name' => '', 'export_name' => '',
'subdefs' => [], 'subdefs' => [],
]; ];
$BF = false; $BF = false;
@@ -423,261 +425,238 @@ class set_export extends set_abstract
$BF = true; $BF = true;
} }
$desc = $this->app['serializer.caption']->serialize($download_element->get_caption(), CaptionSerializer::SERIALIZE_XML, $BF); // $original_name : the original_name WITHOUT extension (ex. "DSC_1234")
// $extension : the extension WITHOUT DOT (ex. "jpg")
// $export_name : the export name WITHOUT extension, (ex. "Hollydays_2016_DSC_1234")
$files[$id]['original_name'] = //
$files[$id]['export_name'] = // build the original_name
$download_element->get_original_name(false); //
$original_name = trim($download_element->get_original_name(false)); // false: don't remove extension
$files[$id]['original_name'] = if($original_name !== '') {
trim($files[$id]['original_name']) != '' ? $infos = pathinfo($original_name); // pathinfo DOES USE the last '.' to find extension
$files[$id]['original_name'] : $id; $original_name = $infos['filename'];
$extension = isset($infos['extension']) ? $infos['extension'] : '';
$infos = pathinfo($files[$id]['original_name']); }
else {
$extension = isset($infos['extension']) ? $infos['extension'] : $extension = '';
substr($files[$id]['original_name'], 0 - strrpos($files[$id]['original_name'], '.')); }
if($original_name == '') {
$original_name = (string)$id;
}
$files[$id]['original_name'] = $original_name;
//
// build the export_name
//
if ($rename_title) { if ($rename_title) {
$title = strip_tags($download_element->get_title(['removeExtension' => true])); // use the title (may be a concat of fields)
$files[$id]['export_name'] = $unicode->remove_nonazAZ09($title, true, true, true); $export_name = strip_tags($download_element->get_title(['removeExtension' => true]));
// if the "title" ends up with a "filename-like" field, remove extension
if (strtolower(substr($export_name, -strlen($extension)-1)) === '.'.strtolower($extension)) {
$export_name = substr($export_name, 0, strlen($export_name)-1-strlen($extension));
}
} else { } else {
$files[$id]["export_name"] = $infos['filename']; $export_name = $original_name;
} }
if (substr(strrev($files[$id]['export_name']), 0, strlen($extension)) != strrev($extension)) { // cleanup the exportname so it can be used as a filename (even if it came from the originale_name)
$files[$id]['export_name'] .= '.' . $extension; $export_name = str_replace([' ', "\t", "\r", "\n"], '_', $export_name);
$export_name = $unicode->remove_nonazAZ09($export_name, true, true, true); // keep '_', '-', '.'
// really no luck if nothing left
if($export_name == '') {
$export_name = (string)$id;
} }
$sizeMaxAjout = 0; //
$sizeMaxExt = 0; // loop on subdefs to be downloaded (this may change the export_name)
//
$sizeMaxAjout = $sizeMaxExt = 0;
$sd = $download_element->get_subdefs();
foreach ($download_element->get_downloadable() as $name => $properties) { foreach ($download_element->get_downloadable() as $subdefName => $properties) {
if ($properties === false || !in_array($name, $subdefs)) { if ($properties === false || !in_array($subdefName, $wantedSubdefs)) {
continue;
}
if (!in_array($subdefName, ['caption', 'caption-yaml']) && !isset($sd[$subdefName])) {
continue; continue;
} }
$subdef = null;
if (!in_array($name, ['caption', 'caption-yaml'])) {
try {
// get_subdef() can throw a 404
$subdef = $download_element->get_subdef($name);
}
catch(\Exception $e) {
continue;
}
}
set_time_limit(0); set_time_limit(0);
$subdef_export = $subdef_alive = false; $subdef_ok = false;
$n_files++; $n_files++;
$tmp_pathfile = ['path' => null, 'file' => null]; $tmp_pathfile = [
'path' => null,
'file' => null
];
switch ($properties['class']) { switch ($properties['class']) {
case 'caption': case 'caption':
case 'caption-yaml': case 'caption-yaml':
$subdef_export = true; $subdef_ok = true;
$subdef_alive = true;
break; break;
case 'thumbnail': case 'thumbnail':
$subdef_ok = true;
$tmp_pathfile = [ $tmp_pathfile = [
'path' => $subdef->get_path(), 'path' => $sd[$subdefName]->get_path(),
'file' => $subdef->get_file(), 'file' => $sd[$subdefName]->get_file()
]; ];
$subdef_export = true;
$subdef_alive = true;
break; break;
case 'document': case 'document':
$subdef_export = true; $subdef_ok = true;
$path = \recordutils_image::stamp($this->app, $subdef);
$tmp_pathfile = [ $tmp_pathfile = [
'path' => $subdef->get_path(), 'path' => $sd[$subdefName]->get_path(),
'file' => $subdef->get_file(), 'file' => $sd[$subdefName]->get_file()
]; ];
if (file_exists($path)) {
$tmp_pathfile = [
'path' => dirname($path),
'file' => basename($path),
];
$subdef_alive = true;
}
break;
case 'preview': if($this->app['conf']->get(['registry', 'actions', 'export-stamp-choice']) !== true || is_null($stampChoice) ){
$subdef_export = true; $path = \recordutils_image::stamp($this->app , $sd[$subdefName]);
$tmp_pathfile = [
'path' => $subdef->get_path(),
'file' => $subdef->get_file(),
];
if (!$this->app->getAclForUser($user)->has_right_on_base($download_element->getBaseId(), \ACL::NOWATERMARK)
&& !$this->app->getAclForUser($user)->has_preview_grant($download_element)
&& $subdef->get_type() == media_subdef::TYPE_IMAGE
) {
$path = recordutils_image::watermark($this->app, $subdef);
if (file_exists($path)) { if (file_exists($path)) {
$tmp_pathfile = [ $tmp_pathfile = [
'path' => dirname($path), 'path' => dirname($path),
'file' => basename($path), 'file' => basename($path)
]; ];
$subdef_alive = true;
} }
} else {
$subdef_alive = true;
} }
break;
case 'preview':
$tmp_pathfile = [
'path' => $sd[$subdefName]->get_path(),
'file' => $sd[$subdefName]->get_file()
];
if (!$this->app->getAclForUser($user)->has_right_on_base($download_element->getBaseId(), \ACL::NOWATERMARK)
&& !$this->app->getAclForUser($user)->has_preview_grant($download_element)
&& $sd[$subdefName]->get_type() == media_subdef::TYPE_IMAGE )
{
$path = recordutils_image::watermark($this->app, $sd[$subdefName]);
if (file_exists($path)) {
$tmp_pathfile = [
'path' => dirname($path),
'file' => basename($path)
];
$subdef_ok = true;
}
}
else {
$subdef_ok = true;
}
break; break;
} }
if ($subdef_export === true && $subdef_alive === true) { if ($subdef_ok) {
switch ($properties['class']) { switch ($properties['class']) {
case 'caption': case 'caption':
if ($name == 'caption-yaml') { $ajout = '_caption';
$suffix = '_captionyaml'; if ($subdefName == 'caption-yaml') {
$extension = 'yml'; $ext = 'txt';
$mime = 'text/x-yaml'; $mime = 'text/plain';
} else { } else {
$suffix = '_caption'; $ext = 'xml';
$extension = 'xml';
$mime = 'text/xml'; $mime = 'text/xml';
} }
$files[$id]["subdefs"][$name]["ajout"] = $suffix; $files[$id]["subdefs"][$subdefName]["ajout"] = $ajout;
$files[$id]["subdefs"][$name]["exportExt"] = $extension; $files[$id]["subdefs"][$subdefName]["exportExt"] = $ext;
$files[$id]["subdefs"][$name]["label"] = $properties['label']; $files[$id]["subdefs"][$subdefName]["label"] = $properties['label'];
$files[$id]["subdefs"][$name]["path"] = null; $files[$id]["subdefs"][$subdefName]["path"] = null;
$files[$id]["subdefs"][$name]["file"] = null; $files[$id]["subdefs"][$subdefName]["file"] = null;
$files[$id]["subdefs"][$name]["size"] = 0; $files[$id]["subdefs"][$subdefName]["size"] = 0;
$files[$id]["subdefs"][$name]["folder"] = $download_element->get_directory(); $files[$id]["subdefs"][$subdefName]["mime"] = $mime;
$files[$id]["subdefs"][$name]["mime"] = $mime; $files[$id]["subdefs"][$subdefName]["folder"] = $download_element->get_directory();
break; break;
case 'document': case 'document':
case 'preview': case 'preview':
case 'thumbnail': case 'thumbnail':
$infos = pathinfo(p4string::addEndSlash($tmp_pathfile["path"]) . $ajout = $subdefName == 'document' ? '' : ("_" . $subdefName);
$tmp_pathfile["file"]); $infos = pathinfo(p4string::addEndSlash($tmp_pathfile["path"]) . $tmp_pathfile["file"]);
$ext = isset($infos['extension']) ? $infos['extension'] : '';
$files[$id]["subdefs"][$name]["ajout"] = $name == 'document' ? '' : "_" . $name; $files[$id]["subdefs"][$subdefName]["ajout"] = $ajout;
$files[$id]["subdefs"][$name]["path"] = $tmp_pathfile["path"]; $files[$id]["subdefs"][$subdefName]["exportExt"] = $ext;
$files[$id]["subdefs"][$name]["file"] = $tmp_pathfile["file"]; $files[$id]["subdefs"][$subdefName]["label"] = $properties['label'];
$files[$id]["subdefs"][$name]["label"] = $properties['label']; $files[$id]["subdefs"][$subdefName]["path"] = $tmp_pathfile["path"];
$files[$id]["subdefs"][$name]["size"] = $subdef->get_size(); $files[$id]["subdefs"][$subdefName]["file"] = $tmp_pathfile["file"];
$files[$id]["subdefs"][$name]["mime"] = $subdef->get_mime(); $files[$id]["subdefs"][$subdefName]["size"] = $sd[$subdefName]->get_size();
$files[$id]["subdefs"][$name]["folder"] = $download_element->get_directory(); $files[$id]["subdefs"][$subdefName]["mime"] = $sd[$subdefName]->get_mime();
$files[$id]["subdefs"][$name]["exportExt"] = isset($infos['extension']) ? $infos['extension'] : ''; $files[$id]["subdefs"][$subdefName]["folder"] = $download_element->get_directory();
$size += $subdef->get_size(); $size += $sd[$subdefName]->get_size();
break;
default: // should no happen
$ajout = $ext = '';
break; break;
} }
$longueurAjoutCourant = $sizeMaxAjout = max($sizeMaxAjout, mb_strlen($ajout));
mb_strlen($files[$id]["subdefs"][$name]["ajout"]); $sizeMaxExt = max($sizeMaxExt, mb_strlen($ext));
$sizeMaxAjout = max($longueurAjoutCourant, $sizeMaxAjout);
$longueurExtCourant =
mb_strlen($files[$id]["subdefs"][$name]["exportExt"]);
$sizeMaxExt = max($longueurExtCourant, $sizeMaxExt);
} }
} }
$max_length = self::$maxFilenameLength - 1 - $sizeMaxExt - $sizeMaxAjout; // end loop on downloadable subdefs
$name = $files[$id]["export_name"]; // check that no exportNames are double, else add a number
// also truncate exportName so the whole filename will not be too long
$start_length = mb_strlen($name); // "aTooLongName_caption.xml" --> "aTooLo_caption-2.xml"
if ($start_length > $max_length)
$name = mb_substr($name, 0, $max_length);
$n = 1; $n = 1;
while (in_array(mb_strtolower($name), $file_names)) { do {
$nSuffix = ($n==1) ? '' : ('#'.$n);
$maxExportNameLength = self::$maxFilenameLength - $sizeMaxAjout - strlen($nSuffix) - 1 - $sizeMaxExt;
$newExportName = mb_substr($export_name, 0, $maxExportNameLength).$nSuffix;
$kName = mb_strtolower($newExportName);
$n++; $n++;
$suffix = "-" . $n; // pour diese si besoin
$max_length = self::$maxFilenameLength - 1 - $sizeMaxExt - $sizeMaxAjout - mb_strlen($suffix);
$name = mb_strtolower($files[$id]["export_name"]);
if ($start_length > $max_length)
$name = mb_substr($name, 0, $max_length) . $suffix;
else
$name = $name . $suffix;
} }
$file_names[] = mb_strtolower($name); while(in_array($kName, $file_names));
$files[$id]["export_name"] = $name;
$files[$id]["export_name"] = $unicode->remove_nonazAZ09($files[$id]["export_name"], true, true, true); // here we have a unique exportName
$files[$id]["original_name"] = $unicode->remove_nonazAZ09($files[$id]["original_name"], true, true, true); $file_names[] = $kName;
$i = 0; $files[$id]["export_name"] = $newExportName;
$name = utf8_decode($files[$id]["export_name"]);
$tmp_name = "";
$good_keys = [
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '-', '_', '.', '#',
];
while (isset($name[$i])) { //
if (!in_array(mb_strtolower($name[$i]), $good_keys)) // add files for caption and/or caption-yaml ?
$tmp_name .= '_'; //
else $caption_dir = null;
$tmp_name .= $name[$i]; foreach(['caption'=>CaptionSerializer::SERIALIZE_XML, 'caption-yaml'=>CaptionSerializer::SERIALIZE_YAML]
as $subdefName => $serializeMethod)
{
if (in_array($subdefName, $wantedSubdefs)) {
if (!$caption_dir) {
// do this only once
$caption_dir = $this->app['tmp.caption.path'] . '/' . time() . $this->app->getAuthenticatedUser()->getId() . '/';
$filesystem->mkdir($caption_dir, 0750);
}
$tmp_name = str_replace('__', '_', $tmp_name); $file = $files[$id]["export_name"]
. $files[$id]["subdefs"][$subdefName]["ajout"] . '.'
. $files[$id]["subdefs"][$subdefName]["exportExt"];
$i++; $desc = $this->app['serializer.caption']->serialize($download_element->get_caption(), $serializeMethod, $BF);
} file_put_contents($caption_dir . $file, $desc);
$files[$id]["export_name"] = $tmp_name;
if (in_array('caption', $subdefs)) { $files[$id]["subdefs"][$subdefName]["path"] = $caption_dir;
$caption_dir = $this->app['tmp.caption.path'] . '/' . time() . $this->app->getAuthenticatedUser()->getId() . '/'; $files[$id]["subdefs"][$subdefName]["file"] = $file;
$files[$id]["subdefs"][$subdefName]["size"] = filesize($caption_dir . $file);
$filesystem->mkdir($caption_dir, 0750); $files[$id]["subdefs"][$subdefName]['businessfields'] = $BF ? '1' : '0';
}
$desc = $this->app['serializer.caption']->serialize($download_element->get_caption(), CaptionSerializer::SERIALIZE_XML, $BF);
$file = $files[$id]["export_name"]
. $files[$id]["subdefs"]['caption']["ajout"] . '.'
. $files[$id]["subdefs"]['caption']["exportExt"];
$path = $caption_dir;
file_put_contents($path . $file, $desc);
$files[$id]["subdefs"]['caption']["path"] = $path;
$files[$id]["subdefs"]['caption']["file"] = $file;
$files[$id]["subdefs"]['caption']["size"] = filesize($path . $file);
$files[$id]["subdefs"]['caption']['businessfields'] = $BF ? '1' : '0';
}
if (in_array('caption-yaml', $subdefs)) {
$caption_dir = $this->app['tmp.caption.path'] . '/' . time() . $this->app->getAuthenticatedUser()->getId() . '/';
$filesystem->mkdir($caption_dir, 0750);
$desc = $this->app['serializer.caption']->serialize($download_element->get_caption(), CaptionSerializer::SERIALIZE_YAML, $BF);
$file = $files[$id]["export_name"]
. $files[$id]["subdefs"]['caption-yaml']["ajout"] . '.'
. $files[$id]["subdefs"]['caption-yaml']["exportExt"];
$path = $caption_dir;
file_put_contents($path . $file, $desc);
$files[$id]["subdefs"]['caption-yaml']["path"] = $path;
$files[$id]["subdefs"]['caption-yaml']["file"] = $file;
$files[$id]["subdefs"]['caption-yaml']["size"] = filesize($path . $file);
$files[$id]["subdefs"]['caption-yaml']['businessfields'] = $BF ? '1' : '0';
} }
} }
$this->list = [ $this->list = [
'files' => $files, 'files' => $files,
'names' => $file_names, 'names' => $file_names,
'size' => $size, 'size' => $size,
'count' => $n_files, 'count' => $n_files,
]; ];

View File

@@ -15,8 +15,30 @@ class unicode
const CONVERT_TO_ND = 'nd'; // no-diacritics const CONVERT_TO_ND = 'nd'; // no-diacritics
const CONVERT_TO_LCND = 'lcnd'; // lowercase no-diacritics const CONVERT_TO_LCND = 'lcnd'; // lowercase no-diacritics
protected static $map = [ private $maps = [];
self::CONVERT_TO_LC => [
private function &getMap($mapId)
{
if(array_key_exists($mapId, $this->maps)) {
return $this->maps[$mapId];
}
switch($mapId) {
case self::CONVERT_TO_LC:
$this->setMap_LC();
return $this->maps[self::CONVERT_TO_LC];
case self::CONVERT_TO_ND:
$this->setMap_ND();
return $this->maps[self::CONVERT_TO_ND];
case self::CONVERT_TO_LCND:
$this->setMap_LCND();
return $this->maps[self::CONVERT_TO_LCND];
}
return [];
}
private function setMap_LC()
{
$this->maps[self::CONVERT_TO_LC] = [
"\x41" => "\x61" , /* U+0041: LATIN CAPITAL LETTER A -> U+0061: LATIN SMALL LETTER A */ "\x41" => "\x61" , /* U+0041: LATIN CAPITAL LETTER A -> U+0061: LATIN SMALL LETTER A */
"\x42" => "\x62" , /* U+0042: LATIN CAPITAL LETTER B -> U+0062: LATIN SMALL LETTER B */ "\x42" => "\x62" , /* U+0042: LATIN CAPITAL LETTER B -> U+0062: LATIN SMALL LETTER B */
"\x43" => "\x63" , /* U+0043: LATIN CAPITAL LETTER C -> U+0063: LATIN SMALL LETTER C */ "\x43" => "\x63" , /* U+0043: LATIN CAPITAL LETTER C -> U+0063: LATIN SMALL LETTER C */
@@ -481,9 +503,13 @@ class unicode
"\xD5\x94" => "\xD6\x84" , /* U+0554: ARMENIAN CAPITAL LETTER KEH -> U+0584: ARMENIAN SMALL LETTER KEH */ "\xD5\x94" => "\xD6\x84" , /* U+0554: ARMENIAN CAPITAL LETTER KEH -> U+0584: ARMENIAN SMALL LETTER KEH */
"\xD5\x95" => "\xD6\x85" , /* U+0555: ARMENIAN CAPITAL LETTER OH -> U+0585: ARMENIAN SMALL LETTER OH */ "\xD5\x95" => "\xD6\x85" , /* U+0555: ARMENIAN CAPITAL LETTER OH -> U+0585: ARMENIAN SMALL LETTER OH */
"\xD5\x96" => "\xD6\x86" /* U+0556: ARMENIAN CAPITAL LETTER FEH -> U+0586: ARMENIAN SMALL LETTER FEH */ "\xD5\x96" => "\xD6\x86" /* U+0556: ARMENIAN CAPITAL LETTER FEH -> U+0586: ARMENIAN SMALL LETTER FEH */
],
self::CONVERT_TO_ND => [ ];
}
private function setMap_ND()
{
$this->maps[self::CONVERT_TO_ND] = [
"\xC2\xA0" => "\x20" , /* U+00A0: NO-BREAK SPACE -> U+0020: SPACE */ "\xC2\xA0" => "\x20" , /* U+00A0: NO-BREAK SPACE -> U+0020: SPACE */
"\xC2\xA8" => "\x20" , /* U+00A8: DIAERESIS -> U+0020: SPACE */ "\xC2\xA8" => "\x20" , /* U+00A8: DIAERESIS -> U+0020: SPACE */
"\xC2\xAA" => "\x61" , /* U+00AA: FEMININE ORDINAL INDICATOR -> U+0061: LATIN SMALL LETTER A */ "\xC2\xAA" => "\x61" , /* U+00AA: FEMININE ORDINAL INDICATOR -> U+0061: LATIN SMALL LETTER A */
@@ -859,9 +885,13 @@ class unicode
"\xD3\xB5" => "\xD1\x87", /* U+04F5: CYRILLIC SMALL LETTER CHE WITH DIAERESIS -> U+0447: CYRILLIC SMALL LETTER CHE */ "\xD3\xB5" => "\xD1\x87", /* U+04F5: CYRILLIC SMALL LETTER CHE WITH DIAERESIS -> U+0447: CYRILLIC SMALL LETTER CHE */
"\xD3\xB8" => "\xD0\xAB", /* U+04F8: CYRILLIC CAPITAL LETTER YERU WITH DIAERESIS -> U+042B: CYRILLIC CAPITAL LETTER YERU */ "\xD3\xB8" => "\xD0\xAB", /* U+04F8: CYRILLIC CAPITAL LETTER YERU WITH DIAERESIS -> U+042B: CYRILLIC CAPITAL LETTER YERU */
"\xD3\xB9" => "\xD1\x8B" /* U+04F9: CYRILLIC SMALL LETTER YERU WITH DIAERESIS -> U+044B: CYRILLIC SMALL LETTER YERU */ "\xD3\xB9" => "\xD1\x8B" /* U+04F9: CYRILLIC SMALL LETTER YERU WITH DIAERESIS -> U+044B: CYRILLIC SMALL LETTER YERU */
],
self::CONVERT_TO_LCND => [ ];
}
private function setMap_LCND()
{
$this->maps[self::CONVERT_TO_LCND] = [
"\x41" => "\x61" , /* U+0041: LATIN CAPITAL LETTER A -> U+0061: LATIN SMALL LETTER A */ "\x41" => "\x61" , /* U+0041: LATIN CAPITAL LETTER A -> U+0061: LATIN SMALL LETTER A */
"\x42" => "\x62" , /* U+0042: LATIN CAPITAL LETTER B -> U+0062: LATIN SMALL LETTER B */ "\x42" => "\x62" , /* U+0042: LATIN CAPITAL LETTER B -> U+0062: LATIN SMALL LETTER B */
"\x43" => "\x63" , /* U+0043: LATIN CAPITAL LETTER C -> U+0063: LATIN SMALL LETTER C */ "\x43" => "\x63" , /* U+0043: LATIN CAPITAL LETTER C -> U+0063: LATIN SMALL LETTER C */
@@ -1538,8 +1568,9 @@ class unicode
"\xD5\x94" => "\xD6\x84" , /* U+0554: ARMENIAN CAPITAL LETTER KEH -> U+0584: ARMENIAN SMALL LETTER KEH */ "\xD5\x94" => "\xD6\x84" , /* U+0554: ARMENIAN CAPITAL LETTER KEH -> U+0584: ARMENIAN SMALL LETTER KEH */
"\xD5\x95" => "\xD6\x85" , /* U+0555: ARMENIAN CAPITAL LETTER OH -> U+0585: ARMENIAN SMALL LETTER OH */ "\xD5\x95" => "\xD6\x85" , /* U+0555: ARMENIAN CAPITAL LETTER OH -> U+0585: ARMENIAN SMALL LETTER OH */
"\xD5\x96" => "\xD6\x86" /* U+0556: ARMENIAN CAPITAL LETTER FEH -> U+0586: ARMENIAN SMALL LETTER FEH */ "\xD5\x96" => "\xD6\x86" /* U+0556: ARMENIAN CAPITAL LETTER FEH -> U+0586: ARMENIAN SMALL LETTER FEH */
]
]; ];
}
protected $endCharacters_utf8 = "\t\r\n !\"#\$%&'()+,-./:;<=>@[\\]^_`{|}~£§¨°"; protected $endCharacters_utf8 = "\t\r\n !\"#\$%&'()+,-./:;<=>@[\\]^_`{|}~£§¨°";
@@ -1555,7 +1586,7 @@ class unicode
*/ */
public function convert($string, $target) public function convert($string, $target)
{ {
$ok_methods = array_keys(self::$map); $ok_methods = [self::CONVERT_TO_LC, self::CONVERT_TO_ND, self::CONVERT_TO_LCND];
if (!in_array($target, $ok_methods)) { if (!in_array($target, $ok_methods)) {
throw new Exception_InvalidArgument( throw new Exception_InvalidArgument(
@@ -1570,7 +1601,7 @@ class unicode
} }
$out = ''; $out = '';
$_map = &self::$map[$target]; // faster in loop $_map = $this->getMap($target); // faster in loop
$length = mb_strlen($string, 'UTF-8'); $length = mb_strlen($string, 'UTF-8');
for ($i = 0; $i < $length; $i++) { for ($i = 0; $i < $length; $i++) {
@@ -1614,28 +1645,16 @@ class unicode
public function remove_nonazAZ09($string, $keep_underscores = true, $keep_minus = true, $keep_dot = false) public function remove_nonazAZ09($string, $keep_underscores = true, $keep_minus = true, $keep_dot = false)
{ {
$regexp = '/[a-zA-Z0-9';
if ($keep_minus === true) {
$regexp .= '-';
}
if ($keep_underscores === true) {
$regexp .= '_';
}
if ($keep_dot === true) {
$regexp .= '\.';
}
$regexp .= ']{1}/';
$string = $this->remove_diacritics($string); $string = $this->remove_diacritics($string);
$out = ''; $out = '';
$l = mb_strlen($string); $l = mb_strlen($string);
for ($i = 0; $i < $l; $i ++) { for ($i = 0; $i < $l; $i ++) {
$c = mb_substr($string, $i, 1); $c = mb_substr($string, $i, 1);
if (preg_match($regexp, $c)) if(($c>='a'&&$c<='z')||($c>='A'&&$c<='Z')||($c>='0'&&$c<='9')
||($keep_underscores&&$c=='_')||($keep_dot&&$c=='.')||($keep_minus&&$c=='-')) {
$out .= $c; $out .= $c;
}
} }
return $out; return $out;

View File

@@ -13,18 +13,23 @@ class unicodeTest extends \PhraseanetTestCase
public function setUp() public function setUp()
{ {
parent::setUp();
$this->object = new unicode(); $this->object = new unicode();
} }
public function tearDown()
{
// no-op
}
/** /**
* @covers \unicode::convert * @covers \unicode::convert
*/ */
public function testConvert() public function testConvert()
{ {
$this->assertEquals('éléphant à rôtir', $this->object->convert('ÉLÉPHANT à rôtir', unicode::CONVERT_TO_LC)); $testStr = 'ÉLÉPHANT à rôtir';
$this->assertEquals('ELEPHANT a rotir', $this->object->convert('ÉLÉPHANT à rôtir', unicode::CONVERT_TO_ND)); $this->assertEquals('éléphant à rôtir', $this->object->convert($testStr, unicode::CONVERT_TO_LC));
$this->assertEquals('elephant a rotir', $this->object->convert('ÉLÉPHANT à rôtir', unicode::CONVERT_TO_LCND)); $this->assertEquals('ELEPHANT a rotir', $this->object->convert($testStr, unicode::CONVERT_TO_ND));
$this->assertEquals('elephant a rotir', $this->object->convert($testStr, unicode::CONVERT_TO_LCND));
} }
/** /**
@@ -33,7 +38,7 @@ class unicodeTest extends \PhraseanetTestCase
*/ */
public function testConvertError() public function testConvertError()
{ {
$this->object->convert('ÉLÉPHANT à rôtir', 'unexistant contant'); $this->object->convert('ÉLÉPHANT à rôtir', 'UNDEFINED_CONSTANT');
} }
/** /**
@@ -41,9 +46,8 @@ class unicodeTest extends \PhraseanetTestCase
*/ */
public function testRemove_diacritics() public function testRemove_diacritics()
{ {
$this->assertEquals('Elephant', $this->object->remove_diacritics('Éléphant')); $testStr = '&é"\'(-èÉ_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿';
$this->assertEquals('&e"\'(-eE_ca)=$*u:;,?./§%μ£°0987654321Œ3~#{[|^`@]}e32÷׿',$this->object->remove_diacritics('&é"\'(-èÉ_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿')); $this->assertEquals('&e"\'(-eE_ca)=$*u:;,?./§%μ£°0987654321Œ3~#{[|^`@]}e32÷׿',$this->object->remove_diacritics($testStr));
$this->assertEquals('PeTARDS', $this->object->remove_diacritics('PéTARDS'));
} }
/** /**
@@ -51,12 +55,12 @@ class unicodeTest extends \PhraseanetTestCase
*/ */
public function testRemove_nonazAZ09() public function testRemove_nonazAZ09()
{ {
$this->assertEquals('Elephant', $this->object->remove_nonazAZ09('Eléphant')); $testStr = 'É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿';
$this->assertEquals('Ee-e_cau.09876543213e32', $this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', true, true, true)); $this->assertEquals('Ee-e_cau.09876543213e32', $this->object->remove_nonazAZ09($testStr, true, true, true));
$this->assertEquals('Ee-e_cau09876543213e32', $this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', true, true, false)); $this->assertEquals('Ee-e_cau09876543213e32' , $this->object->remove_nonazAZ09($testStr, true, true, false));
$this->assertEquals('Eee_cau.09876543213e32', $this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', true, false, true)); $this->assertEquals('Eee_cau.09876543213e32' , $this->object->remove_nonazAZ09($testStr, true, false, true));
$this->assertEquals('Ee-ecau.09876543213e32', $this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', false, true, true)); $this->assertEquals('Ee-ecau.09876543213e32' , $this->object->remove_nonazAZ09($testStr, false, true, true));
$this->assertEquals('Eeecau09876543213e32', $this->object->remove_nonazAZ09('É&é"\'(-è_çà)=$*ù:;,?./§%µ£°0987654321Œ3~#{[|^`@]}ê³²÷׿', false, false, false)); $this->assertEquals('Eeecau09876543213e32' , $this->object->remove_nonazAZ09($testStr, false, false, false));
} }
/** /**