Cut out two queries per page with plugin loader routine
authorDan
Wed, 13 May 2009 09:48:59 -0400
changeset 975 bff25c6113ae
parent 974 6bfe9eb428e9
child 976 50f8eb4f53e1
Cut out two queries per page with plugin loader routine
includes/plugins.php
plugins/admin/PluginManager.php
--- a/includes/plugins.php	Wed May 13 09:47:59 2009 -0400
+++ b/includes/plugins.php	Wed May 13 09:48:59 2009 -0400
@@ -75,37 +75,17 @@
     
     $dir = ENANO_ROOT.'/plugins/';
     
-    $this->load_list = $this->system_plugins;
-    $q = $db->sql_query('SELECT plugin_filename, plugin_version FROM ' . table_prefix . 'plugins WHERE plugin_flags & ~' . PLUGIN_DISABLED . ' = plugin_flags;');
-    if ( !$q )
-      $db->_die();
+    $plugin_list = $this->get_plugin_list();
+    $this->load_list = array();
     
-    while ( $row = $db->fetchrow() )
+    foreach ( $plugin_list as $filename => $data )
     {
-      if ( file_exists(ENANO_ROOT . "/plugins/{$row['plugin_filename']}") )
-      {
-        $this->load_list[] = $row['plugin_filename'];
-      }
-    }
-    $this->load_list = array_unique($this->load_list);
-    
-    $this->loaded_plugins = $this->get_plugin_list($this->load_list);
-    
-    // check for out-of-date plugins
-    foreach ( $this->load_list as $i => $plugin )
-    {
-      if ( in_array($plugin, $this->system_plugins) )
+      if ( $data['status'] & PLUGIN_OUTOFDATE || $data['status'] & PLUGIN_DISABLED )
         continue;
       
-      if ( $this->loaded_plugins[$plugin]['status'] & PLUGIN_OUTOFDATE )
-      {
-        // it's out of date, don't load
-        unset($this->load_list[$i]);
-        unset($this->loaded_plugins[$plugin]);
-      }
+      $this->load_list[] = $filename;
+      $this->loaded_plugins[$filename] = $data;
     }
-    
-    $this->load_list = array_unique($this->load_list);
   }
   
   /**
@@ -275,9 +255,10 @@
     $ta = 0;
     // won't load twice (failsafe automatic skip)
     $this->load_plugins_cache();
-    if ( $use_cache )
+    global $plugins_cache;
+    if ( $use_cache && !empty($plugins_cache) )
     {
-      global $plugins_cache;
+      return $plugins_cache;
     }
     else
     {
@@ -285,6 +266,7 @@
       $plugins_cache = array();
     }
     
+    // List all plugins
     if ( $dirh = @opendir( ENANO_ROOT . '/plugins' ) )
     {
       while ( $dh = @readdir($dirh) )
@@ -298,7 +280,7 @@
           
         // it's a PHP file, attempt to read metadata
         $fullpath = ENANO_ROOT . "/plugins/$dh";
-        $plugin_meta = $this->get_plugin_info($fullpath, $use_cache);
+        $plugin_meta = $this->read_plugin_headers($fullpath, $use_cache);
         
         if ( is_array($plugin_meta) )
         {
@@ -307,7 +289,8 @@
         }
       }
     }
-    // gather info about installed plugins
+    
+    // Populate with additional metadata from database
     $q = $db->sql_query('SELECT plugin_id, plugin_filename, plugin_version, plugin_flags FROM ' . table_prefix . 'plugins;');
     if ( !$q )
       $db->_die();
@@ -347,7 +330,7 @@
    * @return array
    */
   
-  function get_plugin_info($fullpath, $use_cache = true)
+  function read_plugin_headers($fullpath, $use_cache = true)
   {
     global $plugins_cache;
     $dh = basename($fullpath);
@@ -528,7 +511,7 @@
     if ( !file_exists($filename) )
       return false;
     
-    $info = $this->get_plugin_info($filename);
+    $info = $this->read_plugin_headers($filename);
     if ( isset($info['auth plugin']) )
       return true;
     
--- a/plugins/admin/PluginManager.php	Wed May 13 09:47:59 2009 -0400
+++ b/plugins/admin/PluginManager.php	Wed May 13 09:48:59 2009 -0400
@@ -267,6 +267,8 @@
               if ( !$q )
                 $db->die_json();
               
+              $cache->purge('plugins');
+              
               $return = array(
                 'success' => true
               );