Skip to content

Commit

Permalink
Merge pull request #511 from jonasraoni/feature/main/8700-improve-slo…
Browse files Browse the repository at this point in the history
…w-query

Feature/main/8700 improve slow query
  • Loading branch information
jonasraoni authored May 30, 2024
2 parents 2bd857a + d043a79 commit 21a640a
Show file tree
Hide file tree
Showing 11 changed files with 109 additions and 224 deletions.
126 changes: 0 additions & 126 deletions classes/author/DAO.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,132 +18,6 @@

namespace APP\author;

use APP\core\Application;
use Illuminate\Support\Str;
use PKP\db\DAORegistry;
use PKP\db\DAOResultFactory;
use PKP\db\DBResultRange;
use PKP\facades\Locale;
use PKP\identity\Identity;
use PKP\submission\PKPSubmission;

class DAO extends \PKP\author\DAO
{
/**
* Retrieve all published authors for a server by the first letter of the family name.
* Authors will be sorted by (family, given). Note that if serverId is null,
* alphabetized authors for all enabled servers are returned.
* If authors have the same given names, first names and affiliations in all server locales,
* as well as country and email (optional), they are considered to be the same.
*
* @param int $serverId Optional server ID to restrict results to
* @param string $initial An initial a family name must begin with, "-" for authors with no family names
* @param DBResultRange $rangeInfo Range information
* @param bool $includeEmail Whether or not to include the email in the select distinct
*
* @return DAOResultFactory Authors ordered by last name, given name
*
* @deprecated 3.4.0.0
*
*/
public function getAuthorsAlphabetizedByServer($serverId = null, $initial = null, $rangeInfo = null, $includeEmail = false)
{
$locale = Locale::getLocale();
$params = [
Identity::IDENTITY_SETTING_GIVENNAME, $locale,
Identity::IDENTITY_SETTING_GIVENNAME,
Identity::IDENTITY_SETTING_FAMILYNAME, $locale,
Identity::IDENTITY_SETTING_FAMILYNAME,
];
if (isset($serverId)) {
$params[] = $serverId;
}

$supportedLocales = [];
if ($serverId !== null) {
$serverDao = DAORegistry::getDAO('ServerDAO'); /** @var \ServerDAO $serverDao */
$server = $serverDao->getById($serverId);
$supportedLocales = $server->getSupportedLocales();
} else {
$site = Application::get()->getRequest()->getSite();
$supportedLocales = $site->getSupportedLocales();
}
$supportedLocalesCount = count($supportedLocales);
$sqlJoinAuthorSettings = $sqlColumnsAuthorSettings = $initialSql = '';
if (isset($initial)) {
$initialSql = ' AND (';
}
$index = 0;
foreach ($supportedLocales as $locale) {
$localeStr = str_replace('@', '_', $locale);
$sqlColumnsAuthorSettings .= ",
COALESCE(asg{$index}.setting_value, ''), ' ',
COALESCE(asf{$index}.setting_value, ''), ' ',
COALESCE(SUBSTRING(asa{$index}.setting_value FROM 1 FOR 255), ''), ' '
";
$sqlJoinAuthorSettings .= "
LEFT JOIN author_settings asg{$index} ON (asg{$index}.author_id = aa.author_id AND asg{$index}.setting_name = '" . Identity::IDENTITY_SETTING_GIVENNAME . "' AND asg{$index}.locale = '{$locale}')
LEFT JOIN author_settings asf{$index} ON (asf{$index}.author_id = aa.author_id AND asf{$index}.setting_name = '" . Identity::IDENTITY_SETTING_FAMILYNAME . "' AND asf{$index}.locale = '{$locale}')
LEFT JOIN author_settings asa{$index} ON (asa{$index}.author_id = aa.author_id AND asa{$index}.setting_name = 'affiliation' AND asa{$index}.locale = '{$locale}')
";
if (isset($initial)) {
if ($initial == '-') {
$initialSql .= "(asf{$index}.setting_value IS NULL OR asf{$index}.setting_value = '')";
if ($index < $supportedLocalesCount - 1) {
$initialSql .= ' AND ';
}
} else {
$params[] = Str::lower($initial) . '%';
$initialSql .= "LOWER(asf{$index}.setting_value) LIKE LOWER(?)";
if ($index < $supportedLocalesCount - 1) {
$initialSql .= ' OR ';
}
}
}
$index++;
}
if (isset($initial)) {
$initialSql .= ')';
}

$result = $this->deprecatedDao->retrieveRange(
$sql = 'SELECT a.*, ug.show_title, s.locale,
COALESCE(agl.setting_value, agpl.setting_value) AS author_given,
CASE WHEN agl.setting_value <> \'\' THEN afl.setting_value ELSE afpl.setting_value END AS author_family
FROM authors a
JOIN user_groups ug ON (a.user_group_id = ug.user_group_id)
JOIN publications p ON (p.publication_id = a.publication_id)
JOIN submissions s ON (s.current_publication_id = p.publication_id)
LEFT JOIN author_settings agl ON (a.author_id = agl.author_id AND agl.setting_name = ? AND agl.locale = ?)
LEFT JOIN author_settings agpl ON (a.author_id = agpl.author_id AND agpl.setting_name = ? AND agpl.locale = s.locale)
LEFT JOIN author_settings afl ON (a.author_id = afl.author_id AND afl.setting_name = ? AND afl.locale = ?)
LEFT JOIN author_settings afpl ON (a.author_id = afpl.author_id AND afpl.setting_name = ? AND afpl.locale = s.locale)
JOIN (
SELECT
MIN(aa.author_id) as author_id,
CONCAT(
' . ($includeEmail ? 'aa.email, \' \', ' : '') . '
ac.setting_value,
\' \'
' . $sqlColumnsAuthorSettings . '
) as names
FROM authors aa
JOIN publications pp ON (pp.publication_id = aa.publication_id)
LEFT JOIN publication_settings ppss ON (ppss.publication_id = pp.publication_id)
JOIN submissions ss ON (ss.submission_id = pp.submission_id AND ss.current_publication_id = pp.publication_id AND ss.status = ' . PKPSubmission::STATUS_PUBLISHED . ')
JOIN servers j ON (ss.context_id = j.server_id)
LEFT JOIN author_settings ac ON (ac.author_id = aa.author_id AND ac.setting_name = \'country\')
' . $sqlJoinAuthorSettings . '
WHERE j.enabled = 1
' . (isset($serverId) ? ' AND j.server_id = ?' : '')
. $initialSql . '
GROUP BY names
) as t1 ON (t1.author_id = a.author_id)
ORDER BY author_family, author_given',
$params,
$rangeInfo
);

return new DAOResultFactory($result, $this, 'fromRow', [], $sql, $params, $rangeInfo);
}
}
4 changes: 2 additions & 2 deletions classes/doi/Repository.php
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ public function isAssigned(int $doiId, string $pubObjectType): bool
Repo::doi()::TYPE_REPRESENTATION => Repo::galley()
->getCollector()
->filterByDoiIds([$doiId])
->getIds()
->count(),
->getQueryBuilder()
->getCountForPagination() > 0,
default => false,
};

