[35214] trunk/www/ports.php

jmpp at macports.org jmpp at macports.org
Thu Mar 20 20:36:35 PDT 2008


Revision: 35214
          http://trac.macosforge.org/projects/macports/changeset/35214
Author:   jmpp at macports.org
Date:     2008-03-20 20:36:34 -0700 (Thu, 20 Mar 2008)

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

Big commit but with no functional changes but one:

 * Finally eliminated all switching back and forth between markup and php mode for small things, use print statements instead from within php;
 * Eliminated all "" quoting in php mode, resorting to '' and append operations where appropriate;
 * Introduced some comments;
 * Fixed markup in case the search query sent to the db fails, the only functional change in this commit.

All this done in the hope processing of queries returning large result sets gets a little snappier.

Modified Paths:
--------------
    trunk/www/ports.php

Modified: trunk/www/ports.php
===================================================================
--- trunk/www/ports.php	2008-03-21 03:24:47 UTC (rev 35213)
+++ trunk/www/ports.php	2008-03-21 03:36:34 UTC (rev 35214)
@@ -6,11 +6,11 @@
     /* Copyright (c) 2004-2007, The MacPorts Project. */
     
     $MPWEB = $_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['SCRIPT_NAME']);
-    include_once("$MPWEB/includes/common.inc");
+    include_once($MPWEB . '/includes/common.inc');
     print_header('The MacPorts Project -- Available Ports', 'utf-8');
     
     $portsdb_info = portsdb_connect($portsdb_host, $portsdb_user, $portsdb_passwd);
