Open Monograph Press  1.1
 All Classes Namespaces Functions Variables Groups Pages
Filter.inc.php
1 <?php
75 import('lib.pkp.classes.core.DataObject');
76 import('lib.pkp.classes.filter.TypeDescriptionFactory');
77 
78 class Filter extends DataObject {
80  var $_inputType;
81 
83  var $_outputType;
84 
86  var $_input;
87 
89  var $_output;
90 
92  var $_errors = array();
93 
98  var $_runtimeEnvironment = false;
99 
109  function Filter($inputType, $outputType) {
110  // Initialize the filter.
111  parent::DataObject();
112  $this->setTransformationType($inputType, $outputType);
113  }
114 
115  //
116  // Setters and Getters
117  //
122  function setDisplayName($displayName) {
123  $this->setData('displayName', $displayName);
124  }
125 
137  function getDisplayName() {
138  if (!$this->hasData('displayName')) {
139  $this->setData('displayName', get_class($this));
140  }
141 
142  return $this->getData('displayName');
143  }
144 
149  function setSeq($seq) {
150  $this->setData('seq', $seq);
151  }
152 
157  function getSeq() {
158  return $this->getData('seq');
159  }
160 
169  function setTransformationType(&$inputType, &$outputType) {
170  $typeDescriptionFactory = TypeDescriptionFactory::getInstance();
171 
172  // Instantiate the type descriptions if we got string input.
173  if (!is_a($inputType, 'TypeDescription')) {
174  assert(is_string($inputType));
175  $inputType = $typeDescriptionFactory->instantiateTypeDescription($inputType);
176  }
177  if (!is_a($outputType, 'TypeDescription')) {
178  assert(is_string($outputType));
179  $outputType = $typeDescriptionFactory->instantiateTypeDescription($outputType);
180  }
181 
182  $this->_inputType = $inputType;
183  $this->_outputType = $outputType;
184  }
185 
186 
191  function &getInputType() {
192  return $this->_inputType;
193  }
194 
199  function &getOutputType() {
200  return $this->_outputType;
201  }
202 
219  function &getLastOutput() {
220  return $this->_output;
221  }
222 
239  function &getLastInput() {
240  return $this->_input;
241  }
242 
247  function addError($message) {
248  $this->_errors[] = $message;
249  }
250 
255  function getErrors() {
256  return $this->_errors;
257  }
258 
263  function hasErrors() {
264  return (!empty($this->_errors));
265  }
266 
270  function clearErrors() {
271  $this->_errors = array();
272  }
278  function setRuntimeEnvironment(&$runtimeEnvironment) {
279  assert(is_a($runtimeEnvironment, 'RuntimeEnvironment'));
280  $this->_runtimeEnvironment =& $runtimeEnvironment;
282  // Inject the runtime settings into the data object
283  // for persistence.
284  $runtimeSettings = $this->supportedRuntimeEnvironmentSettings();
285  foreach($runtimeSettings as $runtimeSetting => $defaultValue) {
286  $methodName = 'get'.String::ucfirst($runtimeSetting);
287  $this->setData($runtimeSetting, $runtimeEnvironment->$methodName());
288  }
289  }
290 
295  function &getRuntimeEnvironment() {
297  }
298 
299 
300  //
301  // Abstract template methods to be implemented by subclasses
302  //
310  function &process(&$input) {
311  assert(false);
312  }
314  //
315  // Public methods
316  //
340  function supports(&$input, &$output) {
341  // Validate input
342  $inputType =& $this->getInputType();
343  $validInput = $inputType->isCompatible($input);
344 
345  // If output is null then we're done
346  if (is_null($output)) return $validInput;
347 
348  // Validate output
349  $outputType =& $this->getOutputType();
350  $validOutput = $outputType->isCompatible($output);
351 
352  return $validInput && $validOutput;
353  }
354 
365  function supportsAsInput(&$input) {
366  $nullVar = null;
367  return($this->supports($input, $nullVar));
368  }
369 
376  if ($this->_runtimeEnvironment === false) {
377  // The runtime environment has never been
378  // queried before.
379  $runtimeSettings = $this->supportedRuntimeEnvironmentSettings();
380 
381  // Find out whether we have any runtime restrictions set.
382  $hasRuntimeSettings = false;
383  foreach($runtimeSettings as $runtimeSetting => $defaultValue) {
384  if ($this->hasData($runtimeSetting)) {
385  $$runtimeSetting = $this->getData($runtimeSetting);
386  $hasRuntimeSettings = true;
387  } else {
388  $$runtimeSetting = $defaultValue;
389  }
390  }
391 
392  // If we found any runtime restrictions then construct a
393  // runtime environment from the settings.
394  if ($hasRuntimeSettings) {
395  import('lib.pkp.classes.core.RuntimeEnvironment');
396  $this->_runtimeEnvironment = new RuntimeEnvironment($phpVersionMin, $phpVersionMax, $phpExtensions, $externalPrograms);
397  } else {
398  // Set null so that we don't try to construct
399  // a runtime environment object again.
400  $this->_runtimeEnvironment = null;
401  }
402  }
403 
404  if (is_null($this->_runtimeEnvironment) || $this->_runtimeEnvironment->isCompatible()) return true;
405 
406  return false;
407  }
408 
424  function &execute(&$input) {
425  // Make sure that we don't destroy referenced
426  // data somewhere out there.
427  unset($this->_input, $this->_output);
428 
429  // Check the runtime environment
430  if (!$this->isCompatibleWithRuntimeEnvironment()) {
431  // Missing installation requirements.
432  fatalError('Trying to run a transformation that is not supported in your installation environment.');
433  }
434 
435  // Validate the filter input
436  if (!$this->supportsAsInput($input)) {
437  // We have no valid input so return
438  // an empty output (see unset statement
439  // above).
440  return $this->_output;
441  }
443  // Save a reference to the last valid input
444  $this->_input =& $input;
445 
446  // Process the filter
447  $preliminaryOutput =& $this->process($input);
448 
449  // Validate the filter output
450  if (!is_null($preliminaryOutput) && $this->supports($input, $preliminaryOutput)) {
451  $this->_output =& $preliminaryOutput;
452  }
453 
454  // Return processed data
455  return $this->_output;
456  }
457 
458  //
459  // Public helper methods
460  //
467  static function supportedRuntimeEnvironmentSettings() {
468  static $runtimeEnvironmentSettings = array(
469  'phpVersionMin' => PHP_REQUIRED_VERSION,
470  'phpVersionMax' => null,
471  'phpExtensions' => array(),
472  'externalPrograms' => array()
473  );
474 
475  return $runtimeEnvironmentSettings;
476  }
477 }
478 
479 ?>
$_runtimeEnvironment
Definition: Filter.inc.php:116
hasErrors()
Definition: Filter.inc.php:281
isCompatibleWithRuntimeEnvironment()
Definition: Filter.inc.php:393
supports(&$input, &$output)
Definition: Filter.inc.php:358
setTransformationType(&$inputType, &$outputType)
Definition: Filter.inc.php:187
Filter($inputType, $outputType)
Definition: Filter.inc.php:127
setDisplayName($displayName)
Definition: Filter.inc.php:140
Class that provides the basic template for a filter. Filters are generic data processors that take in...
Definition: Filter.inc.php:78
& process(&$input)
Definition: Filter.inc.php:328
& getLastOutput()
Definition: Filter.inc.php:237
& getLastInput()
Definition: Filter.inc.php:257
clearErrors()
Definition: Filter.inc.php:288
getErrors()
Definition: Filter.inc.php:273
& getData($key, $locale=null)
& getInputType()
Definition: Filter.inc.php:209
Class that describes a runtime environment.
supportsAsInput(&$input)
Definition: Filter.inc.php:383
setSeq($seq)
Definition: Filter.inc.php:167
setRuntimeEnvironment(&$runtimeEnvironment)
Definition: Filter.inc.php:296
static supportedRuntimeEnvironmentSettings()
Definition: Filter.inc.php:485
Any class with an associated DAO should extend this class.
setData($key, $value, $locale=null)
addError($message)
Definition: Filter.inc.php:265
hasData($key, $locale=null)
getDisplayName()
Definition: Filter.inc.php:155
& execute(&$input)
Definition: Filter.inc.php:442
& getRuntimeEnvironment()
Definition: Filter.inc.php:313
& getOutputType()
Definition: Filter.inc.php:217