示例#1
0
文件: kbi.php 项目: KIZI/sewebar-cms
 function replace($match)
 {
     if (!isset($match[1]) || empty($match[1])) {
         return;
     }
     try {
         //WYSIWYG editor zalamuje XHTML neparove znacky na \ /> aby zustal validni JSON je potreba to vratit spet na \/>
         $json = str_replace('\\ />', '\\/>', $match[1]);
         $config = json_decode($json, true);
         if ($config === NULL) {
             KBIDebug::log($match[1], 'Element not parsed as JSON');
             return $match[1];
         }
         $transfomator = new KbiModelTransformator($config);
         //var_dump($match);
         //var_dump($config);
         //var_dump($transfomator);
         return $transfomator->transform();
     } catch (Exception $ex) {
         KBIDebug::log(array($ex, $config), 'Query not succesfull');
     }
 }
示例#2
0
 function uploadlocal()
 {
     global $option;
     $application = JFactory::getApplication();
     // Check for request forgeries
     JRequest::checkToken() or jexit('Invalid Token');
     if ($model =& $this->getModel('sources')) {
         $id = JRequest::getVar('id', array(0), 'method', 'array');
         $cid = JRequest::getVar('cid', array(0), 'method', 'array');
         $success = 0;
         require_once JPATH_COMPONENT . DS . 'models' . DS . 'documents.php';
         $documents_model = new DocumentsModel();
         $sourceConfig = $model->getSource($id[0]);
         $source = KBIntegrator::create(get_object_vars($sourceConfig));
         foreach ($cid as $document_id) {
             try {
                 $document = $documents_model->getArticle($document_id, 'all', true);
                 if ($document) {
                     KBIDebug::log($document);
                     $source->addDocument($document->id, $document, FALSE);
                     $success++;
                     $application->enqueueMessage(JText::_('Document uploaded') . "({$document->title})");
                 }
             } catch (Exception $ex) {
                 //TODO: add document title to error message
                 $application->enqueueMessage(JText::_('ERROR ADDING FILE') . " - " . $ex->getMessage(), 'error');
             }
         }
         $application->enqueueMessage(JText::_('Documents uploaded') . "({$success})");
     } else {
         $application->enqueueMessage(JText::_('SOURCE NOT FOUND'), 'error');
     }
     $this->setRedirect("index.php?option={$option}&controller=documents&id[]={$id[0]}");
 }
示例#3
0
 /**
  * Generates final query from skeleton using parameters and/or XSLT transformation.
  *
  * @return string
  */
 public function proccessQuery(&$options)
 {
     $parameters = $this->getParameters();
     $xslt = $this->getXslt();
     /* output parameter */
     $options = array_merge($this->getOptions('GET'), $this->getOptions('POST'));
     if (!empty($parameters)) {
         if (is_array($parameters)) {
             $delimiter = $this->getDelimiter();
             $replace_pairs = array();
             foreach ($parameters as $name => $value) {
                 $replace_pairs[$delimiter . $name . $delimiter] = $value;
             }
             KBIDebug::log($replace_pairs, 'Applying parameters.');
             $this->query = strtr($this->query, $replace_pairs);
         } elseif (empty($this->query) && is_string($parameters)) {
             // in case query is empty and parameters is string then we assume parameters to be query itself
             KBIDebug::info('Parameters considered as query.');
             $this->setQuery($parameters);
         }
     }
     if (!empty($xslt)) {
         $xml = new DOMDocument();
         if ($xml->loadXML($this->query)) {
             // Create XSLT document
             $xsl_document = new DOMDocument();
             $xsl_document->loadXML($xslt, LIBXML_NOCDATA);
             // Process XSLT
             $xslt = new XSLTProcessor();
             $xslt->importStylesheet($xsl_document);
             KBIDebug::info('Applying pre-query transformation.');
             $this->query = $xslt->transformToXML($xml);
         } else {
             KBIDebug::info('Query is not valid XML therefore XSLT could not be executed.');
         }
     }
     return $this->getQuery();
 }
 public function test()
 {
     try {
         $server_id = $this->getMinerId();
         if ($server_id === null) {
             throw new \Exception('LISpMiner ID was not provided.');
         }
         $client = $this->getRestClient();
         $credentials = $this->getUser();
         $url = trim($this->getUrl(), '/');
         $url = "{$url}/miners/{$server_id}";
         $response = $client->get($url, null, $credentials);
         KBIDebug::log($response, "Test executed");
         $this->parseResponse($response, '');
         return true;
     } catch (\Exception $ex) {
         return false;
     }
 }