-    $sql = "SELECT UNIX_TIMESTAMP(activity_time) FROM $portsdb_name.log ORDER BY UNIX_TIMESTAMP(activity_time) DESC";
+    $sql = 'SELECT UNIX_TIMESTAMP(activity_time) FROM ' . $portsdb_name . '.log ORDER BY UNIX_TIMESTAMP(activity_time) DESC';
     $result = mysql_query($sql);
     if ($result && $row = mysql_fetch_row($result)) {
         $date = date('Y-m-d', $row[0]);
@@ -38,12 +38,12 @@
         <p>
             <label>Search by:</label>
             <select name="by">
-                <option value="name"<?php if ($by == "name") { print " selected=\"selected\""; } ?>>Software Title</option>
-                <option value="category"<?php if ($by == "category") { print " selected=\"selected\""; } ?>>Category</option>
-                <option value="maintainer"<?php if ($by == "maintainer") { print " selected=\"selected\""; } ?>>Maintainer</option>
-<!--                <option value="library"<?php if ($by == "dependency") { print " selected=\"selected\""; } ?>>Dependency</option> -->
-                <option value="variant"<?php if ($by == "variant") { print " selected=\"selected\""; } ?>>Variant</option>
-                <option value="platform"<?php if ($by == "platform") { print " selected=\"selected\""; } ?>>Platform</option>
+                <option value="name"<?php if ($by == 'name') { print ' selected="selected"'; } ?>>Software Title</option>
+                <option value="category"<?php if ($by == 'category') { print ' selected="selected"'; } ?>>Category</option>
+                <option value="maintainer"<?php if ($by == 'maintainer') { print ' selected="selected"'; } ?>>Maintainer</option>
+<!--                <option value="library"<?php if ($by == 'dependency') { print ' selected="selected"'; } ?>>Dependency</option> -->
+                <option value="variant"<?php if ($by == 'variant') { print ' selected="selected"'; } ?>>Variant</option>
+                <option value="platform"<?php if ($by == 'platform') { print ' selected="selected"'; } ?>>Platform</option>
             </select>
             <input type="text" name="substr" size="40" />
             <input type="submit" value="Search" />
@@ -55,191 +55,166 @@
     <br />
 
 <?php
-    if (!$by || (!$substr && $by != "all")) {
-        $query = "SELECT DISTINCT category FROM $portsdb_name.categories ORDER BY category";
+    /* If no valid search criteria is specified (by set and not equal to all and a valid substring) we output port categories
+     as possible searches */
+    if (!$by || ($by != 'all' && !$substr)) {
+        $query = 'SELECT DISTINCT category FROM ' . $portsdb_name . '.categories ORDER BY category';
         $result = mysql_query($query);
         if ($result) {
             $max_entries_per_column = floor($portsdb_info['num_categories']/4);
             $columns = 0;
-            print "<h3>Port Categories</h3>\n<div id=\"categories\">\n<ul>";
+            print '<h3>Port Categories</h3><div id="categories"><ul>';
             while ($columns < 4) {
                 $entries_per_colum = 0;
-                print "<li>\n<ul>\n";
+                print '<li><ul>';
                 while ($row = mysql_fetch_assoc($result)) {
                     print '<li><a href="' . $_SERVER['PHP_SELF'] . '?by=category&amp;substr=' . urlencode($row['category']) . '">'
                     . htmlspecialchars($row['category']) . '</a></li>';
                     if ($entries_per_colum == $max_entries_per_column) break;
                     $entries_per_colum++;
                 }
-                print "</ul>\n</li>";
+                print '</ul></li>';
                 $columns++;
             }
-            print "</ul>\n\n</div>";
+            print '</ul></div>';
         }
     }
 
-    if ($by && ($substr || $by == "all")) {
-        $fields = "name, path, version, description";
-        $tables = "$portsdb_name.portfiles AS p";
-        if ($by == "name") {
-            $criteria = "p.name LIKE '%" . mysql_real_escape_string($substr) . "%'";
+    /* If valid criteria is specified (by set and a valid substring, or by set to all) we poll the db and display the results */
+    if ($by && ($substr || $by == 'all')) {
+        $fields = 'name, path, version, description';
+        $tables = $portsdb_name . '.portfiles AS p';
+        if ($by == 'name') {
+            $criteria = 'p.name LIKE \'%' . mysql_real_escape_string($substr) . '%\'';
         }
-        if ($by == "category") {
-            $tables .= ", $portsdb_name.categories AS c";
-            $criteria = "c.portfile = p.name AND c.category = '" . mysql_real_escape_string($substr) . "'";
+        if ($by == 'category') {
+            $tables .= ', ' . $portsdb_name . '.categories AS c';
+            $criteria = 'c.portfile = p.name AND c.category = \'' . mysql_real_escape_string($substr) . '\'';
         }
-        if ($by == "maintainer") {
-            $tables .= ", $portsdb_name.maintainers AS m";
-            $criteria = "m.portfile = p.name AND m.maintainer LIKE '%" . mysql_real_escape_string($substr) . "%'";
+        if ($by == 'maintainer') {
+            $tables .= ', ' . $portsdb_name . '.maintainers AS m';
+            $criteria = 'm.portfile = p.name AND m.maintainer LIKE \'%' . mysql_real_escape_string($substr) . '%\'';
         }
-        if ($by == "library") {
-            $criteria = "p.name = '" . mysql_real_escape_string($substr) . "'";
+        if ($by == 'library') {
+            $criteria = 'p.name = \'' . mysql_real_escape_string($substr) . '\'';
         }
-        if ($by == "variant") {
-            $tables .= ", $portsdb_name.variants AS v";
-            $criteria = "v.portfile = p.name AND v.variant = '" . mysql_real_escape_string($substr) . "'";
+        if ($by == 'variant') {
+            $tables .= ', ' . $portsdb_name . '.variants AS v';
+            $criteria = 'v.portfile = p.name AND v.variant = \'' . mysql_real_escape_string($substr) . '\'';
         }
-        if ($by == "platform") {
-            $tables .= ", $portsdb_name.platforms AS pl";
-            $criteria = "pl.portfile = p.name AND pl.platform = '" . mysql_real_escape_string($substr) . "'";
+        if ($by == 'platform') {
+            $tables .= ', ' . $portsdb_name . '.platforms AS pl';
+            $criteria = 'pl.portfile = p.name AND pl.platform = \'' . mysql_real_escape_string($substr) . '\'';
         }
-        if ($criteria == "") {
-            $where = "";
+        if ($criteria == '') {
+            $where = '';
         } else {
-            $where = "WHERE $criteria";
+            $where = 'WHERE ' . $criteria;
         }
-        $query = "SELECT DISTINCT $fields FROM $tables $where ORDER BY name";
+        $query = 'SELECT DISTINCT ' . $fields . ' FROM ' . $tables . ' ' . $where . 'ORDER BY name';
         $result = mysql_query($query);
+        
+        /* Main query sent to the DB */
         if ($result) {
-?>
-            <h3>Query Results</h3>
+            print '<h3>Query Results</h3><p><i>' . mysql_num_rows($result) . ' ' . (mysql_num_rows($result) == 1 ? 'Portfile' : 'Portfiles')
+            . ' Selected</i></p>';
 
-            <p><i><?php print mysql_num_rows($result) . ' ' . (mysql_num_rows($result) == 1 ? 'Portfile' : 'Portfiles') . 
-            ' Selected'; ?></i></p>
-
-            <dl>
-<?php
+            print '<dl>';
+            /* Iterate over the entire set of returned ports */
             while ($row = mysql_fetch_assoc($result)) {
-?>
-                <dt><b><a href="<?php print $trac_url . 'browser/trunk/dports/' . $row['path'] . '/Portfile'; ?>"><?php print
-                htmlspecialchars($row['name']); ?></a></b> <?php print htmlspecialchars($row['version']); ?></dt>
-                <dd>
-                    <?php print htmlspecialchars($row['description']); ?>
-                    <br />
-<?php
-/* MAINTAINERS */
-                    $nquery = "SELECT maintainer FROM $portsdb_name.maintainers WHERE portfile='" . mysql_real_escape_string($row['name']) .
-                    "' ORDER BY is_primary DESC, maintainer";
+
+                /* Port name and Portfile URL */
+                print '<dt><b><a href="' . $trac_url . 'browser/trunk/dports/' . $row['path'] . '/Portfile">' . htmlspecialchars($row['name'])
+                . '</a></b> ' . htmlspecialchars($row['version']) . '</dt>';
+                
+                print '<dd>';
+                /* Port short description */
+                print htmlspecialchars($row['description']) . '<br />';
+                
+                /* Maintainers */
+                $nquery = 'SELECT maintainer FROM ' . $portsdb_name . '.maintainers WHERE portfile=\'' . mysql_real_escape_string($row['name']) .
+                    '\' ORDER BY is_primary DESC, maintainer';
                     $nresult = mysql_query($nquery);
                     if ($nresult) {
-?>
-                        <i>Maintained by:</i>
-<?php
                         $primary = 1;
+                        print '<i>Maintained by:</i>';
                         while ($nrow = mysql_fetch_array($nresult)) {
-                            if ($primary) { print "<b>"; }
-                            else { print " "; }
-                            $addr = obfuscate_email($nrow[0]);
-                            print $addr;
-                            if ($primary) { print "</b>"; }
+                            if ($primary) { print ' <b>'; }
+                            else { print ' '; }
+                            print obfuscate_email($nrow[0]);;
+                            if ($primary) { print '</b>'; }
                             $primary = 0;
                         }
                     }
 
-/* CATEGORIES */
-                    $nquery = "SELECT category FROM $portsdb_name.categories WHERE portfile='" . mysql_real_escape_string($row['name']) .
-                    "' ORDER BY is_primary DESC, category";
+                    /* Categories */
+                    $nquery = 'SELECT category FROM ' . $portsdb_name . '.categories WHERE portfile=\'' . mysql_real_escape_string($row['name']) .
+                    '\' ORDER BY is_primary DESC, category';
                     $nresult = mysql_query($nquery);
                     if ($nresult) {
-?>
-                        <br />
-                        <i>Categories:</i>
-<?php
                         $primary = 1;
+                        print '<br /><i>Categories:</i>';
                         while ($nrow = mysql_fetch_assoc($nresult)) {
-                            if ($primary) { print "<b>"; }
-?>
-                            <a href="<?php print $_SERVER['PHP_SELF']; ?>?by=category&amp;substr=<?php print urlencode($nrow['category']); ?>">
-                            <?php print htmlspecialchars($nrow['category']); ?></a>
-<?php
-                            if ($primary) { print "</b>"; }
+                            if ($primary) { print ' <b>'; }
+                            else { print ' '; }
+                            print '<a href="' . $_SERVER['PHP_SELF'] . '?by=category&amp;substr=' . urlencode($nrow['category']) . '">'
+                            . htmlspecialchars($nrow['category']) . '</a>';
+                            if ($primary) { print '</b>'; }
                             $primary = 0;
                         }
                     }
 
-/* PLATFORMS */
-                    $nquery = "SELECT platform FROM $portsdb_name.platforms WHERE portfile='" . mysql_real_escape_string($row['name']) .
-                    "' ORDER BY platform";
+                    /* Platforms */
+                    $nquery = 'SELECT platform FROM ' . $portsdb_name . '.platforms WHERE portfile=\'' . mysql_real_escape_string($row['name']) .
+                    '\' ORDER BY platform';
                     $nresult = mysql_query($nquery);
                     if ($nresult && mysql_num_rows($nresult) > 0) {
-?>
-                        <br />
-                        <i>Platforms:</i>
-<?php
+                        print '<br /><i>Platforms:</i> ';
                         while ($nrow = mysql_fetch_array($nresult)) {
-                            $platform = $nrow[0];
-?>
-                            <a href="<?php print $_SERVER['PHP_SELF']; ?>?by=platform&amp;substr=<?php print urlencode($platform); ?>">
-                            <?php print htmlspecialchars($platform); ?></a>
-<?php
+                            print '<a href="' . $_SERVER['PHP_SELF'] . '?by=platform&amp;substr=' . urlencode($nrow[0]) . '">'
+                            . htmlspecialchars($nrow[0]) . '</a> ';
                         }
                     }
 
-/* DEPENDENCIES */
-                    $nquery = "SELECT library FROM $portsdb_name.dependencies WHERE portfile='" . mysql_real_escape_string($row['name']) .
-                    "' ORDER BY library";
+                    /* Dependencies */
+                    $nquery = 'SELECT library FROM ' . $portsdb_name . '.dependencies WHERE portfile=\'' . mysql_real_escape_string($row['name']) .
+                    '\' ORDER BY library';
                     $nresult = mysql_query($nquery);
                     if ($nresult && mysql_num_rows($nresult) > 0) {
-?>
-                        <br />
-                        <i>Dependencies:</i>
-<?php
+                        print '<br /><i>Dependencies:</i> ';
                         while ($nrow = mysql_fetch_array($nresult)) {
                             // lib:libpng.3:libpng -> libpng
                             // might need adapting to the new port: depspec
-                            $library = eregi_replace("^([^:]*:[^:]*:|[^:]*:)", "", $nrow[0]);
-?>
-                            <a href="<?php print $_SERVER['PHP_SELF']; ?>?by=library&amp;substr=<?php print urlencode($library); ?>">
-                            <?php print htmlspecialchars($library); ?></a>
-<?php
+                            $library = eregi_replace('^([^:]*:[^:]*:|[^:]*:)', '', $nrow[0]);
+                            print '<a href="' . $_SERVER['PHP_SELF'] . '?by=library&amp;substr=' . urlencode($library) . '">'
+                            . htmlspecialchars($library) . '</a> ';
                         }
                     }
 
-/* VARIANTS */
-                    $nquery = "SELECT variant FROM $portsdb_name.variants WHERE portfile='" . mysql_real_escape_string($row['name']) .
-                    "' ORDER BY variant";
+                    /* Variants */
+                    $nquery = 'SELECT variant FROM ' . $portsdb_name . '.variants WHERE portfile=\'' . mysql_real_escape_string($row['name']) .
+                    '\' ORDER BY variant';
                     $nresult = mysql_query($nquery);
                     if ($nresult && mysql_num_rows($nresult) > 0) {
-?>
-                        <br />
-                        <i>Variants:</i>
-<?php
+                        print '<br /><i>Variants:</i> ';
                         while ($nrow = mysql_fetch_array($nresult)) {
-                            $variant = $nrow[0];
-?>
-                            <a href="<?php print $_SERVER['PHP_SELF']; ?>?by=variant&amp;substr=<?php print urlencode($variant); ?>">
-                            <?php print htmlspecialchars($variant); ?></a>
-<?php
+                            print '<a href="' . $_SERVER['PHP_SELF'] . '?by=variant&amp;substr=' . urlencode($nrow[0]) . '">'
+                            . htmlspecialchars($nrow[0]) . '</a> ';
                         }
                     }
-?>
-                    <br />
-                    <br />
-                </dd>
+                    print '<br /><br /></dd>';
+                
+            } /* while (listing of macthing ports) */
+            print '</dl>';
 
-<?php
-            }
-
+        /* When we hit this else, the query failed for some reason */
         } else {
             print '<p>An Error Occurred: '. mysql_error($portsdb_info['connection_handler']) . '</p>';
         }
+        
+    } /* if (main query sent to the DB) */
 ?>
-        </dl>
 
-<?php
-    }
-?>
-
 </div>
 
 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/macports-changes/attachments/20080320/4bcbb966/attachment-0001.html 


More information about the macports-changes mailing list