[MediaWiki-commits] [Gerrit] Move articleSource's related stuffs in articlesource.(h|cpp). - change (openzim)

2016-06-22 Thread Kelson (Code Review)
Kelson has submitted this change and it was merged.

Change subject: Move articleSource's related stuffs in articlesource.(h|cpp).
..


Move articleSource's related stuffs in articlesource.(h|cpp).

Change-Id: Iee91484679bf401a693af1ca7e1c7e34f2c741d0
---
M zimwriterfs/Makefile.am
A zimwriterfs/articlesource.cpp
A zimwriterfs/articlesource.h
M zimwriterfs/zimwriterfs.cpp
4 files changed, 305 insertions(+), 229 deletions(-)

Approvals:
  Kelson: Verified; Looks good to me, approved



diff --git a/zimwriterfs/Makefile.am b/zimwriterfs/Makefile.am
index 3383e35..6e46553 100644
--- a/zimwriterfs/Makefile.am
+++ b/zimwriterfs/Makefile.am
@@ -4,4 +4,5 @@
 zimwriterfs_SOURCES= \
 zimwriterfs.cpp \
 tools.cpp \
-article.cpp
+article.cpp \
+articlesource.cpp
diff --git a/zimwriterfs/articlesource.cpp b/zimwriterfs/articlesource.cpp
new file mode 100644
index 000..8b0b34c
--- /dev/null
+++ b/zimwriterfs/articlesource.cpp
@@ -0,0 +1,256 @@
+/*
+ * Copyright 2013-2016 Emmanuel Engelhart 
+ * Copyright 2016 Matthieu Gautier 
+ *
+ * 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.
+ */
+
+#include "articlesource.h"
+#include "article.h"
+#include "tools.h"
+
+#include 
+
+#include 
+#include 
+#include 
+
+bool popFromFilenameQueue(std::string );
+bool isVerbose();
+
+extern std::string welcome;
+extern std::string language;
+extern std::string creator;
+extern std::string publisher;
+extern std::string title;
+extern std::string description;
+extern std::string directoryPath;
+
+std::map counters;
+char *data = NULL;
+unsigned int dataSize = 0;
+
+
+
+ArticleSource::ArticleSource() {
+  /* Prepare metadata */
+  metadataQueue.push("Language");
+  metadataQueue.push("Publisher");
+  metadataQueue.push("Creator");
+  metadataQueue.push("Title");
+  metadataQueue.push("Description");
+  metadataQueue.push("Date");
+  metadataQueue.push("Favicon");
+  metadataQueue.push("Counter");
+}
+
+void ArticleSource::init_redirectsQueue_from_file(const std::string& path){
+std::ifstream in_stream;
+std::string line;
+
+in_stream.open(path.c_str());
+while (std::getline(in_stream, line)) {
+  redirectsQueue.push(line);
+}
+in_stream.close();
+}
+
+std::string ArticleSource::getMainPage() {
+  return welcome;
+}
+
+Article *article = NULL;
+const zim::writer::Article* ArticleSource::getNextArticle() {
+  std::string path;
+
+  if (article != NULL) {
+delete(article);
+  }
+
+  if (!metadataQueue.empty()) {
+path = metadataQueue.front();
+metadataQueue.pop();
+article = new MetadataArticle(path);
+  } else if (!redirectsQueue.empty()) {
+std::string line = redirectsQueue.front();
+redirectsQueue.pop();
+article = new RedirectArticle(line);
+  } else if (popFromFilenameQueue(path)) {
+do {
+  article = new Article(path);
+} while (article && article->isInvalid() && popFromFilenameQueue(path));
+  } else {
+article = NULL;
+  }
+
+  /* Count mimetypes */
+  if (article != NULL && !article->isRedirect()) {
+
+if (isVerbose())
+  std::cout << "Creating entry for " << article->getAid() << std::endl;
+
+std::string mimeType = article->getMimeType();
+if (counters.find(mimeType) == counters.end()) {
+  counters[mimeType] = 1;
+} else {
+  counters[mimeType]++;
+}
+  }
+
+  return article;
+}
+
+zim::Blob ArticleSource::getData(const std::string& aid) {
+
+  if (isVerbose())
+std::cout << "Packing data for " << aid << std::endl;
+
+  if (data != NULL) {
+delete(data);
+data = NULL;
+  }
+
+  if (aid.substr(0, 3) == "/M/") {
+std::string value; 
+
+if ( aid == "/M/Language") {
+  value = language;
+} else if (aid == "/M/Creator") {
+  value = creator;
+} else if (aid == "/M/Publisher") {
+  value = publisher;
+} else if (aid == "/M/Title") {
+  value = title;
+} else if (aid == "/M/Description") {
+  value = description;
+} else if ( aid == "/M/Date") {
+  time_t t = time(0);
+  struct tm * now = localtime( & t );
+  std::stringstream stream;
+  stream << (now->tm_year + 1900) << '-' 
+<< std::setw(2) << std::setfill('0') << 

[MediaWiki-commits] [Gerrit] Move articleSource's related stuffs in articlesource.(h|cpp). - change (openzim)

2016-06-22 Thread Mgautierfr (Code Review)
Mgautierfr has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/295517

Change subject: Move articleSource's related stuffs in articlesource.(h|cpp).
..

Move articleSource's related stuffs in articlesource.(h|cpp).

Change-Id: Iee91484679bf401a693af1ca7e1c7e34f2c741d0
---
M zimwriterfs/Makefile.am
A zimwriterfs/articlesource.cpp
A zimwriterfs/articlesource.h
M zimwriterfs/zimwriterfs.cpp
4 files changed, 305 insertions(+), 229 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/openzim refs/changes/17/295517/1

diff --git a/zimwriterfs/Makefile.am b/zimwriterfs/Makefile.am
index 3383e35..6e46553 100644
--- a/zimwriterfs/Makefile.am
+++ b/zimwriterfs/Makefile.am
@@ -4,4 +4,5 @@
 zimwriterfs_SOURCES= \
 zimwriterfs.cpp \
 tools.cpp \
-article.cpp
+article.cpp \
+articlesource.cpp
diff --git a/zimwriterfs/articlesource.cpp b/zimwriterfs/articlesource.cpp
new file mode 100644
index 000..8b0b34c
--- /dev/null
+++ b/zimwriterfs/articlesource.cpp
@@ -0,0 +1,256 @@
+/*
+ * Copyright 2013-2016 Emmanuel Engelhart 
+ * Copyright 2016 Matthieu Gautier 
+ *
+ * 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.
+ */
+
+#include "articlesource.h"
+#include "article.h"
+#include "tools.h"
+
+#include 
+
+#include 
+#include 
+#include 
+
+bool popFromFilenameQueue(std::string );
+bool isVerbose();
+
+extern std::string welcome;
+extern std::string language;
+extern std::string creator;
+extern std::string publisher;
+extern std::string title;
+extern std::string description;
+extern std::string directoryPath;
+
+std::map counters;
+char *data = NULL;
+unsigned int dataSize = 0;
+
+
+
+ArticleSource::ArticleSource() {
+  /* Prepare metadata */
+  metadataQueue.push("Language");
+  metadataQueue.push("Publisher");
+  metadataQueue.push("Creator");
+  metadataQueue.push("Title");
+  metadataQueue.push("Description");
+  metadataQueue.push("Date");
+  metadataQueue.push("Favicon");
+  metadataQueue.push("Counter");
+}
+
+void ArticleSource::init_redirectsQueue_from_file(const std::string& path){
+std::ifstream in_stream;
+std::string line;
+
+in_stream.open(path.c_str());
+while (std::getline(in_stream, line)) {
+  redirectsQueue.push(line);
+}
+in_stream.close();
+}
+
+std::string ArticleSource::getMainPage() {
+  return welcome;
+}
+
+Article *article = NULL;
+const zim::writer::Article* ArticleSource::getNextArticle() {
+  std::string path;
+
+  if (article != NULL) {
+delete(article);
+  }
+
+  if (!metadataQueue.empty()) {
+path = metadataQueue.front();
+metadataQueue.pop();
+article = new MetadataArticle(path);
+  } else if (!redirectsQueue.empty()) {
+std::string line = redirectsQueue.front();
+redirectsQueue.pop();
+article = new RedirectArticle(line);
+  } else if (popFromFilenameQueue(path)) {
+do {
+  article = new Article(path);
+} while (article && article->isInvalid() && popFromFilenameQueue(path));
+  } else {
+article = NULL;
+  }
+
+  /* Count mimetypes */
+  if (article != NULL && !article->isRedirect()) {
+
+if (isVerbose())
+  std::cout << "Creating entry for " << article->getAid() << std::endl;
+
+std::string mimeType = article->getMimeType();
+if (counters.find(mimeType) == counters.end()) {
+  counters[mimeType] = 1;
+} else {
+  counters[mimeType]++;
+}
+  }
+
+  return article;
+}
+
+zim::Blob ArticleSource::getData(const std::string& aid) {
+
+  if (isVerbose())
+std::cout << "Packing data for " << aid << std::endl;
+
+  if (data != NULL) {
+delete(data);
+data = NULL;
+  }
+
+  if (aid.substr(0, 3) == "/M/") {
+std::string value; 
+
+if ( aid == "/M/Language") {
+  value = language;
+} else if (aid == "/M/Creator") {
+  value = creator;
+} else if (aid == "/M/Publisher") {
+  value = publisher;
+} else if (aid == "/M/Title") {
+  value = title;
+} else if (aid == "/M/Description") {
+  value = description;
+} else if ( aid == "/M/Date") {
+  time_t t = time(0);
+  struct tm * now = localtime( & t );
+  std::stringstream stream;
+  stream << (now->tm_year + 1900) << '-'