Skip to content

Instantly share code, notes, and snippets.

@dragon-fish
Created June 11, 2025 17:43
Show Gist options
  • Select an option

  • Save dragon-fish/d3878f2eeed4fd1228408e981b2a6038 to your computer and use it in GitHub Desktop.

Select an option

Save dragon-fish/d3878f2eeed4fd1228408e981b2a6038 to your computer and use it in GitHub Desktop.
Check MediaWiki autoload.php class files
#!/usr/bin/env python3
"""
Check MediaWiki autoload.php class files
此脚本读取 autoload.php 中定义的类文件,并检查它们是否存在以及大小写是否正确
MediaWiki 的发行版压缩包里有可能会出现文件名不对或者文件缺失的问题,头疼
"""
import os
import re
import sys
def parse_autoload_php(autoload_file):
"""解析 autoload.php 文件,提取类名和文件路径的映射"""
class_files = {}
try:
with open(autoload_file, 'r', encoding='utf-8') as f:
content = f.read()
# 使用正则表达式匹配类定义
# 匹配格式:'ClassName' => __DIR__ . '/path/to/file.php',
pattern = r"'([^']+)'\s*=>\s*__DIR__\s*\.\s*'([^']+)'"
matches = re.findall(pattern, content)
for class_name, file_path in matches:
# 清理文件路径,移除开头的斜杠
file_path = file_path.lstrip('/')
class_files[class_name] = file_path
except Exception as e:
print(f"解析 autoload.php 文件时出错: {e}")
return {}
return class_files
def check_file_exists_with_case(base_dir, relative_path):
"""检查文件是否存在以及大小写是否正确"""
full_path = os.path.join(base_dir, relative_path)
# 检查文件是否存在(不区分大小写)
if not os.path.exists(full_path):
return False, "文件不存在"
# 检查大小写是否正确
# 将路径拆分成各个部分
path_parts = relative_path.split('/')
current_path = base_dir
for i, part in enumerate(path_parts):
try:
# 获取当前目录中的所有条目
entries = os.listdir(current_path)
# 查找匹配的条目(区分大小写)
exact_match = None
case_insensitive_match = None
for entry in entries:
if entry == part:
exact_match = entry
break
elif entry.lower() == part.lower():
case_insensitive_match = entry
if exact_match:
# 大小写完全匹配
current_path = os.path.join(current_path, exact_match)
elif case_insensitive_match:
# 找到了文件但大小写不匹配
expected_path = '/'.join(path_parts)
actual_path_parts = path_parts.copy()
actual_path_parts[i] = case_insensitive_match
actual_path = '/'.join(actual_path_parts)
return True, f"大小写不匹配 - 期望: {expected_path}, 实际: {actual_path}"
else:
# 没找到匹配的条目
return False, f"路径不存在: {'/'.join(path_parts[:i+1])}"
except OSError:
return False, f"无法访问目录: {current_path}"
return True, "OK"
def main():
# 获取脚本所在目录作为基础目录
script_dir = os.path.dirname(os.path.abspath(__file__))
wiki_dir = os.path.join(script_dir, 'wiki')
autoload_file = os.path.join(wiki_dir, 'autoload.php')
print("检查 MediaWiki autoload.php 文件引用的类文件")
print("=" * 60)
print(f"基础目录: {wiki_dir}")
print(f"autoload 文件: {autoload_file}")
print()
# 检查 autoload.php 文件是否存在
if not os.path.exists(autoload_file):
print(f"错误: autoload.php 文件不存在: {autoload_file}")
return 1
# 解析 autoload.php 文件
print("正在解析 autoload.php 文件...")
class_files = parse_autoload_php(autoload_file)
if not class_files:
print("错误: 无法解析 autoload.php 文件或文件为空")
return 1
print(f"找到 {len(class_files)} 个类定义")
print()
# 统计信息
total_files = len(class_files)
missing_files = []
case_issues = []
ok_files = []
# 检查每个文件
print("检查文件存在性和大小写...")
for i, (class_name, file_path) in enumerate(class_files.items(), 1):
exists, message = check_file_exists_with_case(wiki_dir, file_path)
if not exists:
missing_files.append((class_name, file_path, message))
elif message != "OK":
case_issues.append((class_name, file_path, message))
else:
ok_files.append((class_name, file_path))
# 显示进度
if i % 100 == 0 or i == total_files:
print(f"进度: {i}/{total_files} ({i/total_files*100:.1f}%)")
print()
print("检查结果:")
print("=" * 60)
# 显示统计信息
print(f"总文件数: {total_files}")
print(f"正常文件: {len(ok_files)}")
print(f"大小写问题: {len(case_issues)}")
print(f"缺失文件: {len(missing_files)}")
print()
# 显示大小写问题
if case_issues:
print("大小写问题:")
print("-" * 40)
for class_name, file_path, message in case_issues:
print(f"类: {class_name}")
print(f" {message}")
print()
# 显示缺失文件
if missing_files:
print("缺失文件:")
print("-" * 40)
for class_name, file_path, message in missing_files:
print(f"类: {class_name}")
print(f" 文件: {file_path}")
print(f" 状态: {message}")
print()
# 生成修复建议
if case_issues or missing_files:
print("修复建议:")
print("-" * 40)
if case_issues:
print("对于大小写问题,您可以:")
print("1. 运行现有的 fix_git_case_sensitive.py 脚本(*推荐,因为它处理了很多边缘情况)")
print()
if missing_files:
print("对于缺失文件,您需要:")
print("1. 检查文件是否被意外删除")
print("2. 检查是否存在于错误的位置")
print("3. 从备份或 git 历史中恢复文件")
print("4. 如果文件确实不存在,尝试从 MediaWiki 的源代码仓库中获取")
print()
return 1
else:
print("✅ 所有文件都存在且大小写正确!")
return 0
if __name__ == "__main__":
sys.exit(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment