Changeset 26427


Ignore:
Timestamp:
06/21/10 18:01:52 (7 years ago)
Author:
flack
Message:

avoid loading full topic from database where possible, refs #359

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/developers/flack/acltuning/midcom.core/midcom/helper/_basicnav.php

    r26422 r26427  
    289289        $topic_id = (int) $node_id;
    290290
    291         $topic = midcom_db_topic::get_cached($topic_id);
    292 
    293         if (   !$topic
    294             || !$topic->guid)
    295         {
    296             debug_push_class(__CLASS__, __FUNCTION__);
    297             debug_add("Could not load Topic #{$topic_id}: " . midcom_application::get_error_string(), MIDCOM_LOG_ERROR);
    298             debug_pop();
    299             return MIDCOM_ERRCRIT;
    300         }
    301 
    302291        // Load parent nodes also to cache
    303         $parent = $topic;
     292        $parent_id = 0;
    304293        $up_ids = array();
    305294        if ($up)
    306295        {
    307             $parent->up = (int) $up;
     296            $parent_id = (int) $up;
    308297
    309298            $up_ids = explode("_", $up);
     
    311300            array_pop($up_ids);
    312301        }
     302        else
     303        {
     304            $parent_id = $this->_get_parent_id($topic_id);
     305        }
    313306
    314307        $lastgoodnode = null;
    315308
    316         $parent = $parent->get_parent();
    317 
    318         while (   is_object($parent)
    319                && $parent->guid
    320                && !isset(self::$_nodes[$parent->id]))
     309        while (   $parent_id
     310               && !isset(self::$_nodes[$parent_id]))
    321311        {
    322312            // Pass the full topic so _loadNodeData doesn't have to reload it
    323             $result = $this->_loadNodeData($parent);
     313            $result = $this->_loadNodeData($parent_id);
    324314            switch ($result)
    325315            {
     
    327317                    debug_push_class(__CLASS__, __FUNCTION__);
    328318                    $log_level = MIDCOM_LOG_WARN;
    329                     if (!$parent->up)
     319                    if (!$this->_get_parent_id($topic_id))
    330320                    {
    331321                        // This can happen only when a target for a symlink pointing outside the tree is tried to be accessed.
     
    333323                        $log_level = MIDCOM_LOG_INFO;
    334324                    }
    335                     debug_add("The Node {$parent->id} is invisible, could not satisfy the dependency chain to Node #{$node_id}", $log_level);
     325                    debug_add("The Node {$parent_id} is invisible, could not satisfy the dependency chain to Node #{$node_id}", $log_level);
    336326                    debug_pop();
    337327                    return MIDCOM_ERRFORBIDDEN;
     
    343333            if (null === $lastgoodnode)
    344334            {
    345                 $lastgoodnode = $parent->id;
    346             }
     335                $lastgoodnode = $parent_id;
     336            }
     337
     338            $parent_id = $this->_get_parent_id($topic_id);
    347339
    348340            if ($up)
     
    350342                if ($up_id = array_pop($up_ids))
    351343                {
    352                     if ($up_id != $parent->up)
     344                    if ($up_id != $parent_id)
    353345                    {
    354                         $parent->up = $up_id;
     346                        $parent_id = $up_id;
    355347                    }
    356348                }
    357349            }
    358             $parent = $parent->get_parent();
    359350        }
    360351
     
    365356        }
    366357
    367         return $this->_loadNodeData($topic, $up);
    368     }
    369 
    370     /**
    371      * Load the Navigational information associated with the topic $param, which
     358        return $this->_loadNodeData($topic_id, $up);
     359    }
     360
     361    /**
     362     * Load the navigational information associated with the topic $param, which
    372363     * can be passed as an ID or as a MidgardTopic object. This is differentiated
    373364     * by the flag $idmode (true for id, false for MidgardTopic).
     
    384375     * as this can happen if dynamic_load is called before showing the navigation.
    385376     *
    386      * @param mixed $node    Topic to be processed. Object or ID
     377     * @param mixed $topic_id Topic ID to be processed
    387378     * @return int            One of the MGD_ERR constants
    388379     */
    389     private function _loadNodeData($topic, $up = null)
    390     {
    391         $topic_id = $topic->id;
    392 
     380    private function _loadNodeData($topic_id, $up = null)
     381    {
    393382        // Load the node data and check visibility.
    394         $nodedata = $this->_get_node($topic, $up);
     383        $nodedata = $this->_get_node($topic_id, $up);
    395384
    396385        if (! $this->_is_object_visible($nodedata))
     
    412401            {
    413402                debug_push_class(__CLASS__, __FUNCTION__);
    414                 debug_add("Could not get interface class of '{$nodedata[MIDCOM_NAV_COMPONENT]}' to the topic {$topic->id}, cannot add it to the NAP list.",
     403                debug_add("Could not get interface class of '{$nodedata[MIDCOM_NAV_COMPONENT]}' to the topic {$topic_id}, cannot add it to the NAP list.",
    415404                    MIDCOM_LOG_ERROR);
    416405                debug_pop();
     
    435424     * cache, it is retrieved from there. (NOTE: Cache has been disabled. See #252 at Tigris.org.)
    436425     *
    437      * @param mixed $topic The node for which the NAP information is requested.
     426     * @param mixed $topic_id The node ID for which the NAP information is requested.
    438427     * @param mixed $up    The node ID of the parent node.    Optional and not normally needed.
    439428     * @return Array NAP node data structure or NULL in case no NAP information is available for this topic.
    440429     * @access private
    441430     */
    442     private function _get_node($topic, $up = null)
     431    private function _get_node($topic_id, $up = null)
    443432    {
    444433        $nodedata = false;
    445        
     434
    446435        if (!$up)
    447436        {
    448             $nodedata = $this->_nap_cache->get_node($topic->id);
     437            $nodedata = $this->_nap_cache->get_node($topic_id);
    449438        }
    450439
    451440        if (!$nodedata)
    452441        {
    453             $nodedata = $this->_get_node_from_database($topic, $up);
     442            $nodedata = $this->_get_node_from_database($topic_id, $up);
    454443            if (is_null($nodedata))
    455444            {
     
    460449            }
    461450
    462             $this->_nap_cache->put_node($topic->id, $nodedata);
    463             debug_add("Added the guid {$topic->id} to the cache.");
     451            $this->_nap_cache->put_node($topic_id, $nodedata);
     452            debug_add("Added the ID {$topic_id} to the cache.");
    464453        }
    465454
     
    479468     * If the topic is missing a component, it will set the component to midcom.core.nullcomponent.
    480469     *
    481      * @param mixed $id The ID of the node for which the NAP information is requested.
     470     * @param mixed $topic_id The ID of the node for which the NAP information is requested.
    482471     * @param mixed $up    The node ID of the parent node.    Optional and not normally needed.
    483472     * @return Array Node data structure or NULL in case no NAP information is available for this topic.
    484473     * @access private
    485474     */
    486     private function _get_node_from_database($topic, $up = null)
    487     {
     475    private function _get_node_from_database($topic_id, $up = null)
     476    {
     477        $topic = midcom_db_topic::get_cached($topic_id);
     478
     479        if (   !$topic
     480            || !$topic->guid)
     481        {
     482            debug_push_class(__CLASS__, __FUNCTION__);
     483            debug_add("Could not load Topic #{$topic_id}: " . midcom_application::get_error_string(), MIDCOM_LOG_ERROR);
     484            debug_pop();
     485            return null;
     486        }
     487
    488488        $urltopic = $topic;
    489489        $id = $this->_nodeid($urltopic->id, $up);
     
    977977        // Use the midgard_query_builder to get the subnodes
    978978        $mc = midcom_db_topic::new_collector('up', self::$_nodes[$parent_node][MIDCOM_NAV_OBJECT]->id);
     979        $mc->add_value_property('id');
     980
    979981        $mc->add_constraint('name', '<>', '');
    980982
     
    10011003
    10021004        $up = $this->_up($parent_node);
     1005
    10031006        $node = (int) $parent_node;
    10041007
     
    10111014        foreach ($subnodes as $guid => $empty)
    10121015        {
    1013             $subnode = midcom_db_topic::get_cached($guid);
    1014             $subnode_id = $this->_nodeid($subnode->id, $up);
     1016            $subnode_id = $this->_nodeid($mc->get_subkey($guid, 'id'), $up);
    10151017
    10161018            if ($this->_loadNode($subnode_id, $up) !== MIDCOM_ERROK)
     
    10231025            if ($GLOBALS['midcom_config']['i18n_multilang_navigation'])
    10241026            {
     1027                $subnode = midcom_db_topic::get_cached($guid);
     1028
    10251029                $ml_noentry = $subnode->get_parameter('midcom.helper.nav', 'navnoentry_' . $_MIDCOM->i18n->get_content_language());
    10261030                if ($ml_noentry)
     
    14071411    }
    14081412
     1413    /**
     1414     * Small helper to determine a topic's parent id without loading the full object
     1415     *
     1416     * @param integer $topic_id The topic ID
     1417     * @return integer The parent ID or false
     1418     */
     1419    private function _get_parent_id($topic_id)
     1420    {
     1421        $mc = midcom_db_topic::new_collector('id', $topic_id);
     1422        $mc->add_value_property('up');
     1423        $mc->execute();
     1424        $result = $mc->list_keys();
     1425        if (empty($result))
     1426        {
     1427            return false;
     1428        }
     1429        return $mc->get_subkey(key($result), 'up');
     1430    }
    14091431
    14101432    /**
Note: See TracChangeset for help on using the changeset viewer.