Skip to content

Commit

Permalink
[VUFIND-1252] Add id-prefix option to util/deletes tool. (vufind-org#…
Browse files Browse the repository at this point in the history
  • Loading branch information
demiankatz authored Aug 4, 2023
1 parent a1cd110 commit bc58056
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 6 deletions.
14 changes: 11 additions & 3 deletions harvest/batch-delete.bat
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
@echo off
rem Make sure that environment edits are local and that we have access to the
rem Make sure that environment edits are local and that we have access to the
rem Windows command extensions.
setlocal enableextensions
if not errorlevel 1 goto extensionsokay
Expand All @@ -9,7 +9,7 @@ goto end

rem Make sure VUFIND_HOME is set:
if not "!%VUFIND_HOME%!"=="!!" goto vufindhomefound
rem VUFIND_HOME not set -- try to call env.bat to
rem VUFIND_HOME not set -- try to call env.bat to
rem fix the problem before we give up completely
if exist %0\..\..\env.bat goto useenvbat
rem If env.bat doesn't exist, the user hasn't run the installer yet.
Expand All @@ -30,15 +30,22 @@ set SCRIPT_NAME=%0

rem Set default behavior
set SKIP_OPTIMIZE=0
set PREFIX=

rem Process switches
:switchloop
if "%1"=="-s" goto sswitch
if "%1"=="--id-prefix" goto idpswitch
goto switchloopend
:sswitch
set SKIP_OPTIMIZE=1
shift
goto switchloop
:idpswitch
set PREFIX=%2
shift
shift
goto switchloop
:switchloopend

rem Make sure command line parameter was included:
Expand All @@ -58,6 +65,7 @@ echo Example: %SCRIPT_NAME% oai_source
echo.
echo Options:
echo -s: Skip optimize operation after importing.
echo --id-prefix [prefix]: Specify a prefix to prepend to all IDs.
goto end
:paramsokay

Expand All @@ -81,7 +89,7 @@ set FOUNDSOME=0
for %%a in (%BASEPATH%\*.delete) do (
set FOUNDSOME=1
echo Processing %%a...
php deletes.php %%a flat %2
php deletes.php %%a flat %2 --id-prefix=%PREFIX%
move %%a %BASEPATH%\processed\ > nul
)

Expand Down
21 changes: 18 additions & 3 deletions harvest/batch-delete.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh
#!/bin/bash

# Make sure VUFIND_HOME is set:
if [ -z "$VUFIND_HOME" ]
Expand All @@ -13,11 +13,25 @@ then
fi

SKIP_OPTIMIZE=0
PREFIX=

while getopts ":s" OPT
while getopts ":s-:" OPT
do
case $OPT in
s) SKIP_OPTIMIZE=1;;
-)
case "${OPTARG}" in
id-prefix)
PREFIX="${!OPTIND}";
OPTIND=$(( $OPTIND + 1 ))
;;
id-prefix=*)
PREFIX=${OPTARG#*=}
;;
*)
echo "Unknown option -- ${OPTARG}" >&2
;;
esac;;
:)
echo "argument to '-$OPTARG' is missing" >&2
exit -1;;
Expand Down Expand Up @@ -45,6 +59,7 @@ then
echo ""
echo "Options:"
echo "-s: Skip optimize operation after importing."
echo "--id-prefix [prefix]: Specify a prefix to prepend to all IDs."
exit 1
fi

Expand Down Expand Up @@ -78,7 +93,7 @@ do
FOUNDSOME=1
fi
echo "Processing $file ..."
php deletes.php $file flat $2
php deletes.php $file flat $2 --id-prefix=$PREFIX
mv $file $BASEPATH/processed/`basename $file`
fi
done
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use Symfony\Component\Console\Formatter\OutputFormatter;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use VuFind\Marc\MarcCollectionFile;

Expand Down Expand Up @@ -82,6 +83,12 @@ protected function configure()
InputArgument::OPTIONAL,
'Name of Solr core/backend to update',
'Solr'
)->addOption(
'id-prefix',
null,
InputOption::VALUE_REQUIRED,
'Prefix to prepend to all IDs',
''
);
}

Expand Down Expand Up @@ -156,6 +163,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$filename = $input->getArgument('filename');
$mode = $input->getArgument('format');
$index = $input->getArgument('index');
$prefix = $input->getOption('id-prefix');

// File doesn't exist?
if (!file_exists($filename)) {
Expand All @@ -180,6 +188,12 @@ protected function execute(InputInterface $input, OutputInterface $output)
. implode(', ', $ids),
OutputInterface::VERBOSITY_VERBOSE
);
if (!empty($prefix)) {
$callback = function ($id) use ($prefix) {
return $prefix . $id;
};
$ids = array_map($callback, $ids);
}
$this->solr->deleteRecords($index, $ids);
$output->writeln(
'Delete operation completed.',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ public function testSuccessWithFlatFileAndDefaultIndex()
$this->assertEquals("", $commandTester->getDisplay());
}

/**
* Test success with a flat file, ID prefix and default index.
*
* @return void
*/
public function testSuccessWithFlatFileIdPrefixAndDefaultIndex()
{
$writer = $this->getMockWriter();
$writer->expects($this->once())->method('deleteRecords')
->with($this->equalTo('Solr'), $this->equalTo(['x.rec1', 'x.rec2', 'x.rec3']));
$command = new DeletesCommand($writer);
$commandTester = new CommandTester($command);
$fixture = $this->getFixtureDir('VuFindConsole') . 'deletes';
$commandTester->execute(
[
'filename' => $fixture,
'format' => 'flat',
'--id-prefix' => 'x.',
]
);
$this->assertEquals(0, $commandTester->getStatusCode());
$this->assertEquals("", $commandTester->getDisplay());
}

/**
* Test success with a MARC file and non-default index.
*
Expand Down

0 comments on commit bc58056

Please sign in to comment.