您的位置:首页 > 帝国cms教程

帝国cms 内容存文本转数据库处理方案修改优化版

2024-10-05 10:53:27 帝国cms教程 267

使用方法:

根目录建一个 index2.php的文件,把代码复制到里面去,在把数据库信息换成自己的:然后再浏览器打开点击操作就可以了:

重点:先备份 ,先备份,先备份

代码如下:

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);

@set_time_limit(1000000); // 设置超时时间,越长越好

function ReadFiletext($filepath) {
    $filepath = trim($filepath);
    if (!file_exists($filepath)) {
        throw new Exception("File not found: $filepath");
    }
    $string = file_get_contents($filepath);
    if ($string === false) {
        throw new Exception("Failed to read file: $filepath");
    }
    return $string;
}

function GetTxtFieldText($pagetexturl) {
    try {
        $text = ReadFiletext($pagetexturl);
        $text = substr($text, 12); // 去除exit
        return $text;
    } catch (Exception $e) {
        echo "Error: " . $e->getMessage() . "<br>";
        return false;
    }
}

// 配置数据库参数
$options = array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8');
try {
    $dbh = new PDO('mysql:host=数据库地址;dbname=cs', 'cs', '123456', $options);
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    die("Database connection failed: " . $e->getMessage());
}

// 数据表第一条信息的ID
try {
    $stmt = $dbh->prepare("SELECT id FROM phome_ecms_article ORDER BY id ASC LIMIT 1");
    $stmt->execute();
    $num1 = $stmt->fetchColumn();
} catch (PDOException $e) {
    die("Query failed: " . $e->getMessage());
}

// 数据表最后一条信息的ID
try {
    $stmt = $dbh->prepare("SELECT id FROM phome_ecms_article ORDER BY id DESC LIMIT 1");
    $stmt->execute();
    $num2 = $stmt->fetchColumn();
} catch (PDOException $e) {
    die("Query failed: " . $e->getMessage());
}

if (isset($_POST['start'])) {
    $start = $_POST['start'];
    $end = $start + 29999;
    if ($end > $num2) {
        $end = $num2;
    }

    $updateStmt = $dbh->prepare("UPDATE phome_ecms_article SET newstext1 = :text WHERE id = :id");

    // 批量处理
    $batchSize = 100; // 每次处理100条数据
    $batchUpdateData = [];

    for ($i = $start; $i <= $end; $i++) {
        try {
            $stmt = $dbh->prepare("SELECT newstext FROM phome_ecms_article WHERE id = :id");
            $stmt->bindParam(':id', $i, PDO::PARAM_INT);
            $stmt->execute();
            $text = $stmt->fetchColumn();

            // 判断是否是存文本的信息
            if (strlen($text) == 42 && preg_match("/^[0-9a-z\d\/]*$/i", $text)) {
                $pagetexturl = "d/txt/" . $text . ".php"; // 因为帝国存文本中有exit中断,所以需要用读文件的方法去读取代码,文本的路径要正确,存在本文件所以目录下的子目录"d/txt/"下
                $newText = GetTxtFieldText($pagetexturl);

                if ($newText !== false) {
                    $batchUpdateData[] = [
                        'id' => $i,
                        'text' => $newText
                    ];

                    // 达到批量处理的大小,执行批量更新
                    if (count($batchUpdateData) >= $batchSize) {
                        batchUpdate($dbh, $updateStmt, $batchUpdateData);
                        $batchUpdateData = []; // 清空数据
                    }
                }
            }
        } catch (PDOException $e) {
            echo "Error processing ID $i: " . $e->getMessage() . "<br>";
        }
    }

    // 处理剩余的数据
    if (!empty($batchUpdateData)) {
        batchUpdate($dbh, $updateStmt, $batchUpdateData);
    }

    if ($end == $num2) {
        echo "OK,已完成!";
    } else {
        echo "已处理完 $end 条数据,继续处理下一批数据";
        echo "<form method='post' action='index2.php'>";
        echo "<input type='hidden' name='start' value='$end'>";
        echo "<input type='submit' name='submit' value='继续处理'>";
        echo "</form>";
    }
} else {
    echo "<form method='post' action='index2.php'>";
    echo "<input type='hidden' name='start' value='$num1'>";
    echo "<input type='submit' name='submit' value='从 $num1 开始处理'>";
    echo "</form>";
}

function batchUpdate($dbh, $updateStmt, $batchUpdateData) {
    $dbh->beginTransaction();
    try {
        foreach ($batchUpdateData as $data) {
            $updateStmt->bindParam(':text', $data['text'], PDO::PARAM_STR);
            $updateStmt->bindParam(':id', $data['id'], PDO::PARAM_INT);
            $updateStmt->execute();
        }
        $dbh->commit();
    } catch (PDOException $e) {
        $dbh->rollBack();
        echo "Batch update failed: " . $e->getMessage() . "<br>";
    }
}

相关推荐