Commit ae9591c2 authored by Robert Lyon's avatar Robert Lyon

Checking case sensitivity for mysql tag tables (bug #1231920)

Compressing array of tags to unique case insensitive values if needed

Eg
array('Test','cat','test','TEST','Cat');

will become
array('Test','cat');

for mysql sites that have case insensitive tables

Change-Id: Idf0b8a4440ef0f9273d5be685cf7ad22fecc746a
Signed-off-by: Robert Lyon's avatarRobert Lyon <robertl@catalyst.net.nz>
parent 15b384bb
......@@ -487,6 +487,7 @@ function edituser_site_submit(Pieform $form, $values) {
delete_records('usr_tag', 'usr', $user->id);
if (is_array($values['tags'])) {
$values['tags'] = check_case_sensitive($values['tags'], 'usr_tag');
foreach(array_unique($values['tags']) as $tag) {
if (empty($tag)) {
continue;
......
......@@ -453,6 +453,7 @@ abstract class ArtefactType {
delete_records('artefact_tag', 'artefact', $this->id);
if (is_array($this->tags)) {
$this->tags = check_case_sensitive($this->tags, 'artefact_tag');
foreach (array_unique($this->tags) as $tag) {
if (empty($tag)) {
continue;
......
......@@ -166,7 +166,8 @@ class Collection {
if (isset($this->tags)) {
delete_records('collection_tag', 'collection', $this->get('id'));
foreach ($this->get_tags() as $tag) {
$tags = check_case_sensitive($this->get_tags(), 'collection_tag');
foreach ($tags as $tag) {
//truncate the tag before insert it into the database
$tag = substr($tag, 0, 128);
insert_record('collection_tag', (object)array( 'collection' => $this->get('id'), 'tag' => $tag));
......
......@@ -3250,3 +3250,26 @@ function generate_csv($data, $csvfields) {
}
return $csv;
}
/**
* Check to make sure table is case sensitive (currently only for MySql)
* If it is not then reduce supplied array to a case insensitive version
* Preserving the first occurance of any duplicates.
* E.g. 'Test,test,cat,TEST,CAT,Cat' will return 'Test,cat'
*
* @param array Array of case senstive strings
* @param string Name of table
*
* @return array Array of strings
*/
function check_case_sensitive($a, $table) {
if (is_mysql()) {
$db = get_config('dbname');
$result = recordset_to_array(get_recordset_sql("SHOW TABLE STATUS IN `$db` WHERE Name = ?", array($table)));
if (preg_match('/_ci/', $result[0]->Collation)) {
$b = array_unique(array_map('strtolower', $a));
$a = array_intersect_key($a, array_flip(array_keys($b)));
}
}
return $a;
}
......@@ -673,6 +673,7 @@ class View {
}
if (isset($this->tags)) {
$this->tags = check_case_sensitive($this->tags, 'view_tag');
delete_records('view_tag', 'view', $this->get('id'));
foreach ($this->get_tags() as $tag) {
//truncate the tag before insert it into the database
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment