Skip to content

Commit

Permalink
Correctly detect the number of article for zim version <= 6
Browse files Browse the repository at this point in the history
  • Loading branch information
mgautierfr committed Apr 4, 2022
1 parent b1f0338 commit 84e3719
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/book.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ void Book::update(const zim::Archive& archive) {
m_flavour = getMetaFlavour(archive);
m_tags = getMetaTags(archive);
m_category = getCategoryFromTags();
m_articleCount = archive.getArticleCount();
m_articleCount = getArchiveArticleCount(archive);
m_mediaCount = getArchiveMediaCount(archive);
m_size = static_cast<uint64_t>(getArchiveFileSize(archive)) << 10;

Expand Down
24 changes: 24 additions & 0 deletions src/tools/archiveTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,30 @@ unsigned int getArchiveMediaCount(const zim::Archive& archive) {
return counter;
}

unsigned int getArchiveArticleCount(const zim::Archive& archive) {
// [HACK]
// getArticleCount() returns different things depending of the "version" of the zim.
// On old zim (<=6), it returns the number of entry in `A` namespace
// On recent zim (>=7), it returns:
// - the number of entry in `C` namespace (==getEntryCount) if no frontArticleIndex is present
// - the number of front article if a frontArticleIndex is present
// The use case >=7 without frontArticleIndex is pretty rare so we don't care
// We can detect if we are reading a zim <= 6 by checking if we have a newNamespaceScheme.
if (archive.hasNewNamespaceScheme()) {
//The articleCount is "good"
return archive.getArticleCount();
} else {
// We have to parse the `M/Counter` metadata
unsigned int counter = 0;
for(const auto& pair:parseArchiveCounter(archive)) {
if (startsWith(pair.first, "text/html")) {
counter += pair.second;
}
}
return counter;
}
}

unsigned int getArchiveFileSize(const zim::Archive& archive) {
return archive.getFilesize() / 1024;
}
Expand Down
1 change: 1 addition & 0 deletions src/tools/archiveTools.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace kiwix
std::string& content, std::string& mimeType);

unsigned int getArchiveMediaCount(const zim::Archive& archive);
unsigned int getArchiveArticleCount(const zim::Archive& archive);
unsigned int getArchiveFileSize(const zim::Archive& archive);

zim::Item getFinalItem(const zim::Archive& archive, const zim::Entry& entry);
Expand Down

0 comments on commit 84e3719

Please sign in to comment.