Consolidate copy-and-pasted diff mode cookie code
Consolidate copy-and-pasted diff mode cookie code

--- a/include/controller/Controller_Blobdiff.class.php
+++ b/include/controller/Controller_Blobdiff.class.php
@@ -10,21 +10,7 @@
  * @subpackage Controller
  */
 
-/**
- * Constants for blobdiff modes
- */
-define('GITPHP_BLOBDIFF_UNIFIED', 1);
-define('GITPHP_BLOBDIFF_SIDEBYSIDE', 2);
-
-/**
- * Constant of the blobdiff mode cookie in the user's browser
- */
-define('GITPHP_BLOBDIFF_MODE_COOKIE', 'GitPHPBlobdiffMode');
-
-/**
- * Blobdiff mode cookie lifetime
- */
-define('GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME', 60*60*24*365);		// 1 year
+require_once(GITPHP_CONTROLLERDIR . 'Controller_DiffBase.class.php');
 
 /**
  * Blobdiff controller class
@@ -32,7 +18,7 @@
  * @package GitPHP
  * @subpackage Controller
  */
-class GitPHP_Controller_Blobdiff extends GitPHP_ControllerBase
+class GitPHP_Controller_Blobdiff extends GitPHP_Controller_DiffBase
 {
 
 	/**
@@ -106,6 +92,8 @@
 	 */
 	protected function ReadQuery()
 	{
+		parent::ReadQuery();
+
 		if (isset($_GET['f']))
 			$this->params['file'] = $_GET['f'];
 		if (isset($_GET['h']))
@@ -114,55 +102,6 @@
 			$this->params['hashbase'] = $_GET['hb'];
 		if (isset($_GET['hp']))
 			$this->params['hashparent'] = $_GET['hp'];
-
-		if (!isset($this->params['plain']) || $this->params['plain'] != true) {
-
-			$mode = GITPHP_BLOBDIFF_UNIFIED;	// default
-
-			/*
-			 * Check cookie
-			 */
-			if (!empty($_COOKIE[GITPHP_BLOBDIFF_MODE_COOKIE])) {
-				$mode = $_COOKIE[GITPHP_BLOBDIFF_MODE_COOKIE];
-			} else {
-				/*
-				 * Create cookie to prevent browser delay
-				 */
-				setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, $mode, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);
-			}
-
-			if (isset($_GET['o'])) {
-				/*
-				 * User is choosing a new mode
-				 */
-				if ($_GET['o'] == 'sidebyside') {
-					$mode = GITPHP_BLOBDIFF_SIDEBYSIDE;
-					setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, GITPHP_BLOBDIFF_SIDEBYSIDE, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);
-				} else if ($_GET['o'] == 'unified') {
-					$mode = GITPHP_BLOBDIFF_UNIFIED;
-					setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, GITPHP_BLOBDIFF_UNIFIED, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);
-				}
-			}
-
-			if ($mode == GITPHP_BLOBDIFF_SIDEBYSIDE) {
-				$this->params['sidebyside'] = true;
-			}
-
-		}
-	}
-
-	/**
-	 * LoadHeaders
-	 *
-	 * Loads headers for this template
-	 *
-	 * @access protected
-	 */
-	protected function LoadHeaders()
-	{
-		if (isset($this->params['plain']) && ($this->params['plain'] === true)) {
-			$this->headers[] = 'Content-type: text/plain; charset=UTF-8';
-		}
 	}
 
 	/**

--- a/include/controller/Controller_Commitdiff.class.php
+++ b/include/controller/Controller_Commitdiff.class.php
@@ -10,21 +10,7 @@
  * @subpackage Controller
  */
 
-/**
- * Constants for blobdiff modes
- */
-define('GITPHP_BLOBDIFF_UNIFIED', 1);
-define('GITPHP_BLOBDIFF_SIDEBYSIDE', 2);
-
-/**
- * Constant of the blobdiff mode cookie in the user's browser
- */
-define('GITPHP_BLOBDIFF_MODE_COOKIE', 'GitPHPBlobdiffMode');
-
-/**
- * Blobdiff mode cookie lifetime
- */
-define('GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME', 60*60*24*365);           // 1 year
+require_once(GITPHP_CONTROLLERDIR . 'Controller_DiffBase.class.php');
 
 /**
  * Commitdiff controller class
@@ -32,7 +18,7 @@
  * @package GitPHP
  * @subpackage Controller
  */
-class GitPHP_Controller_Commitdiff extends GitPHP_ControllerBase
+class GitPHP_Controller_Commitdiff extends GitPHP_Controller_DiffBase
 {
 
 	/**
@@ -110,45 +96,12 @@
 	 */
 	protected function ReadQuery()
 	{
+		parent::ReadQuery();
+
 		if (isset($_GET['h']))
 			$this->params['hash'] = $_GET['h'];
 		if (isset($_GET['hp']))
 			$this->params['hashparent'] = $_GET['hp'];
-
-		if (!isset($this->params['plain']) || $this->params['plain'] != true) {
-
-			$mode = GITPHP_BLOBDIFF_UNIFIED;        // default
-
-
-			/*
-			 * Check cookie
-			 */
-			if (!empty($_COOKIE[GITPHP_BLOBDIFF_MODE_COOKIE])) {
-				$mode = $_COOKIE[GITPHP_BLOBDIFF_MODE_COOKIE];
-			} else {
-				/*
-				 * Create cookie to prevent browser delay
-				 */
-				setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, $mode, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);
-			}
-
-			if (isset($_GET['o'])) {
-				/*
-				 * User is choosing a new mode
-				 */
-				if ($_GET['o'] == 'sidebyside') {
-					$mode = GITPHP_BLOBDIFF_SIDEBYSIDE;
-					setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, GITPHP_BLOBDIFF_SIDEBYSIDE, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);
-				} else if ($_GET['o'] == 'unified') {
-					$mode = GITPHP_BLOBDIFF_UNIFIED;
-					setcookie(GITPHP_BLOBDIFF_MODE_COOKIE, GITPHP_BLOBDIFF_UNIFIED, time()+GITPHP_BLOBDIFF_MODE_COOKIE_LIFETIME);
-				}
-			}
-
-			if ($mode == GITPHP_BLOBDIFF_SIDEBYSIDE) {
-				$this->params['sidebyside'] = true;
-			}
-		}
 	}
 
 	/**
@@ -160,10 +113,9 @@
 	 */
 	protected function LoadHeaders()
 	{
+		parent::LoadHeaders();
+
 		if (isset($this->params['plain']) && ($this->params['plain'] === true)) {
-			GitPHP_Log::GetInstance()->SetEnabled(false);
-
-			$this->headers[] = 'Content-type: text/plain; charset=UTF-8';
 			$this->headers[] = 'Content-disposition: inline; filename="git-' . $this->params['hash'] . '.patch"';
 		}
 	}

--- /dev/null
+++ b/include/controller/Controller_DiffBase.class.php
@@ -1,1 +1,113 @@
+<?php
+/**
+ * GitPHP Controller DiffBase
+ *
+ * Base controller for diff-type views
+ *
+ * @author Christopher Han <xiphux@gmail.com>
+ * @copyright Copyright (c) 2011 Christopher Han
+ * @package GitPHP
+ * @subpackage Controller
+ */
 
+
+/**
+ * Constants for diff modes
+ */
+define('GITPHP_DIFF_UNIFIED', 1);
+define('GITPHP_DIFF_SIDEBYSIDE', 2);
+
+/**
+ * Constant of the diff mode cookie in the user's browser
+ */
+define('GITPHP_DIFF_MODE_COOKIE', 'GitPHPDiffMode');
+
+/**
+ * Diff mode cookie lifetime
+ */
+define('GITPHP_DIFF_MODE_COOKIE_LIFETIME', 60*60*24*365);           // 1 year
+
+/**
+ * DiffBase controller class
+ *
+ * @package GitPHP
+ * @subpackage Controller
+ */
+abstract class GitPHP_Controller_DiffBase extends GitPHP_ControllerBase
+{
+	
+	/**
+	 * ReadQuery
+	 *
+	 * Read query into parameters
+	 *
+	 * @access protected
+	 */
+	protected function ReadQuery()
+	{
+		if (!isset($this->params['plain']) || $this->params['plain'] != true) {
+
+			if ($this->DiffMode(isset($_GET['o']) ? $_GET['o'] : '') == GITPHP_DIFF_SIDEBYSIDE) {
+				$this->params['sidebyside'] = true;
+			}
+
+		}
+	}
+
+	/**
+	 * DiffMode
+	 *
+	 * Determines the diff mode to use
+	 *
+	 * @param string $overrideMode mode overridden by the user
+	 * @access protected
+	 */
+	protected function DiffMode($overrideMode = '')
+	{
+		$mode = GITPHP_DIFF_UNIFIED;	// default
+
+		/*
+		 * Check cookie
+		 */
+		if (!empty($_COOKIE[GITPHP_DIFF_MODE_COOKIE])) {
+			$mode = $_COOKIE[GITPHP_DIFF_MODE_COOKIE];
+		} else {
+			/*
+			 * Create cookie to prevent browser delay
+			 */
+			setcookie(GITPHP_DIFF_MODE_COOKIE, $mode, time()+GITPHP_DIFF_MODE_COOKIE_LIFETIME);
+		}
+
+		if (!empty($overrideMode)) {
+			/*
+			 * User is choosing a new mode
+			 */
+			if ($overrideMode == 'sidebyside') {
+				$mode = GITPHP_DIFF_SIDEBYSIDE;
+				setcookie(GITPHP_DIFF_MODE_COOKIE, GITPHP_DIFF_SIDEBYSIDE, time()+GITPHP_DIFF_MODE_COOKIE_LIFETIME);
+			} else if ($overrideMode == 'unified') {
+				$mode = GITPHP_DIFF_UNIFIED;
+				setcookie(GITPHP_DIFF_MODE_COOKIE, GITPHP_DIFF_UNIFIED, time()+GITPHP_DIFF_MODE_COOKIE_LIFETIME);
+			}
+		}
+
+		return $mode;
+	}
+
+	/**
+	 * LoadHeaders
+	 *
+	 * Loads headers for this template
+	 *
+	 * @access protected
+	 */
+	protected function LoadHeaders()
+	{
+		if (isset($this->params['plain']) && ($this->params['plain'] === true)) {
+			GitPHP_Log::GetInstance()->SetEnabled(false);
+			$this->headers[] = 'Content-type: text/plain; charset=UTF-8';
+		}
+	}
+
+}
+

comments