Permalink
Browse files

port to postgres

  • Loading branch information...
1 parent ab891aa commit 671021bd5c7fdecb736a25d07f709c95b318de5d @sandsmark committed Aug 3, 2011
Showing with 89 additions and 89 deletions.
  1. +3 −4 databaseconnect.php
  2. +8 −8 getlisting.php
  3. +1 −1 getsong.php
  4. +3 −3 getzip.php
  5. +13 −12 logger.php
  6. +19 −19 statistics.php
  7. +5 −5 testinterface.php
  8. +1 −1 updateapplication.php
  9. +36 −36 updatedatabase.php
View
@@ -1,14 +1,13 @@
<?php
require_once("authenticate.php");
-mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);
-mysql_select_db(DATABASE_NAME);
+pg_connect("host=".DATABASE_SERVER." user=".DATABASE_USERNAME." password=".DATABASE_PASSWORD." dbname=".DATABASE_NAME);
function nullString($string)
{
if($string == "")
{
return "NULL";
}
- return "'".mysql_real_escape_string($string)."'";
+ return "'".pg_escape_string($string)."'";
}
function nullInt($int)
{
@@ -20,7 +19,7 @@ function nullInt($int)
}
function sqlBool($bool)
{
- return $bool ? "1" : "0";
+ return $bool ? "TRUE" : "FALSE";
}
function getFormat($file)
{
View
@@ -14,7 +14,7 @@
$conditions = "WHERE ";
for($i = 0; $i < count($words); $i++)
{
- $word = mysql_real_escape_string($words[$i]);
+ $word = pg_escape_string($words[$i]);
if($_GET["querytype"] == "all" || $_GET["querytype"] == "")
{
$conditions .= "(artist LIKE '%${word}%' OR title LIKE '%${word}%' OR album LIKE '%${word}%' OR sha1='${word}')";
@@ -61,18 +61,18 @@
}
$limiter .= " OFFSET ".intval($_GET["offset"]);
}
-$result = @mysql_query("SELECT SQL_CALC_FOUND_ROWS sha1,track,title,artist,album,format FROM musictags ${conditions} ORDER BY artist,year,album,disc,track,title ${limiter};");
-$totalResult = @mysql_query("SELECT FOUND_ROWS()");
-$totalRows = @mysql_result($totalResult, 0, 0);
-@mysql_free_result($totalResult);
+$result = @pg_query("SELECT sha1,track,title,artist,album,format FROM musictags ${conditions} ORDER BY artist,year,album,disc,track,title ${limiter};");
+$totalResult = @pg_query("SELECT count(*) from musictags ${conditions} ${limiter}");
+$totalRows = @pg_result($totalResult, 0, 0);
+@pg_free_result($totalResult);
$language = strtolower($_GET["language"]);
if($language == "javascript")
{
header("Content-Type: text/javascript; charset=UTF-8");
echo "[$totalRows,[";
$first = true;
- while($row = @mysql_fetch_assoc($result))
+ while($row = @pg_fetch_assoc($result))
{
$sha1 = htmlentities($row["sha1"], ENT_QUOTES, "UTF-8");
$track = htmlentities($row["track"], ENT_QUOTES, "UTF-8");
@@ -100,7 +100,7 @@
$root = $doc->createElement("songs");
$root->appendChild(new DOMAttr("totalsongs", $totalRows));
$doc->appendChild($root);
- while($row = @mysql_fetch_assoc($result))
+ while($row = @pg_fetch_assoc($result))
{
$song = $doc->createElement("song");
foreach($row as $key => $value)
@@ -124,5 +124,5 @@
}
echo $output;
}
-@mysql_free_result($result);
+@pg_free_result($result);
?>
View
@@ -10,7 +10,7 @@
exit;
}
-if(!($row = mysql_fetch_assoc(mysql_query("SELECT file,track,artist,album,title,sha1 FROM musictags WHERE sha1 = ".nullString($_GET["hash"])))))
+if(!($row = pg_fetch_assoc(pg_query("SELECT file,track,artist,album,title,sha1 FROM musictags WHERE sha1 = ".nullString($_GET["hash"])))))
{
exit;
}
View
@@ -19,12 +19,12 @@
}
$query .= "sha1 = ".nullString($_POST["hash".$i]);
}
-$query = mysql_query($query);
-while($row = mysql_fetch_assoc($query))
+$query = pg_query($query);
+while($row = pg_fetch_assoc($query))
{
$rowList[] = $row;
}
-@mysql_free_result($query);
+@pg_free_result($query);
if(count($rowList) == 0)
{
exit;
View
@@ -2,19 +2,19 @@
require_once("databaseconnect.php");
function setupLogDatabase()
{
- mysql_query( "CREATE TABLE IF NOT EXISTS requestlog (
- id INT NOT NULL AUTO_INCREMENT,
+ pg_query( "CREATE TABLE requestlog (
+ id SERIAL,
PRIMARY KEY(id),
leaderid INT,
time INT NOT NULL,
ip VARCHAR(30),
- useragent VARCHAR(255),
+ useragent VARCHAR,
zip BOOL,
sha1 VARCHAR(64),
- artist VARCHAR(255),
- album VARCHAR(255),
- title VARCHAR(255)
- ) CHARACTER SET utf8;"
+ artist VARCHAR,
+ album VARCHAR,
+ title VARCHAR
+ );"
);
}
function logDownload($songArray, $zip)
@@ -26,7 +26,7 @@ function logDownload($songArray, $zip)
$first = -1;
foreach($songArray as $song)
{
- mysql_query("INSERT INTO requestlog (leaderid, time, ip, useragent, zip, sha1, artist, album, title) VALUES (
+ $result = pg_query("INSERT INTO requestlog (leaderid, time, ip, useragent, zip, sha1, artist, album, title) VALUES (
".$first.",
".time().",
".nullString($_SERVER["REMOTE_ADDR"]).",
@@ -35,12 +35,13 @@ function logDownload($songArray, $zip)
".nullString($song["sha1"]).",
".nullString($song["artist"]).",
".nullString($song["album"]).",
- ".nullString($song["title"]).");
- ");
+ ".nullString($song["title"]).")
+ RETURNING id;");
if($first == -1)
{
- $first = mysql_insert_id();
+ $row = pg_fetch_row($result);
+ $first = $row[0];
}
}
}
-?>
+?>
View
@@ -34,28 +34,28 @@
<?php
require_once("logger.php");
echo '<h3 align="center" style="margin-bottom: 0px;">Quick Stats</h3><div align="center"><div align="left" style="display:inline-block; font-size: 10pt;">';
-echo "Since ".date("F j, Y \\a\\t g:i:sa, T", mysql_result(mysql_query("SELECT MIN(time) FROM requestlog;"), 0, 0)).":";
-echo "<li>".mysql_result(mysql_query("SELECT COUNT(*) FROM musictags;"), 0, 0)." songs available</li>";
-echo "<li>".mysql_result(mysql_query("SELECT COUNT(*) FROM requestlog;"), 0, 0)." total songs served</li>";
-echo "<li>".mysql_result(mysql_query("SELECT COUNT(DISTINCT sha1) FROM requestlog;"), 0, 0)." different songs served</li>";
-echo "<li>".mysql_result(mysql_query("SELECT COUNT(*) FROM requestlog WHERE zip=0;"), 0, 0)." songs streamed in the web player</li>";
-echo "<li>".mysql_result(mysql_query("SELECT COUNT(*) FROM requestlog WHERE zip=1;"), 0, 0)." downloaded in zip files</li>";
-echo "<li>".mysql_result(mysql_query("SELECT COUNT(*) FROM requestlog WHERE zip=1 AND leaderid=-1;"), 0, 0)." total zip files downloaded</li>";
-echo "<li>".mysql_result(mysql_query("SELECT COUNT(DISTINCT ip) FROM requestlog;"), 0, 0)." different IP addresses</li>";
-echo "<li>".mysql_result(mysql_query("SELECT COUNT(DISTINCT useragent) FROM requestlog;"), 0, 0)." different user agents</li>";
+echo "Since ".date("F j, Y \\a\\t g:i:sa, T", pg_result(pg_query("SELECT MIN(time) FROM requestlog;"), 0, 0)).":";
+echo "<li>".pg_result(pg_query("SELECT COUNT(*) FROM musictags;"), 0, 0)." songs available</li>";
+echo "<li>".pg_result(pg_query("SELECT COUNT(*) FROM requestlog;"), 0, 0)." total songs served</li>";
+echo "<li>".pg_result(pg_query("SELECT COUNT(DISTINCT sha1) FROM requestlog;"), 0, 0)." different songs served</li>";
+echo "<li>".pg_result(pg_query("SELECT COUNT(*) FROM requestlog WHERE zip=0;"), 0, 0)." songs streamed in the web player</li>";
+echo "<li>".pg_result(pg_query("SELECT COUNT(*) FROM requestlog WHERE zip=1;"), 0, 0)." downloaded in zip files</li>";
+echo "<li>".pg_result(pg_query("SELECT COUNT(*) FROM requestlog WHERE zip=1 AND leaderid=-1;"), 0, 0)." total zip files downloaded</li>";
+echo "<li>".pg_result(pg_query("SELECT COUNT(DISTINCT ip) FROM requestlog;"), 0, 0)." different IP addresses</li>";
+echo "<li>".pg_result(pg_query("SELECT COUNT(DISTINCT useragent) FROM requestlog;"), 0, 0)." different user agents</li>";
echo "It is now ".date("F j, Y \\a\\t g:i:sa, T").".</div></div>";
echo '<h3 align="center" style="margin-bottom: 0px;">Top 10 Artists</h3><div align="center"><div align="left" style="display:inline-block; font-size: 10pt;">';
-$query = mysql_query("SELECT COUNT(artist), artist FROM requestlog GROUP BY artist ORDER BY COUNT(artist) DESC LIMIT 10;");
-while($result = mysql_fetch_assoc($query))
+$query = pg_query("SELECT COUNT(artist), artist FROM requestlog GROUP BY artist ORDER BY COUNT(artist) DESC LIMIT 10;");
+while($result = pg_fetch_assoc($query))
{
echo "<li>".$result["COUNT(artist)"].": ".$result["artist"]."</li>";
}
echo "</div></div>";
echo '<h3 align="center" style="margin-bottom: 0px;">Top 10 User Agents</h3><div align="center"><div align="left" style="display:inline-block; font-size: 10pt;">';
-$query = mysql_query("SELECT COUNT(DISTINCT ip), useragent FROM requestlog GROUP BY useragent ORDER BY COUNT(DISTINCT ip) DESC LIMIT 10;");
-while($result = mysql_fetch_assoc($query))
+$query = pg_query("SELECT COUNT(DISTINCT ip), useragent FROM requestlog GROUP BY useragent ORDER BY COUNT(DISTINCT ip) DESC LIMIT 10;");
+while($result = pg_fetch_assoc($query))
{
echo "<li>".$result["COUNT(DISTINCT ip)"].": ".$result["useragent"]."</li>";
}
@@ -70,23 +70,23 @@ function linkTerm($term, $type)
}
return "<a href=\"/?query=".urlencode(htmlspecialchars_decode($term))."&querytype=$type\">".$term."</a>";
}
-$ipsResult = mysql_query("SELECT ip, MAX(time), COUNT(*) FROM requestlog GROUP BY ip ORDER BY MAX(time) DESC;");
-while($row = mysql_fetch_assoc($ipsResult))
+$ipsResult = pg_query("SELECT ip, MAX(time), COUNT(*) FROM requestlog GROUP BY ip ORDER BY MAX(time) DESC;");
+while($row = pg_fetch_assoc($ipsResult))
{
echo "<table width=\"100%\" cellspacing=\"0\">";
echo "<tr><th colspan=\"6\"><a href=\"http://ws.arin.net/whois?queryinput=".$row["ip"]."\">".$row["ip"]." (".@gethostbyaddr($row["ip"]).")</a> ".$row["COUNT(*)"]." total downloads</th></tr>";
- $requestResult = mysql_query("SELECT * FROM requestlog WHERE ip = '".$row["ip"]."' AND leaderid = -1 ORDER BY time DESC");
- while($listen = mysql_fetch_assoc($requestResult))
+ $requestResult = pg_query("SELECT * FROM requestlog WHERE ip = '".$row["ip"]."' AND leaderid = -1 ORDER BY time DESC");
+ while($listen = pg_fetch_assoc($requestResult))
{
if($listen["zip"])
{
echo "<tr><td><font style=\"font-size:6pt;\">".date("M j, Y g:i:sa T", $listen["time"])."</font></td><td colspan=\"4\" align=\"center\"><i>Zip File</i></td><td><font style=\"font-size: 4pt;\">".$listen["useragent"]."</font></td></tr>";
- $subRequestResult = mysql_query("SELECT * FROM requestlog WHERE leaderid = ".$listen["id"]." ORDER BY time DESC");
+ $subRequestResult = pg_query("SELECT * FROM requestlog WHERE leaderid = ".$listen["id"]." ORDER BY time DESC");
do
{
echo "<tr><td class=\"noborder\"></td><td class=\"noborder\">".linkTerm($listen["artist"], "artist")."</td><td class=\"noborder\">".linkTerm($listen["album"], "album")."</td><td class=\"noborder\">".linkTerm($listen["title"], "title")."</td><td class=\"noborder\"><font style=\"font-size: 4pt;\">".linkTerm($listen["sha1"], "sha1")."</font></td><td class=\"noborder\"></td></tr>";
}
- while($listen = mysql_fetch_assoc($subRequestResult));
+ while($listen = pg_fetch_assoc($subRequestResult));
}
else
{
View
@@ -8,7 +8,7 @@ function nullEq($str)
}
else
{
- return "= '".mysql_real_escape_string($str)."'";
+ return "= '".pg_escape_string($str)."'";
}
}
if($_GET["getlisting"] == true)
@@ -17,19 +17,19 @@ function nullEq($str)
{
$artist = nullEq($_GET["artist"]);
$album = nullEq($_GET["album"]);
- $result = @mysql_query("SELECT title, sha1 FROM musictags WHERE artist $artist AND album $album ORDER BY disc, track, title");
+ $result = @pg_query("SELECT title, sha1 FROM musictags WHERE artist $artist AND album $album ORDER BY disc, track, title");
}
elseif(isset($_GET["artist"]))
{
$artist = nullEq($_GET["artist"]);
- $result = @mysql_query("SELECT DISTINCT album FROM musictags WHERE artist $artist ORDER BY year, album");
+ $result = @pg_query("SELECT DISTINCT album FROM musictags WHERE artist $artist ORDER BY year, album");
}
else
{
- $result = @mysql_query("SELECT DISTINCT artist FROM musictags ORDER BY artist;");
+ $result = @pg_query("SELECT DISTINCT artist FROM musictags ORDER BY artist;");
}
header("Content-Type: text/javascript; charset=UTF-8");
- while($row = @mysql_fetch_row($result))
+ while($row = @pg_fetch_row($result))
{
for ($i = 0; $i < count($row); ++$i)
$row[$i] = utf8_encode($row[$i]);
@@ -65,7 +65,7 @@
echo "<p>Removing old database table and rescanning collection:<pre>";
ob_flush();
flush();
- mysql_query("DROP TABLE `musictags`;");
+ pg_query("DROP TABLE `musictags`;");
require_once("updatedatabase.php");
echo "</pre>";
}
View
@@ -46,17 +46,17 @@ function isExcluded($file)
function deleteBadEntries()
{
global $removeCount;
- $result = mysql_query("SELECT file FROM musictags");
- while($row = mysql_fetch_assoc($result))
+ $result = pg_query("SELECT file FROM musictags");
+ while($row = pg_fetch_assoc($result))
{
if(!file_exists($row["file"]) || isExcluded($row["file"]))
{
echo "Removed ".$row["file"]."<br>";
- mysql_query("DELETE FROM musictags WHERE file = ".nullString($row["file"]));
+ pg_query("DELETE FROM musictags WHERE file = ".nullString($row["file"]));
$removeCount++;
}
}
- mysql_free_result($result);
+ pg_free_result($result);
}
function processFile($file)
@@ -79,26 +79,26 @@ function processFile($file)
$nullifiedFile = nullString($file);
$lastModified = filemtime($file);
$update = false;
- $result = @mysql_query("SELECT sha1,lastmodified FROM musictags WHERE file = ${nullifiedFile}");
+ $result = @pg_query("SELECT sha1,lastmodified FROM musictags WHERE file = ${nullifiedFile}");
if($result)
{
- $row = mysql_fetch_assoc($result);
+ $row = pg_fetch_assoc($result);
if($lastModified == $row["lastmodified"])
{
return;
}
elseif(($sha1 = sha1_file($file)) == $row["sha1"])
{
echo "Updated last modified for ".$file."<br>";
- mysql_query("UPDATE musictags SET lastmodified = ${lastModified} WHERE file = ${nullifiedFile}");
+ pg_query("UPDATE musictags SET lastmodified = ${lastModified} WHERE file = ${nullifiedFile}");
$updateCount++;
return;
}
elseif($row)
{
$update = true;
}
- mysql_free_result($result);
+ pg_free_result($result);
}
$tags = getTags($file);
@@ -135,7 +135,7 @@ function processFile($file)
if($update)
{
- mysql_query("UPDATE musictags SET
+ pg_query("UPDATE musictags SET
sha1=${sha1},
lastmodified=${lastModified},
format=${format},
@@ -163,9 +163,9 @@ function processFile($file)
}
else
{
- mysql_query( "INSERT INTO `musictags` ( `sha1` , `file` , `lastmodified` , `format` , `artist` , `album` ,
- `albumartist` , `title` , `year` , `comment` , `track` , `disc` , `disctotal` ,
- `genre` , `bpm` , `composer` , `compilation` , `bitrate` , `samplerate` , `channels` , `length` )
+ pg_query( "INSERT INTO musictags ( sha1 , file , lastmodified , format , artist , album ,
+ albumartist , title , year , comment , track , disc , disctotal ,
+ genre , bpm , composer , compilation , bitrate , samplerate , channels , length )
VALUES (
${sha1}, ${nullifiedFile}, ${lastModified}, ${format}, ${artist}, ${album},
${albumArtist}, ${title} , ${year}, ${comment}, ${track}, ${disc}, ${discTotal},
@@ -237,30 +237,30 @@ function joinPaths()
function setupDatabase()
{
- mysql_query( "CREATE TABLE IF NOT EXISTS `musictags` (
- `file` VARCHAR( 255 ) NOT NULL ,
- `sha1` VARCHAR( 64 ) NOT NULL ,
- `lastmodified` INT NOT NULL ,
- `format` VARCHAR( 255 ) NULL ,
- `artist` VARCHAR( 255 ) NULL ,
- `album` VARCHAR( 255 ) NULL ,
- `albumartist` VARCHAR( 255 ) NULL ,
- `title` VARCHAR( 255 ) NULL ,
- `year` INT NULL ,
- `comment` VARCHAR( 255 ) NULL ,
- `track` INT NULL ,
- `disc` INT NULL ,
- `disctotal` INT NULL ,
- `genre` VARCHAR( 255 ) NULL ,
- `bpm` INT NULL ,
- `composer` VARCHAR( 255 ) NULL ,
- `compilation` BOOL NULL ,
- `bitrate` INT NULL ,
- `samplerate` INT NULL ,
- `channels` INT NULL ,
- `length` INT NULL ,
- PRIMARY KEY ( `file` )
- ) ENGINE = MYISAM CHARACTER SET utf8 COMMENT = 'music tag table';"
+ pg_query( "CREATE TABLE musictags (
+ file VARCHAR NOT NULL ,
+ sha1 VARCHAR( 64 ) NOT NULL ,
+ lastmodified INT NOT NULL ,
+ format VARCHAR NULL ,
+ artist VARCHAR NULL ,
+ album VARCHAR NULL ,
+ albumartist VARCHAR NULL ,
+ title VARCHAR NULL ,
+ year INT NULL ,
+ comment VARCHAR NULL ,
+ track INT NULL ,
+ disc INT NULL ,
+ disctotal INT NULL ,
+ genre VARCHAR NULL ,
+ bpm INT NULL ,
+ composer VARCHAR NULL ,
+ compilation BOOL NULL ,
+ bitrate INT NULL ,
+ samplerate INT NULL ,
+ channels INT NULL ,
+ length INT NULL ,
+ PRIMARY KEY ( file )
+ );"
);
echo "Connected to database<br>";
}

0 comments on commit 671021b

Please sign in to comment.