فهرست منبع

ajout fonction importation fichiers OPMs OPML

Marien Fressinaud 13 سال پیش
والد
کامیت
5383f6206c

+ 4 - 4
app/controllers/configureController.php

@@ -110,12 +110,12 @@ class configureController extends ActionController {
 			$this->view->categories = $list;
 		} elseif ($this->view->req == 'import' && Request::isPost ()) {
 			if ($_FILES['file']['error'] == 0) {
-				$content = file_get_contents ($_FILES['file']['tmp_name']);
-				$feeds = opml_import ($content);
+				list ($categories, $feeds) = opml_import (file_get_contents ($_FILES['file']['tmp_name']));
 				
-				Request::_param ('q');
+				Request::_param ('q', 'null');
+				Request::_param ('categories', $categories);
 				Request::_param ('feeds', $feeds);
-				Request::forward (array ('c' => 'feed', 'a' => 'massiveInsert'));
+				Request::forward (array ('c' => 'feed', 'a' => 'massiveImport'));
 			}
 		}
 	}

+ 1 - 1
app/controllers/entryController.php

@@ -26,7 +26,7 @@ class entryController extends ActionController {
 		
 		$entryDAO = new EntryDAO ();
 		if ($id == false) {
-			$entries = $entryDAO->listNotReadEntries ();
+			$entries = $entryDAO->listEntries ('not_read');
 		} else {
 			$entry = $entryDAO->searchById ($id);
 			$entries = $entry !== false ? array ($entry) : array ();

+ 42 - 2
app/controllers/feedController.php

@@ -96,16 +96,56 @@ class feedController extends ActionController {
 		Request::forward (array (), true);
 	}
 	
-	public function massiveImport () {
+	public function massiveImportAction () {
+		$entryDAO = new EntryDAO ();
 		$feedDAO = new FeedDAO ();
+		$catDAO = new CategoryDAO ();
+		
+		$categories = Request::param ('categories', array ());
 		$feeds = Request::param ('feeds', array ());
 		
+		foreach ($categories as $cat) {
+			$values = array (
+				'id' => $cat->id (),
+				'name' => $cat->name (),
+				'color' => $cat->color ()
+			);
+			$catDAO->addCategory ($values);
+			$catDAO->save ();
+		}
+		
 		foreach ($feeds as $feed) {
+			$feed->load ();
+			$entries = $feed->entries (false);
+			$feed_entries = array ();
+			
+			// Chargement du flux
+			if ($entries !== false) {
+				foreach ($entries as $entry) {
+					$values = array (
+						'id' => $entry->id (),
+						'guid' => $entry->guid (),
+						'title' => $entry->title (),
+						'author' => $entry->author (),
+						'content' => $entry->content (),
+						'link' => $entry->link (),
+						'date' => $entry->date (true),
+						'is_read' => $entry->isRead (),
+						'is_favorite' => $entry->isFavorite (),
+						'feed' => $feed->id ()
+					);
+					$entryDAO->addEntry ($values);
+					
+					$feed_entries[] = $entry->id ();
+				}
+			}
+			
+			// Enregistrement du flux
 			$values = array (
 				'id' => $feed->id (),
 				'url' => $feed->url (),
 				'category' => $feed->category (),
-				'entries' => array (),
+				'entries' => $feed_entries,
 				'name' => $feed->name (),
 				'website' => $feed->website (),
 				'description' => $feed->description (),

+ 1 - 1
app/views/configure/importExport.phtml

@@ -14,7 +14,7 @@
 
 <form method="post" action="<?php echo Url::display (array ('c' => 'configure', 'a' => 'importExport', 'params' => array ('q' => 'import'))); ?>" enctype="multipart/form-data">
 	<h1>Exporter au format OPML</h1>
-	<a class="button" href="<?php echo Url::display (array ('c' => 'configure', 'a' => 'importExport', 'params' => array ('q' => 'export'))); ?>">Exporter</a>
+	<button formaction="<?php echo Url::display (array ('c' => 'configure', 'a' => 'importExport', 'params' => array ('q' => 'export'))); ?>">Exporter</button>
 	
 	<h1>Importer au format OPML</h1>
 	<label for="file">Fichier</label>

+ 66 - 7
lib/lib_rss.php

@@ -1,13 +1,12 @@
 <?php
 // tiré de Shaarli de Seb Sauvage
 function small_hash ($txt) {
+	$t = rtrim (base64_encode (hash ('crc32', $txt, true)), '=');
+	$t = str_replace ('+', '-', $t); // Get rid of characters which need encoding in URLs.
+	$t = str_replace ('/', '_', $t);
+	$t = str_replace ('=', '@', $t);
 
-    $t = rtrim (base64_encode (hash ('crc32', $txt, true)), '=');
-    $t = str_replace ('+', '-', $t); // Get rid of characters which need encoding in URLs.
-    $t = str_replace ('/', '_', $t);
-    $t = str_replace ('=', '@', $t);
-    
-    return $t;
+	return $t;
 }
 
 function timestamptodate ($t, $hour = true) {
@@ -90,5 +89,65 @@ function opml_export ($cats) {
 }
 
 function opml_import ($xml) {
-	// TODO
+	$opml = @simplexml_load_string ($xml);
+
+	if (!$opml) {
+		return array (array (), array ());
+	}
+
+	$categories = array ();
+	$feeds = array ();
+
+	foreach ($opml->body->outline as $outline) {
+		if (!isset ($outline['xmlUrl'])) {
+			// Catégorie
+			$title = '';
+			
+			if (isset ($outline['text'])) {
+				$title = (string) $outline['text'];
+			} elseif (isset ($outline['title'])) {
+				$title = (string) $outline['title'];
+			}
+			
+			if ($title) {
+				$cat = new Category ($title);
+				$categories[] = $cat;
+				
+				$feeds = array_merge ($feeds, getFeedsOutline ($outline, $cat->id ()));
+			}
+		} else {
+			// Flux rss
+			$feeds[] = getFeed ($outline, '');
+		}
+	}
+
+	return array ($categories, $feeds);
+}
+
+/**
+ * import all feeds of a given outline tag
+ */
+function getFeedsOutline ($outline, $cat_id) {
+	$feeds = array ();
+	
+	foreach ($outline->children () as $child) {
+		if (isset ($child['xmlUrl'])) {
+			$feeds[] = getFeed ($child, $cat_id);
+		} else {
+			$feeds = array_merge(
+				$feeds,
+				getFeedsOutline ($child, $cat_id)
+			);
+		}
+	}
+	
+	return $feeds;
+}
+
+function getFeed ($outline, $cat_id) {
+	$url = (string) $outline['xmlUrl'];
+	$feed = new Feed ($url);
+	$feed->_category ($cat_id);
+
+	return $feed;
 }

+ 3 - 0
lib/lib_simplepie.php

@@ -5184,6 +5184,9 @@ class SimplePie_IRI
 			{
 				$match['fragment'] = null;
 			}
+
+			$match['path'] = preg_replace ('#//#', '/', $match['path']); // fix un bug lorsque 2 slashs se suivent
+
 			return $match;
 		}
 		else

+ 1 - 18
public/theme/base.css

@@ -14,23 +14,6 @@ a {
 	a:hover {
 		text-decoration: underline;
 	}
-	a.add, a.update, a.delete, a.back {
-		height: 30px;
-		padding: 0 20px;
-		line-height: 30px;
-	}
-		a.add {
-			background: url("img/add.png") no-repeat left 3px;
-		}
-		a.update {
-			background: url("img/update.png") no-repeat left 3px;
-		}
-		a.delete {
-			background: url("img/delete.png") no-repeat left 3px;
-		}
-		a.back {
-			background: url("img/back.png") no-repeat left 5px;
-		}
 
 /* LISTES */
 ul, ol {
@@ -89,7 +72,7 @@ form {
 			line-height: 150%;
 			font-family: Monospace;
 		}
-	input[type="submit"] {
+	input[type="submit"], button {
 		width: 100%;
 		margin: 5px 0 5px;
 		padding: 5px 0;