Open Preprint Systems  3.3.0
SearchHandler.inc.php
1 <?php
2 
16 import('classes.search.ArticleSearch');
17 import('classes.handler.Handler');
18 
19 class SearchHandler extends Handler {
20 
24  function authorize($request, &$args, $roleAssignments) {
25  import('classes.security.authorization.OpsServerMustPublishPolicy');
26  if ($request->getContext()) $this->addPolicy(new OpsServerMustPublishPolicy($request));
27 
28  return parent::authorize($request, $args, $roleAssignments);
29  }
30 
36  function index($args, $request) {
37  $this->validate(null, $request);
38  $this->search($args, $request);
39  }
40 
48  function _assignSearchFilters($request, &$templateMgr, $searchFilters) {
49  // Get the journal id (if any).
50  $journal =& $searchFilters['searchJournal'];
51  $journalId = ($journal ? $journal->getId() : null);
52  $searchFilters['searchJournal'] = $journalId;
53 
54  // Assign all filters except for dates which need special treatment.
55  $templateSearchFilters = array();
56  foreach($searchFilters as $filterName => $filterValue) {
57  if (in_array($filterName, array('fromDate', 'toDate'))) continue;
58  $templateSearchFilters[$filterName] = $filterValue;
59  }
60 
61  // Assign the filters to the template.
62  $templateMgr->assign($templateSearchFilters);
63 
64  // Special case: publication date filters.
65  foreach(array('From', 'To') as $fromTo) {
66  $month = $request->getUserVar("date${fromTo}Month");
67  $day = $request->getUserVar("date${fromTo}Day");
68  $year = $request->getUserVar("date${fromTo}Year");
69  if (empty($year)) {
70  $date = NULL;
71  $hasEmptyFilters = true;
72  } else {
73  $defaultMonth = ($fromTo == 'From' ? 1 : 12);
74  $defaultDay = ($fromTo == 'From' ? 1 : 31);
75  $date = date(
76  'Y-m-d H:i:s',
77  mktime(
78  0, 0, 0, empty($month) ? $defaultMonth : $month,
79  empty($day) ? $defaultDay : $day, $year
80  )
81  );
82  $hasActiveFilters = true;
83  }
84  $templateMgr->assign(array(
85  "date${fromTo}Month" => $month,
86  "date${fromTo}Day" => $day,
87  "date${fromTo}Year" => $year,
88  "date${fromTo}" => $date
89  ));
90  }
91 
92  // Assign the year range.
93  $yearRange = Services::get('publication')->getDateBoundaries(['contextIds' => $journalId]);
94  $yearStart = substr($yearRange[1], 0, 4);
95  $yearEnd = substr($yearRange[0], 0, 4);
96  $templateMgr->assign(array(
97  'yearStart' => $yearStart,
98  'yearEnd' => $yearEnd,
99  ));
100  }
101 
107  function search($args, $request) {
108  $this->validate(null, $request);
109 
110  // Get and transform active filters.
111  $articleSearch = new ArticleSearch();
112  $searchFilters = $articleSearch->getSearchFilters($request);
113  $keywords = $articleSearch->getKeywordsFromSearchFilters($searchFilters);
114 
115  // Get the range info.
116  $rangeInfo = $this->getRangeInfo($request, 'search');
117 
118  // Retrieve results.
119  $error = '';
120  $results = $articleSearch->retrieveResults(
121  $request, $searchFilters['searchJournal'], $keywords, $error,
122  $searchFilters['fromDate'], $searchFilters['toDate'],
123  $rangeInfo
124  );
125 
126  // Prepare and display the search template.
127  $this->setupTemplate($request);
128  $templateMgr = TemplateManager::getManager($request);
129  $templateMgr->setCacheability(CACHEABILITY_NO_STORE);
130 
131  // Result set ordering options.
132  $orderByOptions = $articleSearch->getResultSetOrderingOptions($request);
133  $templateMgr->assign('searchResultOrderOptions', $orderByOptions);
134  $orderDirOptions = $articleSearch->getResultSetOrderingDirectionOptions();
135  $templateMgr->assign('searchResultOrderDirOptions', $orderDirOptions);
136 
137  // Result set ordering selection.
138  list($orderBy, $orderDir) = $articleSearch->getResultSetOrdering($request);
139  $templateMgr->assign('orderBy', $orderBy);
140  $templateMgr->assign('orderDir', $orderDir);
141 
142  // Similar documents.
143  $templateMgr->assign('simDocsEnabled', true);
144 
145  // Result set display.
146  $this->_assignSearchFilters($request, $templateMgr, $searchFilters);
147  $templateMgr->assign('results', $results);
148  $templateMgr->assign('error', $error);
149  $templateMgr->display('frontend/pages/search.tpl');
150  }
151 
159  function similarDocuments($args, &$request) {
160  $this->validate(null, $request);
161 
162  // Retrieve the (mandatory) ID of the article that
163  // we want similar documents for.
164  $articleId = $request->getUserVar('articleId');
165  if (!is_numeric($articleId)) {
166  $request->redirect(null, 'search');
167  }
168 
169  // Check whether a search plugin provides terms for a similarity search.
170  $articleSearch = new ArticleSearch();
171  $searchTerms = $articleSearch->getSimilarityTerms($articleId);
172 
173  // Redirect to a search query with the identified search terms (if any).
174  if (empty($searchTerms)) {
175  $searchParams = null;
176  } else {
177  $searchParams = array('query' => implode(' ', $searchTerms));
178  }
179  $request->redirect(null, 'search', 'search', null, $searchParams);
180  }
181 
187  function authors($args, $request) {
188  $this->validate(null, $request);
189  $this->setupTemplate($request);
190 
191  $journal = $request->getJournal();
192  $user = $request->getUser();
193 
194  $authorDao = DAORegistry::getDAO('AuthorDAO');
195 
196  if (isset($args[0]) && $args[0] == 'view') {
197  // View a specific author
198  $authorName = $request->getUserVar('authorName');
199  $givenName = $request->getUserVar('givenName');
200  $familyName = $request->getUserVar('familyName');
201  $affiliation = $request->getUserVar('affiliation');
202  $country = $request->getUserVar('country');
203 
204  $authorRecords = iterator_to_array(Services::get('author')->getMany([
205  'contextIds' => $journal?[$journal->getId()]:[],
206  'givenName' => $givenName,
207  'familyName' => $familyName,
208  'affiliation' => $affiliation,
209  'country' => $country,
210  ]));
211  $publicationIds = array_map(function($author) {
212  return $author->getData('publicationId');
213  }, $authorRecords);
214  $submissionIds = array_map(function($publicationId) {
215  return Services::get('publication')->get($publicationId)->getData('submissionId');
216  }, array_unique($publicationIds));
217  $submissions = array_map(function($submissionId) {
218  return Services::get('submission')->get($submissionId);
219  }, array_unique($submissionIds));
220 
221  // Load information associated with each article.
222  $journals = array();
223  $sections = array();
224 
225  $sectionDao = DAORegistry::getDAO('SectionDAO'); /* @var $sectionDao SectionDAO */
226  $journalDao = DAORegistry::getDAO('JournalDAO'); /* @var $journalDao JournalDAO */
227 
228  foreach ($submissions as $article) {
229  $articleId = $article->getId();
230  $sectionId = $article->getSectionId();
231  $journalId = $article->getData('contextId');
232 
233  if (!isset($journals[$journalId])) {
234  $journals[$journalId] = $journalDao->getById($journalId);
235  }
236  if (!isset($sections[$sectionId])) {
237  $sections[$sectionId] = $sectionDao->getById($sectionId, $journalId, true);
238  }
239  }
240 
241  if (empty($submissions)) {
242  $request->redirect(null, $request->getRequestedPage());
243  }
244 
245  $templateMgr = TemplateManager::getManager($request);
246  $templateMgr->assign(array(
247  'submissions' => $submissions,
248  'sections' => $sections,
249  'journals' => $journals,
250  'givenName' => $givenName,
251  'familyName' => $familyName,
252  'affiliation' => $affiliation,
253  'authorName' => $authorName
254  ));
255 
256  $isoCodes = new \Sokil\IsoCodes\IsoCodesFactory();
257  $countries = $countries = $isoCodes->getCountries();
258  $country = $countries->getByAlpha2($country);
259  $templateMgr->assign('country', $country?$country->getLocalName():'');
260 
261  $templateMgr->display('frontend/pages/searchAuthorDetails.tpl');
262  } else {
263  // Show the author index
264  $searchInitial = $request->getUserVar('searchInitial');
265  $rangeInfo = $this->getRangeInfo($request, 'authors');
266 
267  $authors = $authorDao->getAuthorsAlphabetizedByJournal(
268  isset($journal)?$journal->getId():null,
269  $searchInitial,
270  $rangeInfo
271  );
272 
273  $templateMgr = TemplateManager::getManager($request);
274  $templateMgr->assign(array(
275  'searchInitial' => $request->getUserVar('searchInitial'),
276  'alphaList' => array_merge(array('-'), explode(' ', __('common.alphaList'))),
277  'authors' => $authors,
278  ));
279  $templateMgr->display('frontend/pages/searchAuthorIndex.tpl');
280  }
281  }
282 
287  function setupTemplate($request) {
288  parent::setupTemplate($request);
289  $templateMgr = TemplateManager::getManager($request);
290  $journal = $request->getJournal();
291  if (!$journal || !$journal->getData('restrictSiteAccess')) {
292  $templateMgr->setCacheability(CACHEABILITY_PUBLIC);
293  }
294  }
295 }
296 
297 
OpsServerMustPublishPolicy
Access policy to limit access to servers that do not publish online.
Definition: OpsServerMustPublishPolicy.inc.php:18
DAORegistry\getDAO
static & getDAO($name, $dbconn=null)
Definition: DAORegistry.inc.php:57
SearchHandler\authorize
authorize($request, &$args, $roleAssignments)
Definition: SearchHandler.inc.php:24
PKPHandler\getRangeInfo
static getRangeInfo($request, $rangeName, $contextData=null)
Definition: PKPHandler.inc.php:417
SearchHandler\_assignSearchFilters
_assignSearchFilters($request, &$templateMgr, $searchFilters)
Definition: SearchHandler.inc.php:48
SearchHandler
Handle site index requests.
Definition: SearchHandler.inc.php:19
SearchHandler\authors
authors($args, $request)
Definition: SearchHandler.inc.php:187
SearchHandler\search
search($args, $request)
Definition: SearchHandler.inc.php:107
PKPTemplateManager\getManager
static & getManager($request=null)
Definition: PKPTemplateManager.inc.php:1226
PKPHandler\validate
validate($requiredContexts=null, $request=null)
Definition: PKPHandler.inc.php:351
SearchHandler\setupTemplate
setupTemplate($request)
Definition: SearchHandler.inc.php:287
PKPHandler\addPolicy
addPolicy($authorizationPolicy, $addToTop=false)
Definition: PKPHandler.inc.php:157
SearchHandler\similarDocuments
similarDocuments($args, &$request)
Definition: SearchHandler.inc.php:159
SearchHandler\index
index($args, $request)
Definition: SearchHandler.inc.php:36
ArticleSearch
Class for retrieving article search results.
Definition: ArticleSearch.inc.php:20
Handler
Base request handler application class.
Definition: Handler.inc.php:18
PKPServices\get
static get($service)
Definition: PKPServices.inc.php:49