Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Commands for checking duplicate DOIs and comparing documents #1172

Merged
merged 16 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions library/Application/Console/App.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@

/**
* Command line application for OPUS 4 management tasks.
*
* TODO get list of Commands from configuration/registration (allow modules to add commands, decentralize the code)
*/
class Application_Console_App extends Application
{
Expand All @@ -53,7 +55,11 @@ public function __construct()
$this->add(new ExtractFileCommand());
// $this->add(new Application_Console_Index_RepairCommand());
// $this->add(new Application_Console_Index_CheckCommand());

$this->add(new Application_Console_Document_DeleteCommand());
$this->add(new Application_Console_Document_DuplicatesCommand());
$this->add(new Application_Console_Document_DiffCommand());

$this->add(new BibtexImportCommand());
$this->add(new BibtexListCommand());
$this->add(new Application_Console_Debug_DocumentXmlCommand());
Expand Down
209 changes: 209 additions & 0 deletions library/Application/Console/Document/DiffCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
<?php

/**
* This file is part of OPUS. The software OPUS has been originally developed
* at the University of Stuttgart with funding from the German Research Net,
* the Federal Department of Higher Education and Research and the Ministry
* of Science, Research and the Arts of the State of Baden-Wuerttemberg.
*
* OPUS 4 is a complete rewrite of the original OPUS software and was developed
* by the Stuttgart University Library, the Library Service Center
* Baden-Wuerttemberg, the Cooperative Library Network Berlin-Brandenburg,
* the Saarland University and State Library, the Saxon State Library -
* Dresden State and University Library, the Bielefeld University Library and
* the University Library of Hamburg University of Technology with funding from
* the German Research Foundation and the European Regional Development Fund.
*
* LICENCE
* OPUS is free software; you can redistribute it and/or modify it under the
* terms of the GNU General Public License as published by the Free Software
* Foundation; either version 2 of the Licence, or any later version.
* OPUS is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
* details. You should have received a copy of the GNU General Public License
* along with OPUS; if not, write to the Free Software Foundation, Inc., 51
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* @copyright Copyright (c) 2023, OPUS 4 development team
* @license http://www.gnu.org/licenses/gpl.html General Public License
*/

use Opus\Common\Config\ConfigException;
use Opus\Common\Document;
use Opus\Common\Model\NotFoundException;
use Opus\Common\Repository;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Shows differences between two or more documents.
*
* TODO support different levels of detail
*/
class Application_Console_Document_DiffCommand extends Command
{
public const OPTION_DOI = 'doi';

public const OPTION_SERVER_STATE = 'server-state';

public const OPTION_IGNORE_DELETED = 'ignore-deleted';

public const ARGUMENT_DOC_ID = 'DocID';

protected function configure()
{
parent::configure();

$help = <<<EOT
Shows the differences between two or more documents.

Multiple document IDs can be provided as argument. The fields of the documents
are then compared and differences reported.

If a DOI is provided, using the <fg=green>--doi</> Option, the database is
searched for matching documents. If more than one is found, the differences
between the documents are reported.

<fg=red>NOTES:
- Complex values like persons and patents are not shown with all their metadata.
- For text values, like titles and abstracts, the exact differences are not
highlighted. The report just shows that the values are different.
</>
EOT;

$this->setName('document:diff')
->setDescription('Shows differences between documents.')
->setHelp($help)
->addOption(
self::OPTION_DOI,
null,
InputOption::VALUE_REQUIRED,
'DOI value'
)
->addOption(
self::OPTION_SERVER_STATE,
's',
InputOption::VALUE_REQUIRED,
'Include docs in state (DOI) - <fg=yellow>unpublished</>, <fg=yellow>published</>, <fg=yellow>inprogress</>, <fg=yellow>audited</>, <fg=yellow>restricted</>, <fg=yellow>deleted</>'
)
->addOption(
self::OPTION_IGNORE_DELETED,
null,
InputOption::VALUE_NONE,
'Ignore deleted documents (DOI)'
)
->addArgument(
self::ARGUMENT_DOC_ID,
InputArgument::OPTIONAL + InputArgument::IS_ARRAY,
'Two or more document IDs'
);
}

/**
* @return int
* @throws NotFoundException
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$diff = new Application_Document_DocumentDiff();
$diff->setOutput($output);

$docIds = $this->getDocumentsInput($input, $output);

$docCount = count($docIds);

if ($docCount === 0) {
$output->writeln('No documents for comparison');
} elseif ($docCount === 1) {
$docId = $docIds[0];
$output->writeln("Only one document for comparison (ID = $docId)");
} else {
$diff->diff($docIds);
}

return 0;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return int[]
* @throws ConfigException
*/
protected function getDocumentsInput($input, $output)
{
$docIds = $input->getArgument(self::ARGUMENT_DOC_ID);

if (count($docIds) === 0) {
$doi = $input->getOption(self::OPTION_DOI);

if ($doi !== null) {
$finder = Repository::getInstance()->getDocumentFinder();
$finder->setIdentifierValue('doi', $doi);

$serverStates = $this->getServerStateInput($input, $output);

if ($serverStates !== null && count($serverStates) < 6) {
$finder->setServerState($serverStates);
}

$docIds = $finder->getIds();

if (count($docIds) === 0) {
$output->writeln("No documents found for DOI: $doi");
}
}
}

return $docIds;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return string[]|null
*/
protected function getServerStateInput($input, $output)
{
$validStates = $this->getValidServerStates();

if ($input->getOption(self::OPTION_IGNORE_DELETED)) {
unset($validStates['deleted']);
}

$serverStateOption = $input->getOption(self::OPTION_SERVER_STATE);

$serverStates = [];

if ($serverStateOption !== null) {
$values = explode(',', $serverStateOption);
foreach ($values as $state) {
if (in_array(strtolower($state), $validStates)) {
$serverStates[$state] = $state;
} else {
$output->writeln("Invalid ServerState value: $state");
}
}
} else {
$serverStates = $validStates;
}

return $serverStates;
}

/**
* @return string[]
*
* TODO better way of getting valid states (configurable states in the future?)
*/
protected function getValidServerStates()
{
$doc = Document::new();
$field = $doc->getField('ServerState');
return $field->getDefault();
}
}
Loading