Wittyshare  0.2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
WsMnoGoSearch.cpp
Go to the documentation of this file.
1 
12 #include "WsMnoGoSearch.h"
13 
14 extern "C" {
15  void WsMnoGoSearchInit(void)
16  { }
17 }
18 
21 {
22  void* m_hndlMno = dlopen("libmnogosearch-3.3.so", RTLD_LAZY);
23  if ( m_hndlMno == NULL ) {
24  LOG(ERROR) << "WsMnoGoSearch::WsMnoGoSearch() : Cannot load libmnogosearch.so shared library! " << dlerror();
25  return;
26  }
27  if ( !(mf_UdmEnvInit = (pf_UdmEnvInit) dlsym(m_hndlMno, "UdmEnvInit")) ) {
28  LOG(ERROR) << "WsMnoGoSearch::WsMnoGoSearch() : cannot dlsym on UdmEnvInit function " << dlerror();
29  return;
30  }
31  if ( !(mf_UdmAgentInit = (pf_UdmAgentInit) dlsym(m_hndlMno, "UdmAgentInit")) ) {
32  LOG(ERROR) << "WsMnoGoSearch::WsMnoGoSearch() : cannot dlsym on UdmAgentInit function " << dlerror();
33  return;
34  }
35  if ( !(mf_UdmAgentAddLine = (pf_UdmAgentAddLine) dlsym(m_hndlMno, "UdmAgentAddLine")) ) {
36  LOG(ERROR) << "WsMnoGoSearch::WsMnoGoSearch() : cannot dlsym on UdmAgentAddLine function " << dlerror();
37  return;
38  }
39  if ( !(mf_UdmFind2 = (pf_UdmFind2) dlsym(m_hndlMno, "UdmFind2")) ) {
40  LOG(ERROR) << "WsMnoGoSearch::WsMnoGoSearch() : cannot dlsym on UdmFind2 function " << dlerror();
41  return;
42  }
43  if ( !(mf_UdmVarListFindStr = (pf_UdmVarListFindStr) dlsym(m_hndlMno, "UdmVarListFindStr")) ) {
44  LOG(ERROR) << "WsMnoGoSearch::WsMnoGoSearch() : cannot dlsym on UdmVarListFindStr function " << dlerror();
45  return;
46  }
47  if ( !(mf_UdmVarListFindInt = (pf_UdmVarListFindInt) dlsym(m_hndlMno, "UdmVarListFindInt")) ) {
48  LOG(ERROR) << "WsMnoGoSearch::WsMnoGoSearch() : cannot dlsym on UdmVarListFindInt function " << dlerror();
49  return;
50  }
51  if ( !(mf_UdmResultFree = (pf_UdmResultFree) dlsym(m_hndlMno, "UdmResultFree")) ) {
52  LOG(ERROR) << "WsMnoGoSearch::WsMnoGoSearch() : cannot dlsym on UdmResultFree function " << dlerror();
53  return;
54  }
55  if ( !(mf_UdmAgentFree = (pf_UdmAgentFree) dlsym(m_hndlMno, "UdmAgentFree")) ) {
56  LOG(ERROR) << "WsMnoGoSearch::WsMnoGoSearch() : cannot dlsym on UdmAgentFree function " << dlerror();
57  return;
58  }
59  if ( !(mf_UdmEnvFree = (pf_UdmEnvFree) dlsym(m_hndlMno, "UdmEnvFree")) ) {
60  LOG(ERROR) << "WsMnoGoSearch::WsMnoGoSearch() : cannot dlsym on UdmEnvFree function " << dlerror();
61  return;
62  }
63  if ( !mf_UdmEnvInit(&m_env) ) {
64  LOG(ERROR) << "WsMnoGoSearch::WsMnoGoSearch() : Cannot mf_UdmEnvInit";
65  return;
66  }
67  if ( !mf_UdmAgentInit(&m_agent, &m_env, 0) ) {
68  LOG(ERROR) << "WsMnoGoSearch::WsMnoGoSearch() : Cannot mf_UdmAgentInit";
69  return;
70  }
71  //TODO FIX PATH HERE ??
72  if (UDM_OK != mf_UdmAgentAddLine(&m_agent, "DBAddr sqlite3:///var/www/wt/resources/wsdb.sqlite/"))
73  return;
74  if (UDM_OK != mf_UdmAgentAddLine(&m_agent, "LocalCharset UTF-8"))
75  return;
76  if (UDM_OK != mf_UdmAgentAddLine(&m_agent, "BrowserCharset UTF-8"))
77  return;
78  LOG(INFO) << "WsMnoGoSearch::WsMnoGoSearch() : completed";
79  return;
80 }
81 
83 {
87 }
88 
90 {
91  m_fst = fst;
92  LOG(DEBUG) << "WsMnoGoSearch::load() : Loading results for " << q;
93  snprintf(m_udmQuery, sizeof(m_udmQuery), "q=%s", q.c_str());
94  LOG(INFO) << "WsMnoGoSearch::load() : Load m_udmQuery = " << m_udmQuery;
95  if ( !(m_res = mf_UdmFind2(&m_agent, m_udmQuery)) ) {
96  LOG(INFO) << "WsMnoGoSearch::load() : Load cannot UdmFind2 ";
97  return -2;
98  }
99  LOG(DEBUG) << "WsMnoGoSearch::load() : Filesystem root : " << m_fst->getRootPath().string();
100  for (int row = 0; row < m_res->num_rows; ++row) {
101  UDM_DOCUMENT* Doc = &m_res->Doc[row];
102  string url(mf_UdmVarListFindStr(&Doc->Sections, "URL", ""));
103  boost::algorithm::replace_all(url, "%26", "&");
104  boost::algorithm::replace_all(url, "%20", " ");
105  // remove the file:// prefix
106  //string test = url.substr(7 + m_fst->getRootPath().string().size());
107  path fp(url.substr(7));
108  LOG(DEBUG) << "WsMnoGoSearch::load() : PATH is " << fp.string();
109  /* Create the result item with adequate informations */
110  WsResultItem item(fp, m_fst->getRootPath(),
111  mf_UdmVarListFindStr(&Doc->Sections, "body", ""),
112  mf_UdmVarListFindStr(&Doc->Sections, "Content-Type", ""),
113  mf_UdmVarListFindInt(&Doc->Sections, "Content-Length", 0));
114  m_results.push_back(item);
115  }
116  return m_res->num_rows;
117 }
118 
120 {
121  return m_size;
122 }
123 
124 vector<WsResultItem> WsMnoGoSearch::getResults(const set<string>& groups)
125 {
126  string path;
127  vector<WsResultItem> filteredResults;
128  list<WsResultItem>::iterator it;
129  NodePtr n;
130  LOG(DEBUG) << "WsMnoGoSearch::getResults() : Total found results before filter " << m_results.size();
131  for (it = m_results.begin(); it != m_results.end(); ++it) {
132  /* remove file://tmp/directories from path*/
133  path = (*it).getPath().string();
134  /* Get the node associated with the result */
135  n = m_fst->eatPath(path);
136  if (n.get() != 0) {
137  /* If it's a directory, skip it */
138  if ( n.get()->isDirectory() ) continue;
139  /* Check if user if allowed to access this node */
140  if (n.get()->isAllowed(groups)) {
141  string newPath = (*it).getFullPath().string();
142  std::time_t t = boost::filesystem::last_write_time(newPath);
143  (*it).setModifyDate(t);
144  (*it).setSize(boost::filesystem::file_size(newPath));
145  filteredResults.push_back(*it);
146  }
147  } else
148  LOG(ERROR) << "WsMnoGoSearch::getResults() : Error finding node " << path << endl;
149  }
150  LOG(DEBUG) << "WsMnoGoSearch :: Total found results after filter " << m_results.size();
151  return filteredResults;
152 }
153 
pf_UdmAgentFree mf_UdmAgentFree
Definition: WsMnoGoSearch.h:80
int(* pf_UdmAgentAddLine)(UDM_AGENT *Agent, const char *line)
Definition: WsMnoGoSearch.h:24
UDM_AGENT m_agent
Definition: WsMnoGoSearch.h:84
boost::shared_ptr< WsAbstractNode > NodePtr
#define DEBUG
Definition: WsLogger.h:27
void * m_hndlMno
Definition: WsMnoGoSearch.h:72
FileSystemTreePtr m_fst
Definition: WsMnoGoSearch.h:65
int load(FileSystemTreePtr fst, string query)
Loads all search results.
pf_UdmAgentAddLine mf_UdmAgentAddLine
Definition: WsMnoGoSearch.h:75
pf_UdmVarListFindInt mf_UdmVarListFindInt
Definition: WsMnoGoSearch.h:77
UDM_ENV *(* pf_UdmEnvInit)(UDM_ENV *Env)
Definition: WsMnoGoSearch.h:22
vector< WsResultItem > getResults(const set< string > &groups)
Get list of documents readable by person member of all groups passed in parameter.
pf_UdmAgentInit mf_UdmAgentInit
Definition: WsMnoGoSearch.h:74
list< WsResultItem > m_results
Definition: WsMnoGoSearch.h:69
void(* pf_UdmEnvFree)(UDM_ENV *Env)
Definition: WsMnoGoSearch.h:30
Represents a Result of a search.
Definition: WsResultItem.h:27
#define LOG
Definition: WsLogger.h:22
const char *(* pf_UdmVarListFindStr)(UDM_VARLIST *vars, const char *name, const char *defval)
Definition: WsMnoGoSearch.h:27
#define INFO
Definition: WsLogger.h:32
Abstract class for search.
void WsMnoGoSearchInit(void)
boost::shared_ptr< WsFileSystemTree > FileSystemTreePtr
UDM_AGENT *(* pf_UdmAgentInit)(UDM_AGENT *Agent, UDM_ENV *Env, int id)
Definition: WsMnoGoSearch.h:23
char m_udmQuery[256]
Definition: WsMnoGoSearch.h:66
int getSize()
Loads all search results.
UDM_RESULT *(* pf_UdmFind2)(UDM_AGENT *Agent, const char *query)
Definition: WsMnoGoSearch.h:25
void(* pf_UdmResultFree)(UDM_RESULT *Result)
Definition: WsMnoGoSearch.h:28
pf_UdmResultFree mf_UdmResultFree
Definition: WsMnoGoSearch.h:79
void(* pf_UdmAgentFree)(UDM_AGENT *Agent)
Definition: WsMnoGoSearch.h:29
WsMnoGoSearch()
Constructor for the Search object.
pf_UdmFind2 mf_UdmFind2
Definition: WsMnoGoSearch.h:76
UDM_RESULT * m_res
Definition: WsMnoGoSearch.h:85
pf_UdmEnvFree mf_UdmEnvFree
Definition: WsMnoGoSearch.h:81
virtual ~WsMnoGoSearch()
pf_UdmVarListFindStr mf_UdmVarListFindStr
Definition: WsMnoGoSearch.h:78
#define ERROR
Definition: WsLogger.h:42
int(* pf_UdmVarListFindInt)(UDM_VARLIST *vars, const char *name, const int defval)
Definition: WsMnoGoSearch.h:26
pf_UdmEnvInit mf_UdmEnvInit
Definition: WsMnoGoSearch.h:73