示例#5
0
 function remove()
 {
     global $option;
     // Check for request forgeries
     JRequest::checkToken() or jexit('Invalid Token');
     $this->setRedirect("index.php?option={$option}");
     // Initialize variables
     $db =& JFactory::getDBO();
     $ids = JRequest::getVar('cid', array(0), 'post', 'array');
     $table =& JTable::getInstance('source', 'Table');
     $n = count($ids);
     for ($i = 0; $i < $n; $i++) {
         $sources =& $this->getModel('sources');
         $source = $sources->getSource((int) $ids[$i]);
         // delete miner if it is LISpMiner
         if ($source && $source->type == 'LISPMINER') {
             try {
                 $config = get_object_vars($source);
                 JLoader::import('KBIntegrator', JPATH_PLUGINS . DS . 'kbi');
                 $miner = KBIntegrator::create($config);
                 $miner->unregister();
             } catch (Exception $ex) {
                 // Just log it
                 KBIDebug::log($ex->getMessage());
             }
         }
         if (!$table->delete((int) $ids[$i])) {
             return JError::raiseWarning(500, $table->getError());
         }
     }
     $this->setMessage(JText::sprintf('Items removed', $n));
 }
 /**
  * Implements the query execution. If remote source returned well-formed XML and XSLT is set the transformation is performed.
  *
  * @param KBIQuery|string $query
  * @param string $xsl XSLT
  * @return string The result of query execution.
  */
 public function query($query, $xsl = '')
 {
     $options = array();
     if ($query instanceof KBIQuery) {
         $query = $query->proccessQuery($options);
     }
     $method = strtoupper($this->getMethod());
     switch ($method) {
         case 'POST':
             $xml_data = $this->queryPost($query, $options);
             break;
         case 'SOAP':
             $xml_data = $this->querySoap($query);
             break;
         default:
         case 'GET':
             $xml_data = $this->queryGet($query);
             break;
     }
     KBIDebug::log(array($xml_data), 'Raw result');
     if (empty($xsl)) {
         return $xml_data;
     }
     $xml = new DOMDocument();
     if ($xml->loadXML($xml_data)) {
         // Create XSLT document
         $xsl_document = new DOMDocument();
         $xsl_document->loadXML($xsl, LIBXML_NOCDATA);
         // Process XSLT
         $xslt = new XSLTProcessor();
         $xslt->importStylesheet($xsl_document);
         KBIDebug::info('Applying post-query transformation.');
         return $xslt->transformToXML($xml);
     } else {
         return $xml_data;
     }
 }
示例#7
0
 /**
  *
  * @see http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html
  */
 public function addDocument($id, $document, $path = true)
 {
     $ch = curl_init();
     if (is_object($document)) {
         $data = array('action' => 'addDocument', 'id' => $id, 'docName' => $document->title, 'creationTime' => $document->modified, 'content' => $document->text, 'reportUri' => $document->uri);
     } else {
         $data = array('action' => 'addDocument', 'id' => $id, 'docName' => '', 'creationTime' => '', 'content' => $path ? file_get_contents($document) : $document);
     }
     curl_setopt($ch, CURLOPT_URL, $this->getUrl());
     curl_setopt($ch, CURLOPT_POSTFIELDS, $this->encodeData($data));
     curl_setopt($ch, CURLOPT_VERBOSE, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_POST, true);
     $response = curl_exec($ch);
     $info = curl_getinfo($ch);
     curl_close($ch);
     KBIDebug::log($data, "Adding document to source {$this->getName()}");
     KBIDebug::log($info, 'Document add CURL info');
     KBIDebug::log($response, 'Document add CURL response');
     if ($info['http_code'] != '200') {
         throw new Exception('Error in communication');
     }
     $xml_response = simplexml_load_string($response);
     if ($xml_response === FALSE) {
         throw new Exception('Unexpected response');
     }
     if (isset($xml_response->error)) {
         throw new Exception($xml_response->error);
     }
 }
