]> gitweb.fluxo.info Git - lorea/elgg.git/commitdiff
Refs #4313. Loading ElggPlugin classes with the fully joined objects table.
authorBrett Profitt <brett.profitt@gmail.com>
Tue, 15 May 2012 06:41:31 +0000 (23:41 -0700)
committerBrett Profitt <brett.profitt@gmail.com>
Tue, 15 May 2012 06:41:31 +0000 (23:41 -0700)
This cuts the number of db queries in half for loading plugins with elgg_get_plugins().
Will look to adapt these techniques to other classes in 1.8.6.

engine/classes/ElggPlugin.php
engine/lib/plugins.php

index 33f14ae37b1e2316d3c2f3a8ac249b660b6c5c02..8c9093834d827a5faa2df5db61ae75bbb841c41f 100644 (file)
@@ -78,6 +78,68 @@ class ElggPlugin extends ElggObject {
                }
        }
 
+       /**
+        * Overridden from ElggEntity and ElggObject::load(). Core always inits plugins with
+        * a query joined to the objects_entity table, so all the info is there.
+        *
+        * @param mixed $guid GUID of an ElggObject or the stdClass object from entities table
+        *
+        * @return bool
+        * @throws InvalidClassException
+        */
+       protected function load($guid) {
+
+               $expected_attributes = $this->attributes;
+               unset($expected_attributes['tables_split']);
+               unset($expected_attributes['tables_loaded']);
+
+               // this was loaded with a full join
+               $needs_loaded = false;
+
+               if ($guid instanceof stdClass) {
+                       $row = (array) $guid;
+                       $missing_attributes = array_diff_key($expected_attributes, $row);
+                       if ($missing_attributes) {
+                               $needs_loaded = true;
+                               $old_guid = $guid;
+                               $guid = $row['guid'];
+                       } else {
+                               $this->attributes = $row;
+                       }
+               } else {
+                       $needs_loaded = true;
+               }
+
+               if ($needs_loaded) {
+                       $entity = (array) get_entity_as_row($guid);
+                       $object = (array) get_object_entity_as_row($guid);
+
+                       if (!$entity || !$object) {
+                               return false;
+                       }
+                       
+                       $this->attributes = array_merge($this->attributes, $entity, $object);
+               }
+
+               $this->attributes['tables_loaded'] = 2;
+
+               // Check the type
+               if ($this->attributes['type'] != 'object') {
+                       $msg = elgg_echo('InvalidClassException:NotValidElggStar', array($guid, get_class()));
+                       throw new InvalidClassException($msg);
+               }
+
+               // guid needs to be an int  http://trac.elgg.org/ticket/4111
+               $this->attributes['guid'] = (int)$this->attributes['guid'];
+
+               // cache the entity
+               if ($this->attributes['guid']) {
+                       cache_entity($this);
+               }
+
+               return true;
+       }
+
        /**
         * Save the plugin object.  Make sure required values exist.
         *
index 123fb18d89b5c9b4ffe67aa765261dbb5e4b42b1..39a76db5d9bcfaa3b129dfc217765a8ce8821b21 100644 (file)
@@ -93,10 +93,13 @@ function elgg_get_plugin_ids_in_dir($dir = null) {
 function elgg_generate_plugin_entities() {
        $site = get_config('site');
        $dir = elgg_get_plugins_path();
+       $db_prefix = elgg_get_config('dbprefix');
 
        $options = array(
                'type' => 'object',
                'subtype' => 'plugin',
+               'selects' => array('plugin_oe.*'),
+               'joins' => array("JOIN {$db_prefix}objects_entity plugin_oe on plugin_oe.guid = e.guid"),
                'limit' => ELGG_ENTITIES_NO_VALUE
        );
 
@@ -352,7 +355,11 @@ function elgg_get_plugins($status = 'active', $site_guid = null) {
                'type' => 'object',
                'subtype' => 'plugin',
                'limit' => ELGG_ENTITIES_NO_VALUE,
-               'joins' => array("JOIN {$db_prefix}private_settings ps on ps.entity_guid = e.guid"),
+               'selects' => array('plugin_oe.*'),
+               'joins' => array(
+                       "JOIN {$db_prefix}private_settings ps on ps.entity_guid = e.guid",
+                       "JOIN {$db_prefix}objects_entity plugin_oe on plugin_oe.guid = e.guid"
+                       ),
                'wheres' => array("ps.name = '$priority'"),
                'order_by' => "CAST(ps.value as unsigned), e.guid"
        );