Skip to content

Commit

Permalink
Adapt kiwix-tools to new libkiwix API.
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr committed Oct 18, 2022
1 parent 2a9378e commit 009c841
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 29 deletions.
24 changes: 12 additions & 12 deletions src/manager/kiwix-manage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ using namespace std;

enum supportedAction { NONE, ADD, SHOW, REMOVE };

void show(kiwix::Library* library, const std::string& bookId)
void show(const kiwix::Library& library, const std::string& bookId)
{
try {
auto& book = library->getBookById(bookId);
auto& book = library.getBookById(bookId);
std::cout << "id:\t\t" << book.getId() << std::endl
<< "path:\t\t" << book.getPath() << std::endl
<< "url:\t\t" << book.getUrl() << std::endl
Expand Down Expand Up @@ -96,7 +96,7 @@ void usage()
<< std::endl;
}

int handle_show(kiwix::Library* library, const std::string& libraryPath,
int handle_show(const kiwix::Library& library, const std::string& libraryPath,
int argc, char* argv[])
{
if (argc > 3 ) {
Expand All @@ -105,15 +105,15 @@ int handle_show(kiwix::Library* library, const std::string& libraryPath,
show(library, bookId);
}
} else {
auto booksIds = library->getBooksIds();
auto booksIds = library.getBooksIds();
for(auto& bookId: booksIds) {
show(library, bookId);
}
}
return(0);
}

int handle_add(kiwix::Library* library, const std::string& libraryPath,
int handle_add(std::shared_ptr<kiwix::Library> library, const std::string& libraryPath,
int argc, char* argv[])
{
string zimPath;
Expand Down Expand Up @@ -182,7 +182,7 @@ int handle_add(kiwix::Library* library, const std::string& libraryPath,
return(resultCode);
}

int handle_remove(kiwix::Library* library, const std::string& libraryPath,
int handle_remove(std::shared_ptr<kiwix::Library> library, const std::string& libraryPath,
int argc, char* argv[])
{
std::string bookId;
Expand Down Expand Up @@ -216,7 +216,7 @@ int main(int argc, char** argv)
{
string libraryPath = "";
supportedAction action = NONE;
kiwix::Library library;
auto library = std::make_shared<kiwix::Library>();

/* General argument parsing */
static struct option long_options[] = {
Expand Down Expand Up @@ -261,7 +261,7 @@ int main(int argc, char** argv)
libraryPath = kiwix::isRelativePath(libraryPath)
? kiwix::computeAbsolutePath(kiwix::getCurrentDirectory(), libraryPath)
: libraryPath;
kiwix::Manager manager(&library);
kiwix::Manager manager(library);
if (!manager.readFile(libraryPath, false)) {
if (kiwix::fileExists(libraryPath) || action!=ADD) {
std::cerr << "Cannot read the library " << libraryPath << std::endl;
Expand All @@ -273,13 +273,13 @@ int main(int argc, char** argv)
int exitCode = 0;
switch (action) {
case SHOW:
exitCode = handle_show(&library, libraryPath, argc, argv);
exitCode = handle_show(*library, libraryPath, argc, argv);
break;
case ADD:
exitCode = handle_add(&library, libraryPath, argc, argv);
exitCode = handle_add(library, libraryPath, argc, argv);
break;
case REMOVE:
exitCode = handle_remove(&library, libraryPath, argc, argv);
exitCode = handle_remove(library, libraryPath, argc, argv);
break;
case NONE:
break;
Expand All @@ -292,7 +292,7 @@ int main(int argc, char** argv)
/* Rewrite the library file */
if (action == REMOVE || action == ADD) {
// writeToFile return true (1) if everything is ok => exitCode is 0
if (!library.writeToFile(libraryPath)) {
if (!library->writeToFile(libraryPath)) {
std::cerr << "Cannot write the library " << libraryPath << std::endl;
return 1;
}
Expand Down
35 changes: 18 additions & 17 deletions src/server/kiwix-serve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ int main(int argc, char** argv)
#endif

std::string rootLocation = "/";
kiwix::Library library;
auto library = std::make_shared<kiwix::Library>();
unsigned int nb_threads = DEFAULT_THREADS;
std::vector<std::string> zimPathes;
std::string libraryPath;
Expand Down Expand Up @@ -331,7 +331,7 @@ int main(int argc, char** argv)
}

/* Setup the library manager and get the list of books */
kiwix::Manager manager(&library);
kiwix::Manager manager(library);
std::vector<std::string> libraryPaths;
if (libraryFlag) {
libraryPaths = kiwix::split(libraryPath, ";");
Expand All @@ -340,7 +340,7 @@ int main(int argc, char** argv)
}

/* Check if the library is not empty (or only remote books)*/
if (library.getBookCount(true, false) == 0) {
if (library->getBookCount(true, false) == 0) {
std::cerr << "The XML library file '" << libraryPath
<< "' is empty (or has only remote books)." << std::endl;
}
Expand Down Expand Up @@ -376,8 +376,8 @@ int main(int argc, char** argv)
}
#endif

kiwix::UpdatableNameMapper nameMapper(library, noDateAliasesFlag);
kiwix::Server server(&library, &nameMapper);
auto nameMapper = std::make_shared<kiwix::UpdatableNameMapper>(library, noDateAliasesFlag);
kiwix::Server::Configuration configuration(library, nameMapper);

if (!customIndexPath.empty()) {
try {
Expand All @@ -388,17 +388,18 @@ int main(int argc, char** argv)
}
}

server.setAddress(address);
server.setRoot(rootLocation);
server.setPort(serverPort);
server.setNbThreads(nb_threads);
server.setVerbose(isVerboseFlag);
server.setTaskbar(!noSearchBarFlag, !noLibraryButtonFlag);
server.setBlockExternalLinks(blockExternalLinks);
server.setIndexTemplateString(indexTemplateString);
server.setIpConnectionLimit(ipConnectionLimit);
server.setMultiZimSearchLimit(searchLimit);

configuration.setAddress(address);
configuration.setRoot(rootLocation);
configuration.setPort(serverPort);
configuration.setNbThreads(nb_threads);
configuration.setVerbose(isVerboseFlag);
configuration.setTaskbar(!noSearchBarFlag, !noLibraryButtonFlag);
configuration.setBlockExternalLinks(blockExternalLinks);
configuration.setIndexTemplateString(indexTemplateString);
configuration.setIpConnectionLimit(ipConnectionLimit);
configuration.setMultiZimSearchLimit(searchLimit);

kiwix::Server server(configuration);
if (! server.start()) {
exit(1);
}
Expand Down Expand Up @@ -447,7 +448,7 @@ int main(int argc, char** argv)
if ( libraryMustBeReloaded && !libraryPaths.empty() ) {
libraryFileTimestamp = curLibraryFileTimestamp;
reloadLibrary(manager, libraryPaths);
nameMapper.update();
nameMapper->update();
libraryMustBeReloaded = false;
}
} while (waiting);
Expand Down

0 comments on commit 009c841

Please sign in to comment.