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 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509
|
/*
* Copyright 2005-2008 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 <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
#include <cstdlib>
#include "Url.h"
#include "CrawlHistory.h"
using std::cout;
using std::endl;
using std::string;
using std::set;
using std::map;
CrawlHistory::CrawlHistory(const string &database) :
SQLiteBase(database, false)
{
}
CrawlHistory::~CrawlHistory()
{
}
string CrawlHistory::statusToText(CrawlStatus status)
{
string text;
switch (status)
{
case UNKNOWN:
text = "UNKNOWN";
break;
case CRAWLING:
text = "CRAWLING";
break;
case CRAWLED:
text = "CRAWLED";
break;
case ERROR:
text = "ERROR";
break;
default:
break;
}
return text;
}
CrawlHistory::CrawlStatus CrawlHistory::textToStatus(const string &text)
{
CrawlStatus status = UNKNOWN;
if (text == "CRAWLING")
{
status = CRAWLING;
}
else if (text == "CRAWLED")
{
status = CRAWLED;
}
else if (text == "ERROR")
{
status = ERROR;
}
return status;
}
/// Creates the CrawlHistory table in the database.
bool CrawlHistory::create(const string &database)
{
bool createHistoryTable = false;
// The specified path must be a file
if (SQLiteBase::check(database) == false)
{
return false;
}
SQLiteBase db(database);
// Does CrawlSources exist ?
if (db.executeSimpleStatement("SELECT * FROM CrawlSources LIMIT 1;") == false)
{
if (db.executeSimpleStatement("CREATE TABLE CrawlSources (SourceID INTEGER \
PRIMARY KEY, Url VARCHAR(255));") == false)
{
return false;
}
}
// Does CrawlHistory exist ?
if (db.executeSimpleStatement("SELECT * FROM CrawlHistory LIMIT 1;") == false)
{
createHistoryTable = true;
}
else
{
// Previous versions didn't include a ErrorNum field, so check for it
if (db.executeSimpleStatement("SELECT ErrorNum FROM CrawlHistory LIMIT 1;") == false)
{
#ifdef DEBUG
cout << "CrawlHistory::create: CrawlHistory needs updating" << endl;
#endif
// Ideally, we would use ALTER TABLE but it's not supported by SQLite
if (db.executeSimpleStatement("DROP TABLE CrawlHistory; VACUUM;") == true)
{
createHistoryTable = true;
}
}
}
if (createHistoryTable == true)
{
if (db.executeSimpleStatement("CREATE TABLE CrawlHistory (Url VARCHAR(255) PRIMARY KEY, \
Status VARCHAR(255), SourceID INTEGER, DATE INTEGER, ErrorNum INTEGER);") == false)
{
return false;
}
}
return true;
}
/// Inserts a source.
unsigned int CrawlHistory::insertSource(const string &url)
{
unsigned int sourceId = 0;
SQLResults *results = executeStatement("SELECT MAX(SourceID) FROM CrawlSources;");
if (results != NULL)
{
SQLRow *row = results->nextRow();
if (row != NULL)
{
sourceId = atoi(row->getColumn(0).c_str());
++sourceId;
delete row;
}
delete results;
}
results = executeStatement("INSERT INTO CrawlSources \
VALUES('%u', '%q');",
sourceId, Url::escapeUrl(url).c_str());
if (results != NULL)
{
delete results;
}
return sourceId;
}
/// Checks if the source exists.
bool CrawlHistory::hasSource(const string &url, unsigned int &sourceId)
{
bool success = false;
SQLResults *results = executeStatement("SELECT SourceID FROM CrawlSources \
WHERE Url='%q';", Url::escapeUrl(url).c_str());
if (results != NULL)
{
SQLRow *row = results->nextRow();
if (row != NULL)
{
sourceId = atoi(row->getColumn(0).c_str());
success = true;
delete row;
}
delete results;
}
return success;
}
/// Returns sources.
unsigned int CrawlHistory::getSources(map<unsigned int, string> &sources)
{
unsigned int count = 0;
SQLResults *results = executeStatement("SELECT SourceID, Url FROM CrawlSources;");
if (results != NULL)
{
while (results->hasMoreRows() == true)
{
SQLRow *row = results->nextRow();
if (row == NULL)
{
break;
}
sources[(unsigned int)atoi(row->getColumn(0).c_str())] = Url::unescapeUrl(row->getColumn(1));
++count;
delete row;
}
delete results;
}
return count;
}
/// Deletes a source.
bool CrawlHistory::deleteSource(unsigned int sourceId)
{
bool success = false;
SQLResults *results = executeStatement("DELETE FROM CrawlSources \
WHERE SourceID='%u';", sourceId);
if (results != NULL)
{
success = true;
delete results;
}
return success;
}
/// Inserts an URL.
bool CrawlHistory::insertItem(const string &url, CrawlStatus status, unsigned int sourceId,
time_t date, int errNum)
{
bool success = false;
SQLResults *results = executeStatement("INSERT INTO CrawlHistory \
VALUES('%q', '%q', '%u', '%d', '%d');",
Url::escapeUrl(url).c_str(), statusToText(status).c_str(), sourceId,
(date == 0 ? time(NULL) : date), errNum);
if (results != NULL)
{
success = true;
delete results;
}
return success;
}
/// Checks if an URL is in the history.
bool CrawlHistory::hasItem(const string &url, CrawlStatus &status, time_t &date)
{
bool success = false;
SQLResults *results = executeStatement("SELECT Status, Date FROM CrawlHistory \
WHERE Url='%q';", Url::escapeUrl(url).c_str());
if (results != NULL)
{
SQLRow *row = results->nextRow();
if (row != NULL)
{
status = textToStatus(row->getColumn(0));
date = (time_t)atoi(row->getColumn(1).c_str());
success = true;
delete row;
}
delete results;
}
return success;
}
/// Updates an URL.
bool CrawlHistory::updateItem(const string &url, CrawlStatus status, time_t date, int errNum)
{
bool success = false;
SQLResults *results = executeStatement("UPDATE CrawlHistory \
SET Status='%q', Date='%d', ErrorNum='%d' WHERE Url='%q';",
statusToText(status).c_str(), (date == 0 ? time(NULL) : date), errNum,
Url::escapeUrl(url).c_str());
if (results != NULL)
{
success = true;
delete results;
}
return success;
}
/// Updates URLs.
bool CrawlHistory::updateItems(const map<string, time_t> urls, CrawlStatus status)
{
string statusText(statusToText(status));
bool success = false;
for (map<string, time_t>::const_iterator updateIter = urls.begin();
updateIter != urls.end(); ++updateIter)
{
SQLResults *results = executeStatement("UPDATE CrawlHistory \
SET Status='%q', Date='%d' WHERE Url='%q';",
statusText.c_str(), (updateIter->second == 0 ? time(NULL) : updateIter->second),
Url::escapeUrl(updateIter->first).c_str());
if (results != NULL)
{
success = true;
delete results;
}
}
return success;
}
/// Updates the status of items en masse.
bool CrawlHistory::updateItemsStatus(unsigned int sourceId, CrawlStatus currentStatus, CrawlStatus newStatus)
{
bool success = false;
SQLResults *results = executeStatement("UPDATE CrawlHistory \
SET Status='%q' WHERE SourceId='%u' AND Status='%q';",
statusToText(newStatus).c_str(), sourceId,
statusToText(currentStatus).c_str());
if (results != NULL)
{
success = true;
delete results;
}
return success;
}
/// Gets the error number and date for a URL.
int CrawlHistory::getErrorDetails(const string &url, time_t &date)
{
int errNum = 0;
SQLResults *results = executeStatement("SELECT ErrorNum, Date FROM CrawlHistory WHERE Url='%q';",
Url::escapeUrl(url).c_str());
if (results != NULL)
{
SQLRow *row = results->nextRow();
if (row != NULL)
{
errNum = atoi(row->getColumn(0).c_str());
date = (time_t)atoi(row->getColumn(1).c_str());
delete row;
}
delete results;
}
return errNum;
}
/// Returns items that belong to a source.
unsigned int CrawlHistory::getSourceItems(unsigned int sourceId, CrawlStatus status,
set<string> &urls, time_t minDate)
{
SQLResults *results = NULL;
unsigned int count = 0;
if (minDate > 0)
{
results = executeStatement("SELECT Url FROM CrawlHistory \
WHERE SourceId='%u' AND Status='%q' AND Date>'%d';",
sourceId, statusToText(status).c_str(), minDate);
}
else
{
// Ignore the date
results = executeStatement("SELECT Url FROM CrawlHistory \
WHERE SourceId='%u' AND Status='%q';",
sourceId, statusToText(status).c_str());
}
if (results != NULL)
{
while (results->hasMoreRows() == true)
{
SQLRow *row = results->nextRow();
if (row == NULL)
{
break;
}
urls.insert(Url::unescapeUrl(row->getColumn(0)));
++count;
delete row;
}
delete results;
}
return count;
}
/// Returns the number of URLs.
unsigned int CrawlHistory::getItemsCount(CrawlStatus status)
{
unsigned int count = 0;
SQLResults *results = executeStatement("SELECT COUNT(*) FROM CrawlHistory WHERE Status='%q';",
statusToText(status).c_str());
if (results != NULL)
{
SQLRow *row = results->nextRow();
if (row != NULL)
{
count = atoi(row->getColumn(0).c_str());
delete row;
}
delete results;
}
return count;
}
/// Deletes an URL.
bool CrawlHistory::deleteItem(const string &url)
{
bool success = false;
SQLResults *results = executeStatement("DELETE FROM CrawlHistory \
WHERE Url='%q';", Url::escapeUrl(url).c_str());
if (results != NULL)
{
success = true;
delete results;
}
return success;
}
/// Deletes all items under a given URL.
bool CrawlHistory::deleteItems(const string &url)
{
bool success = false;
SQLResults *results = executeStatement("DELETE FROM CrawlHistory \
WHERE Url LIKE '%q%%';", Url::escapeUrl(url).c_str());
if (results != NULL)
{
success = true;
delete results;
}
return success;
}
/// Deletes URLs belonging to a source.
bool CrawlHistory::deleteItems(unsigned int sourceId, CrawlStatus status)
{
SQLResults *results = NULL;
bool success = false;
if (status == UNKNOWN)
{
results = executeStatement("DELETE FROM CrawlHistory \
WHERE SourceID='%u';", sourceId);
}
else
{
results = executeStatement("DELETE FROM CrawlHistory \
WHERE SourceID='%u' AND Status='%q';",
sourceId, statusToText(status).c_str());
}
if (results != NULL)
{
success = true;
delete results;
}
return success;
}
/// Expires items older than the given date.
bool CrawlHistory::expireItems(time_t expiryDate)
{
bool success = false;
SQLResults *results = executeStatement("DELETE FROM CrawlHistory \
WHERE Date<'%d';", expiryDate);
if (results != NULL)
{
success = true;
delete results;
}
return success;
}
|