diff --git a/src/common/kiwix/reader.cpp b/src/common/kiwix/reader.cpp index 667b23950..80c8c725e 100644 --- a/src/common/kiwix/reader.cpp +++ b/src/common/kiwix/reader.cpp @@ -67,14 +67,68 @@ namespace kiwix { void Reader::reset() { this->currentArticleOffset = this->firstArticleOffset; } + + std::map Reader::parseCounterMetadata() { + std::map counters; + string content, mimeType, item, counterString; + unsigned int contentLength, counter; + string counterUrl = "/M/Counter"; + + this->getContentByUrl(counterUrl, content, contentLength, mimeType); + stringstream ssContent(content); + + while(getline(ssContent, item, ';')) { + stringstream ssItem(item); + getline(ssItem, mimeType, '='); + getline(ssItem, counterString, '='); + if (!counterString.empty() && !mimeType.empty()) { + sscanf(counterString.c_str(), "%u", &counter); + counters.insert(pair(mimeType, counter)); + } + } + + return counters; + } /* Get the count of articles which can be indexed/displayed */ unsigned int Reader::getArticleCount() { - return this->articleCount; + std::map counterMap = this->parseCounterMetadata(); + unsigned int counter = 0; + + if (counterMap.empty()) + counter = this->articleCount; + else { + std::map::const_iterator it = counterMap.find("text/html"); + if (it != counterMap.end()) + counter = it->second; + } + + return counter; } unsigned int Reader::getMediaCount() { - return this->mediaCount; + std::map counterMap = this->parseCounterMetadata(); + unsigned int counter = 0; + + if (counterMap.empty()) + counter = this->mediaCount; + else { + std::map::const_iterator it; + + it = counterMap.find("image/jpeg"); + if (it != counterMap.end()) + counter += it->second; + + it = counterMap.find("image/gif"); + if (it != counterMap.end()) + counter += it->second; + + it = counterMap.find("image/png"); + if (it != counterMap.end()) + counter += it->second; + } + + return counter; } /* Return the UID of the ZIM file */ diff --git a/src/common/kiwix/reader.h b/src/common/kiwix/reader.h index 997c5bcbc..e3452b3f2 100644 --- a/src/common/kiwix/reader.h +++ b/src/common/kiwix/reader.h @@ -24,8 +24,10 @@ #include #include #include +#include #include #include +#include #include "time.h" using namespace std; @@ -71,6 +73,10 @@ namespace kiwix { std::vector suggestions; std::vector::iterator suggestionsOffset; + + private: + std::map parseCounterMetadata(); + }; }