]> 91.132.146.200 Git - bibliotheca-php.git/commitdiff
db query where needed. Reduced db queries in collection view
authorBanana <mail@bananas-playground.net>
Sat, 2 Jan 2021 12:07:29 +0000 (13:07 +0100)
committerBanana <mail@bananas-playground.net>
Sat, 2 Jan 2021 12:07:29 +0000 (13:07 +0100)
13 files changed:
TODO
webclient/lib/mancubus.class.php
webclient/lib/tentacle.class.php
webclient/lib/trite.class.php
webclient/view/default/advancedsearch/advancedsearch.php
webclient/view/default/collections/collections.php
webclient/view/default/entry/field-date.html
webclient/view/default/entry/field-lookupmultiple.html
webclient/view/default/entry/field-number.html
webclient/view/default/entry/field-selection.html
webclient/view/default/entry/field-year.html
webclient/view/default/managecollectionfields/managecollectionfields.html
webclient/view/default/tags/tags.html

diff --git a/TODO b/TODO
index 44c5d0595c4b432f5bf7515a8157764a008062a8..76ad32360e5136492c13e30c88006f08c5e5cef5 100644 (file)
--- a/TODO
+++ b/TODO
@@ -7,4 +7,3 @@
 * delete of a collection
 * sort by filter for collection display
 * responsive and breakpoints
