Commit 1e97f654 authored by jerico.dev's avatar jerico.dev
Browse files

*5268* Performance optimization: lazy load for plug-ins (hook subscription...

*5268* Performance optimization: lazy load for plug-ins (hook subscription cache) - Part I: Preparative Refactoring
parent b97417bd
<?php
/**
* @file classes/plugins/BlockPlugin.inc.php
*
* Copyright (c) 2003-2010 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class BlockPlugin
* @ingroup plugins
*
* @brief Abstract class for block plugins
*/
// $Id$
define('BLOCK_CONTEXT_LEFT_SIDEBAR', 0x00000001);
define('BLOCK_CONTEXT_RIGHT_SIDEBAR', 0x00000002);
define('BLOCK_CONTEXT_HOMEPAGE', 0x00000003);
class BlockPlugin extends Plugin {
function BlockPlugin() {
parent::Plugin();
}
function register($category, $path) {
$success = parent::register($category, $path);
if ($success && $this->getEnabled()) {
$contextMap =& $this->getContextMap();
$blockContext = $this->getBlockContext();
if (isset($contextMap[$blockContext])) {
$hookName = $contextMap[$blockContext];
HookRegistry::register($hookName, array(&$this, 'callback'));
}
}
return $success;
}
/**
* Get the block context (e.g. BLOCK_CONTEXT_...) for this block.
* @return int
*/
function getBlockContext() {
$journal =& Request::getJournal();
$journalId = ($journal?$journal->getId():0);
return $this->getSetting($journalId, 'context');
}
/**
* Set the block context (e.g. BLOCK_CONTEXT_...) for this block.
* @param context int
*/
function setBlockContext($context) {
$journal =& Request::getJournal();
$journalId = ($journal?$journal->getId():0);
return $this->updateSetting($journalId, 'context', $context, 'int');
}
/**
* Get the supported contexts (e.g. BLOCK_CONTEXT_...) for this block.
* @return array
*/
function getSupportedContexts() {
// must be implemented by sub-classes
assert(false);
}
/**
* Determine whether or not this plugin is currently enabled.
* @return boolean
*/
function getEnabled() {
$journal =& Request::getJournal();
$journalId = ($journal?$journal->getId():0);
return $this->getSetting($journalId, 'enabled');
}
/**
* Set whether or not this plugin is currently enabled.
* @param $enabled boolean
*/
function setEnabled($enabled) {
$journal =& Request::getJournal();
$journalId = ($journal?$journal->getId():0);
return $this->updateSetting($journalId, 'enabled', $enabled, 'bool');
}
/**
* Get the sequence information for this plugin.
* Higher numbers move plugins down the page compared to other blocks.
* @return int
*/
function getSeq() {
$journal =& Request::getJournal();
$journalId = ($journal?$journal->getId():0);
return $this->getSetting($journalId, 'seq');
}
/**
* Set the sequence information for this plugin.
* Higher numbers move plugins down the page compared to other blocks.
* @param i int
*/
function setSeq($i) {
$journal =& Request::getJournal();
$journalId = ($journal?$journal->getId():0);
return $this->updateSetting($journalId, 'seq', $i, 'int');
}
/**
* Get an associative array linking block context to hook name.
* @return array
*/
function &getContextMap() {
static $contextMap = array(
BLOCK_CONTEXT_LEFT_SIDEBAR => 'Templates::Common::LeftSidebar',
BLOCK_CONTEXT_RIGHT_SIDEBAR => 'Templates::Common::RightSidebar',
BLOCK_CONTEXT_HOMEPAGE => 'Templates::Index::journal'
);
HookRegistry::call('BlockPlugin::getContextMap', array(&$this, &$contextMap));
return $contextMap;
}
/**
* Get the name of this plugin. The name must be unique within
* its category.
* @return String name of plugin
*/
function getName() {
// This should not be used as this is an abstract class
return 'BlockPlugin';
}
/**
* Get the display name of this plugin.
* @return String
*/
function getDisplayName() {
// This name should never be displayed because child classes
// will override this method.
return 'Abstract Block Plugin';
}
/**
* Get a description of the plugin.
*/
function getDescription() {
return 'This is the BlockPlugin base class. Its functions can be overridden by subclasses to provide support for UI blocks.';
}
/**
* Get the filename of the template block. (Default behavior may
* be overridden through some combination of this function and the
* getContents function.)
* Returning null from this function results in an empty display.
* @return string
*/
function getBlockTemplateFilename() {
return 'block.tpl';
}
/**
* Get the HTML contents for this block.
* @param $templateMgr object
* @return string
*/
function getContents(&$templateMgr) {
$blockTemplateFilename = $this->getBlockTemplateFilename();
if ($blockTemplateFilename === null) return '';
return $templateMgr->fetch($this->getTemplatePath() . $blockTemplateFilename);
}
function callback($hookName, $args) {
$params =& $args[0];
$smarty =& $args[1];
$output =& $args[2];
$output .= $this->getContents($smarty);
return false;
}
}
?>
<?php
/**
* @file classes/plugins/GenericPlugin.inc.php
*
* Copyright (c) 2003-2010 John Willinsky
* Distributed under the GNU GPL v2. For full terms see the file docs/COPYING.
*
* @class GenericPlugin
* @ingroup plugins
*
* @brief Abstract class for generic plugins
*/
// $Id$
import('plugins.Plugin');
class GenericPlugin extends Plugin {
function GenericPlugin() {
parent::Plugin();
}
// No additional functions for now.
}
?>
......@@ -12,8 +12,6 @@
* @brief Abstract class for plugins
*/
// $Id$
import('plugins.PKPPlugin');
......@@ -25,32 +23,46 @@ class Plugin extends PKPPlugin {
parent::PKPPlugin();
}
/**
* Backwards compatible convenience version of
* the generic getContextSpecificSetting() method.
*
* @see PKPPlugin::getContextSpecificSetting()
*
* @param $journalId
* @param $name
*/
function getSetting($journalId, $name) {
if (defined('RUNNING_UPGRADE')) {
// Bug #2504: Make sure plugin_settings table is not
// used if it's not available.
$versionDao =& DAORegistry::getDAO('VersionDAO');
$version =& $versionDao->getCurrentVersion(null, true);
$version =& $versionDao->getCurrentVersion();
if ($version->compare('2.1.0') < 0) return null;
}
return parent::getSetting(array($journalId), $name);
return $this->getContextSpecificSetting(array($journalId), $name);
}
/**
* Update a plugin setting.
* Backwards compatible convenience version of
* the generic updateContextSpecificSetting() method.
*
* @see PKPPlugin::updateContextSpecificSetting()
*
* @param $journalId int
* @param $name string The name of the setting
* @param $value mixed
* @param $type string optional
*/
function updateSetting($journalId, $name, $value, $type = null) {
parent::updateSetting(array($journalId), $name, $value, $type);
$this->updateContextSpecificSetting(array($journalId), $name, $value, $type);
}
/**
* Get the filename of the settings data for this plugin to install
* when a journal is created (i.e. journal-level plugin settings).
* Subclasses using default settings should override this.
*
* @return string
*/
function getContextSpecificPluginSettingsFile() {
......@@ -62,7 +74,11 @@ class Plugin extends PKPPlugin {
/**
* For backwards compatibility only.
*
* New plug-ins should override getContextSpecificPluginSettingsFile
* New plug-ins should override getContextSpecificPluginSettingsFile()
*
* @see PKPPlugin::getContextSpecificPluginSettingsFile()
*
* @return string
*/
function getNewJournalPluginSettingsFile() {
return null;
......
......@@ -18,23 +18,6 @@
import('plugins.BlockPlugin');
class AuthorBiosBlockPlugin extends BlockPlugin {
function register($category, $path) {
$success = parent::register($category, $path);
if ($success) {
$this->addLocaleData();
}
return $success;
}
/**
* Get the name of this plugin. The name must be unique within
* its category.
* @return String name of plugin
*/
function getName() {
return 'AuthorBiosBlockPlugin';
}
/**
* Get the display name of this plugin.
* @return String
......
......@@ -16,4 +16,6 @@
<type>plugins.blocks</type>
<release>1.0.0.0</release>
<date>2010-04-05</date>
<lazy-load>1</lazy-load>
<class>AuthorBiosPlugin</class>
</version>
\ No newline at end of file
......@@ -18,14 +18,6 @@
import('plugins.BlockPlugin');
class DevelopedByBlockPlugin extends BlockPlugin {
function register($category, $path) {
$success = parent::register($category, $path);
if ($success) {
$this->addLocaleData();
}
return $success;
}
/**
* Determine whether the plugin is enabled. Overrides parent so that
* the plugin will be displayed during install.
......@@ -47,7 +39,7 @@ class DevelopedByBlockPlugin extends BlockPlugin {
* Install default settings on journal creation.
* @return string
*/
function getNewJournalPluginSettingsFile() {
function getContextSpecificPluginSettingsFile() {
return $this->getPluginPath() . '/settings.xml';
}
......@@ -61,14 +53,6 @@ class DevelopedByBlockPlugin extends BlockPlugin {
return parent::getBlockContext();
}
/**
* Get the supported contexts (e.g. BLOCK_CONTEXT_...) for this block.
* @return array
*/
function getSupportedContexts() {
return array(BLOCK_CONTEXT_LEFT_SIDEBAR, BLOCK_CONTEXT_RIGHT_SIDEBAR);
}
/**
* Determine the plugin sequence. Overrides parent so that
* the plugin will be displayed during install.
......@@ -78,15 +62,6 @@ class DevelopedByBlockPlugin extends BlockPlugin {
return parent::getSeq();
}
/**
* Get the name of this plugin. The name must be unique within
* its category.
* @return String name of plugin
*/
function getName() {
return 'DevelopedByBlockPlugin';
}
/**
* Get the display name of this plugin.
* @return String
......
......@@ -16,4 +16,6 @@
<type>plugins.blocks</type>
<release>1.0.0.0</release>
<date>2009-07-13</date>
<lazy-load>1</lazy-load>
<class>DevelopedByPlugin</class>
</version>
\ No newline at end of file
......@@ -16,14 +16,6 @@
import('plugins.BlockPlugin');
class DonationBlockPlugin extends BlockPlugin {
function register($category, $path) {
$success = parent::register($category, $path);
if ($success) {
$this->addLocaleData();
}
return $success;
}
/**
* Install default settings on system install.
* @return string
......@@ -36,7 +28,7 @@ class DonationBlockPlugin extends BlockPlugin {
* Install default settings on journal creation.
* @return string
*/
function getNewJournalPluginSettingsFile() {
function getContextSpecificPluginSettingsFile() {
return $this->getPluginPath() . '/settings.xml';
}
......@@ -50,14 +42,6 @@ class DonationBlockPlugin extends BlockPlugin {
return parent::getBlockContext();
}
/**
* Get the supported contexts (e.g. BLOCK_CONTEXT_...) for this block.
* @return array
*/
function getSupportedContexts() {
return array(BLOCK_CONTEXT_LEFT_SIDEBAR, BLOCK_CONTEXT_RIGHT_SIDEBAR);
}
/**
* Determine the plugin sequence. Overrides parent so that
* the plugin will be displayed during install.
......@@ -67,15 +51,6 @@ class DonationBlockPlugin extends BlockPlugin {
return parent::getSeq();
}
/**
* Get the name of this plugin. The name must be unique within
* its category.
* @return String name of plugin
*/
function getName() {
return 'DonationBlockPlugin';
}
/**
* Get the display name of this plugin.
* @return String
......@@ -90,14 +65,14 @@ class DonationBlockPlugin extends BlockPlugin {
function getDescription() {
return Locale::translate('plugins.block.user.description');
}
function getContents(&$templateMgr) {
$journal =& Request::getJournal();
if (!$journal) return '';
import('payment.ojs.OJSPaymentManager');
$paymentManager =& OJSPaymentManager::getManager();
$templateMgr->assign('donationEnabled', $paymentManager->donationEnabled());
return parent::getContents($templateMgr);
}
}
......
......@@ -16,4 +16,6 @@
<type>plugins.blocks</type>
<release>1.0.0.0</release>
<date>2009-07-13</date>
<lazy-load>1</lazy-load>
<class>DonationPlugin</class>
</version>
\ No newline at end of file
......@@ -21,7 +21,6 @@ class FontSizeBlockPlugin extends BlockPlugin {
function register($category, $path) {
$success = parent::register($category, $path);
if ($success) {
$this->addLocaleData();
$templateMgr =& TemplateManager::getManager();
$templateMgr->assign('fontIconPath', 'lib/pkp/templates/images/icons');
$additionalHeadData = $templateMgr->get_template_vars('additionalHeadData');
......@@ -56,7 +55,7 @@ class FontSizeBlockPlugin extends BlockPlugin {
* Install default settings on journal creation.
* @return string
*/
function getNewJournalPluginSettingsFile() {
function getContextSpecificPluginSettingsFile() {
return $this->getPluginPath() . '/settings.xml';
}
......@@ -70,14 +69,6 @@ class FontSizeBlockPlugin extends BlockPlugin {
return parent::getBlockContext();
}
/**
* Get the supported contexts (e.g. BLOCK_CONTEXT_...) for this block.
* @return array
*/
function getSupportedContexts() {
return array(BLOCK_CONTEXT_LEFT_SIDEBAR, BLOCK_CONTEXT_RIGHT_SIDEBAR);
}
/**
* Determine the plugin sequence. Overrides parent so that
* the plugin will be displayed during install.
......@@ -87,15 +78,6 @@ class FontSizeBlockPlugin extends BlockPlugin {
return parent::getSeq();
}
/**
* Get the name of this plugin. The name must be unique within
* its category.
* @return String name of plugin
*/
function getName() {
return 'FontSizeBlockPlugin';
}
/**
* Get the display name of this plugin.
* @return String
......
......@@ -16,4 +16,6 @@
<type>plugins.blocks</type>
<release>1.0.0.0</release>
<date>2009-07-13</date>
<lazy-load>1</lazy-load>
<class>FontSizePlugin</class>
</version>
\ No newline at end of file
......@@ -18,14 +18,6 @@
import('plugins.BlockPlugin');
class HelpBlockPlugin extends BlockPlugin {
function register($category, $path) {
$success = parent::register($category, $path);
if ($success) {
$this->addLocaleData();
}
return $success;
}
/**
* Determine whether the plugin is enabled. Overrides parent so that
* the plugin will be displayed during install.
......@@ -47,7 +39,7 @@ class HelpBlockPlugin extends BlockPlugin {
* Install default settings on journal creation.
* @return string
*/
function getNewJournalPluginSettingsFile() {
function getContextSpecificPluginSettingsFile() {
return $this->getPluginPath() . '/settings.xml';
}
......@@ -61,14 +53,6 @@ class HelpBlockPlugin extends BlockPlugin {
return parent::getBlockContext();
}
/**
* Get the supported contexts (e.g. BLOCK_CONTEXT_...) for this block.
* @return array
*/
function getSupportedContexts() {
return array(BLOCK_CONTEXT_LEFT_SIDEBAR, BLOCK_CONTEXT_RIGHT_SIDEBAR);
}
/**
* Determine the plugin sequence. Overrides parent so that
* the plugin will be displayed during install.
......@@ -78,15 +62,6 @@ class HelpBlockPlugin extends BlockPlugin {
return parent::getSeq();
}
/**
* Get the name of this plugin. The name must be unique within
* its category.
* @return String name of plugin
*/
function getName() {
return 'HelpBlockPlugin';
}
/**
* Get the display name of this plugin.
* @return String
......
......@@ -16,4 +16,6 @@
<type>plugins.blocks</type>
<release>1.0.0.0</release>
<date>2009-07-13</date>
<lazy-load>1</lazy-load>
<class>HelpPlugin</class>
</version>
\ No newline at end of file
......@@ -18,28 +18,11 @@
import('plugins.BlockPlugin');
class InformationBlockPlugin extends BlockPlugin {
function register($category, $path) {
$success = parent::register($category, $path);
if ($success) {
$this->addLocaleData();
}
return $success;
}
/**
* Get the name of this plugin. The name must be unique within
* its category.
* @return String name of plugin
*/
function getName() {
return 'InformationBlockPlugin';
}
/**
* Install default settings on journal creation.
* @return string
*/
function getNewJournalPluginSettingsFile() {
function getContextSpecificPluginSettingsFile() {
return $this->getPluginPath() . '/settings.xml';
}
......@@ -58,14 +41,6 @@ class InformationBlockPlugin extends BlockPlugin {
return Locale::translate('plugins.block.information.description');
}
/**
* Get the supported contexts (e.g. BLOCK_CONTEXT_...) for this block.
* @return array
*/
function getSupportedContexts() {
return array(BLOCK_CONTEXT_LEFT_SIDEBAR, BLOCK_CONTEXT_RIGHT_SIDEBAR);
}
/**
* Get the HTML contents for this block.
* @param $templateMgr object
......
......@@ -16,4 +16,6 @@
<type>plugins.blocks</type>
<release>1.0.0.0</release>