Skip to content

Commit

Permalink
Add default values for instructor/course/department instead of empty …
Browse files Browse the repository at this point in the history
…string (#4023)

- Also improves test coverage.
  • Loading branch information
meganschanz authored Nov 5, 2024
1 parent 4c1535e commit 74fbd66
Show file tree
Hide file tree
Showing 12 changed files with 462 additions and 112 deletions.
5 changes: 5 additions & 0 deletions config/vufind/reserves.ini
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ formatting_rule[*] = "phrase"
[Autocomplete_Types]
Reserves = "SolrReserves:AllFields:course,instructor,department"

[Advanced_Settings]
translated_facets[] = course_str:Reserves
translated_facets[] = department_str:Reserves
translated_facets[] = instructor_str:Reserves

[SearchCache]
;adapter = Memcached
;options[servers] = "localhost:11211,otherhost:11211"
Expand Down
3 changes: 3 additions & 0 deletions languages/Reserves/en.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
no_course_listed = "No Course Listed"
no_department_listed = "No Department Listed"
no_instructor_listed = "No Instructor Listed"
3 changes: 3 additions & 0 deletions languages/en.ini
Original file line number Diff line number Diff line change
Expand Up @@ -889,8 +889,11 @@ No Preference = "No Preference"
No reviews were found for this record = "No reviews were found for this record"
No saved logins = "No saved logins"
No Tags = "No Tags"
no_course_listed = "No Course Listed"
no_department_listed = "No Department Listed"
no_description = "Description not available."
no_email_address = "Email address missing."
no_instructor_listed = "No Instructor Listed"
no_items_selected = "No Items were Selected"
no_proxied_user = "No proxied user (request for yourself)"
nohit_active_filters = "One or more facet filters have been applied to this search. If you remove filters, you may retrieve more results."
Expand Down
33 changes: 33 additions & 0 deletions module/VuFind/src/VuFind/Autocomplete/SolrReserves.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,37 @@ public function __construct(\VuFind\Search\Results\PluginManager $results)
$this->defaultDisplayField = 'course';
$this->searchClassId = 'SolrReserves';
}