-* DB query log and optimization
index fb644e1194da7dbf06351ee500e734d8e8452714..c88e4cdff74acc0cf97a4b361c0a662678eba8a4 100644 (file)
@@ -52,6 +52,20 @@ class Mancubus {
         */
        private $_queryOptions;
 
+       /**
+        * Store the all the values for an entry from lookup table
+        *
+        * @var array
+        */
+       private $_cacheLookupValuesForEntry = array();
+
+       /**
+        * Store entryFields for run time
+        *
+        * @var array
+        */
+       private $_cacheEntryFields = array();
+
        /**
         * Mancubus constructor.
         *
@@ -313,20 +327,9 @@ class Mancubus {
        public function getEntriesByFieldValue($fieldId, $fieldValue) {
                $ret = array();
 
-               $fieldData = array();
-               $queryStr = "SELECT `identifier`, `type`, `id`, `searchtype` FROM `".DB_PREFIX."_sys_fields`
-                                               WHERE `id` = '".$this->_DB->real_escape_string($fieldId)."'";
-               if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
-               try {
-                       $query = $this->_DB->query($queryStr);
-                       if($query !== false && $query->num_rows > 0) {
-                               if(($result = $query->fetch_assoc()) != false) {
-                                       $fieldData = $result;
-                               }
-                       }
-               }
-               catch (Exception $e) {
-                       error_log("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
+               $_entryFields = $this->_getEntryFields();
+               if(isset($_entryFields[$fieldId])) {
+                       $fieldData = $_entryFields[$fieldId];
                }
 
                if(empty($fieldData)) return $ret;
@@ -458,11 +461,14 @@ class Mancubus {
         * @return array
         */
        private function _getEntryFields() {
-               $ret = array();
+
+               if(!empty($this->_cacheEntryFields)) {
+                       return $this->_cacheEntryFields;
+               }
 
                if(!empty($this->_collectionId)) {
                        $queryStr = "SELECT `cf`.`fk_field_id` AS id, `sf`.`type`, `sf`.`displayname`, `sf`.`identifier`,
-                                                               `sf`.`value` AS preValue, `sf`.`apiinfo` 
+                                                               `sf`.`value` AS preValue, `sf`.`apiinfo` , `sf`.`searchtype`
                                                FROM `".DB_PREFIX."_collection_fields_".$this->_DB->real_escape_string($this->_collectionId)."` AS cf
                                                LEFT JOIN `".DB_PREFIX."_sys_fields` AS sf ON `cf`.`fk_field_id` = `sf`.`id`
                                                ORDER BY `cf`.`sort`";
@@ -471,7 +477,7 @@ class Mancubus {
                                $query = $this->_DB->query($queryStr);
                                if($query !== false && $query->num_rows > 0) {
                                        while(($result = $query->fetch_assoc()) != false) {
-                                               $ret[$result['id']] = $result;
+                                               $this->_cacheEntryFields[$result['id']] = $result;
                                        }
                                }
                        }
@@ -480,7 +486,7 @@ class Mancubus {
                        }
                }
 
-               return $ret;
+               return $this->_cacheEntryFields;
        }
 
        /**
@@ -526,21 +532,28 @@ class Mancubus {
                $ret = array();
 
                if(!empty($entryId) && !empty($fieldData) && !empty($this->_collectionId)) {
-                       $queryStr = "SELECT `value` 
-                                               FROM `".DB_PREFIX."_collection_entry2lookup_".$this->_DB->real_escape_string($this->_collectionId)."`
-                                               WHERE `fk_field` = '".$this->_DB->real_escape_string($fieldData['id'])."'
-                                                       AND `fk_entry` = '".$this->_DB->real_escape_string($entryId)."'";
-                       if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
-                       try {
-                               $query = $this->_DB->query($queryStr);
-                               if($query !== false && $query->num_rows > 0) {
-                                       while(($result = $query->fetch_assoc()) != false) {
-                                               $ret[] = $result['value'];
-                                       }
+
+                       // avoid db query for each wanted value
+                       if(isset($this->_cacheLookupValuesForEntry[$this->_collectionId])) {
+                               if(isset($this->_cacheLookupValuesForEntry[$this->_collectionId][$entryId][$fieldData['id']])) {
+                                       $ret =  $this->_cacheLookupValuesForEntry[$this->_collectionId][$entryId][$fieldData['id']];
                                }
                        }
-                       catch (Exception $e) {
-                               error_log("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
+                       else {
+                               $queryStr = "SELECT `fk_field`, `value`, `fk_entry`
+                                                       FROM `".DB_PREFIX."_collection_entry2lookup_".$this->_DB->real_escape_string($this->_collectionId)."`";
+                               if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
+                               try {
+                                       $query = $this->_DB->query($queryStr);
+                                       if($query !== false && $query->num_rows > 0) {
+                                               while(($result = $query->fetch_assoc()) != false) {
+                                                       $this->_cacheLookupValuesForEntry[$this->_collectionId][$result['fk_entry']][$result['fk_field']][$result['value']] = $result['value'];
+                                               }
+                                       }
+                               }
+                               catch (Exception $e) {
+                                       error_log("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
+                               }
                        }
                }
 
index cbe4bfee7f1eb4283dc731017da36e3722ff1ca1..7749678535e7931ec6f50c40ad8161cbe3ec48e2 100644 (file)
@@ -59,6 +59,7 @@ class Tentacle {
                                        FROM `".DB_PREFIX."_tool`  
                                        WHERE ".$this->_User->getSQLRightsString()."
                                        AND `action` = '".$this->_DB->real_escape_string($identifier)."'";
+               if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
                try {
                        $query = $this->_DB->query($queryStr);
                        if ($query !== false && $query->num_rows > 0) {
index 7a2ea8bf2371037c68c7534598a5b4f1eaec0900..31f304656b4075bcdeec561a687728313138d5e8 100644 (file)
@@ -117,6 +117,7 @@ class Trite {
                                        LEFT JOIN `".DB_PREFIX."_group` AS g ON `c`.`group` = `g`.`id`
                                        WHERE ".$this->_User->getSQLRightsString($right, "c")."
                                        AND `c`.`id` = '".$this->_DB->real_escape_string($id)."'";
+                       if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
                        try {
                                $query = $this->_DB->query($queryStr);
                                if ($query !== false && $query->num_rows > 0) {
@@ -124,7 +125,6 @@ class Trite {
                                        $this->_id = $this->_collectionData['id'];
                                }
                        } catch (Exception $e) {
-                               if(DEBUG) error_log("[DEBUG] ".__METHOD__."  mysql query: ".$queryStr);
                                error_log("[ERROR] ".__METHOD__." mysql catch: ".$e->getMessage());
                        }
                }
@@ -165,6 +165,7 @@ class Trite {
                                        LEFT JOIN `".DB_PREFIX."_group` AS g ON `c`.`group` = `g`.`id`
                                        WHERE ".$this->_User->getSQLRightsString("read", "c")."
                                        ORDER BY `c`.`name`";
+               if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
                try {
                        $query = $this->_DB->query($queryStr);
 
@@ -194,6 +195,7 @@ class Trite {
                                                FROM `".DB_PREFIX."_collection_fields_".$this->_id."` AS cf
                                                LEFT JOIN `".DB_PREFIX."_sys_fields` AS sf ON `cf`.`fk_field_id` = `sf`.`id`
                                                ORDER BY `cf`.`sort`";
+               if(QUERY_DEBUG) error_log("[QUERY] ".__METHOD__." query: ".var_export($queryStr,true));
                $query = $this->_DB->query($queryStr);
                try {
                        if($query !== false && $query->num_rows > 0) {
@@ -202,7 +204,6 @@ class Trite {
                                }
                        }
                } catch (Exception $e) {
-                       if(DEBUG) error_log("[DEBUG] ".__METHOD__."  mysql query: ".$queryStr);
                        error_log("[ERROR] ".__METHOD__."  mysql catch: ".$e->getMessage());
                }
 
index baefff02f815fd45a50ce96d3a897e4efc779089..7443331a62fcee9075b0ffcb24e778a8d5ff2ed6 100644 (file)
@@ -61,9 +61,11 @@ if(!empty($_collection)) {
                                                        $_ms = count($_matches[0]);
                                                        for($i=0;$i<$_ms;$i++) {
                                                                $_cn = trim(str_replace(':','',$_matches[1][$i]));
-                                                               $_sData[$i]['colName'] = $_cn;
-                                                               $_sData[$i]['colValue'] = trim(str_replace($_matches[1][$i],'',$_matches[0][$i]));
-                                                               $_sData[$i]['fieldData'] = $TemplateData['collectionFields'][$_cn];
+                                                               if(isset($TemplateData['collectionFields'][$_cn])) {
+                                                                       $_sData[$i]['colName'] = $_cn;
+                                                                       $_sData[$i]['colValue'] = trim(str_replace($_matches[1][$i],'',$_matches[0][$i]));
+                                                                       $_sData[$i]['fieldData'] = $TemplateData['collectionFields'][$_cn];
+                                                               }
                                                        }
 
                                                        $TemplateData['entries'] = $Mancubus->getEntries($_sData);
index dd10b1c18f56332ec3ada6a1da9d40ddc3e91691..43729123350f3c5af8c522785fc0c86fc745cd14 100644 (file)
@@ -27,24 +27,24 @@ if(isset($_GET['collection']) && !empty($_GET['collection'])) {
        $_collection = Summoner::validate($_collection,'digit') ? $_collection : false;
 }
 
-// field id to search within
+// field identifier to search within
 $_fid = false;
 if(isset($_GET['fid']) && !empty($_GET['fid'])) {
        $_fid = trim($_GET['fid']);
-       $_fid = Summoner::validate($_fid,'digit') ? $_fid : false;
+       $_fid = Summoner::validate($_fid,'nospace') ? $_fid : false;
 }
 
 // field value to look up
 $_fv = false;
 if(isset($_GET['fv']) && !empty($_GET['fv'])) {
        $_fv = trim($_GET['fv']);
-       $_fv = Summoner::validate($_fv,'text') ? $_fv : false;
+       $_fv = Summoner::validate($_fv) ? $_fv : false;
 }
 
 $_search = false;
 if(isset($_POST['navSearch'])) {
        $_search = trim($_POST['navSearch']);
-       $_search = Summoner::validate($_search,'text') ? $_search :  false;
+       $_search = Summoner::validate($_search) ? $_search :  false;
 }
 
 require_once(Summoner::themefile('system/pagination_before.php',UI_THEME));
@@ -67,26 +67,30 @@ if(!empty($_collection)) {
                $TemplateData['entryLinkPrefix'] = "index.php?p=entry&collection=".$Trite->param('id');
                $TemplateData['searchAction'] = 'index.php?p=collections&collection='.$Trite->param('id');
 
-               if (!empty($_fv) && !empty($_fid)) {
-                       $TemplateData['entries'] = $Mancubus->getEntriesByFieldValue($_fid, $_fv);
-                       $TemplateData['search'] = $_fv;
+               $_fd = $Trite->getCollectionFields();
 
+               $_sdata = array();
+               if (!empty($_fv) && !empty($_fid)) {
+                       $_sdata[0] = array(
+                               'colName' => $_fd[$_fid]['identifier'],
+                               'colValue' => $_fv,
+                               'fieldData' => $_fd[$_fid]
+                       );
+                       $_search = $_fv;
                        $TemplateData['pagination']['currentGetParameters']['fid'] = $_fid;
                        $TemplateData['pagination']['currentGetParameters']['fv'] = $_fv;
-               } else {
-                       $_fd = $Trite->getCollectionFields();
-                       $TemplateData['entries'] = $Mancubus->getEntries(
-                               array(
-                                       0 => array(
-                                               'colName' => $Trite->param('defaultSearchField'),
-                                               'colValue' => $_search,
-                                               'fieldData' =>$_fd[$Trite->param('defaultSearchField')]
-                                       )
-                               )
+               }
+               else {
+                       $_sdata[0] = array(
+                               'colName' => $Trite->param('defaultSearchField'),
+                               'colValue' => $_search,
+                               'fieldData' =>$_fd[$Trite->param('defaultSearchField')]
                        );
-                       if (!empty($_search)) {
-                               $TemplateData['search'] = $_search;
-                       }
+               }
+
+               $TemplateData['entries'] = $Mancubus->getEntries($_sdata);
+               if (!empty($_search)) {
+                       $TemplateData['search'] = $_search;
                }
        }
        else {
index 890971796e077b6a552678acaba6e275005aa466..7a6769c917ed2f7147aa05d76715d1b53a31da7e 100644 (file)
@@ -1,4 +1,4 @@
 <p data-field-name="date" data-field-id="<?php echo $field['identifier']; ?>">
        <span class="uk-text-muted"><?php echo $field['displayname']; ?>:</span>
-       <a href="index.php?p=collections&collection='.$TemplateData['loadedCollection']['id'].'&fid='.$field['id'].'&fv='.urlencode(Summoner::ifset($field, 'value')).'"><?php echo Summoner::ifset($field, 'value'); ?></a>
+       <a href="index.php?p=collections&collection='.$TemplateData['loadedCollection']['id'].'&fid='.$field['identifier'].'&fv='.urlencode(Summoner::ifset($field, 'value')).'"><?php echo Summoner::ifset($field, 'value'); ?></a>
 </p>
index e15139ca3f8014f2078af33231f4a4499a7208f8..f3175447b65b3e0c9d8ee1987ad10f413207663b 100644 (file)
@@ -2,7 +2,7 @@
        <span class="uk-text-muted"><?php echo $field['displayname']; ?>:</span>
        <?php
        foreach($field['value'] as $_fv) {
-               echo '<a href="index.php?p=collections&collection='.$TemplateData['loadedCollection']['id'].'&fid='.$field['id'].'&fv='.urlencode($_fv).'">'.$_fv.'</a>, ';
+               echo '<a href="index.php?p=collections&collection='.$TemplateData['loadedCollection']['id'].'&fid='.$field['identifier'].'&fv='.urlencode($_fv).'">'.$_fv.'</a>, ';
        }
        ?>
 </p>
index 951b94e1094c083e62b13be5bbe2a81ea6bc9d4e..eb20d83f4eba87fa7b0055d014abb4bce386f9d2 100644 (file)
@@ -1,4 +1,4 @@
 <p data-field-name="number" data-field-id="<?php echo $field['identifier']; ?>">
        <span class="uk-text-muted"><?php echo $field['displayname']; ?>:</span>
-       <a href="index.php?p=collections&collection=<?php echo $TemplateData['loadedCollection']['id']; ?>&fid=<?php echo $field['id']; ?>&fv=<?php echo urlencode(Summoner::ifset($field, 'value')); ?>"><?php echo Summoner::ifset($field, 'value'); ?></a>
+       <a href="index.php?p=collections&collection=<?php echo $TemplateData['loadedCollection']['id']; ?>&fid=<?php echo $field['identifier']; ?>&fv=<?php echo urlencode(Summoner::ifset($field, 'value')); ?>"><?php echo Summoner::ifset($field, 'value'); ?></a>
 </p>
index 3667f9a8dd956dfa415e691c48a01f20d3fc359a..4f149de3b397f3868f7931bcfa917a98ae3fb3b2 100644 (file)
@@ -1,4 +1,4 @@
 <p data-field-name="selection" data-field-id="<?php echo $field['identifier']; ?>">
        <span class="uk-text-muted"><?php echo $field['displayname']; ?>:</span>
-       <a href="index.php?p=collections&collection=<?php echo $TemplateData['loadedCollection']['id']; ?>&fid=<?php echo $field['id']; ?>&fv=<?php echo urlencode(Summoner::ifset($field, 'value')); ?>"><?php echo Summoner::ifset($field, 'value'); ?></a>
+       <a href="index.php?p=collections&collection=<?php echo $TemplateData['loadedCollection']['id']; ?>&fid=<?php echo $field['identifier']; ?>&fv=<?php echo urlencode(Summoner::ifset($field, 'value')); ?>"><?php echo Summoner::ifset($field, 'value'); ?></a>
 </p>
index 037645157662d61db43d92156a90e6061bf6ce53..c3dfbf54abc93485d24294c74cc6d9a0e6c540cc 100644 (file)
@@ -1,4 +1,4 @@
 <p data-field-name="year" data-field-id="<?php echo $field['identifier']; ?>">
        <span class="uk-text-muted"><?php echo $field['displayname']; ?>:</span>
-       <a href="index.php?p=collections&collection=<?php echo $TemplateData['loadedCollection']['id']; ?>&fid=<?php echo $field['id']; ?>&fv=<?php echo urlencode(Summoner::ifset($field, 'value')); ?>"><?php echo Summoner::ifset($field, 'value'); ?></a>
+       <a href="index.php?p=collections&collection=<?php echo $TemplateData['loadedCollection']['id']; ?>&fid=<?php echo $field['identifier']; ?>&fv=<?php echo urlencode(Summoner::ifset($field, 'value')); ?>"><?php echo Summoner::ifset($field, 'value'); ?></a>
 </p>
index c343adcdb73039f9e4c6a390b6c11d4a664f7add..fce9fffb74eda9f3bb76839f0405ed463b20d406 100644 (file)
@@ -7,8 +7,8 @@
                        Just use drag and drop below to add, remove or order your fields.<br />
                        <span class="" uk-icon="icon: warning"></span> Removing a field will remove
                        the stored data from the collection.<br />
-                       <span class="" uk-icon="icon: warning"></span> Make sure at least the <b>title</b>
-                       field is available.
+                       <span class="" uk-icon="icon: warning"></span> Make sure at least the <b>title</b>, <b>cover image</b>
+                       and <b>description</b> fields are available.
                </p>
                <div id="collectionFields" uk-sortable="group: sortable-group">
                        <?php foreach($TemplateData['existingFields'] as $k=>$v) { ?>
index 4034f183723e1018935b5beaa1c1986a0a16805b..14a915d0bd7331ceddf8167470dc4776868ccc7b 100644 (file)
@@ -19,7 +19,7 @@
                <h4 class="uk-heading-line"><span><a uk-toggle="target: #toggle-<?php echo $k; ?>"><?php echo $v['displayname']; ?></a></span></h4>
                <div id="toggle-<?php echo $k; ?>">
                        <?php foreach($v['entries'] as $ek=>$ev) { ?>
-                       <a href="index.php?p=collections&collection=<?php echo $TemplateData['loadedCollection']['id']; ?>&fid=<?php echo $k; ?>&fv=<?php echo urlencode($ev); ?>"><?php echo $ev; ?></a>,
+                       <a href="index.php?p=collections&collection=<?php echo $TemplateData['loadedCollection']['id']; ?>&fid=<?php echo $v['identifier']; ?>&fv=<?php echo urlencode($ev); ?>"><?php echo $ev; ?></a>,
                        <?php } ?>
                </div>
                <?php } ?>