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 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
|
/*
* Copyright 2005-2022 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 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 <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <signal.h>
#include <iostream>
#include <string>
#include <fstream>
#include <giomm/init.h>
#include <glibmm.h>
#include <glibmm/thread.h>
#include <glibmm/miscutils.h>
#include <sigc++/sigc++.h>
#include "config.h"
#include "NLS.h"
#include "Languages.h"
#include "Memory.h"
#include "MIMEScanner.h"
#include "Url.h"
#include "FilterFactory.h"
#include "DownloaderFactory.h"
#include "FilterWrapper.h"
#include "ModuleFactory.h"
#include "ActionQueue.h"
#include "PinotSettings.h"
#include "WorkerThreads.h"
using namespace std;
static struct option g_longOptions[] = {
{"backend", 1, 0, 'b'},
{"check", 0, 0, 'c'},
{"db", 1, 0, 'd'},
{"help", 0, 0, 'h'},
{"index", 0, 0, 'i'},
{"override", 1, 0, 'o'},
{"showinfo", 0, 0, 's'},
{"version", 0, 0, 'v'},
{0, 0, 0, 0}
};
static Glib::RefPtr<Glib::MainLoop> g_refMainLoop;
class IndexingState : public QueueManager
{
public:
IndexingState(const string &indexLocation) :
QueueManager(indexLocation, 60, true),
m_docId(0)
{
// Disable implicit flushing
WorkerThread::immediateFlush(false);
m_onThreadEndSignal.connect(sigc::mem_fun(*this, &IndexingState::on_thread_end));
}
virtual ~IndexingState()
{
}
virtual Glib::ustring queue_index(const DocumentInfo &docInfo)
{
// Index directly
return index_document(docInfo);
}
void on_thread_end(WorkerThread *pThread)
{
string indexedUrl;
if (pThread == NULL)
{
return;
}
string type(pThread->getType());
bool isStopped = pThread->isStopped();
#ifdef DEBUG
clog << "IndexingState::on_thread_end: end of thread " << type << " " << pThread->getId() << endl;
#endif
// What type of thread was it ?
if ((isStopped == false) &&
((type == "IndexingThread") || (type == "DirectoryScannerThread")))
{
IndexingThread *pIndexThread = dynamic_cast<IndexingThread *>(pThread);
if (pIndexThread == NULL)
{
delete pThread;
return;
}
// Get the document ID of the URL we have just indexed
m_docId = pIndexThread->getDocumentID();
// Explicitely flush the index once a directory has been crawled
IndexInterface *pIndex = PinotSettings::getInstance().getIndex(m_defaultIndexLocation);
if (pIndex != NULL)
{
pIndex->flush();
delete pIndex;
}
}
// Delete the thread
delete pThread;
Memory::reclaim();
// Stop there
g_refMainLoop->quit();
}
unsigned int getDocumentID(void) const
{
return m_docId;
}
protected:
unsigned int m_docId;
};
static IndexingState *g_pState = NULL;
static void printHelp(void)
{
map<ModuleProperties, bool> engines;
// Help
ModuleFactory::loadModules(string(LIBDIR) + string("/pinot/backends"));
ModuleFactory::getSupportedEngines(engines);
clog << "pinot-index - Index documents from the command-line\n\n"
<< "Usage: pinot-index [OPTIONS] --db DATABASE URLS\n\n"
<< "Options:\n"
<< " -b, --backend name of back-end to use (default " << PinotSettings::getInstance().m_defaultBackend << ")\n"
<< " -c, --check check whether the given URL is in the index\n"
<< " -d, --db path to, or name of, index to use (mandatory)\n"
<< " -h, --help display this help and exit\n"
<< " -i, --index index the given URL\n"
<< " -o, --override MIME type detection override, as TYPE:EXT\n"
<< " -s, --showinfo show information about the document\n"
<< " -v, --version output version information and exit\n\n"
<< "Supported back-ends are:";
for (map<ModuleProperties, bool>::const_iterator engineIter = engines.begin(); engineIter != engines.end(); ++engineIter)
{
if ((engineIter->second == true) &&
(ModuleFactory::isSupported(engineIter->first.m_name, true) == true))
{
clog << " '" << engineIter->first.m_name << "'";
}
}
ModuleFactory::unloadModules();
clog << "\n\nExamples:\n"
<< "pinot-index --check --showinfo --backend xapian --db ~/.pinot/daemon ../Bozo.txt\n\n"
<< "pinot-index --index --db Docs https://github.com/FabriceColin/pinot\n\n"
<< "pinot-index --index --db Docs --override text/x-rst:rst /usr/share/doc/python-kitchen-1.1.1/docs/index.rst\n\n"
<< "Indexing documents to My Web Pages or My Documents with pinot-index is not recommended\n\n"
<< "Report bugs to " << PACKAGE_BUGREPORT << endl;
}
static void closeAll(void)
{
if (g_pState != NULL)
{
delete g_pState;
g_pState = NULL;
}
// Close everything
ModuleFactory::unloadModules();
DownloaderInterface::shutdown();
MIMEScanner::shutdown();
}
static void quitAll(int sigNum)
{
if (g_refMainLoop->is_running() == true)
{
clog << "Quitting..." << endl;
if (g_pState != NULL)
{
g_pState->mustQuit(true);
}
g_refMainLoop->quit();
}
}
int main(int argc, char **argv)
{
string type, option;
string backendType, databaseName;
int longOptionIndex = 0;
bool checkDocument = false, indexDocument = false, showInfo = false, success = false;
// Look at the options
int optionChar = getopt_long(argc, argv, "b:cd:hio:sv", g_longOptions, &longOptionIndex);
while (optionChar != -1)
{
set<string> engines;
switch (optionChar)
{
case 'b':
if (optarg != NULL)
{
backendType = optarg;
}
break;
case 'c':
checkDocument = true;
break;
case 'd':
if (optarg != NULL)
{
databaseName = optarg;
}
break;
case 'h':
printHelp();
return EXIT_SUCCESS;
case 'i':
indexDocument = true;
break;
case 'o':
if (optarg != NULL)
{
string override(optarg);
string::size_type pos = override.find(':');
if ((pos != string::npos) &&
(pos + 1 < override.length()))
{
MIMEScanner::addOverride(override.substr(0, pos), override.substr(pos + 1));
}
}
break;
case 's':
showInfo = true;
break;
case 'v':
clog << "pinot-index - " << PACKAGE_STRING << "\n\n"
<< "This is free software. You may redistribute copies of it under the terms of\n"
<< "the GNU General Public License <http://www.gnu.org/licenses/old-licenses/gpl-2.0.html>.\n"
<< "There is NO WARRANTY, to the extent permitted by law." << endl;
return EXIT_SUCCESS;
default:
return EXIT_FAILURE;
}
// Next option
optionChar = getopt_long(argc, argv, "b:cd:hio:sv", g_longOptions, &longOptionIndex);
}
#if defined(ENABLE_NLS)
bindtextdomain(GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
bind_textdomain_codeset(GETTEXT_PACKAGE, "UTF-8");
textdomain(GETTEXT_PACKAGE);
#endif //ENABLE_NLS
Glib::init();
Gio::init();
// Initialize threads support before doing anything else
if (Glib::thread_supported() == false)
{
Glib::thread_init();
}
// Initialize GType
#if !GLIB_CHECK_VERSION(2,35,0)
g_type_init();
#endif
if (argc == 1)
{
printHelp();
return EXIT_SUCCESS;
}
if ((argc < 2) ||
(argc - optind == 0))
{
clog << "Not enough parameters" << endl;
return EXIT_FAILURE;
}
if (((indexDocument == false) &&
(checkDocument == false)) ||
(databaseName.empty() == true))
{
clog << "Incorrect parameters" << endl;
return EXIT_FAILURE;
}
g_refMainLoop = Glib::MainLoop::create();
Glib::set_application_name("Pinot Indexer");
// This will create the necessary directories on the first run
PinotSettings &settings = PinotSettings::getInstance();
string confDirectory(PinotSettings::getConfigurationDirectory());
if (MIMEScanner::initialize(PinotSettings::getHomeDirectory() + "/.local",
string(SHARED_MIME_INFO_PREFIX)) == false)
{
clog << "Couldn't load MIME settings" << endl;
}
DownloaderInterface::initialize();
// Load filter libraries, if any
Dijon::FilterFactory::loadFilters(string(LIBDIR) + string("/pinot/filters"));
Dijon::FilterFactory::loadFilters(confDirectory + "/filters");
// Load backends, if any
ModuleFactory::loadModules(string(LIBDIR) + string("/pinot/backends"));
ModuleFactory::loadModules(confDirectory + "/backends");
// Localize language names
Languages::setIntlName(0, _("Unknown"));
Languages::setIntlName(1, _("Danish"));
Languages::setIntlName(2, _("Dutch"));
Languages::setIntlName(3, _("English"));
Languages::setIntlName(4, _("Finnish"));
Languages::setIntlName(5, _("French"));
Languages::setIntlName(6, _("German"));
Languages::setIntlName(7, _("Hungarian"));
Languages::setIntlName(8, _("Italian"));
Languages::setIntlName(9, _("Norwegian"));
Languages::setIntlName(10, _("Portuguese"));
Languages::setIntlName(11, _("Romanian"));
Languages::setIntlName(12, _("Russian"));
Languages::setIntlName(13, _("Spanish"));
Languages::setIntlName(14, _("Swedish"));
Languages::setIntlName(15, _("Turkish"));
// Load the settings
settings.load(PinotSettings::LOAD_ALL);
// Catch interrupts
#ifdef HAVE_SIGACTION
struct sigaction newAction;
sigemptyset(&newAction.sa_mask);
newAction.sa_flags = 0;
newAction.sa_handler = quitAll;
sigaction(SIGINT, &newAction, NULL);
sigaction(SIGQUIT, &newAction, NULL);
sigaction(SIGTERM, &newAction, NULL);
#else
signal(SIGINT, quitAll);
#ifdef SIGQUIT
signal(SIGQUIT, quitAll);
#endif
signal(SIGTERM, quitAll);
#endif
// Is this a known index name ?
PinotSettings::IndexProperties indexProps = settings.getIndexPropertiesByName(databaseName);
if (indexProps.m_name.empty() == true)
{
// No, it's not
indexProps.m_location = databaseName;
}
if (backendType.empty() == true)
{
backendType = settings.m_defaultBackend;
}
// Make sure the index is open in the correct mode
bool wasObsoleteFormat = false;
if (ModuleFactory::openOrCreateIndex(backendType, indexProps.m_location, wasObsoleteFormat, (indexDocument ? false : true)) == false)
{
clog << "Couldn't open index " << indexProps.m_location << endl;
return EXIT_FAILURE;
}
// Do the same for the history database
string historyDatabase(settings.getHistoryDatabaseName());
if ((historyDatabase.empty() == true) ||
(ActionQueue::create(historyDatabase) == false))
{
clog << "Couldn't create history database " << historyDatabase << endl;
return EXIT_FAILURE;
}
else
{
ActionQueue actionQueue(historyDatabase, Glib::get_application_name());
time_t timeNow = time(NULL);
// Expire
actionQueue.expireItems(timeNow);
}
atexit(closeAll);
// Get a read-write index of the given type
IndexInterface *pIndex = ModuleFactory::getIndex(backendType, indexProps.m_location);
if (pIndex == NULL)
{
clog << "Couldn't obtain index for " << indexProps.m_location << endl;
return EXIT_FAILURE;
}
while (optind < argc)
{
string urlParam(argv[optind]);
Url thisUrl(urlParam, "");
// Rewrite the URL, dropping user name and password which we don't support
urlParam = thisUrl.getProtocol();
urlParam += "://";
if (thisUrl.isLocal() == false)
{
urlParam += thisUrl.getHost();
urlParam += "/";
}
urlParam += thisUrl.getLocation();
if (thisUrl.getFile().empty() == false)
{
urlParam += "/";
urlParam += thisUrl.getFile();
}
#ifdef DEBUG
clog << "URL rewritten to " << urlParam << endl;
#endif
DocumentInfo docInfo("", urlParam, MIMEScanner::scanUrl(thisUrl), "");
unsigned int docId = 0;
if ((checkDocument == true) &&
(pIndex->isGood() == true))
{
set<unsigned int> docIds;
docId = pIndex->hasDocument(urlParam);
if (docId > 0)
{
clog << urlParam << ": document ID " << docId << endl;
success = true;
}
else if ((pIndex->listDocuments(urlParam, docIds, IndexInterface::BY_FILE, 100, 0) == true) &&
(docIds.empty() == false))
{
docId = *(docIds.begin());
clog << urlParam << ": document ID " << docId
<< " and at least " << docIds.size() - 1 << " others" << endl;
success = true;
}
else
{
clog << urlParam << ": not found" << endl;
}
}
if (indexDocument == true)
{
if (g_pState == NULL)
{
g_pState = new IndexingState(indexProps.m_location);
}
// Connect to threads' finished signal
g_pState->connect();
Glib::ustring status(g_pState->queue_index(docInfo));
if (status.empty() == true)
{
// Run the main loop
g_refMainLoop->run();
}
else
{
clog << status << endl;
}
// Stop everything
g_pState->disconnect();
docId = g_pState->getDocumentID();
if (g_pState->mustQuit() == true)
{
break;
}
}
if ((showInfo == true) &&
(docId > 0))
{
set<string> labels;
clog << "Index version: " << pIndex->getMetadata("version") << endl;
if (pIndex->getDocumentInfo(docId, docInfo) == true)
{
clog << "Document ID : " << docId << endl;
clog << "Location : '" << docInfo.getLocation(true) << "'" << endl;
clog << "Title : " << docInfo.getTitle() << endl;
clog << "Type : " << docInfo.getType() << endl;
clog << "Language : " << docInfo.getLanguage() << endl;
clog << "Date : " << docInfo.getTimestamp() << endl;
clog << "Size : " << docInfo.getSize() << endl;
}
if (pIndex->getDocumentLabels(docId, labels) == true)
{
clog << "Labels : ";
for (set<string>::const_iterator labelIter = labels.begin();
labelIter != labels.end(); ++labelIter)
{
if (labelIter->substr(0, 2) == "X-")
{
continue;
}
clog << "[" << Url::escapeUrl(*labelIter) << "]";
}
clog << endl;
}
vector<MIMEAction> typeActions;
MIMEScanner::getDefaultActions(docInfo.getType(), thisUrl.isLocal(), typeActions);
for (vector<MIMEAction>::const_iterator actionIter = typeActions.begin();
actionIter != typeActions.end(); ++actionIter)
{
clog << "Action : '" << actionIter->m_name << "' " << actionIter->m_exec << endl;
}
}
// Next
++optind;
}
delete pIndex;
// Did whatever operation we carried out succeed ?
if (success == true)
{
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
|