HEX
Server: nginx
System: Linux f62c58717270 6.1.0-17-amd64 #1 SMP PREEMPT_DYNAMIC Debian 6.1.69-1 (2023-12-30) x86_64
User: www-data (1000)
PHP: 8.0.30
Disabled: NONE
Upload Files
File: //www/wp-cli/cleanblog.sh
#!/bin/sh

# 配置变量
ROOT_DIR="/www/sites/"
LOG_FILE="/www/wp-cli/cleanbloglog.txt"
STATE_FILE="/www/wp-cli/cleanblogstate.txt"
LOCK_FILE="/www/wp-cli/cleanblog.lock"

# 设置错误处理
set -e
trap 'echo "Script interrupted at line $LINENO" >> "$LOG_FILE"; exit 1' INT TERM

# 日志函数
log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE" 2>&1
}

# 确保必要的目录和文件存在
ensure_directories() {
    mkdir -p "$(dirname "$LOG_FILE")" 2>/dev/null || true
    touch "$LOG_FILE" "$STATE_FILE"
}

# 防止脚本重复执行
acquire_lock() {
    if [ -f "$LOCK_FILE" ]; then
        LOCK_PID=$(cat "$LOCK_FILE" 2>/dev/null)
        if kill -0 "$LOCK_PID" 2>/dev/null; then
            log "⚠ Script already running (PID: $LOCK_PID), exiting"
            exit 1
        else
            log "⚠ Removing stale lock file"
            rm -f "$LOCK_FILE"
        fi
    fi
    echo $$ > "$LOCK_FILE"
}

release_lock() {
    rm -f "$LOCK_FILE"
}

# 检查WP-CLI是否可用
check_wp_cli() {
    if ! command -v wp >/dev/null 2>&1; then
        log "❌ WP-CLI not found in PATH"
        exit 1
    fi
}

# 检查站点是否有效
is_valid_wp_site() {
    local site_dir="$1"
    [ -f "$site_dir/wp-config.php" ] && [ -d "$site_dir/wp-admin" ] && [ -d "$site_dir/wp-includes" ]
}

# 执行WP-CLI命令并过滤警告
wp_cmd() {
    local site_dir="$1"
    shift
    # 使用 --quiet 抑制所有警告,只保留实际输出和错误
    wp --path="$site_dir" --allow-root --quiet "$@" 2>/dev/null
}

# 处理单个站点
process_site() {
    local site_dir="$1"
    local skip=false
    
    # 检查是否已处理
    if grep -qxF "$site_dir" "$STATE_FILE" 2>/dev/null; then
        log "⏩ Skipping already processed: $site_dir"
        return 0
    fi
    
    # 验证站点
    if ! is_valid_wp_site "$site_dir"; then
        log "⚠ Invalid WordPress site: $site_dir"
        return 1
    fi
    
    log "🔄 Processing: $site_dir"
    
    local error_count=0
    
    # 使用eval执行SQL删除所有文章
    if wp_cmd "$site_dir" eval 'global $wpdb; $wpdb->query("DELETE FROM wp_posts WHERE post_type = \"post\"");' 2>/dev/null; then
        log "✅ Deleted all posts"
    else
        log "❌ Failed to delete posts"
        error_count=$((error_count + 1))
    fi
    
    # 记录处理结果
    if [ $error_count -eq 0 ]; then
        echo "$site_dir" >> "$STATE_FILE"
        log "✅ Successfully processed: $site_dir"
        return 0
    else
        log "⚠ Completed with $error_count error(s): $site_dir"
        return 1
    fi
}

# 主函数
main() {
    ensure_directories
    acquire_lock
    check_wp_cli
    
    log "🚀 Starting delete all posts script"
    log "📁 Root directory: $ROOT_DIR"
    
    # 统计变量
    total=0
    success=0
    failed=0
    skipped=0
    
    # 使用 find 查找所有 wp-config.php
    while IFS= read -r -d '' config_path; do
        site_dir=$(dirname "$config_path")
        total=$((total + 1))
        
        # 检查是否已处理
        if grep -qxF "$site_dir" "$STATE_FILE" 2>/dev/null; then
            skipped=$((skipped + 1))
            continue
        fi
        
        if process_site "$site_dir"; then
            success=$((success + 1))
        else
            failed=$((failed + 1))
        fi
        
    done < <(find "$ROOT_DIR" -type f -name "wp-config.php" -print0 2>/dev/null || true)
    
    # 输出统计
    log "📊 Summary: Total=$total, Success=$success, Failed=$failed, Skipped=$skipped"
    log "🏁 Script completed"
    
    release_lock
}

# 执行主函数
main "$@"