Skip to content

Commit

Permalink
pkp/pkp-lib#5523: Rename and recontextualize blocks on upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
ctgraham committed Mar 2, 2020
1 parent 1ad051e commit 1a15d95
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 8 deletions.
68 changes: 68 additions & 0 deletions CustomBlockManagerPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ function getDescription() {
return __('plugins.generic.customBlockManager.description');
}

/**
* Translate a user-entered name into a child CustomBlockPlugin name
* @param $blockName string User-entered name
* @return string
*/
function getChildName($blockName) {
return $this->getName().'::'.$blockName;
}

/**
* Translate a system name into a user-facing child CustomBlockPlugin name
* @param $blockName string system name
* @return string
*/
function getChildDisplayName($blockName) {
$prefix = $this->getName().'::';
if (strpos($blockName, $prefix) === 0) {
$blockName = substr($blockName, strlen($prefix));
}
return $blockName;
}

/**
* @copydoc Plugin::register()
*/
Expand Down Expand Up @@ -72,6 +94,7 @@ function register($category, $path, $mainContextId = null) {
// permit administration of custom block plugins.
HookRegistry::register('LoadComponentHandler', array($this, 'setupGridHandler'));
}
HookRegistry::register('Installer::postInstall', array($this, 'upgradeData'));
return true;
}
return false;
Expand Down Expand Up @@ -146,4 +169,49 @@ function manage($args, $request) {
function isSitePlugin() {
return !Application::getRequest()->getContext();
}

/**
* We will need to modify data in certain upgrades
*
* @param $hookName string
* @param $args array
* @return boolean
*/
function upgradeData($hookName, $args) {
// There is no opportunity to hook the upgrade event before the new version is written to the versions table
// So, we need to look at the immediately preceeding version, and (re)apply fixes based on guesswork.
$versionDao = DAORegistry::getDAO('VersionDAO');
$contextDao = Application::getContextDAO();
$historicVersions = $versionDao->getVersionHistory('plugins.generic', 'customBlockManager');
if (count($historicVersions) > 1 && $historicVersions[1]->compare('1.3.0') < 0) {
// The last installed version is prior to 1.3.0
// We need up update the plugin_settings names and move any orphaned sidebar contexts
$contexts = $contextDao->getAll();
while ($context = $contexts->next()) {
// Load the custom blocks we have created
$blocks = $this->getSetting($context->getId(), 'blocks');
if (!is_array($blocks)) $blocks = array();
$pluginSettingsDao = DAORegistry::getDAO('PluginSettingsDAO');
foreach ($blocks as $block) {
// Update plugin_settings, if settings are stored directly as the block name
$settings = $pluginSettingsDao->getPluginSettings($context->getId(), $block);
foreach ($settings as $setting) {
switch ($setting['setting_name']) {
case 'context':
// No constant name (!?!), per https://github.com/pkp/pkp-lib/commit/a76bac72ed068a1d1866398d20cdf28c4977249f#diff-70caff5ef9a513397af1833a3e2a3c7c
$setting['setting_value'] = 1; // BLOCK_CONTEXT_SIDEBAR
case 'blockContent':
case 'enabled':
$pluginSettingsDao->deleteSetting($context->getId(), $block, $setting['setting_name']);
$pluginSettingsDao->updateSetting($context->getId(), $this->getChildName($block), $setting['setting_name'], $setting['setting_value'], $setting['setting_type']);
break;
default:
error_log('found a setting "'.$setting['setting_name'].'", colliding with custom block "'.$block.'"');
}
}
}
}
}
return false;
}
}
4 changes: 2 additions & 2 deletions CustomBlockPlugin.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class CustomBlockPlugin extends BlockPlugin {
* @param $parentPlugin CustomBlockManagerPlugin Custom block plugin management plugin.
*/
function __construct($blockName, $parentPlugin) {
$this->_blockName = $blockName;
$this->_blockName = $parentPlugin->getChildName($blockName);
$this->_parentPlugin = $parentPlugin;
parent::__construct();
}
Expand Down Expand Up @@ -84,7 +84,7 @@ function getEnabled($contextId = null) {
* @copydoc Plugin::getDisplayName()
*/
function getDisplayName() {
return $this->_blockName . ' ' . __('plugins.generic.customBlock.nameSuffix');
return $this->_parentPlugin->getChildDisplayName($this->_blockName) . ' ' . __('plugins.generic.customBlock.nameSuffix');
}

/**
Expand Down
4 changes: 2 additions & 2 deletions controllers/grid/CustomBlockGridHandler.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ function editCustomBlock($args, $request) {
if ($blockName) {
// Create the custom block plugin
import('plugins.generic.customBlockManager.CustomBlockPlugin');
$customBlockPlugin = new CustomBlockPlugin($blockName, CUSTOMBLOCKMANAGER_PLUGIN_NAME);
$customBlockPlugin = new CustomBlockPlugin($blockName, $this->plugin);
}

// Create and present the edit form
Expand Down Expand Up @@ -171,7 +171,7 @@ function updateCustomBlock($args, $request) {
if ($pluginName) {
// Create the custom block plugin
import('plugins.generic.customBlockManager.CustomBlockPlugin');
$customBlockPlugin = new CustomBlockPlugin($pluginName, CUSTOMBLOCKMANAGER_PLUGIN_NAME);
$customBlockPlugin = new CustomBlockPlugin($pluginName, $this->plugin);
}

// Create and populate the form
Expand Down
4 changes: 2 additions & 2 deletions controllers/grid/form/CustomBlockForm.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ function __construct($template, $contextId, $plugin = null) {
$this->addCheck(new FormValidatorPost($this));
$this->addCheck(new FormValidatorCSRF($this));
$this->addCheck(new FormValidator($this, 'blockName', 'required', 'plugins.generic.customBlock.nameRequired'));
$this->addCheck(new FormValidatorRegExp($this, 'blockName', 'required', 'plugins.generic.customBlock.nameRegEx', '/^[a-zA-Z0-9_-]+$/'));
}

/**
Expand All @@ -54,7 +53,8 @@ function initData() {
$blockName = null;
$blockContent = null;
if ($plugin) {
$blockName = $plugin->getName();
$cbm = PluginRegistry::getPlugin('generic', 'customblockmanagerplugin');
$blockName = $cbm->getChildDisplayName($plugin->getName());
$blockContent = $plugin->getSetting($contextId, 'blockContent');
}
$this->setData('blockContent', $blockContent);
Expand Down
4 changes: 2 additions & 2 deletions version.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
<version>
<application>customBlockManager</application>
<type>plugins.generic</type>
<release>1.2.0.0</release>
<date>2014-09-19</date>
<release>1.3.0.0</release>
<date>2020-03-02</date>
<lazy-load>1</lazy-load>
<class>CustomBlockManagerPlugin</class>
</version>

0 comments on commit 1a15d95

Please sign in to comment.