Skip to content

Commit 7bf29a8

Browse files
committed
修正Actions脚本推送diff读取和检测条件判定bug,新增主文件直链读取支持,根据上次检测日志清理无效外部插件条目
1 parent a35f5b7 commit 7bf29a8

File tree

2 files changed

+134
-135
lines changed

2 files changed

+134
-135
lines changed

AUTO-UPDATE.php

Lines changed: 117 additions & 94 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111
$requestUrl = $argv[2] ?? '';
1212

1313
//提取最近变更信息
14-
$urls = [];
14+
$urls = $requestUrl ? [] : ['cron'];
1515
if (str_contains($requestUrl, '.diff')) {
1616
$record = file_get_contents($requestUrl);
1717
$diffs = explode(PHP_EOL, $record);
1818

1919
//查找有关文档变更
20-
$begin = array_search('+++ b/README.md', $diffs) ?? (array_search('+++ b/TESTORE.md', $diffs) ?? 0);
20+
$begin = array_search('+++ b/README_test.md', $diffs) ?: array_search('+++ b/TESTORE.md', $diffs) ?: 0;
2121
foreach ($diffs as $line => $diff) {
2222
if ($line > $begin) {
2323
//匹配变更行repo信息
@@ -28,7 +28,7 @@
2828
}
2929
}
3030
//至非文档部分跳出
31-
if (preg_match('/^diff --git a\/(?!README\.md|TESTORE\.md).*/', $diff)) {
31+
if (preg_match('/^diff --git a\/(?!README_test\.md|TESTORE\.md).*/', $diff)) {
3232
break;
3333
}
3434
}
@@ -40,15 +40,15 @@
4040

4141
//检测文档执行更新
4242
$movable = [];
43-
if (file_exists('README.md')) {
44-
$movable = updatePlugins('README.md', $urls, $authKey);
43+
if (file_exists('README_test.md')) {
44+
$movable = updatePlugins('README_test.md', $urls, $authKey);
4545
} else {
4646
throw new RuntimeException('README.md is missing!');
4747
}
4848
if (file_exists('TESTORE.md')) {
4949
$movable = updatePlugins('TESTORE.md', $urls, $authKey, $movable);
5050
if ($movable) {
51-
updatePlugins('README.md', $urls, 'rec', $movable); //rec情况递归
51+
updatePlugins('README_test.md', $urls, 'rec', $movable); //rec情况递归
5252
}
5353
} else {
5454
throw new RuntimeException('TESTORE.md is missing!');
@@ -68,7 +68,7 @@ function updatePlugins(string $tableFile, array $requested, string $token = '',
6868
//预设出循环变量
6969
$logs = '-------' . $tableFile . '-------' . PHP_EOL . date('Y-m-d', time()) . PHP_EOL;
7070
$descriptions = [];
71-
$tf = $tableFile == 'README.md';
71+
$tf = $tableFile == 'README_test.md';
7272
$all = 0;
7373
$revise = 0;
7474
$creat = 0;
@@ -77,10 +77,12 @@ function updatePlugins(string $tableFile, array $requested, string $token = '',
7777
$release = 0;
7878
$done = 0;
7979
$nameList = 'ZIP_CDN/NAME_LIST.log';
80-
$listConent = file_exists($nameList) ? explode('README.md ALL' . PHP_EOL, trim(file_get_contents($nameList))) : [];
80+
$listConent = file_exists($nameList)
81+
? explode('README_test.md ALL' . PHP_EOL, trim(file_get_contents($nameList)))
82+
: [];
8183
$listNames = $listConent ? explode(PHP_EOL, $listConent[0]) : [];
8284
$movable = [];
83-
$allNames = $tf ? ['README.md ALL'] : (isset($listConent[1]) ? explode(PHP_EOL, $listConent[1]) : []);
85+
$allNames = $tf ? ['README_test.md ALL'] : (isset($listConent[1]) ? explode(PHP_EOL, $listConent[1]) : []);
8486
$tables = [];
8587
$normal = $token && $token !== 'rec';
8688

@@ -128,13 +130,15 @@ function updatePlugins(string $tableFile, array $requested, string $token = '',
128130
if ($name) {
129131
preg_match_all('/(?<=\()[^)]*/', $column, $links);
130132
$url = $links && str_contains($nameMeta, '](') ? trim($links[0][0]) : ''; //取第一个栏位链接内容
131-
$github = parse_url($url, PHP_URL_HOST) == 'github.com';
132-
//处理文档变更或指定插件
133-
if ($requested = array_filter($requested)) {
134-
$condition = in_array($url, $requested) || in_array($name, $requested);
135-
//定期处理全GitHub源插件
136-
} else {
133+
$host = parse_url($url, PHP_URL_HOST);
134+
$github = $host == 'github.com';
135+
$condition = false;
136+
//定期处理全GitHub源插件
137+
if ($requested == ['cron']) {
137138
$condition = $github;
139+
//处理文档变更或指定插件
140+
} elseif ($requested = array_filter($requested)) {
141+
$condition = in_array($url, $requested) || in_array($name, $requested);
138142
}
139143

140144
//处理表格作者名
@@ -189,105 +193,113 @@ function updatePlugins(string $tableFile, array $requested, string $token = '',
189193
if ($condition && $token) {
190194
++$all; //记录检测次数
191195

192-
//提取子目录(分支名)
193-
$paths = preg_split('/\/tree\/([^\/]+)\//', $url, 2, PREG_SPLIT_DELIM_CAPTURE);
194-
$url = $paths[0];
195-
$branch = $paths[1] ?? '';
196-
$folder = !empty($paths[2]) ? rtrim($paths[2], '/') . '/' : '';
197-
198-
$gitee = parse_url($url, PHP_URL_HOST) == 'gitee.com';
199-
$apiUrl = str_replace(
200-
['/github.com/', '/gitee.com/'],
201-
['/api.github.com/repos/', '/api.gitee.com/api/v5/repos/'],
202-
$url
203-
);
196+
$isPlugin = str_ends_with($url, 'Plugin.php') || str_ends_with($url, $name . '.php'); //直链文件情况
197+
$plugin = $isPlugin ? str_replace('/blob/', '/raw/', $url) : '';
198+
$branch = '';
204199
$api = '';
205-
if (!$branch) {
206-
$branch = 'master';
200+
$datas = [];
201+
$infos = [];
202+
//本地定位主文件路径
203+
if ($tf && $isLocal) {
204+
$plugin = pluginRoute($url, $name);
205+
//远程定位主文件地址
206+
} elseif ($isUrl) {
207+
//提取子目录(分支名)
208+
$paths = $isPlugin
209+
? preg_split('/\/(?:blob|raw)\/([^\/]+)\//', $url, 2, PREG_SPLIT_DELIM_CAPTURE)
210+
: preg_split('/\/tree\/([^\/]+)\//', $url, 2, PREG_SPLIT_DELIM_CAPTURE);
211+
$url = $paths[0];
212+
$branch = $paths[1] ?? $branch;
213+
$folder = !empty($paths[2])
214+
? ($isPlugin
215+
? str_replace(basename($paths[2]), '', $paths[2])
216+
: rtrim($paths[2], '/') . '/')
217+
: '';
218+
219+
$gitHosts = $github || $host == 'gitee.com';
220+
$apiUrl = str_replace(
221+
['/github.com/', '/gitee.com/'],
222+
['/api.github.com/repos/', '/api.gitee.com/api/v5/repos/'],
223+
$url
224+
);
207225
//API查询分支名
208-
if ($github || $gitee) {
209-
$api = @file_get_contents(
210-
$apiUrl,
211-
0,
212-
stream_context_create([
213-
'http' => [
214-
'header' => ['User-Agent: PHP', 'Authorization: token ' . $token]
215-
]
216-
])
217-
);
218-
if ($api) {
219-
$branch = json_decode($api, true)['default_branch'];
226+
if (!$branch) {
227+
$branch = 'master';
228+
if ($gitHosts) {
229+
$api = @file_get_contents(
230+
$apiUrl,
231+
0,
232+
stream_context_create([
233+
'http' => [
234+
'header' => ['User-Agent: PHP', 'Authorization: token ' . $token]
235+
]
236+
])
237+
);
238+
if ($api) {
239+
$branch = json_decode($api, true)['default_branch'];
240+
}
220241
}
221242
}
222-
}
223243

224-
$datas = [];
225-
$plugin = '';
226-
$infos = [];
227-
if (!$tf) {
228-
//API查询repo文件树
229-
if ($github || $gitee) {
230-
$api = @file_get_contents(
231-
$apiUrl . '/git/trees/' . $branch . '?recursive=1',
232-
0,
233-
stream_context_create([
234-
'http' => [
235-
'header' => ['User-Agent: PHP', 'Authorization: token ' . $token]
236-
]
237-
])
238-
);
239-
}
240244
$path = '';
241-
if ($api) {
242-
$datas = array_column(
243-
array_filter(
244-
json_decode($api, true)['tree'],
245-
fn($item) => $item['type'] === 'blob' //排除目录
246-
),
247-
'path'
248-
);
249-
//定位主文件路径
250-
$path = pluginRoute($datas, $name);
251-
}
252-
253-
//下载主文件获取信息
254-
if ($isUrl) {
255-
$pluginUri = $url . '/raw/' . $branch . '/' . $folder;
256-
$plugin = $path
257-
? $url . '/raw/' . $branch . '/' . $path
258-
: $pluginUri . 'Plugin.php';
259-
$infos = parseInfo($plugin);
260-
//无API重试单文件
261-
if (!$infos['version'] && !$path) {
262-
$plugin = $pluginUri . $name . '.php';
263-
$infos = parseInfo($plugin);
245+
$pluginUri = $url . '/raw/' . $branch . '/';
246+
//API查询repo文件树
247+
if (!$isPlugin) {
248+
if ($gitHosts) {
249+
$api = @file_get_contents(
250+
$apiUrl . '/git/trees/' . $branch . '?recursive=1',
251+
0,
252+
stream_context_create([
253+
'http' => [
254+
'header' => ['User-Agent: PHP', 'Authorization: token ' . $token]
255+
]
256+
])
257+
);
258+
}
259+
if ($api) {
260+
$datas = array_column(
261+
array_filter(
262+
json_decode($api, true)['tree'],
263+
fn($item) => $item['type'] === 'blob' //排除目录
264+
),
265+
'path'
266+
);
267+
//定位主文件路径
268+
$path = pluginRoute($datas, $name);
264269
}
270+
$plugin = $path ? $pluginUri . $path : $pluginUri . $folder . 'Plugin.php'; //盲试目录型
265271
}
266-
} elseif ($isLocal) {
267-
//本地读取主文件信息
268-
$plugin = pluginRoute($url, $name);
269-
if ($plugin) {
272+
}
273+
274+
//通过表格repo信息读取主文件
275+
if ($plugin) {
276+
$infos = parseInfo($plugin);
277+
//无API盲试单文件
278+
if (!$infos['version'] && $isUrl && !$path) {
279+
$plugin = $pluginUri . $folder . $name . '.php';
270280
$infos = parseInfo($plugin);
271281
}
272282
}
273283

274284
$noPlugin = empty($infos['version']); //表格repo信息无效
275-
$gitIsh = !$noPlugin && !$api && !$tf; //有效但无API
285+
$gitIsh = !$noPlugin && !$api && !$tf; //有效但无API(盲试)
276286
$zip = str_contains($zipMeta, '](') ? trim(end($links[0])) : ''; //取最后一个栏位链接地址
277287
$tmpSub = $tmpDir . '/' . $all . '_' . $name;
278288
$pluginZip = '';
279-
//解压zip包获取信息
289+
//通过解压缩zip包读取主文件
280290
if ($noPlugin || $gitIsh) {
281291
$download = @file_get_contents($zip);
282292
if ($download) {
283293
$tmpZip = $tmpSub . '_origin.zip';
284294
file_put_contents($tmpZip, $download);
285295
$phpZip = new ZipArchive();
286296
if ($phpZip->open($tmpZip) !== true) {
297+
//宽松校验
287298
$logs .= 'Error: Table zip - "' . $zip . '" is not valid!' . PHP_EOL;
288299
} else {
289300
mkdir($tmpSub, 0777, true);
290301
$phpZip->extractTo($tmpSub);
302+
//定位主文件路径
291303
$pluginZip = pluginRoute($tmpSub, $name);
292304
if ($pluginZip && !$gitIsh) {
293305
$infos = parseInfo($pluginZip);
@@ -298,7 +310,7 @@ function updatePlugins(string $tableFile, array $requested, string $token = '',
298310
}
299311
}
300312

301-
//有主文件信息即修正
313+
//有主文件头信息即修正
302314
if (!empty($infos['version'])) {
303315
++$revise; //记录修正次数
304316
$fixed = '';
@@ -467,7 +479,7 @@ function updatePlugins(string $tableFile, array $requested, string $token = '',
467479
!in_array(explode('_', $outName)[0], $requested)
468480
) {
469481
updatePlugins($tableFile, [$outName], '', $latest); //空token情况递归
470-
updatePlugins($tf ? 'TESTORE.md' : 'README.md', [$outName], '', $latest);
482+
updatePlugins($tf ? 'TESTORE.md' : 'README_test.md', [$outName], '', $latest);
471483
}
472484

473485
//记录插件改动明细
@@ -550,10 +562,21 @@ function updatePlugins(string $tableFile, array $requested, string $token = '',
550562
exec('find "' . $tmpDir . '" -mindepth 1 ! -name "updates.log" -exec rm -rf {} +');
551563

552564
//保存zip名表记录
565+
$listNames = array_filter(array_unique($listNames));
566+
$allNames = array_filter($allNames);
553567
if ($tf) {
554568
$listNames = array_merge($listNames, $allNames); //临时记录全表
569+
} else {
570+
if ($outNames = array_diff($listNames, $allNames)) {
571+
$logs .=
572+
'Warning: Table info about "' .
573+
implode(' / ', $outNames) .
574+
'" will be removed from NAME_LIST.log.' .
575+
PHP_EOL;
576+
$listNames = array_intersect($listNames, $allNames); //清理移除条目
577+
}
555578
}
556-
file_put_contents($nameList, implode(PHP_EOL, array_filter($listNames)));
579+
file_put_contents($nameList, implode(PHP_EOL, $listNames));
557580

558581
if ($allNames) {
559582
//检查重复项
@@ -567,7 +590,7 @@ function updatePlugins(string $tableFile, array $requested, string $token = '',
567590
}
568591
//清除冗余zip
569592
if (!$tf) {
570-
$allNames = array_merge(array_unique($allNames), ['NAME_LIST.log', 'README.md']);
593+
$allNames = array_merge($allNames, ['NAME_LIST.log', 'README.md']);
571594
$api = @file_get_contents(
572595
'https://api.github.com/repositories/14101953/contents/ZIP_CDN',
573596
0,
@@ -648,9 +671,9 @@ function pluginRoute(string|array $pluginData, string $name, bool $needTree = fa
648671
foreach ($routes as $route) {
649672
//带路径目录型优先
650673
$priority = match (true) {
651-
str_contains($route, $name . '/Plugin.php') => 4,
674+
stripos($route, $name . '/Plugin.php') !== false => 4, //兼容下小写
652675
str_contains($route, $name . '/' . $name . '.php') => 3,
653-
str_contains($route, 'Plugin.php') => 2,
676+
stripos($route, 'Plugin.php') !== false => 2,
654677
str_contains($route, $name . '.php') => 1,
655678
default => 0
656679
};
@@ -793,7 +816,7 @@ function dispatchZips(
793816
$host = parse_url($url, PHP_URL_HOST);
794817
$github = $host == 'github.com';
795818
$folder = realpath('../') . '/TMP/' . $index . '_' . $name;
796-
$tf = $md == 'README.md';
819+
$tf = $md == 'README_test.md';
797820
$tfLocal = $tf && is_dir($url);
798821
if (!is_dir($folder) && !$tfLocal) {
799822
mkdir($folder, 0777, true);

0 commit comments

Comments
 (0)