Miscellaneous Code
  Home arrow Miscellaneous Code arrow [PHP5] NOTIMEOUT PACKAGE
Codewalker Forums 
  Tutorials  
Database Articles  
Miscellaneous  
Navigation Usability  
PEAR Articles  
Programming Basics  
Server Administration  
XML Tutorials  
  Reviews  
Database Book Reviews  
Linux Book Reviews  
Miscellaneous Reviews  
PHP Book Reviews  
PHP Software Reviews  
Server Admin Reviews  
SQL Tool Reviews  
  Code Gallery  
Content Management Code  
Contest Code  
Counters Code  
Database Code  
Date Time Code  
Discussion Board Code  
Email Code  
File Manipulation Code  
GUI Code  
Link Farm Code  
Miscellaneous Code  
Search Code  
Site Navigation Code  
User Management Code  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Download TestComplete 
Forums Sitemap 
Weekly Newsletter 
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
MISCELLANEOUS CODE

[PHP5] NOTIMEOUT PACKAGE
By: Codewalkers
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2006-10-09

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    2006/10/24
    - Fixed the JS TIMEOUT bug. Now, it works perfectly.
    2006/10/20
    - Added multiple threads handling via stacks.
    - Added onsShot method, and ARGS property
    noTimeOut package
    @author : Johan Barbier
    @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.








    By : malalam

    <?php
    /**
    @author : Johan Barbier <johan.barbier@gmail.com>
    @Version : 2006/10/09
    */

    class noTimeOut {
    private $aProps = array (
    'TYPE' => null,
    'DB' => null,
    'HOST' => null,
    'LOGIN' => null,
    'PWD' => null,
    'QUERY' => null,
    'DBSERVER' => null,
    'FILE' => null,
    'START' => null,
    'LIMIT' => null,
    'STEP' => null
    );

    private $aDbServers = array (
    'MYSQL', 'MSSQL'
    );

    private $aTypes = array (
    'DEFAULT', 'FILE_OCTET', 'FILE_PATTERN', 'FILE_LINE', 'DB'
    );


    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) {
    echo $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) {
    echo $e -> getMessage ();
    }
    }

    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) {
    echo $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) {
    echo $e -> getMessage ();
    }
    }

    private function getFilePat () {
    $sTmp = '';
    try {
    if (false === ($fp = fopen ($this -> aProps['FILE'], 'r'))) {
    throw new Exception ('Failed to open file : '.$this -> aProps['FILE']);
    }
    if ( -1 === (fseek ($fp, $this -> aProps['START'], SEEK_SET))) {
    throw new Exception ('Failed to modify cursor on : '.$this -> aProps['FILE']);
    }
    while (false === ($iEnd = strpos ($sTmp, $this -> aProps['STEP'])) && !feof ($fp)) {
    $sTmp .= @fgets ($fp, 1024);
    }
    $sTmp = substr ($sTmp, 0, $iEnd + strlen ($this -> aProps['STEP']));
    @fclose ($fp);
    } catch (Exception $e) {
    echo $e -> getMessage ();
    }
    return $sTmp;
    }

    private function getDefault ($aWork) {
    try {
    if (!is_array ($aWork)) {
    throw new Exception ('Parameter must be an array');
    }
    $aTmp = array ();
    for ($i = $this -> aProps['START']; $i < $this -> aProps['START'] + $this -> aProps['STEP']; $i ++) {
    if (isset ($aWork[$i])) {
    $aTmp[] = $aWork[$i];
    }
    }
    return $aTmp;
    } catch (Exception $e) {
    echo $e -> getMessage ();
    }
    }

    private function getFileOctet () {
    try {
    $sTmp = '';
    if (false === ($fp = fopen ($this -> aProps['FILE'], 'r'))) {
    throw new Exception ('Failed to open file : '.$this -> aProps['FILE']);
    }
    if ( -1 === (@fseek ($fp, $this -> aProps['START'], SEEK_SET))) {
    throw new Exception ('Failed to modify cursor on : '.$this -> aProps['FILE']);
    }
    if (false === ($sTmp .= @fread ($fp, $this -> aProps['STEP']))) {
    throw new Exception ('Failed to read file : '.$this -> aProps['FILE']);
    }
    @fclose ($fp);
    return $sTmp;
    } catch (Exception $e) {
    echo $e -> getMessage ();
    }
    }

    private function getFileLine () {
    $sTmp = '';
    try {
    if (false === ($fp = fopen ($this -> aProps['FILE'], 'r'))) {
    throw new Exception ('Failed to open file : '.$this -> aProps['FILE']);
    }
    if ( -1 === (fseek ($fp, $this -> aProps['START'], SEEK_SET))) {
    throw new Exception ('Failed to modify cursor on : '.$this -> aProps['FILE']);
    }
    $iEnd = 0;
    while ($iEnd < $this -> aProps['STEP']) {
    $sTmp .= @fgets ($fp);
    $iEnd ++;
    }
    @fclose ($fp);
    } catch (Exception $e) {
    echo $e -> getMessage ();
    }
    return $sTmp;
    }

    private function getDB () {
    $sDb = strtolower ($this -> aProps['DBSERVER']);
    try {
    $rLink = @call_user_func ($sDb.'_connect', $this -> aProps['HOST'], $this -> aProps['LOGIN'], $this -> aProps['PWD']);
    if (false === $rLink) {
    throw new Exception ('Failed to connect to host : '.$this -> aProps['HOST']);
    }
    if (false === (@call_user_func ($sDb.'_select_db', $this -> aProps['DB'], $rLink))) {
    throw new Exception ('Failed to select database : '.$this -> aProps['DB']);
    }
    if (false === ($rRes = @call_user_func ($sDb.'_query', $this -> aProps['QUERY'], $rLink))) {
    throw new Exception ('Query failed : '.$this -> aProps['QUERY']);
    }
    if (false === (@call_user_func ($sDb.'_data_seek', $rRes, $this -> aProps['START']))) {
    throw new Exception ('Query failed : '.$this -> aProps['QUERY']);
    }
    $iCpt = 0;
    $aTmp = array ();
    while (($aRes = call_user_func ($sDb.'_fetch_assoc', $rRes)) && $iCpt < $this -> aProps['STEP']) {
    $aTmp[] = $aRes;
    $iCpt ++;
    }
    @call_user_func ($sDb.'_close', $rLink);
    return $aTmp;
    } catch (Exception $e) {
    echo $e -> getMessage ();
    }
    }
    }
    ?>

    Click to Download File



    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

    More Miscellaneous Code Articles
    More By Codewalkers

     

    IBM® developerWorks developerWorks - FREE Tools!


    NEW! Driving Business Success with Rational Process Library

    Join this webcast, to learn how the Rational Process Library can help with compliance issues, drive process improvement, and assist in service-oriented architecture (SOA) or Agile development. We will take a peek into the Rational Process Library with content around software and systems engineering (including RUP), operations and systems management, program and portfolio management, and asset and SOA governance.
    FREE! Go There Now!


    NEW! Accelerating Software Innovation on i on Power Systems

    Attend this launch webcast with Scott Hebner, Vice President of IBM Rational Marketing and Strategy, for an overview of Rational’s new software offerings and resources to help modernize and accelerate software innovation on i on Power Systems – while ensuring past application investments are protected and continue to grow. Learn how these solutions are helping customers extend their core i5/OS solutions toward modern architectures such as SOA and web technologies to deliver business improvements that stand the test of time.
    FREE! Go There Now!


    NEW! Did you say mainframe? e-kit

    Learn how you can extend modern application lifecycle management to IBM System z through the IBM Rational Software Delivery Platform (SDP). The Did you say mainframe? e-kit includes podcasts, webcasts, tutorials, white and red papers, demos, and articles designed to help ease the challenges of modernizing your enterprise. This complimentary kit for mainframe developers is a practical, how-to guide for making the most of an existing development environment, including the skills and infrastructure already in place at an established enterprise.
    FREE! Go There Now!


    NEW! Discovering the value of WebSphere Process Server

    WebSphere Process Server delivers a unique integration framework that simplifies existing IT resources. Often, as IT assets grow to support business demand, so too does their complexity and manageability. In this webcast, we’ll discuss how WebSphere Process Server helps deliver an SOA infrastructure that provides a common model to orchestrate, mediate, connect, map, and execute the underlying IT functions. Discover how WebSphere Process Server simplifies integration of business processes by leveraging existing IT assets as reusable services without the complexities of traditional integration methodologies.
    FREE! Go There Now!


    NEW! Evaluate WebSphere Extended Deployment Compute Grid V6.1

    Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points.
    FREE! Go There Now!


    NEW! Harnessing the power of SQL and Java for high performance data access

    Join this webcast to see how IBM Data Studio Developer and pureQuery can take the pain out of Java data access. uApplications developed using both Java and SQL have become a common requirement. Database connectivity using Java Database Connectivity (JDBC) to create an application is a multi-step tedious process, and tooling that covers both SQL and Java has been unavailable, until now. IBM Data Studio introduces the pureQuery platform: a high-performance, Java data access platform focused on simplifying the tasks of developing, managing, and optimizing database applications and services.
    FREE! Go There Now!


    NEW! Rational Asset Manager eKit

    Learn how to do more with your reusable assets with the free Rational Asset Manager eKit. The eKit includes demos on how Rational Asset Manager tracks and audits your assets in order to utilize them for reuse. Plus you’ll find white papers and a Webcast that discuss the challenges of a Service Oriented Architecture and how Rational Asset Manager can provide quick and effective solutions.
    FREE! Go There Now!


    NEW! Try the IBM SOA Sandbox for People

    Visit IBM developerWorks to try the IBM SOA Sandbox for people. The SOA Sandbox for people provides a trial environment with the necessary tooling and components required to enable consistent human and process interaction and collaboration, showing how you can improve user experience and business productivity.
    FREE! Go There Now!


    NEW! Webcast: Extreme transaction processing with WebSphere Extended Deployment

    In this webcast, you'll get an introduction to the eXtreme Transaction Processing (XTP) features of WebSphere Extended Deployment and the common architectural traits required by XTP applications. See how WebSphere Extended Deployment's ObjectGrid feature provides a state-of-the-art infrastructure for hosting XTP applications.
    FREE! Go There Now!


    Refresh! IBM Rational Systems Development Solution eKit

    With IBM Rational Systems Development Solution, you can deliver products faster with higher quality. Within this kit, Read the “Model Driven Systems Development” white paper to see how to improve product quality and communication. Then check out the rest of the e-Kit to learn more about important topics that can affect the success of any software project through customer examples, tutorials, informative Webcasts, and best practices for designing, building and managing systems. From start to finish, at every stage in your projects, Rational Systems Development Solution can help your company reach its full potential.
    FREE! Go There Now!



    All FREE IBM® developerWorks Tools!

    MISCELLANEOUS CODE ARTICLES

    - A Web App Based on a Model for the CodeIgnit...
    - Completing a Model for the CodeIgniter PHP F...
    - Validating Input Data with the CodeIgniter P...
    - Deleting Database Records with the CodeIgnit...
    - Inserting Database Records with a CodeIgnite...
    - Fetching Database Rows with a Model for the ...
    - Model Data and Validation Rules for a Generi...
    - Building a Generic Model for the CodeIgniter...
    - upload image to database sql
    - Random Password Generator
    - BCroot, get the root of a number with BC fun...
    - Find pi in a high precision
    - [PHP5] FORMCHECKER : data validation
    - SPL and ITERATOR : examples
    - Xml with Rss Feeds





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek