Bläddra i källkod

Fix a set of TODO and bugs

- Context object raises correct Exception if get is invalid
- RSS feed is well-indicated on the home page
- State is better calculated
- Add some comments

See https://github.com/marienfressinaud/FreshRSS/issues/634
Marien Fressinaud 11 år sedan
förälder
incheckning
98587d5d61

+ 27 - 12
app/Controllers/indexController.php

@@ -5,10 +5,10 @@
  */
 class FreshRSS_index_Controller extends Minz_ActionController {
 
+	/**
+	 * This action only redirect on the default view mode (normal or global)
+	 */
 	public function indexAction() {
-		// TODO: update the context with information from request.
-		// TODO: then, in dedicated action, get corresponding entries
-
 		$prefered_output = FreshRSS_Context::$conf->view_mode;
 		Minz_Request::forward(array(
 			'c' => 'index',
@@ -27,12 +27,12 @@ class FreshRSS_index_Controller extends Minz_ActionController {
 
 		try {
 			$this->updateContext();
-		} catch (Minz_Exception $e) {
+		} catch (FreshRSS_Context_Exception $e) {
 			Minz_Error::error(404);
 		}
 
 		try {
-			$entries = $this->listByContext();
+			$entries = $this->listEntriesByContext();
 
 			if (count($entries) > FreshRSS_Context::$number) {
 				// We have more elements for pagination
@@ -48,6 +48,7 @@ class FreshRSS_index_Controller extends Minz_ActionController {
 
 		$this->view->categories = FreshRSS_Context::$categories;
 
+		$this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
 		$title = FreshRSS_Context::$name;
 		if (FreshRSS_Context::$get_unread > 0) {
 			$title = '(' . FreshRSS_Context::$get_unread . ') · ' . $title;
@@ -68,12 +69,13 @@ class FreshRSS_index_Controller extends Minz_ActionController {
 
 		try {
 			$this->updateContext();
-		} catch (Minz_Exception $e) {
+		} catch (FreshRSS_Context_Exception $e) {
 			Minz_Error::error(404);
 		}
 
 		$this->view->categories = FreshRSS_Context::$categories;
 
+		$this->view->rss_title = FreshRSS_Context::$name . ' | ' . Minz_View::title();
 		Minz_View::prependTitle(_t('gen.title.global_view') . ' · ');
 	}
 
@@ -94,12 +96,12 @@ class FreshRSS_index_Controller extends Minz_ActionController {
 
 		try {
 			$this->updateContext();
-		} catch (Minz_Exception $e) {
+		} catch (FreshRSS_Context_Exception $e) {
 			Minz_Error::error(404);
 		}
 
 		try {
-			$this->view->entries = $this->listByContext();
+			$this->view->entries = $this->listEntriesByContext();
 		} catch (FreshRSS_EntriesGetter_Exception $e) {
 			Minz_Log::notice($e->getMessage());
 			Minz_Error::error(404);
@@ -113,15 +115,25 @@ class FreshRSS_index_Controller extends Minz_ActionController {
 
 	/**
 	 * This action updates the Context object by using request parameters.
+	 *
+	 * Parameters are:
+	 *   - state (default: conf->default_view)
+	 *   - search (default: empty string)
+	 *   - order (default: conf->sort_order)
+	 *   - nb (default: conf->posts_per_page)
+	 *   - next (default: empty string)
 	 */
 	private function updateContext() {
 		FreshRSS_Context::_get(Minz_Request::param('get', 'a'));
 
-		FreshRSS_Context::$state |= Minz_Request::param(
+		// TODO: change default_view by default_state.
+		FreshRSS_Context::$state = Minz_Request::param(
 			'state', FreshRSS_Context::$conf->default_view
 		);
-		if (FreshRSS_Context::$state & FreshRSS_Entry::STATE_NOT_READ &&
-				FreshRSS_Context::$get_unread <= 0) {
+		$state_forced_by_user = Minz_Request::param('state', false) !== false;
+		if (FreshRSS_Context::isStateEnabled(FreshRSS_Entry::STATE_NOT_READ) &&
+				FreshRSS_Context::$get_unread <= 0 &&
+				!$state_forced_by_user) {
 			FreshRSS_Context::$state |= FreshRSS_Entry::STATE_READ;
 		}
 
@@ -135,7 +147,10 @@ class FreshRSS_index_Controller extends Minz_ActionController {
 		FreshRSS_Context::$first_id = Minz_Request::param('next', '');
 	}
 
-	private function listByContext() {
+	/**
+	 * This method returns a list of entries based on the Context object.
+	 */
+	private function listEntriesByContext() {
 		$entryDAO = FreshRSS_Factory::createEntryDao();
 
 		$get = FreshRSS_Context::currentGet(true);

+ 10 - 0
app/Exceptions/ContextException.php

@@ -0,0 +1,10 @@
+<?php
+
+/**
+ * An exception raised when a context is invalid
+ */
+class FreshRSS_Context_Exception extends Exception {
+	public function __construct($message) {
+		parent::__construct($message);
+	}
+}

+ 6 - 10
app/Models/Context.php

@@ -17,14 +17,15 @@ class FreshRSS_Context {
 		'unread' => 0,
 	);
 
-	public static $state = 0;
+	public static $get_unread = 0;
 	public static $current_get = array(
 		'all' => false,
 		'starred' => false,
 		'feed' => false,
 		'category' => false,
 	);
-	public static $get_unread = 0;
+
+	public static $state = 0;
 	public static $order = 'DESC';
 	public static $number = 0;
 	public static $search = '';
@@ -48,8 +49,6 @@ class FreshRSS_Context {
 		$catDAO = new FreshRSS_CategoryDAO();
 		$entryDAO = FreshRSS_Factory::createEntryDao();
 
-		// Get the current state.
-		// self::$state = self::$conf->default_view;
 		self::$categories = $catDAO->listCategories();
 
 		// Update number of read / unread variables.
@@ -97,8 +96,7 @@ class FreshRSS_Context {
 				$feed = $feedDAO->searchById($id);
 
 				if (!$feed) {
-					// TODO: raise an exception
-					return false;
+					throw new FreshRSS_Context_Exception('Invalid feed: ' . $id);
 				}
 			}
 
@@ -112,8 +110,7 @@ class FreshRSS_Context {
 				$cat = $catDAO->searchById($id);
 
 				if (!$cat) {
-					// TODO: raise an exception
-					return false;
+					throw new FreshRSS_Context_Exception('Invalid category: ' . $id);
 				}
 			} else {
 				$cat = self::$categories[$id];
@@ -123,8 +120,7 @@ class FreshRSS_Context {
 			self::$get_unread = $cat->nbNotRead();
 			break;
 		default:
-			// TODO: raise an exception!
-			return false;
+			throw new FreshRSS_Context_Exception('Invalid getter: ' . $get);
 		}
 	}
 

+ 9 - 8
app/layout/layout.phtml

@@ -10,21 +10,22 @@
 <?php $this->renderHelper('javascript_vars'); ?>
 		//]]></script>
 <?php
+	$url_base = Minz_Request::currentRequest();
 	if (FreshRSS_Context::$next_id !== '') {
-		$params = Minz_Request::params();
-		$params['next'] = FreshRSS_Context::$next_id;
-		$params['ajax'] = 1;
+		$url_next = $url_base;
+		$url_next['params']['next'] = FreshRSS_Context::$next_id;
+		$url_next['params']['ajax'] = 1;
 ?>
-		<link id="prefetch" rel="next prefetch" href="<?php echo Minz_Url::display(array('c' => Minz_Request::controllerName(), 'a' => Minz_Request::actionName(), 'params' => $params)); ?>" />
+		<link id="prefetch" rel="next prefetch" href="<?php echo Minz_Url::display($url_next); ?>" />
 <?php } ?>
 		<link rel="shortcut icon" id="favicon" type="image/x-icon" sizes="16x16 64x64" href="<?php echo Minz_Url::display('/favicon.ico'); ?>" />
 		<link rel="icon msapplication-TileImage apple-touch-icon" type="image/png" sizes="256x256" href="<?php echo Minz_Url::display('/themes/icons/favicon-256.png'); ?>" />
 <?php
-	if (isset($this->url)) {
-		$rss_url = $this->url;
-		$rss_url['params']['output'] = 'rss';
+	if (isset($this->rss_title)) {
+		$url_rss = $url_base;
+		$url_rss['a'] = 'rss';
 ?>
-		<link rel="alternate" type="application/rss+xml" title="<?php echo $this->rss_title; ?>" href="<?php echo Minz_Url::display($rss_url); ?>" />
+		<link rel="alternate" type="application/rss+xml" title="<?php echo $this->rss_title; ?>" href="<?php echo Minz_Url::display($url_rss); ?>" />
 <?php } ?>
 		<link rel="prefetch" href="<?php echo FreshRSS_Themes::icon('starred', true); ?>">
 		<link rel="prefetch" href="<?php echo FreshRSS_Themes::icon('non-starred', true); ?>">