moving featured personas and featured designers into the db
authorToby Elliott <telliott@mozilla.com>
Tue, 14 Jul 2009 14:41:42 -0700
changeset 1074 088bc2dc19290ce386f8761ea8dd88666fc859ef
parent 1073 eb0cdda69eabb7b5dee35ec4af54ad04282110b1
child 1075 a7dabf09550da9bef10d3de15b6a4b86aacfb74f
push id691
push usertelliott@mozilla.com
push dateTue, 14 Jul 2009 21:41:46 +0000
moving featured personas and featured designers into the db
compile.php
personas.sql
server/admin/featured.php
server/lib/storage.php
server/templates/featured_designer.php
server/templates/featured_personas.php
server/templates/movers.php
--- a/compile.php
+++ b/compile.php
@@ -39,17 +39,16 @@
 	
 	require_once 'server/lib/personas_constants.php';	
 	require_once 'server/lib/personas_functions.php';	
 	require_once 'server/lib/storage.php';
 
 	
 	$db = new PersonaStorage();
 	$categories = $db->get_categories();
-	$page_size = 501;
 	
 	#Top level popular page
 	$popular_list = array_slice($db->get_popular_personas(null), 0, 21);
 	foreach ($popular_list as $item)
 	{
 		$data = extract_record_data($item);
 		$data['recent'] = (time() - strtotime($item['approve']) < 604800) ? true : false;
 		$popular_json[] = $data;
@@ -66,17 +65,17 @@
 	}
 	$master['recent'] = $recent_json;
 
 
 	foreach ($categories as $category)
 	{
 		#get category counts for pagination
 		$category_total = $db->get_active_persona_count($category);
-		$pages = floor($category_total/$page_size) + 1;
+		$pages = floor($category_total/PERSONA_GALLERY_ALL_PAGE_SIZE) + 1;
 		
 		$popular_list = array_slice($db->get_popular_personas($category), 0, 10);
 	
 		$short_cat_list = array();
 		foreach ($popular_list as $item)
 		{
 			$data = extract_record_data($item);
 			$data['recent'] = (time() - strtotime($item['approve']) < 604800) ? true : false;
--- a/personas.sql
+++ b/personas.sql
@@ -68,8 +68,13 @@ CREATE TABLE `users` (
   `email` varchar(64) default NULL,
   `privs` tinyint(4) default '1',
   `change_code` varbinary(20) default NULL,
   `news` tinyint(4) default '0',
   `description` text,
   PRIMARY KEY  (`username`)
 ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
 
+CREATE TABLE `meta` (
+  `meta` varchar(31) NOT NULL,
+  `value` text,
+  PRIMARY KEY  (`meta`)
+)
new file mode 100644
--- /dev/null
+++ b/server/admin/featured.php
@@ -0,0 +1,107 @@
+<?php
+
+# ***** BEGIN LICENSE BLOCK *****
+# Version: MPL 1.1/GPL 2.0/LGPL 2.1
+#
+# The contents of this file are subject to the Mozilla Public License Version
+# 1.1 (the "License"); you may not use this file except in compliance with
+# the License. You may obtain a copy of the License at
+# http://www.mozilla.org/MPL/
+#
+# Software distributed under the License is distributed on an "AS IS" basis,
+# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
+# for the specific language governing rights and limitations under the
+# License.
+#
+# The Original Code is Personas Server
+#
+# The Initial Developer of the Original Code is
+# Mozilla Labs.
+# Portions created by the Initial Developer are Copyright (C) 2008
+# the Initial Developer. All Rights Reserved.
+#
+# Contributor(s):
+#	Toby Elliott (telliott@mozilla.com)
+#
+# Alternatively, the contents of this file may be used under the terms of
+# either the GNU General Public License Version 2 or later (the "GPL"), or
+# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
+# in which case the provisions of the GPL or the LGPL are applicable instead
+# of those above. If you wish to allow use of your version of this file only
+# under the terms of either the GPL or the LGPL, and not to allow others to
+# use your version of this file under the terms of the MPL, indicate your
+# decision by deleting the provisions above and replace them with the notice
+# and other provisions required by the GPL or the LGPL. If you do not delete
+# the provisions above, a recipient may use your version of this file under
+# the terms of any one of the MPL, the GPL or the LGPL.
+#
+# ***** END LICENSE BLOCK *****
+	
+	require_once '../lib/personas_constants.php';
+	require_once '../lib/personas_functions.php';
+	require_once '../lib/storage.php';
+	require_once '../lib/user.php';
+
+	try 
+	{
+		$user = new PersonaUser();
+		$user->authenticate();
+		$user->force_signin(1);
+		if (!$user->has_admin_privs())
+		{
+			$_errors['error'] = 'This account does not have privileges for this operation. Please <a href="/signin?action=logout">log in</a> with an account that does.';
+			include '../templates/user_error.php';
+			exit;
+		}
+	}
+	catch(Exception $e)
+	{
+		error_log($e->getMessage());
+		print("Database problem. Please try again later.");
+		exit;
+	}
+	
+	
+	
+	$result = "";
+	
+	try 
+	{
+		$db = new PersonaStorage();
+
+		if (array_key_exists('designers', $_POST))
+		{
+			$designers = ini_get('magic_quotes_gpc') ? stripslashes($_POST['designers']) : $_POST['designers'];
+			$designer_list = explode('[^0-9]+', trim($designers));
+			$db->set_featured_designers($designer_list);
+		}
+
+		if (array_key_exists('personas', $_POST))
+		{
+			$personas = ini_get('magic_quotes_gpc') ? stripslashes($_POST['personas']) : $_POST['personas'];
+			$persona_list = explode('[^0-9]+', trim($personas));
+			$db->set_featured_personas($persona_list);
+		}
+	}
+	catch(Exception $e)
+	{
+		throw new Exception("Database problem. Please try again later.");
+	}
+	
+?>
+<html>
+<body>
+<div id="message"><?= $result ?></div>
+<form action="featured.php" method="POST">
+Current Featured Designers: 
+<input type=text name=designers value="<?= implode(':', $db->get_featured_designers()) ?>">
+<input type=submit value="Change">
+</form>
+<p>
+<form action="featured.php" method="POST">
+Current Featured Personas: 
+<input type=text name=personas value="<?= implode(':', $db->get_featured_personas()) ?>">
+<input type=submit value="Change">
+</form>
+</body>
+</html>
--- a/server/lib/storage.php
+++ b/server/lib/storage.php
@@ -119,16 +119,18 @@
 #  ca:<page>:<category> - gallery 'All' pages entries for a category
 #  cr:<category> - gallery 'Recent' page entries for a category
 #  cp:<category> - gallery 'Popular' page entries for a category
 #  cm:<category> - movers and shakers for a category
 #  pc:<category> - current persona count for a category
 #  au:<author>:<category> - personas from an author
 #  fav:<author>:<category> - favorite personas
 #  categories - a list of categories
+#  feat_des - the featured designers
+#  feat_p - the featured personas
 
 require_once 'personas_constants.php';
 
 class PersonaStorage
 {
 	var $_dbh;
 	var $_memcache;
 	
@@ -766,16 +768,90 @@ class PersonaStorage
 			$categories[] = $result[0];
 		}		
 
 		if ($this->_memcache)
 			$this->_memcache->set("categories", $categories, false, MEMCACHE_DECAY);
 
 		return $categories;
 	}
+
+	#####
+	# Gets the current list of featured designers and returns it as an array
+	
+	function get_featured_designers()
+	{
+		if ($this->_memcache)
+		{
+			$result = $this->_memcache->get("feat_des");
+			if ($result)
+				return $result;
+		}
+
+		if (!$this->_dbh)
+			$this->db_connect();		
+
+		try
+		{
+			$statement = 'select value from meta where meta = "featureddesigner"';
+			$sth = $this->_dbh->prepare($statement);
+			$sth->execute();
+		}
+		catch( PDOException $exception )
+		{
+			error_log($exception->getMessage());
+			throw new Exception("Database unavailable", 503);
+		}
+		
+		$result = explode(':', $sth->fetchColumn());
+
+		if ($this->_memcache)
+			$this->_memcache->set("feat_des", $result, false, MEMCACHE_DECAY);
+
+		return $result;
+		
+	}
+	
+	
+	#####
+	# Gets the current list of featured designers and returns it as an array
+	
+	function get_featured_personas()
+	{
+		if ($this->_memcache)
+		{
+			$result = $this->_memcache->get("feat_p");
+			if ($result)
+				return $result;
+		}
+
+		if (!$this->_dbh)
+			$this->db_connect();		
+
+		try
+		{
+			$statement = 'select value from meta where meta = "featuredpersona"';
+			$sth = $this->_dbh->prepare($statement);
+			$sth->execute();
+		}
+		catch( PDOException $exception )
+		{
+			error_log($exception->getMessage());
+			throw new Exception("Database unavailable", 503);
+		}
+		
+		$result = explode(':', $sth->fetchColumn());
+
+		if ($this->_memcache)
+			$this->_memcache->set("feat_p", $result, false, MEMCACHE_DECAY);
+
+		return $result;
+		
+	}
+	
 	
 	
 #####
 # Gets all personas that a user has 'favorited'. Category is optional
 	
 	function get_user_favorites($username, $category = null)
 	{
 		if (!$username)
@@ -1153,16 +1229,18 @@ class PersonaStorage
 		while ($result = $sth->fetch(PDO::FETCH_ASSOC))
 		{
 			$personas[] = $result;
 		}		
 		return $personas;
 		
 	}
 
+
+
 #####
 # Changes a persona category. This is separate from editing because a) it doesn't require approval
 # if an admin is doing it and b) it's a lot more common. Should only be available to admins
 	
 
 	function change_persona_category($id, $category)
 	{
 		#no need for memcache here, as it's usually pre-approval and will work 
@@ -1356,16 +1434,75 @@ class PersonaStorage
 		{
 			error_log($exception->getMessage());
 			throw new Exception("Database unavailable", 503);
 		}
 		return 1;
 		
 	}
 
+
+#####
+# Takes an array of featured personas and sets them in the metadata db
+	
+	function set_featured_personas($personas)
+	{
+		if (!$this->_dbh)
+			$this->db_connect();		
+
+		try
+		{
+			$statement = 'update meta set value = :featured where meta = "featuredpersona"';
+			$sth = $this->_dbh->prepare($statement);
+			$sth->bindParam(':featured', implode(':', $personas));
+			$sth->execute();
+		}
+		catch( PDOException $exception )
+		{
+			error_log($exception->getMessage());
+			throw new Exception("Database unavailable", 503);
+		}
+
+		if ($this->_memcache)
+		{
+			$this->_memcache->delete("feat_p");
+		}
+		return 1;
+		
+	}
+
+#####
+# Takes an array of featured designers and sets them in the metadata db
+	
+	function set_featured_designers($personas)
+	{
+		if (!$this->_dbh)
+			$this->db_connect();		
+
+		try
+		{
+			$statement = 'update meta set value = :featured where meta = "featureddesigner"';
+			$sth = $this->_dbh->prepare($statement);
+			$sth->bindParam(':featured', implode(':', $personas));
+			$sth->execute();
+		}
+		catch( PDOException $exception )
+		{
+			error_log($exception->getMessage());
+			throw new Exception("Database unavailable", 503);
+		}
+
+		if ($this->_memcache)
+		{
+			$this->_memcache->delete("feat_des");
+		}
+		return 1;
+		
+	}
+
 #####
 # Get the log history for a particular persona
 	
 	function get_log_by_persona_id($id)
 	{
 		if (!$this->_dbh)
 			$this->db_connect();		
 
--- a/server/templates/featured_designer.php
+++ b/server/templates/featured_designer.php
@@ -1,16 +1,19 @@
 <?php
 	$featured_description_max = 140;
 
 	if (!isset($user))
 		$user = new PersonaUser();
 
+	if (!isset($db))
+		$db = new PersonaStorage();
+
     $personas = array();
-	foreach (explode(":", FEATURED_DESIGNERS) as $id)
+	foreach ($db->get_featured_designers() as $id)
 	{
 		$persona = $db->get_persona_by_id($id); 
 		if (!$persona)
 			continue;
 		
 		
 		$persona['json'] = htmlentities(json_encode(extract_record_data($persona)));
 		$persona['designer_description'] = $user->get_description($persona['author']);
--- a/server/templates/featured_personas.php
+++ b/server/templates/featured_personas.php
@@ -1,12 +1,15 @@
 <?php
 	$featured_description_max = 140;
 	
-	foreach (explode(":", FEATURED_PERSONAS) as $id)
+	if (!isset($db))
+		$db = new PersonaStorage();
+
+	foreach ($db->get_featured_personas() as $id)
 	{
 		$persona = $db->get_persona_by_id($id); 
 		if (!$persona)
 			continue;
 
 		$persona['json'] = htmlentities(json_encode(extract_record_data($persona)));
 		$persona['detail_url'] = "/gallery/persona/" . url_prefix($persona['id']);
 		$persona['preview_image'] = PERSONAS_LIVE_PREFIX . '/' . url_prefix($persona['id']);
--- a/server/templates/movers.php
+++ b/server/templates/movers.php
@@ -1,17 +1,17 @@
             <div class="feature last">
                 <h3>Movers and Shakers</h3>
                 <ol class="popular">
 <?php
 	$list = $db->get_movers(null);
 	$count = 1;
 	
-	$featured_designer_list = explode(":", FEATURED_DESIGNERS);
-	$featured_persona_list = explode(":", FEATURED_PERSONAS);
+	$featured_designer_list = $db->get_featured_designers();
+	$featured_persona_list = $db->get_featured_personas();
 	
 	foreach ($list as $persona)
 	{
 		if (in_array($persona['id'], $featured_designer_list) 
 			|| in_array($persona['id'], $featured_persona_list))
 			continue;
 		
 		$persona_json = htmlentities(json_encode(extract_record_data($persona)));