1

由于mysql_*不推荐使用,我正在考虑一种简单的方法来替换所有不推荐使用的代码。

这是我的正则表达式;而是find我想要找到的,repl也是我想要替换它的。

$__db是我声明的mysqli_connect变量

Change MySQL into MySQLi
--
find: mysql_select_db\(([\$"a-zA-Z0-9_]+)\)
repl: \$__db->select_db($1)
--
find: mysql_fetch_object\((\$[a-zA-Z0-9_]+)\)
repl: $1->fetch_object()
--
find: mysql_fetch_array\((\$[a-zA-Z0-9_]+)\)
repl: $1->fetch_array()
--
find: mysql_num_rows\((\$[a-zA-Z0-9_]+)\)
repl: $1->num_rows
--
find: mysql_free_result\((\$[a-zA-Z0-9_]+)\)
repl: 
--
find: mysql_query
repl: \$__db->query
--
find: mysql_error\(\)
repl: mysqli_error\(\)
--
find: ([\$a-zA-Z0-9_]+) = mysql_result\(([\$a-zA-Z0-9_]+), (\d+)\)
repl: \$row = $2->fetch_array();\r\n$1 = \$row[$3]

我的问题是,我可以运行多个正则表达式替换(以便我可以同时替换所有代码)?

我知道我可以使用管道|来分隔查找部分,但它如何与替换部分一起使用?

我还没有找到在 Aptana Studio 3 中制作宏的方法。

4

1 回答 1

1

这成为我的解决方案,来自HamZa的提示(谢谢!)

我创建了一个脚本,它遍历选择的目录(在 中指定$dir

我们还可以通过以下方式说出我们在文件中要查找的内容$lookFor

我在代码中添加了一些注释,因此您可以按照我的操作进行操作。这不会解决function-db 的问题。

因此,如果您有带有 DB 连接的类,则必须为函数添加一些东西。

现在这个脚本不会改变你的文件(我把它注释掉了,所以你可以用它来浏览你的代码和“推荐”的改变)

而且..我还没有做到,所以脚本准备了语句。

(这是第二步,这只是为了修复mysql_*被移除时会损坏的东西)

结果将如下所示:(实际上显示了我谈到的缺少的功能..我必须添加global $__db;到每个function

在此处输入图像描述

然后终于!这是代码。

<?
/*IGNORE-MYSQL*/
function startsWith($haystack, $needle)
{
    return strpos($haystack, $needle) === 0;
}
function endsWith($haystack, $needle)
{
    return substr($haystack, -strlen($needle)) == $needle;
}
function doFlush()
{
    @flush();
    @ob_flush();
}

function runMySQLToMySQLi_replace($str, $useBoldShow = false, $replace = true)
{
    $catchVars = '\$"\'a-zA-Z0-9_\.\*\->,\[\] ';
    $regexTests = Array();
    $regexTests['(?:['.$catchVars.']+\s?=\s?)?mysql_connect\((['.$catchVars.']+),\s?(['.$catchVars.']+),\s?(['.$catchVars.']+)\)'] = '\$__db = mysqli_connect($1, $2, $3)';
    $regexTests['mysql_select_db\((['.$catchVars.']+)(?:,\s?['.$catchVars.']+)?\)'] = '\$__db->select_db($1)';
    $regexTests['mysql_query\((['.$catchVars.'\(\)=]+)(?:,\s?['.$catchVars.']+)?\)'] = '\$__db->query($1)';
    $regexTests['mysql_errno\(\)'] = '\$__db->errno';
    $regexTests['mysql_error\(\)'] = '\$__db->error';
    $regexTests['mysql_error\((['.$catchVars.']+)\)'] = '\$__db->error';
    $regexTests['mysql_fetch_object\((['.$catchVars.']+)\)'] = '$1->fetch_object()';
    $regexTests['mysql_fetch_row\((['.$catchVars.']+)\)'] = '$1->fetch_array()';
    $regexTests['mysql_fetch_array\((['.$catchVars.']+)\)'] = '$1->fetch_array()';
    $regexTests['mysql_fetch_assoc\((['.$catchVars.']+)\)'] = '$1->fetch_assoc()';
    $regexTests['mysql_num_rows\((['.$catchVars.']+)\)'] = '$1->num_rows';
    $regexTests['mysql_free_result\((['.$catchVars.']+)\)'] = '$1->free()';
    $regexTests['mysql_insert_id\(\)'] = '\$__db->insert_id';
    $regexTests['(['.$catchVars.']+) = mysql_result\((['.$catchVars.']+), (\d+)\)'] = "\$row = $2->fetch_array(); $1 = \$row[$3]";
    $tmpVal = $str;
    foreach($regexTests as $reg => $rep)
    {
        $match = preg_match("/" . $reg . "/i", $tmpVal);
        if($match)
        {
            if($replace)
                $tmpVal = preg_replace("/" . $reg . "/i", ($useBoldShow ? "[{b}]" : "") . $rep . ($useBoldShow ? "[/{b}]" : ""), $tmpVal);
            else
                $tmpVal = preg_replace("/" . $reg . "/i", ($useBoldShow ? "[{b}]" : "") . "$0" . ($useBoldShow ? "[/{b}]" : ""), $tmpVal);
        }
    }
    return $tmpVal;
}

?>
<html>
    <head>
        <style>
        body { margin: 0; padding: 0; }
        .mysql_found { background-color: mistyrose; padding: 10px; border: 1px solid #c9c9c9; border-radius: 5px; margin: 10px; }
        .no_select {
            -webkit-touch-callout: none;
            -webkit-user-select: none;
            -khtml-user-select: none;
            -moz-user-select: moz-none;
            -ms-user-select: none;
            user-select: none; 
            display: inline-block;
        }
        </style>
    </head>
    <body>
<pre><?
    // Directory to search in
    $dir = "/dir/to/search/in/";
    $objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($dir), RecursiveIteratorIterator::SELF_FIRST);

    // What we are looking for in all files
    $lookFor = "mysql_";
    foreach($objects as $name => $object)
    {
        // Ensure that it is PHP-files we're going through
        if(endsWith($object->getFilename(), '.php')) 
        {
            // Get all contents
            $contents = file_get_contents($object->getPathname());
            // Split it into rows
            $rowSplit = preg_split('/$\R?^/m', $contents);

            // Check the contents for $lookFor
            if(strpos($contents, $lookFor) > 0 && strpos($contents, "/*IGNORE-MYSQL*/") === false)
            {
                echo "<div class=\"mysql_found\">\"" . $lookFor . "\" found in: " . $object->getPathname() . "\n";
                echo "<hr noshade=\"noshade\" />\n";
                echo "Source code:\n";
                $lCount = 1;
                foreach($rowSplit as $row)
                {
                    echo "<div class=\"no_select\" unselectable=\"on\">" . str_pad($lCount++, strlen(count($rowSplit)), " ", STR_PAD_LEFT) . ": </div>" . str_replace(Array("[{b}]", "[/{b}]"), Array("<b style=\"background-color: #cfaaaa;\">", "</b>"), htmlentities(runMySQLToMySQLi_replace($row, true, false))) . "\n";
                }
                echo "\n\n";

                $lCount = 1;
                echo "Fixed code:<br /><br />";
                $doneCode = "";
                foreach($rowSplit as $row)
                {
                    echo "<div class=\"no_select\" unselectable=\"on\">" . str_pad($lCount++, strlen(count($rowSplit)), " ", STR_PAD_LEFT) . ": </div>" . str_replace(Array("[{b}]", "[/{b}]"), Array("<b style=\"background-color: #cfaaaa;\">", "</b>"), htmlentities(runMySQLToMySQLi_replace($row, true))) . "\n";
                    // This is the code that actually does the replacing.
                    $doneCode .= runMySQLToMySQLi_replace($row) . "\n";
                }
                // This is commented out, since I want to make sure it works before I accept some changes.
                // I actually tried it on 3 files without problems.
                //if(isset($_GET['Accepted']))
                //  file_put_contents($object->getPathname(), $doneCode);
                echo "</div>";
            }

        }
        doFlush();
    }
?></pre>
</body>
</html>

如果你想问我一些关于这段代码的事情,那就去做吧。:)

于 2013-08-07T14:56:22.937 回答