Bloated and greedy search, no pagination yet (other than the limit for # of records per page).

I have concerns about how much this hammers the database.  We may have to trim down the constructor for the AddOn class.
This commit is contained in:
mike.morgan%oregonstate.edu 2005-07-25 20:12:22 +00:00
parent 1fe50fe884
commit b1b91e495a
3 changed files with 200 additions and 21 deletions

View File

@ -33,22 +33,17 @@ if (isset($_GET['platform'])&&ctype_digit($_GET['platform'])) {
}
// Date.
if (isset($_GET['date'])&&ctype_alpha($_GET['date'])) {
if (isset($_GET['date'])&&$_GET['date']!='null'&&ctype_alpha($_GET['date'])) {
$clean['date'] = $_GET['date'];
}
// Application.
if (isset($_GET['app'])&&ctype_alpha($_GET['app'])) {
if (isset($_GET['app'])&&$_GET['app']!='null'&&ctype_alpha($_GET['app'])) {
$clean['app'] = $_GET['app'];
}
// Sort.
if (isset($_GET['sort'])&&ctype_digit($_GET['sort'])) {
$clean['sort'] = $_GET['sort'];
}
// Query.
if (isset($_GET['q'])&&preg_match("/^[a-zA-Z0-9'\.-]*$/",$_GET['q'])) {
if (isset($_GET['q'])&&preg_match("/^[a-zA-Z0-9'\.-\s]*$/",$_GET['q'])) {
$clean['q'] = $_GET['q'];
}
@ -57,12 +52,34 @@ if (isset($_GET['sort'])&&ctype_alpha($_GET['sort'])) {
$clean['sort'] = $_GET['sort'];
}
// Starting point.
$clean['left'] = (isset($_GET['left'])) ? intval($_GET['left']) : 0;
// Per page.
$_GET['perpage'] = (isset($_GET['perpage'])) ? intval($_GET['perpage']) : null;
switch ($_GET['perpage']) {
case '10':
default:
$clean['perpage'] = 10;
break;
case '25':
$clean['perpage'] = 25;
break;
case '50':
$clean['perpage'] = 50;
break;
}
// Ending point.
$clean['right'] = $clean['left'] + $clean['perpage'];
// Prepared verified inputs for their destinations.
foreach ($clean as $key=>$val) {
$sql[$key] = mysql_real_escape_string($val);
}
// Instantiate AMO_Object so we can get our categories and platforms.
$amo = new AMO_Object();
$amo->getCats();
@ -79,6 +96,7 @@ $sort = array(
'name' => 'Name',
'rating' => 'Rating',
'downloads' => 'Popularity',
'newest' => 'Newest'
);
$apps = array(
@ -87,18 +105,150 @@ $apps = array(
'Mozilla' => 'Mozilla'
);
$perpage = array(
10 => '10',
25 => '25',
50 => '50'
);
// Now we need to build our query. Our query starts with four parts:
// Select and joins.
$select = "
SELECT DISTINCT
main.ID
FROM
main
";
// Where clause.
$where = "
WHERE
";
// Order by.
$orderby = "
ORDER BY
";
if (!empty($sql['platform'])||!empty($sql['app'])) {
$select .= " INNER JOIN version ON version.ID = main.ID ";
}
if (!empty($sql['cat'])) {
$select .= " INNER JOIN categoryxref ON categoryxref.ID = main.ID ";
$where .= " categoryxref.CategoryID = '{$sql['cat']}' AND ";
}
if (!empty($sql['platform'])) {
$where .= " version.OSID = '{$sql['platform']}' AND ";
}
if (!empty($sql['app'])) {
$select .= " INNER JOIN applications ON version.AppID = applications.AppID ";
$where .= " applications.AppName = '{$sql['app']}' AND ";
}
if (!empty($sql['q'])) {
$where .= " main.Name LIKE '%{$sql['q']}%' AND ";
}
if (!empty($sql['date'])) {
switch ($sql['date']) {
case 'day':
$compareTimestamp = strtotime('-1 day');
break;
case 'week':
$compareTimestamp = strtotime('-1 week');
break;
case 'month':
$compareTimestamp = strtotime('-1 month');
break;
case 'year':
$compareTimestamp = strtotime('-1 year');
break;
}
$compareDate = date('Y-m-d',$compareTimestamp);
$where .= " main.DateUpdated > '{$compareDate}' AND ";
unset($compareTimestamp);
unset($compareDate);
}
if (!empty($sql['sort'])) {
switch ($sql['sort']) {
case 'name':
default:
$orderby .= " main.Name ASC";
break;
case 'rating':
$orderby .= " main.Rating DESC";
break;
case 'downloads':
$orderby .= " main.TotalDownloads DESC";
break;
case 'newest':
$orderby .= " main.DateUpdated DESC";
break;
}
} else {
$orderby .= " main.Name ASC ";
}
$where .= ' 1 ';
$query = $select.$where.$orderby;
$results = array();
$rawResults = array();
$db->query($query, SQL_ALL);
if (is_array($db->record)) {
foreach ($db->record as $row) {
$rawResults[] = $row[0];
}
}
for ($i=$clean['left'];$i<$clean['right'];$i++) {
if (isset($rawResults[$i])) {
$results[] = new Addon($rawResults[$i]);
}
}
$resultCount = count($rawResults);
if ($resultCount<$clean['right']) {
$clean['right'] = $resultCount;
}
unset($select);
unset($where);
unset($orderby);
unset($query);
// Pass variables to template object.
$tpl->assign(
array(
'clean' => $clean,
'cats' => $amo->Cats,
'platforms' => $amo->Platforms,
'apps' => $apps,
'dates' => $dates,
'sort' => $sort,
'content' => 'search.tpl',
'title' => 'Search',
'left' => $clean['left']+1,
'right' => $clean['right'],
'perpage' => $clean['perpage'],
'resultcount' => $resultCount,
'results' => $results,
'clean' => $clean,
'cats' => $amo->Cats,
'platforms' => $amo->Platforms,
'apps' => $apps,
'dates' => $dates,
'sort' => $sort,
'perpage' => $perpage,
'content' => 'search.tpl',
'title' => 'Search'
)
);
// Set a non-default wrapper.
$wrapper = 'inc/wrappers/nonav.tpl';
?>

View File

@ -20,7 +20,7 @@ released on {$addon->VersionDateAdded|date_format}
<p>{$addon->Description}</p>
<p class="requires">
Requires: {$addon->AppName} 1.0 - 1.0+ <img src="{$config.webpath}/img/{$addon->AppName|lower}_icon.png" width="34" height="34" alt="{$addon->AppName}">
Requires: {$addon->AppName} {$addon->MinAppVer} - {$addon->MaxAppVer} <img src="{$config.webpath}/img/{$addon->AppName|lower}_icon.png" width="34" height="34" alt="{$addon->AppName}">
</p>
<div class="key-point install-box">

View File

@ -32,7 +32,7 @@
<div class="search-option">
<label for="app">App:</label>
<select id="app" name="app">
<option>Any</option>
<option value="null">Any</option>
{html_options options=$apps selected=$clean.app}
</select>
</div>
@ -60,6 +60,13 @@
</select>
</div>
<div class="search-option">
<label for="perpage">Per page:</label>
<select id="perpage" name="perpage">
{html_options options=$perpage selected=$clean.perpage}
</select>
</div>
</fieldset>
<input type="submit" value="Search"><br><br>
@ -74,10 +81,32 @@
<div id="mainContent">
<h2>Addon Search</h2>
{if $results}
<h2>Addon Search Results</h2>
<p class="first">{$resultcount} Addons found. Showing records {$left}-{$right}.</p>
{section name=r loop=$results}
<div class="item">
<div class="rating" title="4.67 Stars out of 5">Rating: {$results[r]->Rating}</div>
<h2 class="first"><a href="./addon.php?id={$results[r]->ID}">{$results[r]->Name}</a></h2>
<p class="first">By <a href="author.php?id={$results[r]->UserID}">{$results[r]->UserName}</a></p>
<p class="first">{$results[r]->Description}</p>
<div style="margin-top: 30px; height: 34px">
<div class="iconbar">
<a href="{$results[r]->DownloadURI}" onclick="return install(event,'{$results[r]->Name}', '{$config.webpath}/img/default.png');">
<img src="{$config.webpath}/img/download.png" height="32" width="32" title="Install {$results[r]->Name}" ALT="">Install</a><br>
<span class="filesize">&nbsp;&nbsp;{$results[r]->Size} kb</span>
</div>
<div class="iconbar">
<img src="{$config.webpath}/img/{$results[r]->AppName|lower}_icon.png" height="34" width="34" ALT="">&nbsp;For {$results[r]->AppName}:<BR>&nbsp;&nbsp;{$results[r]->MinAppVer} - {$results[r]->MaxAppVer}
</div>
</div>
<div class="baseline">Last Update: {$results[r]->DateUpdated|date_format} | Downloads Last 7 Days: {$results[r]->downloadcount} | Total Downloads: {$results[r]->TotalDownloads}</DIV>
</div>
{/section}
{else}
<p class="first">There are currently no results. Please use the options at the left to begin a search or try adjusting your search terms.</p>
<h2>Addon Search</h2>
<p class="first">There are currently no results. Please use the options at the left to begin a search or try adjusting your search terms.</p>
{/if}
</div>