Example #1
0
 /**
  * Internal utility function for initializing
  * UrlQueryHelper for a Results-object with search ids for all tabs.
  *
  * @param ResultsManager $results Search results.
  * @param ServiceManager $sm      Service manager.
  *
  * @return Results Search results with initialized UrlQueryHelper
  */
 public static function initUrlQueryHelper(\VuFind\Search\Base\Results $results, $sm)
 {
     $helper = new UrlQueryHelper($results->getParams());
     $savedSearches = $sm->getServiceLocator()->get('Request')->getQuery('search');
     if ($savedSearches) {
         $helper->setDefaultParameter('search', $savedSearches);
     }
     $results->setHelper('urlQuery', $helper);
     return $results;
 }
Example #2
0
 /**
  * Internal utility function for initializing
  * UrlQueryHelper for a Results-object with search ids for all tabs.
  *
  * @param ResultsManager $results Search results.
  * @param ServiceManager $locator Service locator.
  *
  * @return Results Search results with initialized UrlQueryHelper
  */
 public static function initUrlQueryHelper(\VuFind\Search\Base\Results $results, $locator)
 {
     if (Console::isConsole()) {
         return $results;
     }
     $helper = new UrlQueryHelper($results->getParams());
     $savedSearches = $locator->get('Request')->getQuery('search');
     if ($savedSearches) {
         $helper->setDefaultParameter('search', $savedSearches);
     }
     $results->setHelper('urlQuery', $helper);
     return $results;
 }
Example #3
0
 /**
  * Send scheduled alerts for a view.
  *
  * @return void
  */
 protected function processViewAlerts()
 {
     $this->msg("  Sending scheduled alerts for view: {$this->localDir} " . "(base: {$this->scheduleBaseUrl})");
     $iso8601 = 'Y-m-d\\TH:i:s\\Z';
     $configLoader = $this->serviceManager->get('VuFind\\Config');
     $this->iniReader = new IniReader();
     $this->iniReader->setNestSeparator(chr(0));
     $hmac = $this->serviceManager->get('VuFind\\HMAC');
     $backend = $this->serviceManager->get('VuFind\\Search\\BackendManager')->get('Solr');
     $viewManager = $this->serviceManager->get('viewmanager');
     $viewModel = $viewManager->getViewModel();
     $renderer = $viewManager->getRenderer();
     $emailer = $this->serviceManager->get('VuFind\\Mailer');
     $translator = $renderer->plugin('translate');
     $urlHelper = $renderer->plugin('url');
     $todayTime = new \DateTime();
     $user = false;
     $institution = false;
     $institutionConfigs = false;
     $scheduled = $this->searchTable->getScheduledSearches($this->scheduleBaseUrl);
     $this->msg(sprintf('    Processing %d searches', count($scheduled)));
     foreach ($scheduled as $s) {
         $lastTime = new \DateTime($s->finna_last_executed);
         $schedule = $s->finna_schedule;
         if ($schedule == 1) {
             // Daily
             if ($todayTime->format('Y-m-d') == $lastTime->format('Y-m-d')) {
                 $this->msg('      Bypassing search ' . $s->id . ': previous execution too recent (daily, ' . $lastTime->format($iso8601) . ')');
                 continue;
             }
         } else {
             if ($schedule == 2) {
                 $diff = $todayTime->diff($lastTime);
                 if ($diff->days < 6) {
                     $this->msg('      Bypassing search ' . $s->id . ': previous execution too recent (weekly, ' . $lastTime->format($iso8601) . ')');
                     continue;
                 }
             } else {
                 $this->err('Search ' . $s->id . ': unknown schedule: ' . $s->schedule);
                 continue;
             }
         }
         if ($user === false || $s->user_id != $user->id) {
             if (!($user = $this->userTable->getById($s->user_id))) {
                 $this->err('Search ' . $s->id . ': user ' . $s->user_id . ' does not exist ');
                 continue;
             }
         }
         if (!$user->email || trim($user->email) == '') {
             $this->warn('User ' . $user->username . ' does not have an email address, bypassing alert ' . $s->id);
             continue;
         }
         $scheduleUrl = parse_url($s->finna_schedule_base_url);
         if (!isset($scheduleUrl['host'])) {
             $this->err('Could not resolve institution for search ' . $s->id . ' with schedule_base_url: ' . var_export($scheduleUrl, true));
             continue;
         }
         // Set email language
         $language = $this->mainConfig->Site->language;
         if ($user->finna_language != '' && in_array($user->finna_language, array_keys($this->mainConfig->Languages->toArray()))) {
             $language = $user->finna_language;
         }
         $this->serviceManager->get('VuFind\\Translator')->addTranslationFile('ExtendedIni', null, $this::DEFAULT_PATH, $language)->setLocale($language);
         $limit = 50;
         // Prepare query
         $searchService = $this->serviceManager->get('VuFind\\Search');
         $searchObject = $s->getSearchObject();
         if ($searchObject->cl != 'Solr') {
             $this->err('Unsupported search class ' . $s->cl . ' for search ' . $s->id);
             continue;
         }
         $options = new Options($configLoader);
         $dateConverter = new DateConverter();
         $params = new Params($options, $configLoader, $dateConverter);
         $params->deminify($searchObject);
         $params->setLimit($limit);
         $params->setSort('first_indexed+desc');
         $query = $params->getQuery();
         $searchParams = $params->getBackendParameters();
         $searchTime = gmdate($iso8601, time());
         try {
             $collection = $searchService->search('Solr', $query, 0, $limit, $searchParams);
             $resultsTotal = $collection->getTotal();
             if ($resultsTotal < 1) {
                 $this->msg('      No results found for search ' . $s->id);
                 continue;
             }
             $records = $collection->getRecords();
         } catch (\VuFindSearch\Backend\Exception\BackendException $e) {
             $this->err('Error processing search ' . $s->id . ': ' . $e->getMessage());
         }
         $newestRecordDate = date($iso8601, strtotime($records[0]->getFirstIndexed()));
         $lastExecutionDate = $lastTime->format($iso8601);
         if ($newestRecordDate < $lastExecutionDate) {
             $this->msg('      No new results for search ' . $s->id . ": {$newestRecordDate} < {$lastExecutionDate}");
             continue;
         }
         // Collect records that have been indexed (for the first time)
         // after previous scheduled alert run
         $newRecords = [];
         foreach ($collection->getRecords() as $rec) {
             $recDate = date($iso8601, strtotime($rec->getFirstIndexed()));
             if ($recDate < $lastExecutionDate) {
                 break;
             }
             $newRecords[] = $rec;
         }
         // Prepare email content
         $viewBaseUrl = $searchUrl = $s->finna_schedule_base_url;
         $searchUrl .= $urlHelper->__invoke($options->getSearchAction());
         $urlQueryHelper = new UrlQueryHelper($params);
         $searchUrl .= str_replace('&amp;', '&', $urlQueryHelper->getParams());
         $secret = $s->getUnsubscribeSecret($hmac, $user);
         $unsubscribeUrl = $s->finna_schedule_base_url;
         $unsubscribeUrl .= $urlHelper->__invoke('myresearch-unsubscribe') . "?id={$s->id}&key={$secret}";
         $params->setServiceLocator($this->serviceManager);
         $filters = $this->processFilters($params->getFilterList());
         $params = ['records' => $newRecords, 'info' => ['baseUrl' => $viewBaseUrl, 'description' => $params->getDisplayQuery(), 'recordCount' => count($newRecords), 'url' => $searchUrl, 'unsubscribeUrl' => $unsubscribeUrl, 'filters' => $filters]];
         $message = $renderer->render('Email/scheduled-alert.phtml', $params);
         $subject = $this->mainConfig->Site->title . ': ' . $translator->__invoke('Scheduled Alert Results');
         $from = $this->mainConfig->Site->email;
         $to = $user->email;
         try {
             $this->serviceManager->get('VuFind\\Mailer')->send($to, $from, $subject, $message);
         } catch (\Exception $e) {
             $this->err("Failed to send message to {$user->email}: " . $e->getMessage());
             continue;
         }
         if ($s->setLastExecuted($searchTime) === 0) {
             $this->msg('Error updating last_executed date for search ' . $s->id);
         }
     }
 }