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


使用方法:

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

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

代码如下:

<?phperror_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());}// 数据表第一条信息的IDtry { $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());}// 数据表最后一条信息的IDtry { $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>"; }}

上一篇:wordpress后台发文章为什么会很卡?为什么WordPress发布文章如此缓慢

下一篇:帝国cms列表页模板自动把标题图片生成缩放图并调用出来


存文本
Copyright © 2002-2019 测速网 https://www.inhv.cn/ 皖ICP备2023010105号 城市 地区 街道
温馨提示:部分文章图片数据来源与网络,仅供参考!版权归原作者所有,如有侵权请联系删除!
热门搜索