示例#8
0
 /**
  * @param $id
  * @throws Exception
  */
 public function remove($id)
 {
     $table =& JTable::getInstance('source', 'Table');
     $id = (int) $id;
     $source = $this->getSource($id);
     // delete miner if it is LISpMiner
     if ($source && $source->type == 'LISPMINER') {
         try {
             $config = get_object_vars($source);
             JLoader::import('KBIntegrator', JPATH_LIBRARIES . DS . 'kbi');
             $miner = KBIntegrator::create($config);
             $miner->unregister();
         } catch (Exception $ex) {
             // Just log it
             KBIDebug::log($ex->getMessage());
         }
     }
     if (!$table->delete($id)) {
         throw new Exception($table->getError());
     }
 }
示例#9
0
 public function cancelQuery($taskName)
 {
     $source = $this->getSource();
     if ($source != NULL && $source instanceof LispMiner) {
         KBIDebug::log(array('source' => $source, 'task' => $taskName), 'Canceling task');
         return $source->cancelTask($taskName);
     } else {
         return JText::_('Given source does not support query cancelation (only LISpMiner sources does).');
     }
 }
示例#10
0
 /**
  *
  * Sends PMML document to be indexed to service
  *
  * @see http://dtbaker.com.au/random-bits/uploading-a-file-using-curl-in-php.html
  */
 public function addDocument($id, $document, $path = true)
 {
     $ch = curl_init();
     if (is_object($document)) {
         $data = array('action' => 'addDocument', 'id' => $id, 'docName' => $document->title, 'creationTime' => $document->modified, 'content' => $document->text, 'reportUri' => $document->uri);
     } else {
         $data = array('action' => 'addDocument', 'id' => $id, 'docName' => '', 'creationTime' => '', 'content' => $path ? file_get_contents($document) : $document);
     }
     curl_setopt($ch, CURLOPT_URL, $this->getUrl());
     curl_setopt($ch, CURLOPT_POSTFIELDS, $this->encodeData($data));
     curl_setopt($ch, CURLOPT_VERBOSE, false);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     curl_setopt($ch, CURLOPT_POST, true);
     $response = curl_exec($ch);
     $info = curl_getinfo($ch);
     curl_close($ch);
     KBIDebug::log($data);
     KBIDebug::log($info);
     KBIDebug::log($response);
     var_dump($data);
     var_dump($info);
     var_dump($response);
     if ($info['http_code'] != '200') {
         throw new Exception('Error in communication');
     }
 }
示例#11
0
 function hits()
 {
     $document =& JFactory::getDocument();
     $viewName = JRequest::getVar('view', 'hits');
     $viewType = $document->getType();
     $view =& $this->getView($viewName, $viewType);
     $data = JRequest::getVar('data', '', 'post', 'string', JREQUEST_ALLOWRAW);
     if ($viewType == 'raw' && $data != NULL) {
         $config = array('source' => JRequest::getVar('id_source', NULL, 'default', 'none', JREQUEST_ALLOWRAW), 'query' => NULL, 'xslt' => NULL, 'parameters' => NULL);
         try {
             $com_kbi = JComponentHelper::getComponent('com_kbi', true);
             if (!$com_kbi->enabled) {
                 throw new Exception('KBI component not intalled or enabled');
             }
             JLoader::import('transformator', JPATH_COMPONENT . DS . '..' . DS . $com_kbi->option . DS . 'models');
             $model = new KbiModelTransformator($config);
             $sr = self::createSerializeRules($model->getSource());
             $model->setQuery($sr->serializeRules($data));
             $dd = null;
             $fl = null;
             $er = $model->transform();
             $sr = new GetDataARBuilderQuery($dd, $fl, $er, 'en');
             $data = $sr->getData();
             $view->assignRef('value', $data);
             KBIDebug::log($er);
         } catch (Exception $e) {
             $view->assign('value', "<p class=\"kbierror\">Chyba dotazu: {$e->getMessage()}</p>");
         }
     }
     $view->display();
 }
