1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
|
/*
* Copyright 2005,2006 Fabrice Colin
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <algorithm>
#include <fstream>
#include <iostream>
#include <cstring>
#include "config.h"
#include "Document.h"
#include "StringManip.h"
#include "OpenSearchParser.h"
#ifdef HAVE_BOOST_SPIRIT
#include "SherlockParser.h"
#endif
#include "PluginWebEngine.h"
using std::cout;
using std::cerr;
using std::endl;
PluginWebEngine::PluginWebEngine(const string &fileName) :
WebEngine(),
m_pResponseParser(NULL)
{
load(fileName);
}
PluginWebEngine::~PluginWebEngine()
{
if (m_pResponseParser != NULL)
{
delete m_pResponseParser;
}
}
void PluginWebEngine::load(const string &fileName)
{
if (fileName.empty() == true)
{
return;
}
PluginParserInterface *pParser = getPluginParser(fileName);
if (pParser == NULL)
{
return;
}
m_pResponseParser = pParser->parse(m_properties);
delete pParser;
}
bool PluginWebEngine::getPage(const string &formattedQuery, unsigned int maxResultsCount)
{
if ((m_pResponseParser == NULL) ||
(formattedQuery.empty() == true))
{
return false;
}
DocumentInfo docInfo("Results Page", formattedQuery,
"text/html", "");
Document *pResponseDoc = downloadPage(docInfo);
if (pResponseDoc == NULL)
{
cerr << "PluginWebEngine::getPage: couldn't download "
<< formattedQuery << endl;
return false;
}
unsigned int contentLen;
const char *pContent = pResponseDoc->getData(contentLen);
if ((pContent == NULL) ||
(contentLen == 0))
{
#ifdef DEBUG
cout << "PluginWebEngine::getPage: downloaded empty page" << endl;
#endif
delete pResponseDoc;
return false;
}
#ifdef DEBUG
ofstream pageBackup("PluginWebEngine.html");
pageBackup.write(pContent, contentLen);
pageBackup.close();
#endif
bool success = m_pResponseParser->parse(pResponseDoc, m_resultsList,
maxResultsCount, m_properties.m_nextBase);
vector<DocumentInfo>::iterator resultIter = m_resultsList.begin();
while (resultIter != m_resultsList.end())
{
if (processResult(formattedQuery, *resultIter) == false)
{
// Remove this result
if (resultIter == m_resultsList.begin())
{
m_resultsList.erase(resultIter);
resultIter = m_resultsList.begin();
}
else
{
vector<DocumentInfo>::iterator badResultIter = resultIter;
--resultIter;
m_resultsList.erase(badResultIter);
}
}
else
{
// Next
++resultIter;
}
}
delete pResponseDoc;
return success;
}
PluginParserInterface *PluginWebEngine::getPluginParser(const string &fileName)
{
if (fileName.empty() == true)
{
return NULL;
}
// What type of plugin is it ?
// Look at the file extension
string::size_type pos = fileName.find_last_of(".");
if (pos == string::npos)
{
// No way to tell
return NULL;
}
string extension(fileName.substr(pos + 1));
#ifdef HAVE_BOOST_SPIRIT_CORE_HPP
if (strncasecmp(extension.c_str(), "src", 3) == 0)
{
return new SherlockParser(fileName);
}
else
#endif
if (strncasecmp(extension.c_str(), "xml", 3) == 0)
{
return new OpenSearchParser(fileName);
}
return NULL;
}
bool PluginWebEngine::getDetails(const string &fileName, string &name, string &channel)
{
if (fileName.empty() == true)
{
return false;
}
PluginParserInterface *pParser = getPluginParser(fileName);
if (pParser == NULL)
{
return false;
}
SearchPluginProperties properties;
ResponseParserInterface *pResponseParser = pParser->parse(properties, true);
if (pResponseParser == NULL)
{
cerr << "PluginWebEngine::getDetails: couldn't parse "
<< fileName << endl;
delete pParser;
return false;
}
delete pResponseParser;
delete pParser;
if (properties.m_response == SearchPluginProperties::UNKNOWN_RESPONSE)
{
#ifdef DEBUG
cout << "PluginWebEngine::getDetails: bad response type for "
<< fileName << endl;
#endif
return false;
}
name = properties.m_name;
channel = properties.m_channel;
return true;
}
//
// Implementation of SearchEngineInterface
//
/// Runs a query; true if success.
bool PluginWebEngine::runQuery(QueryProperties& queryProps,
unsigned int startDoc)
{
string queryString(queryProps.getFreeQuery(true));
char countStr[64];
unsigned int maxResultsCount(queryProps.getMaximumResultsCount());
unsigned int currentIncrement = 0, count = 0;
m_resultsList.clear();
m_resultsCountEstimate = 0;
if (queryProps.getType() != QueryProperties::XAPIAN_QP)
{
cerr << "PluginWebEngine::runQuery: query type not supported" << endl;
return false;
}
if (queryString.empty() == true)
{
#ifdef DEBUG
cout << "PluginWebEngine::runQuery: query is empty" << endl;
#endif
return false;
}
map<SearchPluginProperties::Parameter, string>::iterator paramIter = m_properties.m_parameters.find(SearchPluginProperties::SEARCH_TERMS_PARAM);
if (paramIter == m_properties.m_parameters.end())
{
#ifdef DEBUG
cout << "PluginWebEngine::runQuery: no user input tag" << endl;
#endif
return false;
}
string userInputTag(paramIter->second);
string formattedQuery = m_properties.m_baseUrl;
formattedQuery += "?";
formattedQuery += userInputTag;
formattedQuery += "=";
formattedQuery += queryString;
if (m_properties.m_parametersRemainder.empty() == false)
{
formattedQuery += "&";
formattedQuery += m_properties.m_parametersRemainder;
}
setQuery(queryProps);
#ifdef DEBUG
cout << "PluginWebEngine::runQuery: querying "
<< m_properties.m_name << endl;
#endif
while (count < maxResultsCount)
{
string pageQuery(formattedQuery);
// How do we scroll ?
if (m_properties.m_scrolling == SearchPluginProperties::PER_INDEX)
{
paramIter = m_properties.m_parameters.find(SearchPluginProperties::COUNT_PARAM);
if ((paramIter != m_properties.m_parameters.end()) &&
(paramIter->second.empty() == false))
{
// Number of results requested
pageQuery += "&";
pageQuery += paramIter->second;
pageQuery += "=";
snprintf(countStr, 64, "%u", maxResultsCount);
pageQuery += countStr;
}
paramIter = m_properties.m_parameters.find(SearchPluginProperties::START_INDEX_PARAM);
if ((paramIter != m_properties.m_parameters.end()) &&
(paramIter->second.empty() == false))
{
// The offset of the first result (typically 1 or 0)
pageQuery += "&";
pageQuery += paramIter->second;
pageQuery += "=";
snprintf(countStr, 64, "%u", count + m_properties.m_nextBase);
pageQuery += countStr;
}
}
else
{
paramIter = m_properties.m_parameters.find(SearchPluginProperties::START_PAGE_PARAM);
if ((paramIter != m_properties.m_parameters.end()) &&
(paramIter->second.empty() == false))
{
// The offset of the page
pageQuery += "&";
pageQuery += paramIter->second;
pageQuery += "=";
snprintf(countStr, 64, "%u", currentIncrement + m_properties.m_nextBase);
pageQuery += countStr;
}
}
if (getPage(pageQuery, queryProps.getMaximumResultsCount()) == false)
{
break;
}
if (m_properties.m_nextIncrement == 0)
{
// That one page should have all the results...
#ifdef DEBUG
cout << "PluginWebEngine::runQuery: performed one off call" << endl;
#endif
break;
}
else
{
if (m_resultsList.size() < count + m_properties.m_nextIncrement)
{
// We got less than the maximum number of results per page
// so there's no point in requesting the next page
#ifdef DEBUG
cout << "PluginWebEngine::runQuery: last page wasn't full" << endl;
#endif
break;
}
// Increase factor
currentIncrement += m_properties.m_nextIncrement;
}
count = m_resultsList.size();
}
return true;
}
|