From 7426585e7556fffd42c57d29341248fadd6d319b Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 22 Jun 2015 13:37:48 -0400
Subject: [PATCH] Added --verbose switch to delete tool. - Resolves
 VUFIND-1120.

---
 .../Controller/UtilController.php             | 39 +++++++++++++++++--
 1 file changed, 35 insertions(+), 4 deletions(-)

diff --git a/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php b/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php
index da9ca98eabb..c18b9145c2a 100644
--- a/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php
+++ b/module/VuFindConsole/src/VuFindConsole/Controller/UtilController.php
@@ -307,9 +307,15 @@ class UtilController extends AbstractBase
      */
     public function deletesAction()
     {
-        // Parse the command line parameters -- see if we are in "flat file" mode,
-        // find out what file we are reading in,
-        // and determine the index we are affecting!
+        // Parse the command line parameters -- check verbosity, see if we are in
+        // "flat file" mode, find out what file we are reading in, and determine
+        // the index we are affecting!
+        $this->consoleOpts->addRules(
+            [
+                'verbose' => 'Verbose mode',
+            ]
+        );
+        $verbose = $this->consoleOpts->getOption('verbose');
         $argv = $this->consoleOpts->getRemainingArgs();
         $filename = isset($argv[0]) ? $argv[0] : null;
         $mode = isset($argv[1]) ? $argv[1] : 'marc';
@@ -355,6 +361,9 @@ class UtilController extends AbstractBase
         $ids = [];
 
         // Flat file mode:
+        if ($verbose) {
+            Console::writeLine("Loading IDs in {$mode} mode.");
+        }
         if ($mode == 'flat') {
             foreach (explode("\n", file_get_contents($filename)) as $id) {
                 $id = trim($id);
@@ -369,17 +378,39 @@ class UtilController extends AbstractBase
                 ? new File_MARCXML($filename) : new File_MARC($filename);
 
             // Once the records are loaded, the rest of the logic is always the same:
+            $missingIdCount = 0;
             while ($record = $collection->next()) {
                 $idField = $record->getField('001');
-                $ids[] = (string)$idField->getData();
+                if ($idField) {
+                    $ids[] = (string)$idField->getData();
+                } else {
+                    $missingIdCount++;
+                }
+            }
+            if ($verbose) {
+                Console::writeLine(
+                    "Encountered $missingIdCount record(s) without IDs."
+                );
             }
         }
 
         // Delete, Commit and Optimize if necessary:
         if (!empty($ids)) {
+            if ($verbose) {
+                Console::writeLine(
+                    'Attempting to delete ' . count($ids) . ' record(s): '
+                    . implode(', ', $ids)
+                );
+            }
             $writer = $this->getServiceLocator()->get('VuFind\Solr\Writer');
             $writer->deleteRecords($index, $ids);
+            if ($verbose) {
+                Console::writeLine('Delete operation completed.');
+            }
+        } elseif ($verbose) {
+            Console::writeLine('Nothing to delete.');
         }
+
         return $this->getSuccessResponse();
     }
 
-- 
GitLab