Skip to content

Commit

Permalink
*8122* Fix pre/post processor plugins
Browse files Browse the repository at this point in the history
  • Loading branch information
asmecher committed Feb 22, 2013
1 parent 606ded8 commit be40165
Show file tree
Hide file tree
Showing 14 changed files with 217 additions and 401 deletions.
6 changes: 4 additions & 2 deletions classes/harvester/Harvester.inc.php
Expand Up @@ -172,13 +172,15 @@ function _insertRecord($identifier, &$contents) {
$record->setSchemaId($schemaId);
$record->setArchiveId($this->archive->getArchiveId());
$record->setContents($contents);
$record->setParsedContents($schemaPlugin->parseContents($contents));
HookRegistry::call('Harvester::preprocessRecord', array(&$record, &$this->archive, &$schema));
$record->setParsedContents($schemaPlugin->parseContents($record->getContents()));
$record->setIdentifier($identifier);
$this->recordDao->insertRecord($record);

$this->indexRecordSorting($record);

HookRegistry::call('Harvester::insertRecord', array(&$record));
HookRegistry::call('Harvester::insertRecord', array(&$record, &$this->archive, &$schema));
HookRegistry::call('Harvester::postprocessRecord', array(&$record, &$this->archive, &$schema));

return true;
}
Expand Down
77 changes: 65 additions & 12 deletions classes/plugins/PostprocessorPlugin.inc.php
Expand Up @@ -20,25 +20,82 @@
import('classes.plugins.Plugin');

