Record the live legacy article handler baseline

This commit is contained in:
TTC operations committed 2026-11-10 15:20:50 +08:00
1 parent 0a99190125
commit ecd7105bee
1 file changed
+49 -97
+49 -97
View File
@@ -2,6 +2,9 @@
namespace App\Http\Controllers\Api;
use App\Services\Ranking\ArticleDocument;
use App\Services\Ranking\SitePublisher;
use App\Services\Ranking\WeeklyRanking;
use App\Services\TomTool\Flow;
use App\Services\TomTool\HttpV2;
use App\Services\TomTool\Telegram\Slave as TeleSlave;
@@ -39,18 +42,20 @@ class ArticleController
$this->sKey = $aArg["key"] ?? '';
$this->aConfig = $aArg["config"] ?? '';
if ($this->sCmd == "del") {
$this->cmd_del();
} else if ($this->sCmd == "new") {
$this->cmd_new();
} else {
$sMsg = "未找到cmd的对应处理:".$this->sCmd;
echo $sMsg;
throw new \Exception($sMsg);
}
$this->_npmBuild();
$publisher = new SitePublisher(dirname(base_path()), env('vue_path_node', 'node'));
$publisher->locked(function () use ($publisher) {
if ($this->sCmd == "del") {
$this->cmd_del();
} elseif ($this->sCmd == "new") {
$this->cmd_new();
} else {
throw new \Exception("未找到cmd的对应处理:".$this->sCmd);
}
// Updating availability never changes this week's membership or score.
(new WeeklyRanking($publisher))->renderCurrent();
$publisher->build();
});
$arr["aNewPath"] = $this->aNewPath;
echo json_encode($oFlow->setDataA($arr)->done()->getResult());
@@ -71,8 +76,14 @@ class ArticleController
{
$aCategory = $this->aConfig["category"] ?? ["⚠️ 未得到category"];
if (!preg_match('/^[A-Za-z0-9_:-]+$/', $this->sArticleCode)) {
throw new \RuntimeException('文章编号格式异常');
}
foreach ($aCategory as $sCategoryRow) {
$sFileDir = "../../vuepress/docs/{$sCategoryRow}/";
if (!is_string($sCategoryRow) || $sCategoryRow === '' || preg_match('~[\\\\/]|^\.{1,2}$~', $sCategoryRow)) {
throw new \RuntimeException('文章分类格式异常');
}
$sFileDir = dirname(base_path())."/vuepress/docs/{$sCategoryRow}/";
$sFileName = $this->sArticleCode.".md";
$sFilePath = $sFileDir.$sFileName;
@@ -92,94 +103,35 @@ class ArticleController
}
private function _npmBuild()
{
$sVuePathNode = $_ENV['vue_path_node'];
$sVuePath = base_path();
$sVuePath = dirname($sVuePath);
$sVuePath .= "/vuepress";
$sCmd = "cd {$sVuePath} && {$sVuePathNode} ./node_modules/.bin/vuepress build docs";
exec($sCmd . " 2>&1", $output, $returnVar);
$logFile = $sVuePath . "/build.log";
file_put_contents($logFile, implode("\n", $output) . "\n", FILE_APPEND);
if ($returnVar === 0) {
// echo " - VuePress 构建成功!静态文件已生成在 docs/.vuepress/dist\n";
} else {
$sMsg = "VuePress构建失败,请查看日志: ".json_encode($output);
throw new \Exception($sMsg);
}
}
public function cmd_new()
{
$aCategory = $this->aConfig["category"] ?? ["⚠️ 未得到category"];
$aTag = $this->aConfig["tag"] ?? ["⚠️ 未得到tag"];
$sTitle = $this->aArg["article"]["title"] ?? "⚠️ 未得到title";
$sContent = $this->aArg["article"]["content"] ?? "⚠️ 未得到content";
$sEnter = "
";
list(, $iCode) = explode("-", $this->sArticleCode);
foreach ($aCategory as $sCategoryRow) {
$sHead = "---";
$sHead .= $sEnter;
$sHead .= "title: ".$sTitle;
$sHead .= $sEnter;
$sHead .= "tags: ";
$sHead .= $sEnter;
foreach ($aTag as $sTagRow){
$sHead .= " - ".$sTagRow;
$sHead .= $sEnter;
}
$sHead .= "createTime: ".$this->sDate;
$sHead .= $sEnter;
$sTmp = "/{$sCategoryRow}/".$iCode."/";
$this->aNewPath[] = $sTitle." -> new https://".$_ENV["APP_URL"].$sTmp;
$sHead .= "permalink: ".$sTmp;
$sHead .= $sEnter;
$sHead .= "---";
$sArticleFull = $sHead;
$sArticleFull .= $sContent;
$sFileDir = "../../vuepress/docs/{$sCategoryRow}/";
$sFileName = $this->sArticleCode.".md";
$sFilePath = $sFileDir.$sFileName;
if (!file_exists($sFileDir)) {
if (mkdir($sFileDir, 0777, true)) {
// $oFlow->step("文件夹创建成功:$sFileDir\n");
} else {
$sMsg = "创建失败,请检查权限。\n";
echo $sMsg;
throw new \Exception($sMsg);
}
}
$fileHandle = fopen($sFilePath, 'w');
if ($fileHandle === false) {
$sMsg = "错误:无法打开或创建文件 $sFilePath。";
echo $sMsg;
throw new \Exception($sMsg);
} else {
fwrite($fileHandle, $sArticleFull);
fclose($fileHandle);
}
$categories = $this->aConfig['category'] ?? [];
$tags = $this->aConfig['tag'] ?? [];
$title = $this->aArg['article']['title'] ?? '⚠️ 未得到title';
$content = $this->aArg['article']['content'] ?? '⚠️ 未得到content';
$parts = explode('-', $this->sArticleCode, 2);
if (count($parts) !== 2 || !preg_match('/^[A-Za-z0-9_:-]+$/', $this->sArticleCode)) {
throw new \RuntimeException('文章编号格式异常');
}
foreach ($categories as $category) {
if (!is_string($category) || $category === '' || preg_match('~[\\\\/]|^\.{1,2}$~', $category)) {
throw new \RuntimeException('文章分类格式异常');
}
$path = dirname(base_path()).'/vuepress/docs/'.$category.'/'.$this->sArticleCode.'.md';
$existing = is_file($path) ? ArticleDocument::read($path)['meta'] : [];
$meta = $existing;
$meta['title'] = $title;
$meta['baseTitle'] = \App\Services\Ranking\Score::baseTitle($title);
$meta['tags'] = $tags;
$meta['createTime'] = $existing['createTime'] ?? $this->sDate;
$meta['permalink'] = $existing['permalink'] ?? '/'.$category.'/'.$parts[1].'/';
$meta['articleCode'] = $this->sArticleCode;
$document = ArticleDocument::enrich(['meta' => $meta, 'body' => $content]);
ArticleDocument::write($path, ArticleDocument::render($document));
$this->aNewPath[] = $document['meta']['title'].' -> new https://'.$_ENV['APP_URL'].$meta['permalink'];
}
}
}
//---