Expand Down
54 changes: 32 additions & 22 deletions classes/plugins/PubObjectsExportPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,20 @@
use PKP\submission\PKPSubmission;
use PKP\user\User;

// The statuses.
define('EXPORT_STATUS_ANY', '');
define('EXPORT_STATUS_NOT_DEPOSITED', 'notDeposited');
define('EXPORT_STATUS_MARKEDREGISTERED', 'markedRegistered');
define('EXPORT_STATUS_REGISTERED', 'registered');

// The actions.
define('EXPORT_ACTION_EXPORT', 'export');
define('EXPORT_ACTION_MARKREGISTERED', 'markRegistered');
define('EXPORT_ACTION_DEPOSIT', 'deposit');

// Configuration errors.
define('EXPORT_CONFIG_ERROR_SETTINGS', 0x02);

abstract class PubObjectsExportPlugin extends ImportExportPlugin
{
/** @var PubObjectCache */
// The statuses
public const EXPORT_STATUS_ANY = '';
public const EXPORT_STATUS_NOT_DEPOSITED = 'notDeposited';
public const EXPORT_STATUS_MARKEDREGISTERED = 'markedRegistered';
public const EXPORT_STATUS_REGISTERED = 'registered';
// The actions
public const EXPORT_ACTION_EXPORT = 'export';
public const EXPORT_ACTION_MARKREGISTERED = 'markRegistered';
public const EXPORT_ACTION_DEPOSIT = 'deposit';
// Configuration errors.
public const EXPORT_CONFIG_ERROR_SETTINGS = 2; /** @var PubObjectCache */

public $_cache;

/**
Expand Down Expand Up @@ -355,10 +352,10 @@ public function getRepresentationFilter()
public function getStatusNames()
{
return [
EXPORT_STATUS_ANY => __('plugins.importexport.common.status.any'),
EXPORT_STATUS_NOT_DEPOSITED => __('plugins.importexport.common.status.notDeposited'),
EXPORT_STATUS_MARKEDREGISTERED => __('plugins.importexport.common.status.markedRegistered'),
EXPORT_STATUS_REGISTERED => __('plugins.importexport.common.status.registered'),
PubObjectsExportPlugin::EXPORT_STATUS_ANY => __('plugins.importexport.common.status.any'),
PubObjectsExportPlugin::EXPORT_STATUS_NOT_DEPOSITED => __('plugins.importexport.common.status.notDeposited'),
PubObjectsExportPlugin::EXPORT_STATUS_MARKEDREGISTERED => __('plugins.importexport.common.status.markedRegistered'),
PubObjectsExportPlugin::EXPORT_STATUS_REGISTERED => __('plugins.importexport.common.status.registered'),
];
}

Expand Down Expand Up @@ -459,7 +456,7 @@ public function exportXML($objects, $filter, $context, $noValidation = null, &$o
public function markRegistered($context, $objects)
{
foreach ($objects as $object) {
$object->setData($this->getDepositStatusSettingName(), EXPORT_STATUS_MARKEDREGISTERED);
$object->setData($this->getDepositStatusSettingName(), PubObjectsExportPlugin::EXPORT_STATUS_MARKEDREGISTERED);
$this->updateObject($object);
}
}
Expand Down Expand Up @@ -568,7 +565,7 @@ public function getUnregisteredPreprints($context)
null,
null,
$this->getDepositStatusSettingName(),
EXPORT_STATUS_NOT_DEPOSITED,
PubObjectsExportPlugin::EXPORT_STATUS_NOT_DEPOSITED,
null
);
return $preprints->toArray();
Expand Down Expand Up @@ -856,5 +853,18 @@ protected function _checkForExportAction(string $exportAction): bool
}

if (!PKP_STRICT_MODE) {
class_alias('\APP\plugins\PubObjectsExportPlugin', '\PubObjectExportsPlugin');
class_alias('\APP\plugins\PubObjectsExportPlugin', '\PubObjectsExportPlugin');

foreach ([
'EXPORT_STATUS_ANY',
'EXPORT_STATUS_NOT_DEPOSITED',
'EXPORT_STATUS_MARKEDREGISTERED',
'EXPORT_STATUS_REGISTERED',
'EXPORT_ACTION_EXPORT',
'EXPORT_ACTION_MARKREGISTERED',
'EXPORT_ACTION_DEPOSIT',
'EXPORT_CONFIG_ERROR_SETTINGS',
] as $constantName) {
define($constantName, constant('\PubObjectsExportPlugin::' . $constantName));
}
}
9 changes: 4 additions & 5 deletions classes/services/queryBuilders/GalleyQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,11 @@ public function getCount()
{
return $this
->getQuery()
->select('g.galley_id')
->get()
->count();
->getCountForPagination();
}

/**
* @copydoc PKP\services\queryBuilders\interfaces\EntityQueryBuilderInterface::getCount()
* @copydoc PKP\services\queryBuilders\interfaces\EntityQueryBuilderInterface::getIds()
*/
public function getIds()
{
Expand All @@ -78,7 +76,8 @@ public function getIds()
}

/**
* @copydoc PKP\services\queryBuilders\interfaces\EntityQueryBuilderInterface::getCount()
* @copydoc PKP\services\queryBuilders\interfaces\EntityQueryBuilderInterface::getQuery()
*
* @hook Galley::getMany::queryObject [[&$q, $this]]
*/
public function getQuery()
Expand Down
Loading

0 comments on commit 21a640a

Please sign in to comment.