class PostprocessorPlugin extends Plugin {
/**
* Constructor
*/
function PostprocessorPlugin() {
parent::Plugin();
}

/**
* Determine whether or not this plugin is currently enabled.
* @return boolean
*/
function isEnabled() {
return $this->getSetting('enabled');
}

function register($category, $path) {
$result = parent::register($category, $path);
if ($result) {
HookRegistry::register('SchemaPlugin::indexRecord', array(&$this, '_postprocessEntry'));
HookRegistry::register('Harvester::postprocessRecord', array(&$this, '_postprocessRecord'));
}
return $result;
}

function _postprocessEntry($hookName, $args) {
$archive =& $args[0];
$record =& $args[1];
$field =& $args[2];
$value =& $args[3];
$attributes =& $args[4];
return $this->postprocessEntry($archive, $record, $field, $value, $attributes);
/**
* Return the set of management verbs supported by this plugin for the
* administration interface.
* @return array
*/
function getManagementVerbs() {
if ($this->isEnabled()) return array(
array('disable', __('common.disable'))
);
else return array(
array('enable', __('common.enable'))
);
}

/**
* Perform a management function on this plugin.
* @param $verb string
* @param $params array
*/
function manage($verb, $params) {
switch ($verb) {
case 'enable':
$this->updateSetting('enabled', true);
break;
case 'disable':
$this->updateSetting('enabled', false);
break;
}
Request::redirect('admin', 'plugins');
}

/**
* Wrapper around postprocessRecord to process a record via a hook.
* @param $hookName string
* @param $args array
* @retrn boolean Hook callback status
*/
function _postprocessRecord($hookName, $args) {
$record =& $args[0];
$archive =& $args[1];
$schema =& $args[2];
return $this->postprocessRecord($record, $archive, $schema);
}

/**
* Post-process a record.
* @param $record Record record
* @param $archive Archive Archive in which this record will be inserted
* @param $schema Schema The schema this record is described in
* @return boolean Hook processing status
*/
function postprocessRecord(&$record, &$archive, &$schema) {
assert(false); // Subclasses to override
}

/**
Expand All @@ -55,10 +112,6 @@ function getName() {
function getDescription() {
fatalError('ABSTRACT CLASS');
}

function postprocessEntry(&$archive, &$record, &$field, &$value, &$attributes) {
fatalError('ABSTRACT CLASS');
}
}

?>
83 changes: 70 additions & 13 deletions classes/plugins/PreprocessorPlugin.inc.php
Expand Up @@ -18,25 +18,86 @@
import('classes.plugins.Plugin');

class PreprocessorPlugin extends Plugin {
/**
* Constructor
*/
function PreprocessorPlugin() {
parent::Plugin();
}

/**
* Determine whether or not this plugin is currently enabled.
* @return boolean
*/
function isEnabled() {
return $this->getSetting('enabled');
}

/**
* Register the plugin.
* @see Plugin::register
*/
function register($category, $path) {
$result = parent::register($category, $path);
if ($result) {
HookRegistry::register('Harvester::insertEntry', array(&$this, '_preprocessEntry'));
if ($result && $this->isEnabled()) {
HookRegistry::register('Harvester::preprocessRecord', array(&$this, '_preprocessRecord'));
}
return $result;
}

function _preprocessEntry($hookName, $args) {
$archive =& $args[0];
$record =& $args[1];
$field =& $args[2];
$value =& $args[3];
$attributes =& $args[4];
return $this->preprocessEntry($archive, $record, $field, $value, $attributes);
/**
* Return the set of management verbs supported by this plugin for the
* administration interface.
* @return array
*/
function getManagementVerbs() {
if ($this->isEnabled()) return array(
array('disable', __('common.disable'))
);
else return array(
array('enable', __('common.enable'))
);
}

/**
* Perform a management function on this plugin.
* @param $verb string
* @param $params array
*/
function manage($verb, $params) {
switch ($verb) {
case 'enable':
$this->updateSetting('enabled', true);
break;
case 'disable':
$this->updateSetting('enabled', false);
break;
}
Request::redirect('admin', 'plugins');
}

/**
* Hook handler for record preprocessing (wraps around preprocessRecord)
* @param $hookName string
* @param $args array
* @return boolean Hook callback status
*/
function _preprocessRecord($hookName, $args) {
$record =& $args[0];
$archive =& $args[1];
$schema =& $args[2];
return $this->preprocessRecord($record, $archive, $schema);
}

/**
* Preprocess a record.
* @param $record Record Record object ready for insertion
* @param $archive Archive Archive in which this record will be inserted
* @param $schema Schema The schema this record is described in
* @return boolean Hook callback status
*/
function preprocessRecord(&$record, &$archive, &$schema) {
assert(false); // To be overridden by subclasses.
}

/**
Expand All @@ -53,10 +114,6 @@ function getName() {
function getDescription() {
fatalError('ABSTRACT CLASS');
}

function preprocessEntry(&$archive, &$record, &$field, &$value, &$attributes) {
fatalError('ABSTRACT CLASS');
}
}

?>
2 changes: 0 additions & 2 deletions classes/plugins/SchemaPlugin.inc.php
Expand Up @@ -28,8 +28,6 @@ function SchemaPlugin() {
function register($category, $path) {
$success = parent::register($category, $path);
if ($success) {
// Make sure postprocessors are loaded.
PluginRegistry::loadCategory('postprocessors');
HookRegistry::register('OAI::metadataFormats', array(&$this, 'callback_formatRequest'));
}
return $success;
Expand Down
2 changes: 2 additions & 0 deletions pages/admin/AdminArchiveHandler.inc.php
Expand Up @@ -178,6 +178,8 @@ function updateIndex($args, &$request) {
@set_time_limit(0);

// Get the harvester for this archive
PluginRegistry::loadCategory('preprocessors');
PluginRegistry::loadCategory('postprocessors');
$plugins =& PluginRegistry::loadCategory('harvesters');
$pluginName = $archive->getHarvesterPluginName();
if (!isset($plugins[$pluginName])) $request->redirect('admin', 'manage', $archive->getArchiveId());
Expand Down
1 change: 1 addition & 0 deletions plugins/generic/mysqlIndex/MysqlIndexPlugin.inc.php
Expand Up @@ -51,6 +51,7 @@ function register($category, $path) {

// Record handling & harvesting
HookRegistry::register('Harvester::insertRecord', array(&$this, 'insertRecordCallback'));
HookRegistry::register('Harvester::indexRecord', array(&$this, 'insertRecordCallback'));
HookRegistry::register('Harvester::updateRecord', array(&$this, 'updateRecordCallback'));
HookRegistry::register('Harvester::deleteRecord', array(&$this, 'deleteRecordCallback'));

Expand Down
1 change: 1 addition & 0 deletions plugins/generic/zendSearch/ZendSearchPlugin.inc.php
Expand Up @@ -53,6 +53,7 @@ function register($category, $path) {

// Record handling & harvesting
HookRegistry::register('Harvester::insertRecord', array(&$this, 'insertRecordCallback'));
HookRegistry::register('Harvester::indexRecord', array(&$this, 'insertRecordCallback'));
HookRegistry::register('Harvester::updateRecord', array(&$this, 'updateRecordCallback'));
HookRegistry::register('Harvester::deleteRecord', array(&$this, 'deleteRecordCallback'));

Expand Down
Expand Up @@ -9,41 +9,44 @@
* @package plugins.preprocessors.languagemap
* @class LanguageMapPreprocessorPlugin
*
* Test preprocessor plugin
*
* $Id$
*/

import('classes.plugins.PreprocessorPlugin');

define('LANGUAGE_MAP_FILE', 'mapping.xml');

class LanguageMapPreprocessorPlugin extends PreprocessorPlugin {
/** @var $languageCrosswalk object */
var $languageCrosswalkFieldIds;
/** @var $languageCrosswalkFieldNames object */
var $languageCrosswalkFieldNames;

/** @var $mappingCache */
var $mappingCache;

/**
* Constructor
*/
function LanguageMapPreprocessorPlugin() {
parent::PreprocessorPlugin();
}

/**
* Register the plugin.
* @param $category string
* @param $path string
*/
function register($category, $path) {
$success = parent::register($category, $path);
$this->languageCrosswalkFieldIds = null;
$this->languageCrosswalkFieldNames = array();
if ($success && $this->isEnabled()) {
// Fetch the list of field IDs that the language
// crosswalk uses; we will map all languages mentioned
// in these fields.
$this->languageCrosswalkFieldIds = array();
$crosswalkDao =& DAORegistry::getDAO('CrosswalkDAO');
$languageCrosswalk =& $crosswalkDao->getCrosswalkByPublicCrosswalkId('language');
if ($languageCrosswalk) {
$fields =& $languageCrosswalk->getFields();
while ($field =& $fields->next()) {
$this->languageCrosswalkFieldIds[] = $field->getFieldId();
$this->languageCrosswalkFieldNames[$field->getSchemaId()][] = $field->getName();
unset($field);
}
}
Expand Down Expand Up @@ -136,60 +139,26 @@ function getDescription() {
}

/**
* This callback implements the actual map and is called before an
* entry is inserted.
* @param $archive object
* @param $record object
* @param $field object
* @param $value string
* @param $attributes array
* @return boolean
* Preprocess a record.
* @param $record Record Record object ready for insertion
* @param $archive Archive
* @param $schema Schema
* @return boolean Hook callback status
*/
function preprocessEntry(&$archive, &$record, &$field, &$value, &$attributes) {
if (is_array($this->languageCrosswalkFieldIds) && in_array($field->getFieldId(), $this->languageCrosswalkFieldIds)) {
$value = $this->mapLanguage($value);
function preprocessRecord(&$record, &$archive, &$schema) {
if (isset($this->languageCrosswalkFieldNames[$schema->getSchemaId()])) {
$doc = new DOMDocument();
$doc->loadXML($record->getContents());
foreach($this->languageCrosswalkFieldNames[$schema->getSchemaId()] as $fieldName) {
foreach ($doc->getElementsByTagName($fieldName) as $element) {
$element->nodeValue = $this->mapLanguage($archive, $element->nodeValue);
}
}
$record->setContents($doc->saveXML());
}
return false;
}

/**
* Return the set of management verbs supported by this plugin for the
* administration interface.
* @return array
*/
function getManagementVerbs() {
if ($this->isEnabled()) return array(
array('disable', __('common.disable'))
);
else return array(
array('enable', __('common.enable'))
);
}

/**
* Perform a management function on this plugin.
* @param $verb string
* @param $params array
*/
function manage($verb, $params) {
switch ($verb) {
case 'enable':
$this->updateSetting('enabled', true);
break;
case 'disable':
$this->updateSetting('enabled', false);
break;
}
Request::redirect('admin', 'plugins');
}

/**
* Determine whether or not this plugin is currently enabled.
* @return boolean
*/
function isEnabled() {
return $this->getSetting('enabled');
}
}

?>

0 comments on commit be40165

Please sign in to comment.