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!


    Role of Integrated Requirements Management in Software Delivery

    As organizations integrate software into every aspect of business, they are constantly pressured to deliver faster, better, and cheaper results. Unfortunately, a “dis-integrated” software delivery approach reduces returns while increasing costs. This IBM Rational White Paper shows how Integrated Requirements Management aligns organizations around maximizing value and keeping pace with change.
    FREE! Go There Now!


    NEW! Best Practices: The Integrated Project and Portfolio Management Platform.

    Hear how IBM Rational Project and Portfolio Management integrated solutions help teams put the right tools and processes in place to maximize the effectiveness and efficiency of project teams and ensure that the business vision is being executed correctly. Learn how to automate and integrate requirements prioritization, top-down project planning, communications and controls, and methodology deployment to keep your scope, costs, and schedules under control. Tackle with an end-to-end approach the management of scope and scope changes, usage of methodology to control and empower project teams, and optimization of resources to align activity costs with the overall project plan.
    FREE! Go There Now!


    NEW! Rational Talks to You: Scott Ambler on being agile in a global development environment

    Join this Rational Talks to You teleconference on December 6 at 1:00 pm ET to participate in an agile application development discussion and get your questions answered on using IBM Rational Method Composer in a distributed environment.Get your questions answered!
    FREE! Go There Now!


    NEW! Try IBM Rational Asset Manager V7.0 online!

    You can now evaluate IBM Rational Asset Manager V7.0 online without installing or configuring it on your own system! Rational Asset Manager helps create, modify, govern, find, and reuse any type of development assets, including SOA and systems development assets. Rational Asset Manager helps you reduce software development costs and improve quality by facilitating the reuse of all types of software development-related assets. Visit developerWorks to learn more about this product and register to explore its capabilities online.
    FREE! Go There Now!


    NEW! Section 508 of the U.S. Rehabilitation Act: Web accessibility compliance

    Because access to government information continues to be an area of concern for many U.S. citizens with disabilities, the U.S. government enacted Section 508 of the Rehabilitation Act in 2001 to ensure that government agencies create accessible Web content, enabling all citizens to access the information they need. A fully accessible Web site makes Web content accessible to all individuals, including those with disabilities, who may be accessing Web content via a variety of user agents. Common user agents include standard Web browsers, text-only browsers, assistive devices and mobile devices such as cell phones or personal digital assistants (PDAs).
    FREE! Go There Now!


    NEW! IBM Rational ClearCase Innovator's Series

    Learn from the best! Find out how developers use Rational ClearCase to be more flexible, innovative and deliver higher quality code in the Rational ClearCase Power Users eKit. This complimentary eKit provides a collection of materials, like articles, whitepapers, and demos that can help you become a power user of Rational ClearCase.
    FREE! Go There Now!


    NEW! Innovate don't duplicate! Asset reuse strategies for success

    Asset Reuse is a key strategy for companies looking to create innovative solutions to solve complex software development problems. Searching for, identifying, updating, using and deploying software assets can be a difficult challenge. Listen to this webcast, to learn about strategies and tools that you can leverage for a successful project, including Rational Asset Manager, Rational Software Architect and WebSphere Service Registry and Repository.
    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! Rational Testing eKits

    Discover how Rational tools and best practices for testing can make your job easier. The new Rational Testing eKits provide you with valuable resources – including demos, webcasts, tutorials, and articles – that help you address your specific testing needs across the software lifecycle. Five new eKits are available covering the topics of Requirements and Test Management, Functional Testing, Performance Testing, Code Quality and Embedded Systems, and SOA and Web Services Testing.
    FREE! Go There Now!


    NEW! The role of integrated requirements management in software delivery

    This paper is about the critical role that a discipline called integrated require­ments management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrat­ing, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way.
    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
    Stay green...Green IT