Skip to content

Commit

Permalink
#10671 Fix filters for user import/export plugin and classloading mod…
Browse files Browse the repository at this point in the history
…ernization (#10780)
  • Loading branch information
asmecher authored Jan 8, 2025
1 parent fcaa4ef commit 4fd34f7
Show file tree
Hide file tree
Showing 15 changed files with 127 additions and 70 deletions.
10 changes: 6 additions & 4 deletions classes/filter/ClassTypeDescription.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @file classes/filter/ClassTypeDescription.php
*
Expand All @@ -20,10 +21,10 @@
class ClassTypeDescription extends TypeDescription
{
/** @var string a valid class name */
public $_className;
public string $_className;

/** @var string a valid package name */
public $_packageName;
public string $_packageName;

//
// Setters and Getters
Expand All @@ -43,7 +44,7 @@ public function getNamespace()
/**
* @see TypeDescription::parseTypeName()
*/
public function parseTypeName($typeName)
public function parseTypeName(string $typeName): bool
{
$splitName = $this->splitClassName($typeName);
if ($splitName === false) {
Expand All @@ -61,7 +62,7 @@ public function parseTypeName($typeName)
/**
* @see TypeDescription::checkType()
*/
public function checkType($object)
public function checkType($object): bool
{
// We expect an object
if (!is_object($object)) {
Expand Down Expand Up @@ -93,6 +94,7 @@ public function checkType($object)
public function splitClassName($typeName)
{
// This should be a class - identify package and class name
// This behaviour is DEPRECATED with pkp/pkp-lib#8186
$typeNameParts = explode('.', $typeName);

$className = array_pop($typeNameParts);
Expand Down
15 changes: 8 additions & 7 deletions classes/filter/Filter.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @file classes/filter/Filter.php
*
Expand Down Expand Up @@ -187,15 +188,15 @@ public function setTransformationType(TypeDescription|string &$inputType, TypeDe
/**
* Get the input type
*/
public function &getInputType(): TypeDescription
public function getInputType(): TypeDescription
{
return $this->_inputType;
}

/**
* Get the output type
*/
public function &getOutputType(): TypeDescription
public function getOutputType(): TypeDescription
{
return $this->_outputType;
}
Expand Down Expand Up @@ -278,9 +279,9 @@ public function clearErrors(): void
* Set the required runtime environment
*
*/
public function setRuntimeEnvironment(RuntimeEnvironment &$runtimeEnvironment): void
public function setRuntimeEnvironment(RuntimeEnvironment $runtimeEnvironment): void
{
$this->_runtimeEnvironment = &$runtimeEnvironment;
$this->_runtimeEnvironment = $runtimeEnvironment;

// Inject the runtime settings into the data object
// for persistence.
Expand All @@ -294,7 +295,7 @@ public function setRuntimeEnvironment(RuntimeEnvironment &$runtimeEnvironment):
/**
* Get the required runtime environment
*/
public function &getRuntimeEnvironment(): RuntimeEnvironment
public function getRuntimeEnvironment(): RuntimeEnvironment
{
return $this->_runtimeEnvironment;
}
Expand Down Expand Up @@ -339,7 +340,7 @@ abstract public function &process(&$input);
public function supports(&$input, &$output): bool
{
// Validate input
$inputType = &$this->getInputType();
$inputType = $this->getInputType();
$validInput = $inputType->isCompatible($input);

// If output is null then we're done
Expand All @@ -348,7 +349,7 @@ public function supports(&$input, &$output): bool
}

// Validate output
$outputType = &$this->getOutputType();
$outputType = $this->getOutputType();
$validOutput = $outputType->isCompatible($output);

return $validInput && $validOutput;
Expand Down
4 changes: 2 additions & 2 deletions classes/filter/FilterGroupDAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class FilterGroupDAO extends \PKP\db\DAO
*
* @return int the new filter group id
*/
public function insertObject(FilterGroup &$filterGroup): int
public function insertObject(FilterGroup $filterGroup): int
{
$this->update(
sprintf('INSERT INTO filter_groups
Expand Down Expand Up @@ -87,7 +87,7 @@ public function getObjectBySymbolic(string $filterGroupSymbolic): ?FilterGroup
/**
* Update an existing filter group.
*/
public function updateObject(FilterGroup $filterGroup)
public function updateObject(FilterGroup $filterGroup): void
{
$this->update(
'UPDATE filter_groups
Expand Down
5 changes: 3 additions & 2 deletions classes/filter/PrimitiveTypeDescription.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @file classes/filter/PrimitiveTypeDescription.php
*
Expand Down Expand Up @@ -39,7 +40,7 @@ public function getNamespace()
/**
* @see TypeDescription::parseTypeName()
*/
public function parseTypeName($typeName)
public function parseTypeName(string $typeName): bool
{
// This should be a primitive type
if (!in_array($typeName, $this->_supportedPrimitiveTypes())) {
Expand All @@ -53,7 +54,7 @@ public function parseTypeName($typeName)
/**
* @see TypeDescription::checkType()
*/
public function checkType($object)
public function checkType($object): bool
{
// We expect a primitive type
if (!is_scalar($object)) {
Expand Down
12 changes: 3 additions & 9 deletions classes/filter/TypeDescription.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @file classes/filter/TypeDescription.php
*
Expand Down Expand Up @@ -179,20 +180,13 @@ public function isCompatible($object)
//
/**
* Parse a type name
*
* @param string $typeName
*
* @return bool true if success, otherwise false
*/
abstract public function parseTypeName($typeName);
abstract public function parseTypeName(string $typeName): bool;

/**
* Validates an object against the internal type description.
*
*
* @return bool
*/
abstract public function checkType($object);
abstract public function checkType($object): bool;


//
Expand Down
34 changes: 15 additions & 19 deletions classes/filter/TypeDescriptionFactory.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @file classes/filter/TypeDescriptionFactory.php
*
Expand Down Expand Up @@ -80,29 +81,28 @@ public static function getInstance()
*
* @return TypeDescription|null if the type description is invalid.
*/
public function &instantiateTypeDescription($typeDescription)
public function instantiateTypeDescription($typeDescription): ?TypeDescription
{
$nullVar = null;

// Identify the namespace
$typeDescriptionParts = explode('::', $typeDescription);
if (count($typeDescriptionParts) != 2) {
return $nullVar;
return null;
}

// Map the namespace to a type description class
$typeDescriptionClass = $this->_namespaceMap($typeDescriptionParts[0]);
if (is_null($typeDescriptionClass)) {
return $nullVar;
return null;
}

// Instantiate and return the type description object
$typeDescriptionObject = & instantiate($typeDescriptionClass, 'TypeDescription', null, null, $typeDescriptionParts[1]);
if (!is_object($typeDescriptionObject)) {
return $nullVar;
if (strpos($typeDescriptionClass, '.')) {
// DEPRECATED: Use old instantiate function
return instantiate($typeDescriptionClass, 'TypeDescription', null, null, $typeDescriptionParts[1]);
} else {
// New behaviour: Use fully qualified class name
return new $typeDescriptionClass();
}

return $typeDescriptionObject;
}


Expand All @@ -114,21 +114,17 @@ public function &instantiateTypeDescription($typeDescription)
* class name.
*
* FIXME: Move this map to the Application object.
*
* @param string $namespace
*
* @return string
*/
public function _namespaceMap($namespace)
public function _namespaceMap(string $namespace): ?string
{
static $namespaceMap = [
return match($namespace) {
self::TYPE_DESCRIPTION_NAMESPACE_PRIMITIVE => 'lib.pkp.classes.filter.PrimitiveTypeDescription',
self::TYPE_DESCRIPTION_NAMESPACE_CLASS => 'lib.pkp.classes.filter.ClassTypeDescription',
self::TYPE_DESCRIPTION_NAMESPACE_METADATA => 'lib.pkp.classes.metadata.MetadataTypeDescription',
self::TYPE_DESCRIPTION_NAMESPACE_XML => 'lib.pkp.classes.xslt.XMLTypeDescription',
self::TYPE_DESCRIPTION_NAMESPACE_VALIDATOR => 'lib.pkp.classes.validation.ValidatorTypeDescription'
];
return $namespaceMap[$namespace] ?? null;
self::TYPE_DESCRIPTION_NAMESPACE_VALIDATOR => 'lib.pkp.classes.validation.ValidatorTypeDescription',
default => null
};
}
}

Expand Down
5 changes: 3 additions & 2 deletions classes/metadata/MetadataTypeDescription.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

/**
* @file classes/metadata/MetadataTypeDescription.php
*
Expand Down Expand Up @@ -77,7 +78,7 @@ public function getAssocType()
/**
* @see TypeDescription::parseTypeName()
*/
public function parseTypeName($typeName)
public function parseTypeName(string $typeName): bool
{
// Configure the parent class type description
// with the expected meta-data class.
Expand Down Expand Up @@ -116,7 +117,7 @@ public function parseTypeName($typeName)
/**
* @see TypeDescription::checkType()
*/
public function checkType($object)
public function checkType($object): bool
{
// First of all check whether this is a
// meta-data description at all.
Expand Down
61 changes: 61 additions & 0 deletions classes/migration/upgrade/v3_5_0/FilterClassNames.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

/**
* @file classes/migration/upgrade/v3_5_0/FilterClassNames.php
*
* Copyright (c) 2014-2024 Simon Fraser University
* Copyright (c) 2000-2024 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class FilterClassNames
*
* @brief Update filter class names in the database to match renamed classnames/namespaces in PHP.
*/

namespace PKP\migration\upgrade\v3_5_0;

use Illuminate\Support\Facades\DB;
use PKP\migration\Migration;

class FilterClassNames extends Migration
{
protected array $emailTemplatesInstalled = [];

public function up(): void
{
// UserGroup became an Eloquent model and moved; use namespacing
DB::table('filter_groups')
->where('input_type', 'class::lib.pkp.classes.security.UserGroup[]')
->update(['input_type' => 'class::PKP\\userGroup\\UserGroup[]']);
DB::table('filter_groups')
->where('output_type', 'class::lib.pkp.classes.security.UserGroup[]')
->update(['output_type' => 'class::PKP\\userGroup\\UserGroup[]']);

// User class didn't move, but use namespacing
DB::table('filter_groups')
->where('input_type', 'class::lib.pkp.classes.user.User[]')
->update(['input_type' => 'class::PKP\\user\\User[]']);
DB::table('filter_groups')
->where('output_type', 'class::classes.users.User[]')
->update(['output_type' => 'class::PKP\\user\\User[]']);
}

public function down(): void
{
// UserGroup
DB::table('filter_groups')
->where('input_type', 'class::PKP\\userGroup\\UserGroup[]')
->update(['input_type' => 'class::lib.pkp.classes.security.UserGroup[]']);
DB::table('filter_groups')
->where('output_type', 'class::PKP\\userGroup\\UserGroup[]')
->update(['output_type' => 'class::lib.pkp.classes.security.UserGroup[]']);

// User
DB::table('filter_groups')
->where('input_type', 'class::PKP\\user\\User[]')
->update(['input_type' => 'class::lib.pkp.classes.user.User[]']);
DB::table('filter_groups')
->where('output_type', 'class::PKP\\user\\User[]')
->update(['output_type' => 'class::classes.users.User[]']);
}
}
4 changes: 2 additions & 2 deletions classes/validation/ValidatorTypeDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public function getNamespace()
/**
* @see TypeDescription::parseTypeName()
*/
public function parseTypeName($typeName)
public function parseTypeName(string $typeName): bool
{
// Standard validators are based on string input.
parent::parseTypeName('string');
Expand Down Expand Up @@ -85,7 +85,7 @@ public function parseTypeName($typeName)
/**
* @see TypeDescription::checkType()
*/
public function checkType($object)
public function checkType($object): bool
{
// Check primitive type.
if (!parent::checkType($object)) {
Expand Down
4 changes: 2 additions & 2 deletions classes/xslt/XMLTypeDescription.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function setValidationStrategy($validationStrategy)
/**
* @copydoc TypeDescription::parseTypeName()
*/
public function parseTypeName($typeName)
public function parseTypeName(string $typeName): bool
{
// We expect a validation strategy and an optional validation argument
$typeNameParts = explode('(', $typeName);
Expand Down Expand Up @@ -107,7 +107,7 @@ public function parseTypeName($typeName)
/**
* @copydoc TypeDescription::checkType()
*/
public function checkType($object)
public function checkType($object): bool
{
// We only accept DOMDocument objects and source strings.
if (!$object instanceof DOMDocument && !is_string($object)) {
Expand Down
Loading

0 comments on commit 4fd34f7

Please sign in to comment.