Jelajahi Sumber

New version to export articles + opml

It does not work yet! A lot of work has still to be done.
Next versions should fix TODOs

- OPML export works fine but can be improved
- a framework has been created for articles export
Marien Fressinaud 12 tahun lalu
induk
melakukan
d1c5db7461

+ 44 - 4
app/Controllers/importExportController.php

@@ -57,12 +57,40 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 	}
 
 	public function exportAction() {
-		Minz_View::_title ('freshrss_feeds.opml');
+		if (Minz_Request::isPost()) {
+			$this->view->_useLayout (false);
 
-		$this->view->_useLayout (false);
-		header('Content-Type: application/xml; charset=utf-8');
-		header('Content-disposition: attachment; filename=freshrss_feeds.opml');
+			$export_opml = Minz_Request::param('export_opml', false);
+			$export_starred = Minz_Request::param('export_starred', false);
+			$export_all = Minz_Request::param('export_all', false);
 
+			// code from https://stackoverflow.com/questions/1061710/php-zip-files-on-the-fly
+			$file = tempnam("tmp", "zip");
+			$zip = new ZipArchive();
+			$zip->open($file, ZipArchive::OVERWRITE);
+
+			// Stuff with content
+			if ($export_opml) {
+				$zip->addFromString('feeds.opml', $this->generate_opml());
+			}
+			if ($export_starred) {
+				$zip->addFromString('starred.json', $this->generate_articles('starred'));
+			}
+			if ($export_all) {
+				$zip->addFromString('all.json', $this->generate_articles('all'));
+			}
+
+			// Close and send to users
+			$zip->close();
+			header('Content-Type: application/zip');
+			header('Content-Length: ' . filesize($file));
+			header('Content-Disposition: attachment; filename="freshrss_export.zip"');
+			readfile($file);
+			unlink($file);
+		}
+	}
+
+	private function generate_opml() {
 		$feedDAO = new FreshRSS_FeedDAO ();
 		$catDAO = new FreshRSS_CategoryDAO ();
 
@@ -73,5 +101,17 @@ class FreshRSS_importExport_Controller extends Minz_ActionController {
 		}
 
 		$this->view->categories = $list;
+
+		// TODO: add a parameter to renderHelper in order to get a variable
+		ob_start();
+		$this->view->renderHelper('export/opml');
+		return ob_get_clean();
+	}
+
+	private function generate_articles($type) {
+		// TODO: same here + we should get articles according to $type
+		ob_start();
+		$this->view->renderHelper('export/articles');
+		return ob_get_clean();
 	}
 }

+ 30 - 0
app/views/helpers/export/articles.phtml

@@ -0,0 +1,30 @@
+<?php
+    // TODO: A lot have to be done!
+    $username = Minz_Session::param('currentUser', '_');
+    $type_id = "TODO";
+    $title = Minz_Translate::t("TODO");
+    $entries = [];
+?>{
+    "id": "user/<?php echo str_replace("\"", "", $username); ?>/state/org.freshrss/<?php echo $type_id; ?>",
+    "title": "<?php echo addslashes($title); ?>",
+    "author": "<?php echo addslashes($username); ?>",
+    "items": [
+        <?php $i = 0; foreach($entries as $entry) { $i++;
+        echo $i > 1 ? ', ': ''; ?>{
+            "id": "<?php echo $entry->id(); ?>",
+            "categories": [<?php /* TODO */ ?>],
+            "title": "<?php echo addslashes($entry->title()); ?>",
+            "published": <?php echo $entry->date(true); ?>,
+            "updated": <?php echo $entry->date(true); ?>,
+            "content": "<?php echo addslashes($entry->content()); ?>",
+            "origin": {
+                <?php /* TODO */ ?>
+                "streamId": "",
+                "title": "",
+                "htmlUrl": "",
+                "feedUrl": ""
+            }
+        }
+        <?php } ?>
+    ]
+}

+ 0 - 0
app/views/importExport/export.phtml → app/views/helpers/export/opml.phtml


+ 30 - 3
app/views/importExport/index.phtml

@@ -4,7 +4,7 @@
 	<a href="<?php echo _url ('index', 'index'); ?>"><?php echo Minz_Translate::t ('back_to_rss_feeds'); ?></a>
 
 	<form method="post" action="<?php echo _url('importExport', 'import'); ?>" enctype="multipart/form-data">
-		<legend><?php echo Minz_Translate::t ('import_export'); ?></legend>
+		<legend><?php echo Minz_Translate::t ('import'); ?></legend>
 		<div class="form-group">
 			<label class="group-name" for="file"><?php echo Minz_Translate::t ('file_to_import'); ?></label>
 			<div class="group-controls">
@@ -15,8 +15,35 @@
 		<div class="form-group form-actions">
 			<div class="group-controls">
 				<button type="submit" class="btn btn-important"><?php echo Minz_Translate::t ('import'); ?></button>
-				<?php echo Minz_Translate::t ('or'); ?>
-				<a target="_blank" class="btn btn-important" href="<?php echo _url ('importExport', 'export'); ?>"><?php echo Minz_Translate::t ('export'); ?></a>
+			</div>
+		</div>
+	</form>
+
+	<form method="post" action="<?php echo _url('importExport', 'export'); ?>">
+		<legend><?php echo Minz_Translate::t ('export'); ?></legend>
+		<div class="form-group">
+			<div class="group-controls">
+				<label class="checkbox" for="export_opml">
+					<input type="checkbox" name="export_opml" id="export_opml" value="1" checked="checked" />
+					<?php echo Minz_Translate::t ('export_opml'); ?>
+				</label>
+
+				<label class="checkbox" for="export_starred">
+					<input type="checkbox" name="export_starred" id="export_starred" value="1" checked="checked" />
+					<?php echo Minz_Translate::t ('export_starred'); ?>
+				</label>
+
+				<label class="checkbox" for="export_all">
+					<input type="checkbox" name="export_all" id="export_all" value="1" />
+					<?php echo Minz_Translate::t ('export_all'); ?>
+					<?php echo FreshRSS_Themes::icon('help'); ?> <?php echo Minz_Translate::t('export_all_is_long'); ?>
+				</label>
+			</div>
+		</div>
+
+		<div class="form-group form-actions">
+			<div class="group-controls">
+				<button type="submit" class="btn btn-important"><?php echo Minz_Translate::t ('export'); ?></button>
 			</div>
 		</div>
 	</form>