noTimeOut package
@author : Johan Barbier <johan.barbier@gmail.com>
@Version : 2006/10/09
Needed : PHP 5, short_open_tags to Off
DESCRIPTION:
This package is meant to retrieve a huge amount of data, or process a big php script, and not being annoyed by PHP time limit (ie : set_time_limit, and max_execution_time).
Sometimes, you do not want, or can, modify this timeout. But it can be pretty annoying if you have a big file to read, or a huge query to process.
Well, this package can help you!
Basically, it uses xmlhttp requests to ask the server to process a part of the main process.
For exemple, for a file, it will read lines 0 to 10, then 11 to 20, and so on.
That way, you will never hit PHP time limit, but your 50000 lines file will be read and displayed anyway.
The file (or query results, or whatever) will be flushed step by step.
Just have a look at the indexX.php exemple pages.
In most of them, PHP time limit has been set to 2s.
Addition from the 20th of October 2006 : now, stacks are supported. You can declare several stacks and have them work asynchronously.
The package consists of 2 classes:
class/class.noTimeOut.js
=> used to initialize the xmlhttp object and the required methods.
noTimeOut::initialize() method shows the properties that can/must be set
NOTA : the ARGS property is an array. It can be used to send other variables to the PHP script (useful for the new oneShot method : a basic Ajax one shot request)
noTimeOut::getData() method shows you the types of processes that can be used.
Basically :
- instanciate your object :
var oBuffer = new noTimeOut ();
- declare a stack :
oBuffer.declareStack (Stack_Name);
- initialize your properties :
oBuffer.initialize (Stack_Name, Prop_Name, Prop_Value);
- start it!
oBuffer.startWork (Stack_Name);
class/class.noTimeOut.php
=> used to process the scripts.
noTimeOut::aProps shows all the properties that can/must be initialized (it depends on the type of process chosen).
noTimeOut::aTypes shows the possible types of process (up to now)
noTimeOut::aDbServers shows the sql servers supported up to now, in a DB type of process
noTimeOut::flushMe () is the method used to flush the process
In the scripts/ folder, you will find the scripts using this class.
Basically : initialize your properties, and use flushMe() to process.
Have a look at the indexX.php exemple pages to see how to use the classes, and the different types of processes.
If you want to use the index2.php you first have to create a 'tests' database, and use the test.sql file to insert the data.
class.noTimeOut.js
/**
@author : Johan Barbier <johan.barbier@gmail.com>
@Version : 2006/10/20
*/
function noTimeOut () {
var aStack = new Array;
var aStacks = new Array;
function getObject () {
if (window.XMLHttpRequest) {
var oXmlhttp = new XMLHttpRequest();
} else if (window.ActiveXObject) {
var oXmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
return oXmlhttp;
}
function addToData (sStack) {
var sData = '';
var iArgs = aStack[sStack]['ARGS'].length;
if ( iArgs > 0) {
for (var iCpt = 0; iCpt < iArgs; iCpt ++) {
sData += '&arg_'+iCpt+'='+aStack[sStack]['ARGS'][iCpt];
}
}
return sData;
}
function getDefault (sStack, iStart) {
var j = iStart + aStack[sStack]['STEP'];
aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
if (aStack[sStack]['OXMLHTTP'].readyState==1) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
}
}
if (aStack[sStack]['OXMLHTTP'].readyState==2) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
}
}
if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
}
/**
* DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
*
*/
if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
var oNode = parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
} else {
parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
}
/*
parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML += aStack[sStack]['OXMLHTTP'].responseText;
*/
if (j < aStack[sStack]['LIMIT']) {
aStacks.unshift (sStack);
aStack[sStack]['START'] = j;
checkStack ();
}
}
}
aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = 'sType=DEFAULT&iStart='+iStart+'&iStep='+STEP;
var iArgs = aStack[sStack]['ARGS'].length;
data += addToData (sStack);
aStack[sStack]['OXMLHTTP'].send (data);
}
function getDB (sStack, iStart) {
var j = iStart + aStack[sStack]['STEP'];
aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
if (aStack[sStack]['OXMLHTTP'].readyState==1) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
}
}
if (aStack[sStack]['OXMLHTTP'].readyState==2) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
}
}
if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
}
/**
* DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
*
*/
if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
var oNode = parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
} else {
parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
}
//parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML += aStack[sStack]['OXMLHTTP'].responseText;
if (j < aStack[sStack]['LIMIT']) {
aStacks.unshift (sStack);
aStack[sStack]['START'] = j;
checkStack ();
}
}
}
aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = 'sType=DB&iStart='+iStart+'&iStep='+aStack[sStack]['STEP']+'&sQuery='+aStack[sStack]['QUERY'];
data += addToData (sStack);
aStack[sStack]['OXMLHTTP'].send (data);
}
function getFile (sStack, iStart) {
var j = iStart + aStack[sStack]['STEP'];
aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
if (aStack[sStack]['OXMLHTTP'].readyState==1) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
}
}
if (aStack[sStack]['OXMLHTTP'].readyState==2) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
}
}
if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
}
/**
* DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
*
*/
if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
var oNode = parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
} else {
parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
}
/*
parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML += aStack[sStack]['OXMLHTTP'].responseText;
*/
if (j < aStack[sStack]['LIMIT']) {
aStacks.unshift (sStack);
aStack[sStack]['START'] = j;
checkStack ();
}
}
}
aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = 'sType=FILE_LINE&iStart='+iStart+'&iStep='+aStack[sStack]['STEP']+'&sFile='+aStack[sStack]['FILE'];
data += addToData (sStack);
aStack[sStack]['OXMLHTTP'].send (data);
}
function getFileLine (sStack, iStart) {
var j = iStart;
aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
if (aStack[sStack]['OXMLHTTP'].readyState==1) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
}
}
if (aStack[sStack]['OXMLHTTP'].readyState==2) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
}
}
if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
}
/**
* DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
*
*/
if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
var oNode = parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
} else {
parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
}
/*
parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML += aStack[sStack]['OXMLHTTP'].responseText;
*/
j += aStack[sStack]['OXMLHTTP'].responseText.length;
if (aStack[sStack]['SUBSTR'] != '') {
j -= aStack[sStack]['SUBSTR'];
}
if (j < aStack[sStack]['LIMIT']) {
aStacks.unshift (sStack);
aStack[sStack]['START'] = j;
checkStack ();
}
}
}
aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = 'sType=FILE_LINE&iStart='+iStart+'&iStep='+aStack[sStack]['STEP']+'&sFile='+aStack[sStack]['FILE'];
data += addToData (sStack);
aStack[sStack]['OXMLHTTP'].send (data);
}
function getFilePat (sStack, iStart) {
var j = iStart;
aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
if (aStack[sStack]['OXMLHTTP'].readyState==1) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
}
}
if (aStack[sStack]['OXMLHTTP'].readyState==2) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
}
}
if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById (aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_READY'];
}
/**
* DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
*
*/
if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
var oNode = parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
} else {
parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
}
/*
parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML += aStack[sStack]['OXMLHTTP'].responseText;
*/
j += aStack[sStack]['OXMLHTTP'].responseText.length;
if (aStack[sStack]['SUBSTR'] != '') {
j -= aStack[sStack]['SUBSTR'];
}
if (j < aStack[sStack]['LIMIT']) {
aStacks.unshift (sStack);
aStack[sStack]['START'] = j;
checkStack ();
}
}
}
aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
var data = 'sType=FILE_PATTERN&iStart='+iStart+'&iStep='+aStack[sStack]['STEP']+'&sFile='+aStack[sStack]['FILE'];
data += addToData (sStack);
aStack[sStack]['OXMLHTTP'].send (data);
}
function oneShot (sStack) {
aStack[sStack]['OXMLHTTP'].open(aStack[sStack]['METHOD'], aStack[sStack]['SCRIPT'], true);
aStack[sStack]['OXMLHTTP'].onreadystatechange=function() {
if (aStack[sStack]['OXMLHTTP'].readyState==1) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_WAITING'];
}
}
if (aStack[sStack]['OXMLHTTP'].readyState==2) {
if (aStack[sStack]['MSG'] != '') {
parent.document.getElementById(aStack[sStack]['MSG']).firstChild.data = aStack[sStack]['MSG_LOADED'];
}
}
if (aStack[sStack]['OXMLHTTP'].readyState==4 && aStack[sStack]['OXMLHTTP'].status == 200) {
/**
* DOM version, but it does not allow to read HTML tags, or, at least, I did not manage to do it
*
*/
/*
if (!parent.document.getElementById (aStack[sStack]['TARGET']).hasChildNodes()) {
var oNode = parent.document.createTextNode (aStack[sStack]['OXMLHTTP'].responseText);
parent.document.getElementById (aStack[sStack]['TARGET']).appendChild (oNode);
} else {
parent.document.getElementById (aStack[sStack]['TARGET']).firstChild.appendData (aStack[sStack]['OXMLHTTP'].responseText);
}
*/
parent.document.getElementById(aStack[sStack]['TARGET']).innerHTML = aStack[sStack]['OXMLHTTP'].responseText;
checkStack ();
}
}
aStack[sStack]['OXMLHTTP'].setRequestHeader("Content-type", "application/x-www-form-urlencoded");
data = addToData (sStack);
aStack[sStack]['OXMLHTTP'].send (data);
checkStack ();
}
function getData (sStack, sType) {
var bGo = false;
switch (sType) {
case 'DEFAULT':
getDefault (sStack, aStack[sStack]['START']);
break;
case 'FILE_OCTET':
getFile (sStack, aStack[sStack]['START']);
break;
case 'FILE_LINE':
getFileLine (sStack, aStack[sStack]['START']);
break;
case 'FILE_PATTERN':
getFilePat (sStack, aStack[sStack]['START']);
break;
case 'DB':
getDB (sStack, aStack[sStack]['START']);
break;
case 'ONE_SHOT':
oneShot (sStack);
break;
}
}
function checkStack () {
var iLen = aStacks.length;
if (iLen > 0) {
var sStack = aStacks[iLen - 1];
var sType = aStack[aStacks[iLen - 1]]['TYPE'];
aStacks.pop ();
getData (sStack, sType);
}
}
public function __construct () {
// might be useful later
}
public function __set ($sType, $sVal) {
try {
if (!array_key_exists ($sType, $this -> aProps)) {
throw new Exception ($sType.' is not a valid property');
}
} catch (Exception $e) {
$this -> aErrors[] = $e -> getMessage ();
}
try {
switch ($sType) {
case 'TYPE':
if (!in_array ($sVal, $this -> aTypes)) {
throw new Exception ($sVal.' is not a valid TYPE value');
}
break;
case 'FILE':
if (!file_exists ($sVal)) {
throw new Exception ('File '.$sVal.' has not been found');
}
break;
case 'DBSERVER':
if (!in_array ($sVal, $this -> aDbServers)) {
throw new Exception ('DB SERVER '.$sVal.' is not supported');
}
break;
default :
break;
}
$this -> aProps[$sType] = $sVal;
} catch (Exception $e) {
$this -> aErrors[] = $e -> getMessage ();
}
}
public function __get ($sType) {
try {
if (!array_key_exists ($sType, $this -> aProps) && $sType !== 'ERRORS') {
throw new Exception ($sType.' is not a valid property');
}
} catch (Exception $e) {
$this -> aErrors[] = $e -> getMessage ();
}
if ($sType === 'ERRORS') {
return $this -> aErrors;
} else {
return $this -> $sType;
}
}
private static function isNull () {
foreach (func_get_args() as $sArg) {
if (is_null ($sArg)) {
return false;
}
}
return true;
}
public function flushMe ($aWork = null) {
try {
if (is_null ($this -> aProps['TYPE'])) {
throw new Exception ('TYPE has not been defined');
}
} catch (Exception $e) {
$this -> aErrors[] = $e -> getMessage ();
}
try {
switch ($this -> aProps['TYPE']) {
case 'DB':
if (false === self::isNull ($this -> aProps['DB'], $this -> aProps['HOST'], $this -> aProps['LOGIN'], $this -> aProps['PWD'], $this -> aProps['QUERY'], $this -> aProps['DBSERVER'], $this -> aProps['START'], $this -> aProps['STEP'])) {
throw new Exception ('DB properties have not been fully defined');
}
$mTmp = $this -> getDB ();
break;
case 'FILE_OCTET':
if (false === self::isNull ($this -> aProps['FILE'], $this -> aProps['START'], $this -> aProps['STEP'])) {
throw new Exception ('FILE properties have not been fully defined');
}
$mTmp = $this -> getFileOctet ();
break;
case 'DEFAULT':
if (false === self::isNull ($this -> aProps['START'], $this -> aProps['STEP'], $aWork)) {
throw new Exception ('DEFAULT properties have not been fully defined');
}
$mTmp = $this -> getDefault ($aWork);
break;
case 'FILE_PATTERN':
if (false === self::isNull ($this -> aProps['FILE'], $this -> aProps['START'], $this -> aProps['STEP'])) {
throw new Exception ('FILE properties have not been fully defined');
}
$mTmp = $this -> getFilePat ();
break;
case 'FILE_LINE':
if (false === self::isNull ($this -> aProps['FILE'], $this -> aProps['START'], $this -> aProps['STEP'])) {
throw new Exception ('FILE properties have not been fully defined');
}
$mTmp = $this -> getFileLine ();
break;
}
return $mTmp;
} catch (Exception $e) {
$this -> aErrors[] = $e -> getMessage ();
}
}