/**
  * Load all plugins for a given category.
  * @param $category string The name of the category to load
  * @param $enabledOnly boolean if true load only enabled
  *  plug-ins (db-installation required), otherwise look on
  *  disk and load all available plug-ins (no db required).
  * @param $mainContextId integer To identify enabled plug-ins
  *  we need a context. This context is usually taken from the
  *  request but sometimes there is no context in the request
  *  (e.g. when executing CLI commands). Then the main context
  *  can be given as an explicit ID.
  */
 function &loadCategory($category, $enabledOnly = false, $mainContextId = null)
 {
     $plugins = array();
     $categoryDir = PLUGINS_PREFIX . $category;
     if (!is_dir($categoryDir)) {
         return $plugins;
     }
     if ($enabledOnly && Config::getVar('general', 'installed')) {
         // Get enabled plug-ins from the database.
         $application =& PKPApplication::getApplication();
         $products =& $application->getEnabledProducts('plugins.' . $category, $mainContextId);
         foreach ($products as $product) {
             $file = $product->getProduct();
             $plugin =& PluginRegistry::_instantiatePlugin($category, $categoryDir, $file, $product->getProductClassname());
             if ($plugin && is_object($plugin)) {
                 $plugins[$plugin->getSeq()]["{$categoryDir}/{$file}"] =& $plugin;
                 unset($plugin);
             }
         }
     } else {
         // Get all plug-ins from disk. This does not require
         // any database access and can therefore be used during
         // first-time installation.
         $handle = opendir($categoryDir);
         while (($file = readdir($handle)) !== false) {
             if ($file == '.' || $file == '..') {
                 continue;
             }
             $plugin =& PluginRegistry::_instantiatePlugin($category, $categoryDir, $file);
             if ($plugin && is_object($plugin)) {
                 $plugins[$plugin->getSeq()]["{$categoryDir}/{$file}"] =& $plugin;
                 unset($plugin);
             }
         }
         closedir($handle);
     }
     // If anyone else wants to jump category, here is the chance.
     HookRegistry::call('PluginRegistry::loadCategory', array(&$category, &$plugins));
     // Register the plugins in sequence.
     ksort($plugins);
     foreach ($plugins as $seq => $junk1) {
         foreach ($plugins[$seq] as $pluginPath => $junk2) {
             PluginRegistry::register($category, $plugins[$seq][$pluginPath], $pluginPath);
         }
     }
     unset($plugins);
     // Return the list of successfully-registered plugins.
     $plugins =& PluginRegistry::getPlugins($category);
     return $plugins;
 }