diff --git a/README.md b/README.md index c457446..5e0150f 100644 --- a/README.md +++ b/README.md @@ -384,6 +384,9 @@ Export strings in one language from editora database to excel file or output ## import-translation.php Import strings from an excel, json file or input to editora database +## field parents and full_niceurl +Now the omp_niceurl table will have the parents field, which will contain the IDs of its parents separated by commas, and the full_niceurl field will have the parents + the niceurl to distinguish the niceurl in case there are two duplicates. All of this will be filled in automatically if the instances have a relationship called 'Childs'. + ## Experimental ### data-transfer.php diff --git a/data/editora.sql b/data/editora.sql index 4055101..c8711e2 100644 --- a/data/editora.sql +++ b/data/editora.sql @@ -354,3 +354,13 @@ CREATE TABLE IF NOT EXISTS `omp_values` ( KEY `omp_values_n2` (`date_val`) USING BTREE, KEY `omp_values_n3` (`num_val`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4; + + +-- ---------------------------- +-- Update structure for omp_niceurl +-- ---------------------------- +ALTER TABLE `omp_niceurl` + ADD COLUMN `parents` VARCHAR(255) DEFAULT NULL, + ADD COLUMN `full_niceurl` VARCHAR(255) CHARACTER SET latin1 DEFAULT NULL AFTER `parents`, + DROP INDEX `omp_niceurl_u1`, + ADD UNIQUE KEY `omp_niceurl_u1` (`language`, `niceurl`, `parents`) USING BTREE; diff --git a/src/Omatech/Editora/DBInterfaceBase.php b/src/Omatech/Editora/DBInterfaceBase.php index c976c2f..f6d2d1d 100644 --- a/src/Omatech/Editora/DBInterfaceBase.php +++ b/src/Omatech/Editora/DBInterfaceBase.php @@ -401,9 +401,16 @@ public function getInstanceRandomClassID($class_id) public function getInstanceLink($inst_id, $lang = null) { + $sql = "SHOW COLUMNS FROM omp_niceurl LIKE 'full_niceurl'"; + $row = $this->fetchAssoc($sql); + $select = 'niceurl'; + + if ($row) { + $select .= ', full_niceurl '; + } $this->lang = $this->lang ?? $lang; $inst_id = $this->conn->quote($inst_id); - $sql = "select niceurl + $sql = "select $select from omp_niceurl where inst_id=$inst_id and language='" . $this->lang . "'"; @@ -412,9 +419,9 @@ public function getInstanceLink($inst_id, $lang = null) $niceurl_row = $this->fetchAssoc($sql); if ($niceurl_row) { if ($this->lang == 'ALL') { - $link = '/' . $niceurl_row['niceurl']; + $link = '/' . ($niceurl_row['full_niceurl'] ?? $niceurl_row['niceurl']); } else { - $link = '/' . $this->lang . '/' . $niceurl_row['niceurl']; + $link = '/' . $this->lang . '/' . ($niceurl_row['full_niceurl'] ?? $niceurl_row['niceurl']); } } else { if ($this->lang == 'ALL') { @@ -474,24 +481,41 @@ public function getUrlData($language, $nice_url) //return ['type' => 'ChangeLanguage', 'language' => $language]; } } else {// check valid urlnice - $sql = "select n.inst_id, n.niceurl, i.class_id, c.tag, i.key_fields nom_intern - from omp_niceurl n - , omp_instances i - , omp_classes c - where n.language = :language - and n.niceurl = :nice_url - and i.id=n.inst_id - and i.class_id=c.id - " . $this->getPreviewFilter() . " - "; + $segments = request()->segments(); + $parents = null; + + $prepare = $this->conn->prepare("select * from omp_niceurl LIMIT 1"); + $resultSet = $prepare->executeQuery(); + $row=$resultSet->fetchAssociative(); + + $haveFullNiceUrl = count($segments) > 2 && array_key_exists('full_niceurl', $row); + + $sql = "select n.inst_id, n.niceurl, i.class_id, c.tag, i.key_fields nom_intern"; + + if ($haveFullNiceUrl) $sql .= ", n.full_niceurl"; + + $sql .= " from omp_niceurl n + , omp_instances i + , omp_classes c + where n.language = :language"; + + if ($haveFullNiceUrl) { + $sql .= " and n.full_niceurl = :nice_url"; + } else { + $sql .= " and n.niceurl = :nice_url"; + } + + $sql .= " + and i.id=n.inst_id + and i.class_id=c.id + " . $this->getPreviewFilter() . " + "; $prepare = $this->conn->prepare($sql); $prepare->bindValue('language', $language); $prepare->bindValue('nice_url', $nice_url); $resultSet = $prepare->executeQuery(); $row=$resultSet->fetchAssociative(); - //$prepare->execute(); - //$row = $prepare->fetch(); if ($row) { $result = ['type' => 'Instance' diff --git a/src/Omatech/Editora/Extractor/Extractor.php b/src/Omatech/Editora/Extractor/Extractor.php index d76c126..36ae703 100644 --- a/src/Omatech/Editora/Extractor/Extractor.php +++ b/src/Omatech/Editora/Extractor/Extractor.php @@ -627,24 +627,63 @@ public function findInstancesByUrl(string $url, $params = null, callable $callba $niceurls = explode('/', $url); $niceurls = array_map(fn($n) => $n === '' ? 'home' : $n, $niceurls); - $sql = "select inst_id - from omp_instances i - , omp_niceurl u - where 1=1 - and i.id = u.inst_id - and u.language = '" . $this->lang . "' - and u.niceurl IN ('" . implode("','", $niceurls) . "') - order by FIND_IN_SET(u.niceurl, '" . implode(",", $niceurls) . "') - "; - - $this->debug("SQL a findInstancesByNiceurl\n"); - $this->debug($sql); - $rows = $this->fetchAll($sql); - $inst_ids = array_reduce($rows, function ($acc, $row) { - $acc .= ','.$row['inst_id']; - return $acc; - }, ''); - $inst_ids = ltrim($inst_ids, ','); + $sql = "SHOW COLUMNS FROM omp_niceurl LIKE 'full_niceurl'"; + $row = $this->fetchAssoc($sql); + $inst_ids = ''; + + if ($row) { + $url = (substr($url, 0, 1) === '/') ? substr($url, 1) : $url; + + if (isset($niceurls[0])) { + $sql = "select inst_id + from omp_instances i + , omp_niceurl u + where 1=1 + and i.id = u.inst_id + and u.language = '" . $this->lang . "' + and u.niceurl = '" . $niceurls[0] . "' + "; + $row = $this->fetchAssoc($sql); + if (isset($row['inst_id'])) { + $inst_ids = $row['inst_id']; + } + + $sql = "select CONCAT(parents, ',', inst_id) as ids + from omp_instances i + , omp_niceurl u + where 1=1 + and i.id = u.inst_id + and u.language = '" . $this->lang . "' + and u.full_niceurl = '" . $url . "' + "; + $row = $this->fetchAssoc($sql); + if (isset($row['ids'])) { + $inst_ids .= ',' . $row['ids']; + } + } + } + + if (!$inst_ids) { + + $sql = "select inst_id + from omp_instances i + , omp_niceurl u + where 1=1 + and i.id = u.inst_id + and u.language = '" . $this->lang . "' + and u.niceurl IN ('" . implode("','", $niceurls) . "') + order by FIND_IN_SET(u.niceurl, '" . implode(",", $niceurls) . "') + "; + + $this->debug("SQL a findInstancesByNiceurl\n"); + $this->debug($sql); + $rows = $this->fetchAll($sql); + $inst_ids = array_reduce($rows, function ($acc, $row) { + $acc .= ','.$row['inst_id']; + return $acc; + }, ''); + $inst_ids = ltrim($inst_ids, ','); + } $instances = $this->findInstancesInList($inst_ids, null, null, [ 'order' => 'ignore', diff --git a/src/Omatech/Editora/Generator/Generator.php b/src/Omatech/Editora/Generator/Generator.php index f11cb3d..7a598e0 100644 --- a/src/Omatech/Editora/Generator/Generator.php +++ b/src/Omatech/Editora/Generator/Generator.php @@ -173,6 +173,38 @@ public function modernize() $changes++; } + /* Column parents in omp_niceurl */ + $sql = "show columns from omp_niceurl"; + $rows = $this->fetchAll($sql); + $parents_found = false; + foreach ($rows as $row) { + if ($row['Field'] == 'parents') { + $parents_found = true; + } + } + if (!$parents_found) { + $sql = "alter table omp_niceurl add column parents varchar(255) default null\n"; + $this->conn->executeQuery($sql); + $changes++; + echo "Create column parents in omp_niceurl table\n"; + } + + /* Column full_niceurl in omp_niceurl */ + $sql = "show columns from omp_niceurl"; + $rows = $this->fetchAll($sql); + $full_niceurl_found = false; + foreach ($rows as $row) { + if ($row['Field'] == 'full_niceurl') { + $full_niceurl_found = true; + } + } + if (!$full_niceurl_found) { + $sql = "alter table omp_niceurl add column full_niceurl varchar(255) default null\n"; + $this->conn->executeQuery($sql); + $changes++; + echo "Create column full_niceurl in omp_niceurl table\n"; + } + /* Column json_val in omp_values */ $sql = "show columns from omp_values"; $rows = $this->fetchAll($sql); diff --git a/src/Omatech/Editora/Loader/Loader.php b/src/Omatech/Editora/Loader/Loader.php index f161c17..e5ab52e 100644 --- a/src/Omatech/Editora/Loader/Loader.php +++ b/src/Omatech/Editora/Loader/Loader.php @@ -391,16 +391,20 @@ public function updateUrlNice($nice_url, $inst_id, $language) return $inst_id; } - public function insertUrlNice($nice_url, $inst_id, $language) + public function insertUrlNice($nice_url, $inst_id, $language, $parents = null, $full_niceurl = null) { - if ($this->existsURLNice($nice_url, $language)) { - return -1; - } - - $sql = "insert into omp_niceurl + if (!$parents && !$full_niceurl) { + $sql = "insert into omp_niceurl (inst_id, language , niceurl) values ($inst_id, '$language','$nice_url')"; + } else { + $sql = "insert into omp_niceurl + (inst_id, language , niceurl, parents, full_niceurl) + values + ($inst_id, '$language','$nice_url','$parents','$full_niceurl')"; + } + $ret = $this->conn->executeQuery($sql); return $this->conn->lastInsertId(); }