/**
* Try to turn an array of record drivers into an array of suggestions.
* Excluding `no_*_listed` matches since those are the translation values
* when there is no data in that field.
*
* @param array $searchResults An array of record drivers
* @param string $query User search query
* @param bool $exact Ignore non-exact matches?
*
* @return array
*/
protected function getSuggestionsFromSearch($searchResults, $query, $exact)
{
$results = [];
foreach ($searchResults as $object) {
$current = $object->getRawData();
foreach ($this->displayField as $field) {
if (isset($current[$field]) && !preg_match('/no_.*_listed/', $current[$field])) {
$bestMatch = $this->pickBestMatch(
$current[$field],
$query,
$exact
);
if ($bestMatch) {
$results[] = $bestMatch;
break;
}
}
}
}
return $results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
use function count;
use function in_array;
use function ini_get;
use function sprintf;

/**
* Console command: index course reserves into Solr.
Expand All @@ -56,6 +57,13 @@
)]
class IndexReservesCommand extends AbstractSolrAndIlsCommand
{
/**
* Output interface
*
* @var OutputInterface
*/
protected $output;

/**
* Default delimiter for reading files
*
Expand Down Expand Up @@ -154,16 +162,57 @@ protected function buildReservesIndex(
'id' => $id,
'bib_id' => [],
'instructor_id' => $instructorId,
'instructor' => $instructors[$instructorId] ?? '',
'instructor' => $instructors[$instructorId] ?? 'no_instructor_listed',
'course_id' => $courseId,
'course' => $courses[$courseId] ?? '',
'course' => $courses[$courseId] ?? 'no_course_listed',
'department_id' => $departmentId,
'department' => $departments[$departmentId] ?? '',
'department' => $departments[$departmentId] ?? 'no_department_listed',
];
}
if (!in_array($record['BIB_ID'], $index[$id]['bib_id'])) {
$index[$id]['bib_id'][] = $record['BIB_ID'];
}

// Show a warning if the any of the IDs were set, but was not found in the resulting data
if (!empty($instructorId) && !isset($instructors[$instructorId])) {
$this->showTimestampedMessage(
sprintf(
'WARNING! The instructor (ID: %s) for the course: %s (ID: %s) ' .
'and department: %s (ID: %s) did not match any found instructors.',
$index[$id]['instructor_id'],
$index[$id]['course'],
$index[$id]['course_id'],
$index[$id]['department'],
$index[$id]['department_id']
)
);
}
if (!empty($departmentId) && !isset($departments[$departmentId])) {
$this->showTimestampedMessage(
sprintf(
'WARNING! The department (ID: %s) for the course: %s (ID: %s) ' .
'and instructor: %s (ID: %s) did not match any found departments.',
$index[$id]['department_id'],
$index[$id]['course'],
$index[$id]['course_id'],
$index[$id]['instructor'],
$index[$id]['instructor_id']
)
);
}
if (!empty($courseId) && !isset($courses[$courseId])) {
$this->showTimestampedMessage(
sprintf(
'WARNING! The course (ID: %s) for the instructor: %s (ID: %s) ' .
'and department: %s (ID: %s) did not match any found courses.',
$index[$id]['course_id'],
$index[$id]['instructor'],
$index[$id]['instructor_id'],
$index[$id]['department'],
$index[$id]['department_id']
)
);
}
}

$updates = new UpdateDocument();
Expand Down Expand Up @@ -194,6 +243,18 @@ protected function getCsvReader(
return new CsvReader($files, $delimiter, $template);
}

/**
* Print the message to the provided output stream prefixed with a timestamp.
*
* @param string $message Message to display
*
* @return null
*/
protected function showTimestampedMessage(string $message)
{
$this->output->writeln(date('Y-m-d H:i:s') . ' ' . $message);
}

/**
* Run the command.
*
Expand All @@ -204,6 +265,8 @@ protected function getCsvReader(
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->output = $output;
$startTime = date('Y-m-d H:i:s');
// Check time limit; increase if necessary:
if (ini_get('max_execution_time') < 3600) {
ini_set('max_execution_time', '3600');
Expand All @@ -214,30 +277,50 @@ protected function execute(InputInterface $input, OutputInterface $output)

if ($file = $input->getOption('filename')) {
try {
$this->showTimestampedMessage('Starting reserves processing from file');

$reader = $this->getCsvReader($file, $delimiter, $template);
$this->showTimestampedMessage('Retrieving instructors');
$instructors = $reader->getInstructors();
$this->showTimestampedMessage('Found instructor count: ' . count($instructors));
$this->showTimestampedMessage('Retrieving courses');
$courses = $reader->getCourses();
$this->showTimestampedMessage('Found course count: ' . count($courses));
$this->showTimestampedMessage('Retrieving departments');
$departments = $reader->getDepartments();
$this->showTimestampedMessage('Found department count: ' . count($departments));
$this->showTimestampedMessage('Retrieving reserves');
$reserves = $reader->getReserves();
$this->showTimestampedMessage('Found reserve count: ' . count($reserves));
} catch (\Exception $e) {
$output->writeln($e->getMessage());
$this->showTimestampedMessage($e->getMessage());
return 1;
}
} elseif ($delimiter !== $this->defaultDelimiter) {
$output->writeln('-d (delimiter) is meaningless without -f (filename)');
$this->output->writeln('-d (delimiter) is meaningless without -f (filename)');
return 1;
} elseif ($template !== $this->defaultTemplate) {
$output->writeln('-t (template) is meaningless without -f (filename)');
$this->output->writeln('-t (template) is meaningless without -f (filename)');
return 1;
} else {
try {
$this->showTimestampedMessage('Starting reserves processing from ILS');

// Connect to ILS and load data:
$this->showTimestampedMessage('Retrieving instructors');
$instructors = $this->catalog->getInstructors();
$this->showTimestampedMessage('Found instructor count: ' . count($instructors ?? []));
$this->showTimestampedMessage('Retrieving courses');
$courses = $this->catalog->getCourses();
$this->showTimestampedMessage('Found course count: ' . count($courses ?? []));
$this->showTimestampedMessage('Retrieving departments');
$departments = $this->catalog->getDepartments();
$this->showTimestampedMessage('Found department count: ' . count($departments ?? []));
$this->showTimestampedMessage('Retrieving reserves');
$reserves = $this->catalog->findReserves('', '', '');
$this->showTimestampedMessage('Found reserve count: ' . count($reserves ?? []));
} catch (\Exception $e) {
$output->writeln($e->getMessage());
$this->showTimestampedMessage($e->getMessage());
return 1;
}
}
Expand All @@ -249,22 +332,27 @@ protected function execute(InputInterface $input, OutputInterface $output)
&& !empty($reserves)
) {
// Delete existing records
$this->showTimestampedMessage('Clearing existing reserves');
$this->solr->deleteAll('SolrReserves');

// Build and Save the index
$this->showTimestampedMessage('Building new reserves index');
$index = $this->buildReservesIndex(
$instructors,
$courses,
$departments,
$reserves
);
$this->showTimestampedMessage('Writing new reserves index');
$this->solr->save('SolrReserves', $index);

// Commit and Optimize the Solr Index
$this->solr->commit('SolrReserves');
$this->solr->optimize('SolrReserves');

$output->writeln('Successfully loaded ' . count($reserves) . ' rows.');
$this->showTimestampedMessage('Successfully loaded ' . count($reserves) . ' rows.');
$endTime = date('Y-m-d H:i:s');
$this->showTimestampedMessage('Started at: ' . $startTime . ' Completed at: ' . $endTime);
return 0;
}
$missing = array_merge(
Expand All @@ -273,9 +361,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
empty($departments) ? ['departments'] : [],
empty($reserves) ? ['reserves'] : []
);
$output->writeln(
'Unable to load data. No data found for: ' . implode(', ', $missing)
);
$this->showTimestampedMessage('Unable to load data. No data found for: ' . implode(', ', $missing));
return 1;
}
}
Loading

0 comments on commit 74fbd66

Please sign in to comment.