Make it read from file line by line

Larger files (e.g. CSDB and EAB) can overload the server because of how
much data it has to hold and process. Reading the files line by line
makes that drop to more than half of what it was.
This commit is contained in:
Matt Nadareski
2016-04-04 17:26:35 -07:00
parent fe47d8459d
commit 18b5fb3e7d
2 changed files with 26 additions and 7 deletions

View File

@@ -224,10 +224,19 @@ function import_dat ($filename)
$xmlr = new XmlReader;
// If the file doesn't start with the right thing, convert it
$file = file($importroot.$filename);
if (strpos($file[0], "<") !== 0)
$handle = fopen($importroot.$filename, "r");
if ($handle === false)
{
$xmlr->XML(rv2xml($file));
echo "The file was not valid!<p/>\n";
return;
}
$file = fgets($handle);
fclose($handle);
if (strpos($file, "<") !== 0)
{
$xmlr->XML(rv2xml($importroot.$filename));
}
else
{
@@ -237,7 +246,7 @@ function import_dat ($filename)
if (!$result)
{
echo "The file was not valid!<p/>\n";
die();
return;
}
}

View File

@@ -41,7 +41,7 @@ function get_data($url)
}
// Convert an old-style DAT to XML
function rv2xml ($file)
function rv2xml ($filename)
{
//ob_end_clean();
ini_set('max_execution_time', 0); // Set the execution time to infinite. This is a bad idea in production.
@@ -51,6 +51,15 @@ function rv2xml ($file)
$itemPattern = @"/^(.*?) (.*)/";
$endPattern = @"/^\)\s*$/";
// Get the file open for reading
$file = fopen($filename, "r");
// If there's an error, return null
if ($file === false)
{
return null;
}
// Create the XMLWriter
$xmlw = new XMLWriter;
$xmlw->openMemory();
@@ -60,9 +69,9 @@ function rv2xml ($file)
$xmlw->startElement("datafile");
$block = false; $header = false;
for ($k = 0; $k < sizeof($file); $k++)
while (($line = fgets($file)) !== false)
{
$line = trim($file[$k]);
$line = trim($line);
// If the line is the header or a game
if (preg_match($headerPattern, $line, $matches) !== 0)
@@ -179,6 +188,7 @@ function rv2xml ($file)
}
$xmlw->endElement();
$xmlw->endDocument();
fclose($file);
return $xmlw->outputMemory();
}