示例#12
0
 public function getDatabasePassword($username, $password, $db_id)
 {
     $url = trim($this->getUrl(), '/');
     $url = "{$url}/Users/Get";
     $data = array('name' => $username, 'password' => $password, 'db_id' => $db_id);
     $response = $this->requestGet($url, $data);
     KBIDebug::log(array('data' => $data, 'response' => $response, 'url' => $url), "Password retring");
     return $this->parseDatabasePassword($response);
 }
示例#13
0
 public function setDatabasePassword($username, $password, $db_id, $old_password, $new_password)
 {
     $client = $this->getRestClient();
     $url = trim($this->getUrl(), '/');
     $url = "{$url}/users/{$username}/databases/{$db_id}";
     $data = array('db_id' => $db_id, 'db_password' => $new_password);
     $response = $client->put($url, $data, array('username' => $username, 'password' => $password));
     KBIDebug::log(array('url' => "PUT {$url}", 'data' => $data, 'response' => $response), "Password changed");
     return $this->parseResponse($response, "User's datbases password changed successfully.");
 }
示例#14
0
 /**
  * Generates JSON from FeaturesList and DataDescription that initializes ARDesigner.
  *
  */
 function features()
 {
     $document =& JFactory::getDocument();
     $document->setMimeEncoding('application/json');
     $viewName = JRequest::getVar('view', 'features');
     $viewType = 'raw';
     $view =& $this->getView($viewName, $viewType);
     $query_id = JRequest::getInt('id_query', NULL);
     $view->assign('value', '');
     if ($query_id != NULL) {
         if (!class_exists('KbiModelQueries')) {
             $kbi = JComponentHelper::getComponent('com_kbi', true);
             if ($kbi->enabled) {
                 JLoader::import('queries', self::$com_kbi_admin . DS . 'models');
                 JLoader::import('sources', self::$com_kbi_admin . DS . 'models');
             } else {
                 throw new Exception(JText::_('Component com_kbi not found / enabled!'));
             }
         }
         $model_queries = new KbiModelQueries();
         $query = $model_queries->getQuery($query_id);
         $model_sources = new KbiModelSources();
         $source = $model_sources->getSource(JRequest::getInt('id_source', NULL));
         KBIDebug::log($source);
         $featurelist = !empty($query->featurelist) ? $query->featurelist : $this->featurelist;
         if (!empty($source->dictionaryquery)) {
             $datadescription = $source->dictionaryquery;
         } else {
             $kbi_source = KBIntegrator::create(get_object_vars($source));
             if ($kbi_source instanceof ISynchronable) {
                 $datadescription = $kbi_source->getDataDescription();
             } else {
                 $datadescription = $this->datadescription;
             }
         }
     } else {
         $featurelist = $this->featurelist;
         $datadescription = $this->datadescription;
     }
     if (class_exists('KBIDebug')) {
         KBIDebug::log(array('featurelist' => $featurelist, 'datadescription' => $datadescription), 'Loading ARD with FL and DL');
     }
     $sr = new GetDataARBuilderQuery($datadescription, $featurelist, null, 'en');
     $result = $sr->getData();
     $view->assignRef('value', $result);
     $view->display();
 }