/**
  * Parse into structures the XML contents of an email data file.
  * @param $contents string
  * @return array
  */
 function parseEmails($contents)
 {
     $parser = new XMLParser();
     $result = $parser->parseTextStruct($contents, array('email_text', 'subject', 'body'));
     $parser->destroy();
     return $result;
 }
 function solrQuery(&$xmlDoc)
 {
     $ch = curl_init();
     curl_setopt($ch, CURLOPT_URL, $this->getSetting('solrUrl') . '/update');
     curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type:text/xml; charset=utf-8"));
     curl_setopt($ch, CURLOPT_POST, 1);
     curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
     curl_setopt($ch, CURLOPT_POSTFIELDS, XMLCustomWriter::getXml($xmlDoc));
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
     $data = curl_exec($ch);
     if (curl_errno($ch)) {
         curl_close($ch);
         fatalError('CURL Error: ' . curl_error($ch));
     } else {
         curl_close($ch);
         $xmlParser = new XMLParser();
         $result = null;
         @($result =& $xmlParser->parseTextStruct($data, array("int")));
         if ($result) {
             foreach ($result as $nodeSet) {
                 foreach ($nodeSet as $node) {
                     if (isset($node['attributes']['name']) && $node['attributes']['name'] == 'status' && $node['value'] == 0) {
                         return true;
                     }
                 }
             }
         }
         return false;
     }
 }
 /**
  * Display search results.
  */
 function searchResults()
 {
     ZendSearchHandler::setupTemplate();
     $plugin =& PluginRegistry::getPlugin('generic', 'ZendSearchPlugin');
     $isUsingSolr = $plugin->isUsingSolr();
     if ($isUsingSolr) {
         $ch = curl_init();
         curl_setopt($ch, CURLOPT_URL, $plugin->getSetting('solrUrl') . '/select');
         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
         curl_setopt($ch, CURLOPT_TIMEOUT, 10);
         curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
         curl_setopt($ch, CURLOPT_ENCODING, '');
         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
         curl_setopt($ch, CURLOPT_DNS_USE_GLOBAL_CACHE, 0);
         curl_setopt($ch, CURLOPT_POST, 1);
         $query = '';
     } else {
         $index =& $plugin->getIndex();
         $query = new Zend_Search_Lucene_Search_Query_Boolean();
     }
     $q = Request::getUserVar('q');
     if (!empty($q)) {
         if ($isUsingSolr) {
             $query .= 'text:"' . ZendSearchHandler::luceneEscape($q) . '" ';
         } else {
             $query->addSubquery(Zend_Search_Lucene_Search_QueryParser::parse($q));
         }
     }
     $searchFormElementDao =& DAORegistry::getDAO('SearchFormElementDAO');
     $searchFormElements =& $searchFormElementDao->getSearchFormElements();
     while ($searchFormElement =& $searchFormElements->next()) {
         $searchFormElementId = $searchFormElement->getSearchFormElementId();
         $symbolic = $searchFormElement->getSymbolic();
         switch ($searchFormElement->getType()) {
             case SEARCH_FORM_ELEMENT_TYPE_SELECT:
             case SEARCH_FORM_ELEMENT_TYPE_STRING:
                 $term = Request::getUserVar($symbolic);
                 if (!empty($term)) {
                     if ($isUsingSolr) {
                         $query .= $symbolic . ':"' . ZendSearchHandler::luceneEscape($term) . '" ';
                     } else {
                         $query->addSubquery(new Zend_Search_Lucene_Search_Query_Term(new Zend_Search_Lucene_Index_Term($term, $symbolic)), true);
                     }
                 }
                 break;
             case SEARCH_FORM_ELEMENT_TYPE_DATE:
                 $from = Request::getUserDateVar($symbolic . '-from');
                 $to = Request::getUserDateVar($symbolic . '-to');
                 if (!empty($from) && !empty($to)) {
                     if ($isUsingSolr) {
                         $query .= $symbolic . ':[' . strftime('%Y-%m-%dT%H:%M:%SZ', $from) . ' TO ' . strftime('%Y-%m-%dT%H:%M:%SZ', $to) . '] ';
                     } else {
                         $fromTerm = new Zend_Search_Lucene_Index_Term($from, $symbolic);
                         $toTerm = new Zend_Search_Lucene_Index_Term($to, $symbolic);
                         $query->addSubquery(new Zend_Search_Lucene_Search_Query_Range($fromTerm, $toTerm, true), true);
                     }
                 }
                 break;
             default:
                 fatalError('Unknown element type!');
         }
         unset($searchFormElement);
     }
     $rangeInfo =& PKPHandler::getRangeInfo('results');
     if ($isUsingSolr) {
         $itemsPerPage = Config::getVar('interface', 'items_per_page');
         curl_setopt($ch, CURLOPT_POSTFIELDS, 'q=' . trim(urlencode($query)) . '&rows=' . urlencode($itemsPerPage) . ($rangeInfo ? '&start=' . $rangeInfo->getPage() * $itemsPerPage : ''));
         $data = curl_exec($ch);
         $xmlParser = new XMLParser();
         $result = null;
         $numFound = 0;
         @($result =& $xmlParser->parseTextStruct($data, array('str', 'result')));
         $recordIds = array();
         if ($result) {
             foreach ($result as $nodeSet) {
                 foreach ($nodeSet as $node) {
                     if (isset($node['attributes']['name']) && $node['attributes']['name'] == 'id') {
                         $recordIds[] = $node['value'];
                     } elseif (isset($node['attributes']['numFound'])) {
                         $numFound = $node['attributes']['numFound'];
                     }
                 }
             }
         }
         $plugin->import('SolrResultIterator');
         $resultsIterator =& SolrResultIterator::fromRangeInfo($recordIds, $numFound, $rangeInfo);
         unset($recordIds);
     } else {
         $resultsArray = $index->find($query);
         $plugin->import('ZendSearchResultIterator');
         $resultsIterator =& ZendSearchResultIterator::fromRangeInfo($resultsArray, $rangeInfo);
         unset($resultsArray);
     }
     $templateMgr =& TemplateManager::getManager();
     $templateMgr->assign_by_ref('recordDao', DAORegistry::getDAO('RecordDAO'));
     $templateMgr->assign_by_ref('results', $resultsIterator);
     $templateMgr->assign_by_ref('q', $q);
     $templateMgr->display($plugin->getTemplatePath() . 'results.tpl');
 }