[31983] trunk/www

jmpp at macports.org jmpp at macports.org
Wed Dec 12 20:26:48 PST 2007


Revision: 31983
          http://trac.macosforge.org/projects/macports/changeset/31983
Author:   jmpp at macports.org
Date:     2007-12-12 20:26:47 -0800 (Wed, 12 Dec 2007)

Log Message:
-----------

Concentrate all db connection and error handling code into a single function, portsdb_connect(),
which returns the appropriate information in case of success or failure, thus letting us remove
the php code from index.php and some other redundant code from ports.php.

Thanks to both Ryan and Bill for reviewing this and providing suggestions!

Modified Paths:
--------------
    trunk/www/includes/common.inc
    trunk/www/index.php
    trunk/www/ports.php

Modified: trunk/www/includes/common.inc
===================================================================
--- trunk/www/includes/common.inc	2007-12-13 03:40:13 UTC (rev 31982)
+++ trunk/www/includes/common.inc	2007-12-13 04:26:47 UTC (rev 31983)
@@ -62,6 +62,50 @@
 
 ######################################################################
 
+# Connect to the portsdb and gather miscellaneous information:
+function portsdb_connect($portsdb_host, $portsdb_user, $portsdb_passwd) {
+
+    $script = basename($_SERVER['PHP_SELF']);
+    
+    $portsdb_connection = mysql_connect($portsdb_host, $portsdb_user, $portsdb_passwd);
+    if ($portsdb_connection === false) {
+        switch ($script) {
+            case "index.php":
+                $portsdb_info = array(
+                    'connection_stream' => false,
+                    'num_ports' => 'a lot of',
+                    'num_categories' => 'many'
+                );
+                break;
+            case "ports.php":
+                print "
+                    <div id='content'>
+                        
+                        <h2 class='hdr'>MacPorts Portfiles</h2>
+                        <p>Our database is currently unavailable. We hope to have it back soon!</p>
+                        
+                    </div>
+                ";
+            default:
+                print_footer();
+                die();
+                break;
+        }
+    } else {
+        $portsdb_info = array(
+            'connection_stream' => $portsdb_connection,
+            'num_ports' => ports_count(),
+            'num_categories' => categories_count()
+        );
+    }
+    
+    return $portsdb_info;
+    
+}
+
+
+######################################################################
+
 # Total count of currently available ports:
 function ports_count() {
     global $portsdb_name;
@@ -112,18 +156,3 @@
 
     include("$MPWEB/includes/footer.inc");
 }
-
-
-######################################################################
-
-function print_db_error() {
-       print "
-        <div id='content'>
-          <h2 class='hdr'>MacPorts Portfiles</h2>
-          <p>Our database is currently unavailable. We hope to have it back soon!</p>
-        </div>
-             ";
-       print_footer();
-       die();
-}
-

Modified: trunk/www/index.php
===================================================================
--- trunk/www/index.php	2007-12-13 03:40:13 UTC (rev 31982)
+++ trunk/www/index.php	2007-12-13 04:26:47 UTC (rev 31983)
@@ -5,14 +5,7 @@
     $MPWEB = $_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['SCRIPT_NAME']);
     include_once("$MPWEB/includes/common.inc");
     print_header('The MacPorts Project -- Home', 'utf-8');
-    $portsdb_connect = mysql_connect($portsdb_host, $portsdb_user, $portsdb_passwd);
-    if ($portsdb_connect === false) {
-       $port_count = 'a lot of';
-       $cat_count = 'many';
-    } else {
-       $port_count = ports_count();
-       $cat_count = categories_count();
-    }
+    $portsdb_info = portsdb_connect($portsdb_host, $portsdb_user, $portsdb_passwd);
 ?>
 
 
@@ -30,8 +23,8 @@
     <p>We provide a single software tree that attempts to track the latest release of every software title (port) we distribute,
     without splitting them into &ldquo;stable&rdquo; Vs. &ldquo;unstable&rdquo; branches, targetting mainly the current Mac OS
     X release (10.5, A.K.A Leopard) and the immediately previous one (10.4, A.K.A. Tiger). There are currently <a href="ports.php">
-    <b><?= $port_count ?></b> ports</a> in our tree, distributed among <?= $cat_count ?> different
-    categories, and more are being added on a regular basis.</p>
+    <b><?php print $portsdb_info['num_ports']; ?></b> ports</a> in our tree, distributed among <?php print $portsdb_info['num_categories']; ?>
+    different categories, and more are being added on a regular basis.</p>
     
     
     <h3 class="subhdr">Getting started</h3>
@@ -77,5 +70,7 @@
 
 <?php
     print_footer();
-    mysql_close($portsdb_connect);
+    if ($portsdb_info['connection_stream'] !== false) {
+        mysql_close($portsdb_info['connection_stream']);
+    }
 ?>

Modified: trunk/www/ports.php
===================================================================
--- trunk/www/ports.php	2007-12-13 03:40:13 UTC (rev 31982)
+++ trunk/www/ports.php	2007-12-13 04:26:47 UTC (rev 31983)
@@ -7,9 +7,7 @@
     $MPWEB = $_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['SCRIPT_NAME']);
     include_once("$MPWEB/includes/common.inc");
     print_header('The MacPorts Project -- Available Ports', 'utf-8');
-    $portsdb_connect = mysql_connect($portsdb_host, $portsdb_user, $portsdb_passwd) or print_db_error();
-    
-    $num_categories = categories_count();
+    $portsdb_info = portsdb_connect($portsdb_host, $portsdb_user, $portsdb_passwd);
     $by = isset($_GET['by']) ? $_GET['by'] : '';
     $substr = isset($_GET['substr']) ? $_GET['substr'] : '';
 ?>
@@ -27,9 +25,10 @@
         $time = date('H:i', $row[0]);
     }
 ?>
-    <p>The MacPorts Project currently distributes <b><?php print ports_count(); ?></b> ports, distributed across <?php print
-    $num_categories; ?> different categories and available below for viewing. This form allows you to search the MacPorts software
-    index, last updated on <b><?php echo $date; ?></b> at <b><?php echo $time; ?></b>.</p>
+    <p>The MacPorts Project currently distributes <b><?php print $portsdb_info['num_ports']; ?></b> ports, distributed
+    across <?php print $portsdb_info['num_categories']; ?> different categories and available below for viewing. This form
+    allows you to search the MacPorts software index, last updated on <b><?php echo $date; ?></b> at <b><?php echo $time; ?>
+    </b>.</p>
 
     <br />
 
@@ -47,7 +46,7 @@
         </p>
     </form>
 
-    <p>Or view the complete <a href="<?php echo $_SERVER['PHP_SELF']; ?>?by=all">ports list (<?php print ports_count(); ?>
+    <p>Or view the complete <a href="<?php echo $_SERVER['PHP_SELF']; ?>?by=all">ports list (<?php print $portsdb_info['num_ports']; ?>
     ports)</a>.</p>
     <br />
 
@@ -65,7 +64,7 @@
                 <ul>
 
 <?php
-                    $max_entries_per_column = floor($num_categories/4);
+                    $max_entries_per_column = floor($portsdb_info['num_categories']/4);
                     $columns = 0;
                     while ($columns < 4) {
 ?>
@@ -264,5 +263,5 @@
 
 <?php
     print_footer();
-    mysql_close($portsdb_connect);
+    mysql_close($portsdb_info['connection_stream']);
 ?>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macports-changes/attachments/20071212/3a936879/attachment-0001.html


More information about the macports-changes mailing list