first commit

This commit is contained in:
CHIEFSOFT\ameye
2024-06-08 17:09:23 -04:00
commit df3a033196
17887 changed files with 8637778 additions and 0 deletions
+199
View File
@@ -0,0 +1,199 @@
<?php
/**
* @file classes/institution/Collector.php
*
* Copyright (c) 2022 Simon Fraser University
* Copyright (c) 2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class Collector
*
* @brief A helper class to configure a Query Builder to get a collection of institutions
*/
namespace PKP\institution;
use Illuminate\Database\Query\Builder;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\LazyCollection;
use PKP\core\interfaces\CollectorInterface;
/**
* @template T of Institution
*/
class Collector implements CollectorInterface
{
public DAO $dao;
public ?array $contextIds = null;
public ?array $ips = null;
public string $searchPhrase = '';
public ?int $count = null;
public ?int $offset = null;
public bool $includeSoftDeletes = false;
public function __construct(DAO $dao)
{
$this->dao = $dao;
}
public function getCount(): int
{
return $this->dao->getCount($this);
}
/**
* @return Collection<int,int>
*/
public function getIds(): Collection
{
return $this->dao->getIds($this);
}
/**
* @copydoc DAO::getMany()
* @return LazyCollection<int,T>
*/
public function getMany(): LazyCollection
{
return $this->dao->getMany($this);
}
/**
* Filter institutions by one or more contexts
*/
public function filterByContextIds(?array $contextIds): self
{
$this->contextIds = $contextIds;
return $this;
}
/**
* Filter institutions by one or more IPs
*/
public function filterByIps(?array $ips): self
{
$this->ips = $ips;
return $this;
}
/**
* Filter institutions by those matching a search query
*/
public function searchPhrase(string $phrase): self
{
$this->searchPhrase = $phrase;
return $this;
}
/**
* Limit the number of objects retrieved
*/
public function limit(?int $count): self
{
$this->count = $count;
return $this;
}
/**
* Offset the number of objects retrieved, for example to
* retrieve the second page of contents
*/
public function offset(?int $offset): self
{
$this->offset = $offset;
return $this;
}
/**
* Consider soft deleted institutions
*/
public function includeSoftDeletes(bool $include = true): self
{
$this->includeSoftDeletes = $include;
return $this;
}
/**
* @copydoc CollectorInterface::getQueryBuilder()
*/
public function getQueryBuilder(): Builder
{
$qb = DB::table($this->dao->table . ' as i')->select('i.*');
if (!is_null($this->contextIds)) {
$qb->whereIn('i.context_id', $this->contextIds);
}
if (!$this->includeSoftDeletes) {
$qb->whereNull('i.deleted_at');
}
if (!empty($this->searchPhrase)) {
$words = explode(' ', $this->searchPhrase);
if (count($words)) {
foreach ($words as $word) {
$word = addcslashes($word, '%_');
$qb->where(function ($qb) use ($word) {
$qb->whereIn('i.institution_id', function ($qb) use ($word) {
$qb->select('iss.institution_id')
->from($this->dao->settingsTable . ' as iss')
->where('iss.setting_name', '=', 'name')
->where(DB::raw('lower(iss.setting_value)'), 'LIKE', DB::raw("lower('%{$word}%')"));
})
->orWhereIn('i.institution_id', function ($qb) use ($word) {
$qb->select('ips.institution_id')
->from('institution_ip as ips')
->where(DB::raw('lower(ips.ip_string)'), 'LIKE', DB::raw("lower('%{$word}%')"));
});
});
}
}
}
if (!is_null($this->ips)) {
foreach ($this->ips as $index => $ip) {
$ip = sprintf('%u', ip2long($ip));
if ($index === 0) {
$qb->whereIn('i.institution_id', function ($qb) use ($ip) {
$qb->select('ip.institution_id')
->from('institution_ip as ip')
->where(function ($qb) use ($ip) {
$qb->whereNotNull('ip.ip_end')
->where('ip.ip_start', '<=', $ip)
->where('ip.ip_end', '>=', $ip);
})
->orWhere(function ($qb) use ($ip) {
$qb->whereNull('ip.ip_end')
->where('ip.ip_start', '=', $ip);
});
});
continue;
}
$qb->orWhereIn('i.institution_id', function ($qb) use ($ip) {
$qb->select('ip.institution_id')
->from('institution_ip as ip')
->where(function ($qb) use ($ip) {
$qb->whereNotNull('ip.ip_end')
->where('ip.ip_start', '<=', $ip)
->where('ip.ip_end', '>=', $ip);
})
->orWhere(function ($qb) use ($ip) {
$qb->whereNull('ip.ip_end')
->where('ip.ip_start', '=', $ip);
});
});
}
}
if (!is_null($this->count)) {
$qb->limit($this->count);
}
if (!is_null($this->offset)) {
$qb->offset($this->offset);
}
return $qb;
}
}
+275
View File
@@ -0,0 +1,275 @@
<?php
/**
* @file classes/institution/DAO.php
*
* Copyright (c) 2022 Simon Fraser University
* Copyright (c) 2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class DAO
*
* @ingroup institution
*
* @see Institution
*
* @brief Operations for retrieving and modifying Institution objects.
*/
namespace PKP\institution;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\LazyCollection;
use PKP\core\EntityDAO;
use PKP\core\SoftDeleteTrait;
use PKP\core\traits\EntityWithParent;
use PKP\services\PKPSchemaService;
/**
* @template T of Institution
* @extends EntityDAO<T>
*/
class DAO extends EntityDAO
{
use EntityWithParent;
use SoftDeleteTrait;
/** @copydoc EntityDAO::$schema */
public $schema = PKPSchemaService::SCHEMA_INSTITUTION;
/** @copydoc EntityDAO::$table */
public $table = 'institutions';
/** @copydoc EntityDAO::$settingsTable */
public $settingsTable = 'institution_settings';
/** @copydoc EntityDAO::$primaryKeyColumn */
public $primaryKeyColumn = 'institution_id';
/** @copydoc EntityDAO::$primaryTableColumns */
public $primaryTableColumns = [
'id' => 'institution_id',
'contextId' => 'context_id',
'ror' => 'ror',
'deletedAt' => 'deleted_at'
];
/**
* Get the parent object ID column name
*/
public function getParentColumn(): string
{
return 'context_id';
}
/**
* Instantiate a new DataObject
*/
public function newDataObject(): Institution
{
return App::make(Institution::class);
}
/**
* Get the number of institutions matching the configured query
*/
public function getCount(Collector $query): int
{
return $query
->getQueryBuilder()
->select('i.' . $this->primaryKeyColumn)
->count();
}
/**
* Get a list of institution ids matching the configured query
*
* @return Collection<int,int>
*/
public function getIds(Collector $query): Collection
{
return $query
->getQueryBuilder()
->select('i.' . $this->primaryKeyColumn)
->pluck('i.' . $this->primaryKeyColumn);
}
/**
* Get a collection of institutions matching the configured query
* @return LazyCollection<int,T>
*/
public function getMany(Collector $query): LazyCollection
{
$rows = $query
->getQueryBuilder()
->select(['i.*'])
->get();
return LazyCollection::make(function () use ($rows) {
foreach ($rows as $row) {
yield $row->institution_id => $this->fromRow($row);
}
});
}
/**
* Get a collection of deleted institutions matching the configured query
*/
public function getSoftDeleted(Collector $query): LazyCollection
{
$rows = $query
->includeSoftDeletes(true)
->getQueryBuilder()
->whereNotNull('deleted_at')
->select(['i.*'])
->get();
return LazyCollection::make(function () use ($rows) {
foreach ($rows as $row) {
yield $row->institution_id => $this->fromRow($row);
}
});
}
/**
* @copydoc EntityDAO::fromRow()
*/
public function fromRow(object $row): Institution
{
/** @var Institution */
$institution = parent::fromRow($row);
$ipRanges = DB::table('institution_ip')
->where($this->primaryKeyColumn, '=', $institution->getId())
->pluck('ip_string')
->toArray();
$institution->setIPRanges($ipRanges);
return $institution;
}
/**
* @copydoc EntityDAO::insert()
*/
public function insert(Institution $institution): int
{
if (isset($institution->_data['ipRanges'])) {
$ipRanges = $institution->getData('ipRanges');
unset($institution->_data['ipRanges']);
}
$institutionId = parent::_insert($institution);
if (isset($ipRanges)) {
$this->insertIPRanges($institutionId, $ipRanges);
}
return $institutionId;
}
/**
* @copydoc EntityDAO::update()
*/
public function update(Institution $institution): void
{
if (isset($institution->_data['ipRanges'])) {
$ipRanges = $institution->getData('ipRanges');
unset($institution->_data['ipRanges']);
}
parent::_update($institution);
if (isset($ipRanges)) {
$this->deleteIPRanges($institution->getId());
$this->insertIPRanges($institution->getId(), $ipRanges);
}
}
/**
* @copydoc EntityDAO::delete()
*/
public function delete(Institution $institution): void
{
// If the reference in the table institutional_subscriptions exists, soft delete the institution
$shouldSoftDelete = DB::table('institutional_subscriptions')
->where('institution_id', '=', $institution->getId())
->exists();
if ($shouldSoftDelete) {
$this->_softDelete($institution);
} else {
$this->deleteIPRanges($institution->getId());
parent::_delete($institution);
}
}
/**
* Insert institution IP ranges.
*/
protected function insertIPRanges(int $institutionId, array $ipRanges): void
{
if (empty($ipRanges) || empty($institutionId)) {
return;
}
foreach ($ipRanges as $ipRange) {
$ipStart = null;
$ipEnd = null;
$ipRange = trim($ipRange);
// Parse and check single IP string
if (strpos($ipRange, Institution::IP_RANGE_RANGE) === false) {
// Check for wildcards in IP
if (strpos($ipRange, Institution::IP_RANGE_WILDCARD) === false) {
// Get non-CIDR IP
if (strpos($ipRange, '/') === false) {
$ipStart = sprintf('%u', ip2long($ipRange));
// Convert CIDR IP to IP range
} else {
[$cidrIPString, $cidrBits] = explode('/', $ipRange);
if ($cidrBits == 0) {
$cidrMask = 0;
} else {
$cidrMask = (0xffffffff << (32 - $cidrBits));
}
$ipStart = sprintf('%u', ip2long($cidrIPString) & $cidrMask);
if ($cidrBits != 32) {
$ipEnd = sprintf('%u', ip2long($cidrIPString) | (~$cidrMask & 0xffffffff));
}
}
// Convert wildcard IP to IP range
} else {
$ipStart = sprintf('%u', ip2long(str_replace(Institution::IP_RANGE_WILDCARD, '0', $ipRange)));
$ipEnd = sprintf('%u', ip2long(str_replace(Institution::IP_RANGE_WILDCARD, '255', $ipRange)));
}
// Convert wildcard IP range to IP range
} else {
[$ipStart, $ipEnd] = explode(Institution::IP_RANGE_RANGE, $ipRange);
// Replace wildcards in start and end of range
$ipStart = sprintf('%u', ip2long(str_replace(Institution::IP_RANGE_WILDCARD, '0', trim($ipStart))));
$ipEnd = sprintf('%u', ip2long(str_replace(Institution::IP_RANGE_WILDCARD, '255', trim($ipEnd))));
}
// Insert IP or IP range
if ($ipStart != null) {
DB::table('institution_ip')->insert([
'institution_id' => $institutionId,
'ip_string' => $ipRange,
'ip_start' => $ipStart,
'ip_end' => $ipEnd
]);
}
}
}
/**
* Delete institution IP ranges by institution ID.
*/
private function deleteIPRanges(int $institutionId): void
{
DB::table('institution_ip')->where('institution_id', '=', $institutionId)->delete();
}
}
+102
View File
@@ -0,0 +1,102 @@
<?php
/**
* @defgroup institution Institution
* Implements institutions that are used for subscriptions and statistics.
*/
/**
* @file classes/institution/Institution.php
*
* Copyright (c) 2022 Simon Fraser University
* Copyright (c) 2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class Institution
*
* @ingroup institution
*
* @see DAO
*
* @brief Basic class describing an institution.
*/
namespace PKP\institution;
class Institution extends \PKP\core\DataObject
{
public const IP_RANGE_RANGE = '-';
public const IP_RANGE_WILDCARD = '*';
/**
* Get the context ID
*/
public function getContextId(): int
{
return $this->getData('contextId');
}
/**
* Set the context ID
*/
public function setContextId(int $contextId): void
{
$this->setData('contextId', $contextId);
}
/**
* Get the ROR
*/
public function getROR(): ?string
{
return $this->getData('ror');
}
/**
* Set the ROR
*/
public function setROR(string $ror): void
{
$this->setData('ror', $ror);
}
/**
* Get the localized name of the institution
*/
public function getLocalizedName(string $preferredLocale = null): string
{
return $this->getLocalizedData('name', $preferredLocale);
}
/**
* Get the name of the institution
*/
public function getName(string $locale = null): string|array
{
return $this->getData('name', $locale);
}
/**
* Set the name of the institution
*/
public function setName(string $name, string $locale = null): void
{
$this->setData('name', $name, $locale);
}
/**
* Get institution ip ranges.
*/
public function getIPRanges(): array
{
return $this->getData('ipRanges');
}
/**
* Set institution ip ranges.
*/
public function setIPRanges(array $ipRanges): void
{
$this->setData('ipRanges', $ipRanges);
}
}
+183
View File
@@ -0,0 +1,183 @@
<?php
/**
* @file classes/institution/Repository.php
*
* Copyright (c) 2022 Simon Fraser University
* Copyright (c) 2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class Repository
*
* @brief A repository to find and manage institutions.
*/
namespace PKP\institution;
use APP\core\Request;
use APP\core\Services;
use Illuminate\Support\Facades\App;
use Illuminate\Support\LazyCollection;
use PKP\core\PKPString;
use PKP\plugins\Hook;
use PKP\services\PKPSchemaService;
use PKP\validation\ValidatorFactory;
class Repository
{
public DAO $dao;
public string $schemaMap = maps\Schema::class;
protected Request $request;
protected PKPSchemaService $schemaService;
public function __construct(DAO $dao, Request $request, PKPSchemaService $schemaService)
{
$this->dao = $dao;
$this->request = $request;
$this->schemaService = $schemaService;
}
/** @copydoc DAO::newDataObject() */
public function newDataObject(array $params = []): Institution
{
$object = $this->dao->newDataObject();
if (!empty($params)) {
$object->setAllData($params);
}
return $object;
}
/** @copydoc DAO::exists() */
public function exists(int $id, int $contextId = null): bool
{
return $this->dao->exists($id, $contextId);
}
/** @copydoc DAO::get() */
public function get(int $id, int $contextId = null): ?Institution
{
return $this->dao->get($id, $contextId);
}
/** @copydoc DAO::getSoftDeleted() */
public function getSoftDeleted(Collector $query): LazyCollection
{
return $this->dao->getSoftDeleted($query);
}
/** @copydoc DAO::getCollector() */
public function getCollector(): Collector
{
return App::make(Collector::class);
}
/**
* Get an instance of the map class for mapping
* institutions to their schema
*/
public function getSchemaMap(): maps\Schema
{
return app('maps')->withExtensions($this->schemaMap);
}
/**
* Validate properties for an institution
*
* Perform validation checks on data used to add or edit an institution.
*
* @param Institution|null $object Institution being edited. Pass `null` if creating a new submission
* @param array $props A key/value array with the new data to validate
* @param array $allowedLocales The context's supported locales
* @param string $primaryLocale The context's primary locale
*
* @return array A key/value array with validation errors. Empty if no errors
*/
public function validate(?Institution $object, array $props, array $allowedLocales, string $primaryLocale): array
{
$errors = [];
$validator = ValidatorFactory::make(
$props,
$this->schemaService->getValidationRules($this->dao->schema, $allowedLocales)
);
// Check required fields if we're adding an institution
ValidatorFactory::required(
$validator,
$object,
$this->schemaService->getRequiredProps($this->dao->schema),
$this->schemaService->getMultilingualProps($this->dao->schema),
$allowedLocales,
$primaryLocale
);
// Check for input from disallowed locales
ValidatorFactory::allowedLocales($validator, $this->schemaService->getMultilingualProps($this->dao->schema), $allowedLocales);
// The contextId must match an existing context
$validator->after(function ($validator) use ($props) {
if (isset($props['contextId']) && !$validator->errors()->get('contextId')) {
$institutionContext = Services::get('context')->get($props['contextId']);
if (!$institutionContext) {
$validator->errors()->add('contextId', __('manager.institutions.noContext'));
}
}
if (!empty($props['ipRanges']) && !$validator->errors()->get('ipRanges')) {
foreach ($props['ipRanges'] as $ipRange) {
if (!PKPString::regexp_match(
'/^' .
// IP4 address (with or w/o wildcards) or IP4 address range (with or w/o wildcards) or CIDR IP4 address
'((([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]|[' . Institution::IP_RANGE_WILDCARD . '])([.]([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]|[' . Institution::IP_RANGE_WILDCARD . '])){3}((\s)*[' . Institution::IP_RANGE_RANGE . '](\s)*([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]|[' . Institution::IP_RANGE_WILDCARD . '])([.]([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5]|[' . Institution::IP_RANGE_WILDCARD . '])){3}){0,1})|(([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])([.]([0-9]|[1-9][0-9]|[1][0-9]{2}|[2][0-4][0-9]|[2][5][0-5])){3}([\/](([3][0-2]{0,1})|([1-2]{0,1}[0-9])))))' .
'$/i',
trim($ipRange)
)) {
$validator->errors()->add('ipRanges', __('manager.institutions.invalidIPRange'));
break;
}
}
}
});
if ($validator->fails()) {
$errors = $this->schemaService->formatValidationErrors($validator->errors());
}
Hook::call('Institution::validate', [&$errors, $object, $props, $allowedLocales, $primaryLocale]);
return $errors;
}
/** @copydoc DAO::insert() */
public function add(Institution $institution): int
{
$id = $this->dao->insert($institution);
Hook::call('Institution::add', [$institution]);
return $id;
}
/** @copydoc DAO::update() */
public function edit(Institution $institution, array $params): void
{
$newInstitution = clone $institution;
$newInstitution->setAllData(array_merge($newInstitution->_data, $params));
Hook::call('Institution::edit', [$newInstitution, $institution, $params]);
$this->dao->update($newInstitution);
}
/** @copydoc DAO::delete() */
public function delete(Institution $institution): void
{
Hook::call('Institution::delete::before', [$institution]);
$this->dao->delete($institution);
Hook::call('Institution::delete', [$institution]);
}
/**
* Delete a collection of institutions
*/
public function deleteMany(Collector $collector): void
{
foreach ($collector->getMany() as $institution) {
$this->delete($institution);
}
}
}
@@ -0,0 +1,94 @@
<?php
/**
* @file classes/institution/maps/Schema.php
*
* Copyright (c) 2022 Simon Fraser University
* Copyright (c) 2022 John Willinsky
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING.
*
* @class Schema
*
* @brief Map institutions to the properties defined in the institution schema
*/
namespace PKP\institution\maps;
use Illuminate\Support\Enumerable;
use PKP\institution\Institution;
use PKP\services\PKPSchemaService;
class Schema extends \PKP\core\maps\Schema
{
/** @copydoc \PKP\core\maps\Schema::$collection */
public Enumerable $collection;
/** @copydoc \PKP\core\maps\Schema::$schema */
public string $schema = PKPSchemaService::SCHEMA_INSTITUTION;
/**
* Map an institution
*
* Includes all properties in the institution schema.
*/
public function map(Institution $item): array
{
return $this->mapByProperties($this->getProps(), $item);
}
/**
* Summarize an institution
*
* Includes properties with the apiSummary flag in the institution schema.
*/
public function summarize(Institution $item): array
{
return $this->mapByProperties($this->getSummaryProps(), $item);
}
/**
* Map a collection of Institutions
*
* @see self::map
*/
public function mapMany(Enumerable $collection): Enumerable
{
$this->collection = $collection;
return $collection->map(function ($item) {
return $this->map($item);
});
}
/**
* Summarize a collection of Institutions
*
* @see self::summarize
*/
public function summarizeMany(Enumerable $collection): Enumerable
{
$this->collection = $collection;
return $collection->map(function ($item) {
return $this->summarize($item);
});
}
/**
* Map schema properties of an Institution to an assoc array
*/
protected function mapByProperties(array $props, Institution $item): array
{
$output = [];
foreach ($props as $prop) {
switch ($prop) {
case '_href':
$output[$prop] = $this->getApiUrl('institutions/' . $item->getId());
break;
default:
$output[$prop] = $item->getData($prop);
break;
}
}
$output = $this->schemaService->addMissingMultilingualValues($this->schema, $output, $this->context->getSupportedFormLocales());
ksort($output);
return $this->withExtensions($output, $item);
}
}