Re: Getting queries from files FYI

From: Date: Tue, 23 Aug 2005 15:16:27 +0000
Subject: Re: Getting queries from files FYI
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-221173@lists.php.net to get a copy of this message
Jay Blanchard wrote:
You may (or may not) remember me posting to the list a couple of weeks ago asking about using REGEX to get queries out of PHP files for a migration project. I had to let it go for several days, but started working on it again yesterday, here is the code (no REGEX was used in the making of this code);
heh Jay, I have been playing with this ... I rewrote your code abit (including removing the check for the string 'CHA' - cos that was limiting my search too much ;-) I made it recursive + a few other tweaks, haven't look into the proper guts of 'grep' action yet - would be nice to clean that up a little. anyway another handy tool in the bag :-) here is the code btw: <?php /* * Query Finder * Jay Blanchard * August 2005 * NOT REALLY TOO EXTENSIBLE *
* usage:    call from command line, perform manual output to text file
*           i.e. php qryfind.php > <nameOfFileToSave.txt>
*/ /* cruise the directory looking for PHP files */ function findTheQueries($theDirectory) {
    static $arrQueryStarters, $arrQueryStartersCnt, $dirSep;
    if (!isset($arrQueryStarters)) {
        $arrQueryStarters       = array('SELECT ', 'INSERT ', 'UPDATE ', 'FROM ',
'EXECUTE ', 'WHERE ', 'ORDER BY ', 'LEFT JOIN ');
        $arrQueryStartersCnt    = count($arrQueryStarters);
       // Determine OS specific settings
        $uname = php_uname();
        if (substr($uname, 0, 7) == "Windows") {
            $dirSep = "\\";
        } else if (substr($uname, 0, 3) == "Mac") {
            $dirSep = "/";
        } else {
            $dirSep = "/";
        }
    }
    if (is_dir($theDirectory)) {
        echo "Searching for queries in php files in: $theDirectory\n";
        if ($dh = opendir($theDirectory)) {
            while (($theFile = readdir($dh)) !== false) {
                /* recurse subdirs */
                if (is_dir($theDirectory.$dirSep.$theFile)) {
                    if ($theFile != '.' && $theFile != '..') {
                        findTheQueries($theDirectory.$dirSep.$theFile);
                    }
                    continue;
                }
                /* we only want to look at PHP files */
                $fileParts = array_reverse(explode('.', $theFile));
                if("php" == $fileParts[0]){
                    /* always echo the file name, even if no queries */
                    echo "Filename: {$theDirectory}{$dirSep}{$theFile}\n";
                    $lineNo = 0;
                    /* cruise the file looking for queries */
                    $openFile = fopen($theDirectory.$dirSep.$theFile, "r");
                    while(!feof($openFile)){
                        $fileLine = fgets($openFile, 4096);
                        $lineNo++;
                        /* loop through query starter array */
                        for($i = 0; $i < $arrQueryStartersCnt; $i++){
                            if(strstr($fileLine, $arrQueryStarters[$i]) /* && strstr($fileLine, 'CHA') */){
                                echo "Line Number: " . $lineNo . " " .  $fileLine;
// if we find a line no need to find it again // because it contains more than one keyword.
                                break;
                            }
                        }
                    }
                    fclose($openFile);
                }
            }
            closedir($dh);
        } else {
            echo "Could not open: $theDirectory\n";
        }
    } else {
        echo "Bad directory: $theDirectory\n";
    }
} /* which directory will we be opening? this one, of course */ findTheQueries(getcwd() /* $_SERVER['PWD'] */);

« previous php.general (#221173) next »