Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop wrappers from Internal Server #536

Merged
merged 9 commits into from
Jul 6, 2021
2 changes: 2 additions & 0 deletions format_code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ files=(
"include/common/otherTools.h"
"include/common/regexTools.h"
"include/common/networkTools.h"
"include/common/archiveTools.h"
"include/manager.h"
"include/reader.h"
"include/kiwix.h"
Expand All @@ -22,6 +23,7 @@ files=(
"src/common/pathTools.cpp"
"src/common/regexTools.cpp"
"src/common/otherTools.cpp"
"src/common/archiveTools.cpp"
"src/common/networkTools.cpp"
"src/common/stringTools.cpp"
"src/xapianSearcher.cpp"
Expand Down
3 changes: 3 additions & 0 deletions include/library.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <vector>
#include <map>
#include <memory>
#include <zim/archive.h>

#include "book.h"
#include "bookmark.h"
Expand Down Expand Up @@ -146,6 +147,7 @@ class Library
{
std::map<std::string, kiwix::Book> m_books;
std::map<std::string, std::shared_ptr<Reader>> m_readers;
std::map<std::string, std::shared_ptr<zim::Archive>> m_archives;
maneeshpm marked this conversation as resolved.
Show resolved Hide resolved
maneeshpm marked this conversation as resolved.
Show resolved Hide resolved
std::vector<kiwix::Bookmark> m_bookmarks;
class BookDB;
std::unique_ptr<BookDB> m_bookDB;
Expand Down Expand Up @@ -198,6 +200,7 @@ class Library
const Book& getBookByPath(const std::string& path) const;
Book& getBookByPath(const std::string& path);
std::shared_ptr<Reader> getReaderById(const std::string& id);
std::shared_ptr<zim::Archive> getArchiveById(const std::string& id);

/**
* Remove a book from the library.
Expand Down
1 change: 1 addition & 0 deletions include/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ install_headers(
'tools/pathTools.h',
'tools/regexTools.h',
'tools/stringTools.h',
'tools/archiveTools.h',
subdir:'kiwix/tools'
)

13 changes: 10 additions & 3 deletions include/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ namespace kiwix
* The SuggestionItem is a helper class that contains the info about a single
* suggestion item.
*/

class SuggestionItem
{
// Functions
private:
// Temporarily making the constructor public until the code move is complete
public:
// Create a sugggestion item.
explicit SuggestionItem(std::string title, std::string normalizedTitle,
std::string path, std::string snippet = "") :
Expand Down Expand Up @@ -91,6 +91,13 @@ class Reader
* (.zim extesion).
*/
explicit Reader(const string zimFilePath);

/**
* Create a Reader to read a zim file given by the Archive.
*
* @param archive The shared pointer to the Archive object.
*/
explicit Reader(const std::shared_ptr<zim::Archive> archive);
#ifndef _WIN32
explicit Reader(int fd);
Reader(int fd, zim::offset_type offset, zim::size_type size);
Expand Down Expand Up @@ -488,7 +495,7 @@ class Reader
zim::Archive* getZimArchive() const;

protected:
std::unique_ptr<zim::Archive> zimArchive;
std::shared_ptr<zim::Archive> zimArchive;
std::string zimFilePath;

SuggestionsList_t suggestions;
Expand Down
5 changes: 4 additions & 1 deletion include/search_renderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#define KIWIX_SEARCH_RENDERER_H

#include <string>
#include <zim/search.h>

namespace kiwix
{
Expand All @@ -40,6 +41,8 @@ class SearchRenderer
* Used to generate pagination links.
*/
SearchRenderer(Searcher* searcher, NameMapper* mapper);
SearchRenderer(zim::SearchResultSet srs, NameMapper* mapper,
unsigned int start, unsigned int estimatedResultCount);

~SearchRenderer();

Expand Down Expand Up @@ -74,7 +77,7 @@ class SearchRenderer

protected:
std::string beautifyInteger(const unsigned int number);
Searcher* mp_searcher;
zim::SearchResultSet m_srs;
NameMapper* mp_nameMapper;
std::string searchContent;
std::string searchPattern;
Expand Down
7 changes: 7 additions & 0 deletions include/searcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "tools/pathTools.h"
#include "tools/stringTools.h"

#include <zim/search.h>

using namespace std;

namespace kiwix
Expand Down Expand Up @@ -142,6 +144,11 @@ class Searcher
*/
unsigned int getEstimatedResultCount();

/**
* Get a SearchResultSet object for current search
*/
zim::SearchResultSet getSearchResultSet();

unsigned int getResultStart() { return resultStart; }
unsigned int getResultEnd() { return resultEnd; }

Expand Down
47 changes: 47 additions & 0 deletions include/tools/archiveTools.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
* Copyright 2021 Maneesh P M <manu.pm55@gmail.com>
*
* 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 3 of the License, or
* 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

#ifndef KIWIX_ARCHIVETOOLS_H
#define KIWIX_ARCHIVETOOLS_H

#include <zim/archive.h>

/**
* This file contains all the functions that would make handling data related to
* an archive easier.
**/

namespace kiwix
{
std::string getMetadata(const zim::Archive& archive, const std::string& name);
std::string getArchiveTitle(const zim::Archive& archive);
std::string getMetaDescription(const zim::Archive& archive);
std::string getMetaTags(const zim::Archive& archive, bool original = false);
bool getArchiveFavicon(const zim::Archive& archive,
std::string& content, std::string& mimeType);
std::string getMetaLanguage(const zim::Archive& archive);
std::string getMetaName(const zim::Archive& archive);
std::string getMetaDate(const zim::Archive& archive);
std::string getMetaCreator(const zim::Archive& archive);
std::string getMetaPublisher(const zim::Archive& archive);
zim::Item getFinalItem(const zim::Archive& archive, const zim::Entry& entry);
zim::Entry getEntryFromPath(const zim::Archive& archive, const std::string& path);
}

#endif
2 changes: 2 additions & 0 deletions include/tools/stringTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,7 @@ T extractFromString(const std::string& str) {
}

bool startsWith(const std::string& base, const std::string& start);

std::vector<std::string> getTitleVariants(const std::string& title);
} //namespace kiwix
#endif
29 changes: 27 additions & 2 deletions src/library.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ bool Library::removeBookById(const std::string& id)
{
m_bookDB->delete_document("Q" + id);
m_readers.erase(id);
m_archives.erase(id);
return m_books.erase(id) == 1;
}

Expand Down Expand Up @@ -146,11 +147,35 @@ std::shared_ptr<Reader> Library::getReaderById(const std::string& id)
return m_readers.at(id);
} catch (std::out_of_range& e) {}

try {
auto reader = make_shared<Reader>(m_archives.at(id));
m_readers[id] = reader;
return reader;
} catch (std::out_of_range& e) {}

auto book = getBookById(id);
if (!book.isPathValid())
return nullptr;

auto archive = make_shared<zim::Archive>(book.getPath());
m_archives[id] = archive;
auto reader = make_shared<Reader>(archive);
m_readers[id] = reader;
return reader;
}

std::shared_ptr<zim::Archive> Library::getArchiveById(const std::string& id)
{
try {
return m_archives.at(id);
} catch (std::out_of_range& e) {}

auto book = getBookById(id);
if (!book.isPathValid())
return nullptr;
auto sptr = make_shared<Reader>(book.getPath());
m_readers[id] = sptr;

auto sptr = make_shared<zim::Archive>(book.getPath());
m_archives[id] = sptr;
return sptr;
}

Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ kiwix_sources = [
'tools/stringTools.cpp',
'tools/networkTools.cpp',
'tools/otherTools.cpp',
'tools/archiveTools.cpp',
'kiwixserve.cpp',
'name_mapper.cpp',
'server/byte_range.cpp',
Expand Down
65 changes: 17 additions & 48 deletions src/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <zim/error.h>

#include "tools/otherTools.h"
#include "tools/archiveTools.h"

inline char hi(char v)
{
Expand Down Expand Up @@ -86,6 +87,11 @@ Reader::Reader(const string zimFilePath)
srand(time(nullptr));
}

Reader::Reader(const std::shared_ptr<zim::Archive> archive)
: zimArchive(archive),
zimFilePath(archive->getFilename())
{}

#ifndef _WIN32
Reader::Reader(int fd)
: zimArchive(new zim::Archive(fd)),
Expand Down Expand Up @@ -183,14 +189,7 @@ Entry Reader::getMainPage() const

bool Reader::getFavicon(string& content, string& mimeType) const
{
try {
auto item = zimArchive->getIllustrationItem();
content = item.getData();
mimeType = item.getMimetype();
return true;
} catch(zim::EntryNotFound& e) {};

return false;
return kiwix::getArchiveFavicon(*zimArchive, content, mimeType);
}

string Reader::getZimFilePath() const
Expand All @@ -212,47 +211,32 @@ bool Reader::getMetadata(const string& name, string& value) const

string Reader::getName() const
{
METADATA("Name")
return kiwix::getMetaName(*zimArchive);
}

string Reader::getTitle() const
{
string value = zimArchive->getMetadata("Title");
if (value.empty()) {
value = getLastPathElement(zimFilePath);
std::replace(value.begin(), value.end(), '_', ' ');
size_t pos = value.find(".zim");
value = value.substr(0, pos);
}
return value;
return kiwix::getArchiveTitle(*zimArchive);
}

string Reader::getCreator() const
{
METADATA("Creator")
return kiwix::getMetaCreator(*zimArchive);
}

string Reader::getPublisher() const
{
METADATA("Publisher")
return kiwix::getMetaPublisher(*zimArchive);
}

string Reader::getDate() const
{
METADATA("Date")
return kiwix::getMetaDate(*zimArchive);
}

string Reader::getDescription() const
{
string value;
this->getMetadata("Description", value);

/* Mediawiki Collection tends to use the "Subtitle" name */
if (value.empty()) {
this->getMetadata("Subtitle", value);
}

return value;
return kiwix::getMetaDescription(*zimArchive);
}

string Reader::getLongDescription() const
Expand All @@ -262,7 +246,7 @@ string Reader::getLongDescription() const

string Reader::getLanguage() const
{
METADATA("Language")
return kiwix::getMetaLanguage(*zimArchive);
}

string Reader::getLicense() const
Expand All @@ -272,13 +256,7 @@ string Reader::getLicense() const

string Reader::getTags(bool original) const
{
string tags_str;
getMetadata("Tags", tags_str);
if (original) {
return tags_str;
}
auto tags = convertTags(tags_str);
return join(tags, ";");
return kiwix::getMetaTags(*zimArchive, original);
}


Expand Down Expand Up @@ -342,12 +320,8 @@ string Reader::getOrigId() const

Entry Reader::getEntryFromPath(const std::string& path) const
{
if (path.empty() || path == "/") {
return getMainPage();
}

try {
return zimArchive->getEntryByPath(path);
return kiwix::getEntryFromPath(*zimArchive, path);
} catch (zim::EntryNotFound& e) {
throw NoEntry();
}
Expand Down Expand Up @@ -460,12 +434,7 @@ bool Reader::searchSuggestions(const string& prefix,
std::vector<std::string> Reader::getTitleVariants(
const std::string& title) const
{
std::vector<std::string> variants;
variants.push_back(title);
variants.push_back(kiwix::ucFirst(title));
variants.push_back(kiwix::lcFirst(title));
variants.push_back(kiwix::toTitle(title));
return variants;
return kiwix::getTitleVariants(title);
}


Expand Down
Loading