Skip to content

Commit 61ccbc6

Browse files
authored
Merge pull request #743 from kiwix/fix_article_count
Correctly detect the number of article for zim version <= 6
2 parents a17258f + 85a9d35 commit 61ccbc6

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

src/book.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ void Book::update(const zim::Archive& archive) {
8383
m_flavour = getMetaFlavour(archive);
8484
m_tags = getMetaTags(archive);
8585
m_category = getCategoryFromTags();
86-
m_articleCount = archive.getArticleCount();
86+
m_articleCount = getArchiveArticleCount(archive);
8787
m_mediaCount = getArchiveMediaCount(archive);
8888
m_size = static_cast<uint64_t>(getArchiveFileSize(archive)) << 10;
8989

src/tools/archiveTools.cpp

+24
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,30 @@ unsigned int getArchiveMediaCount(const zim::Archive& archive) {
125125
return counter;
126126
}
127127

128+
unsigned int getArchiveArticleCount(const zim::Archive& archive) {
129+
// [HACK]
130+
// getArticleCount() returns different things depending of the "version" of the zim.
131+
// On old zim (<=6), it returns the number of entry in `A` namespace
132+
// On recent zim (>=7), it returns:
133+
// - the number of entry in `C` namespace (==getEntryCount) if no frontArticleIndex is present
134+
// - the number of front article if a frontArticleIndex is present
135+
// The use case >=7 without frontArticleIndex is pretty rare so we don't care
136+
// We can detect if we are reading a zim <= 6 by checking if we have a newNamespaceScheme.
137+
if (archive.hasNewNamespaceScheme()) {
138+
//The articleCount is "good"
139+
return archive.getArticleCount();
140+
} else {
141+
// We have to parse the `M/Counter` metadata
142+
unsigned int counter = 0;
143+
for(const auto& pair:parseArchiveCounter(archive)) {
144+
if (startsWith(pair.first, "text/html")) {
145+
counter += pair.second;
146+
}
147+
}
148+
return counter;
149+
}
150+
}
151+
128152
unsigned int getArchiveFileSize(const zim::Archive& archive) {
129153
return archive.getFilesize() / 1024;
130154
}

src/tools/archiveTools.h

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ namespace kiwix
4646
std::string& content, std::string& mimeType);
4747

4848
unsigned int getArchiveMediaCount(const zim::Archive& archive);
49+
unsigned int getArchiveArticleCount(const zim::Archive& archive);
4950
unsigned int getArchiveFileSize(const zim::Archive& archive);
5051

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

0 commit comments

Comments
 (0)