You are here: Re: [PHP] Getting queries from files FYI « PHP « IT news, forums, messages
Re: [PHP] Getting queries from files FYI

Posted by Jochem Maas on 08/23/05 18:16

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'] */);

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация