EverydayTech Platform - Developer Reference
Complete Source Code Documentation - All Applications
Loading...
Searching...
No Matches
scan_wordpress.js
Go to the documentation of this file.
1#!/usr/bin/env node
2
3const { exec } = require('child_process');
4const fs = require('fs');
5const path = require('path');
6const util = require('util');
7const execPromise = util.promisify(exec);
8
9const SSH_KEY = `${process.env.HOME}/PreciseITServices.pem`;
10const SSH_USER = 'centos';
11const WHM_HOST = 'precisewebhosting.com.au';
12const OUTPUT_DIR = 'exports/wordpress-inventory';
13
14if (!fs.existsSync(OUTPUT_DIR)){
15 fs.mkdirSync(OUTPUT_DIR, { recursive: true });
16}
17
18console.log('========================================');
19console.log('WordPress Site Inventory Scanner');
20console.log('========================================\n');
21
22/**
23 *
24 */
25async function scanWordPressSites() {
26 console.log('šŸ” Scanning for WordPress installations...\n');
27
28 // Get list of WordPress installations
29 const findCmd = `ssh -i "${SSH_KEY}" ${SSH_USER}@${WHM_HOST} "ls /home/*/public_html/wp-config.php 2>/dev/null"`;
30
31 let wpConfigPaths;
32 try {
33 const { stdout } = await execPromise(findCmd);
34 wpConfigPaths = stdout.trim().split('\n').filter(p => p);
35 } catch (error) {
36 console.error('āŒ Failed to find WordPress sites:', error.message);
37 process.exit(1);
38 }
39
40 console.log(` Found ${wpConfigPaths.length} WordPress installations\n`);
41
42 const sites = [];
43 let processed = 0;
44
45 for (const wpConfigPath of wpConfigPaths) {
46 const wpPath = path.dirname(wpConfigPath);
47 const username = wpPath.split('/')[2];
48
49 processed++;
50 console.log(` [${processed}/${wpConfigPaths.length}] ${username} (${wpPath})`);
51
52 try {
53 // Single SSH command to extract all data (using sudo for file access)
54 const cmd = `ssh -i "${SSH_KEY}" ${SSH_USER}@${WHM_HOST} sudo bash -c '
55WP_PATH="${wpPath}"
56WP_CONFIG="${wpConfigPath}"
57
58# Extract database credentials
59DB_NAME=$(cat "$WP_CONFIG" 2>/dev/null | grep "define.*DB_NAME" | head -1 | sed "s/.*[\\x27\\x22]\\‍([^\\x27\\x22]*\\‍)[\\x27\\x22].*/\\1/")
60DB_USER=$(cat "$WP_CONFIG" 2>/dev/null | grep "define.*DB_USER" | head -1 | sed "s/.*[\\x27\\x22]\\‍([^\\x27\\x22]*\\‍)[\\x27\\x22].*/\\1/")
61DB_PASS=$(cat "$WP_CONFIG" 2>/dev/null | grep "define.*DB_PASSWORD" | head -1 | sed "s/.*[\\x27\\x22]\\‍([^\\x27\\x22]*\\‍)[\\x27\\x22].*/\\1/")
62DB_HOST=$(cat "$WP_CONFIG" 2>/dev/null | grep "define.*DB_HOST" | head -1 | sed "s/.*[\\x27\\x22]\\‍([^\\x27\\x22]*\\‍)[\\x27\\x22].*/\\1/" || echo "localhost")
63TABLE_PREFIX=$(cat "$WP_CONFIG" 2>/dev/null | grep "table_prefix" | sed "s/.*[\\x27\\x22]\\‍([^\\x27\\x22]*\\‍)[\\x27\\x22].*/\\1/" || echo "wp_")
64
65# Get WordPress version
66WP_VERSION=$(cat "$WP_PATH/wp-includes/version.php" 2>/dev/null | grep "\\$wp_version" | head -1 | sed "s/.*[\\x27\\x22]\\‍([^\\x27\\x22]*\\‍)[\\x27\\x22].*/\\1/" || echo "unknown")
67
68# Get directory size
69SIZE=$(du -sh "$WP_PATH" 2>/dev/null | cut -f1 || echo "unknown")
70
71# Get site URL from database (with error handling)
72SITE_URL=$(mysql -h"$DB_HOST" -u"$DB_USER" -p"$DB_PASS" -D"$DB_NAME" -N -B -e "SELECT option_value FROM ${TABLE_PREFIX}options WHERE option_name=\\x27siteurl\\x27 LIMIT 1" 2>/dev/null | head -1 || echo "unknown")
73
74# Check if multisite
75IS_MULTISITE=false
76cat "$WP_CONFIG" 2>/dev/null | grep -q "MULTISITE" && IS_MULTISITE=true
77
78echo "$DB_NAME|||$DB_USER|||$DB_PASS|||$DB_HOST|||$TABLE_PREFIX|||$WP_VERSION|||$SIZE|||$SITE_URL|||$IS_MULTISITE"
79'`;
80
81 const { stdout } = await execPromise(cmd, { maxBuffer: 1024 * 1024 * 10 });
82 const parts = stdout.trim().split('|||');
83
84 if (parts.length >= 9) {
85 const siteData = {
86 username,
87 path: wpPath,
88 location: 'public_html',
89 version: parts[5] || 'unknown',
90 site_url: parts[7] || 'unknown',
91 size: parts[6] || 'unknown',
92 multisite: parts[8] === 'true',
93 database: {
94 db_name: parts[0] || '',
95 db_user: parts[1] || '',
96 db_password: parts[2] || '',
97 db_host: parts[3] || 'localhost',
98 table_prefix: parts[4] || 'wp_'
99 }
100 };
101
102 sites.push(siteData);
103 console.log(` āœ… ${siteData.site_url} (${siteData.version}) - ${siteData.size}`);
104 } else {
105 console.log(` āš ļø Incomplete data extracted`);
106 }
107
108 } catch (error) {
109 console.log(` āŒ Error: ${error.message}`);
110 }
111 }
112
113 // Save results
114 const outputFile = path.join(OUTPUT_DIR, 'wordpress_sites.json');
115 fs.writeFileSync(outputFile, JSON.stringify(sites, null, 2));
116
117 console.log('\n========================================');
118 console.log(`āœ… WordPress inventory saved: ${outputFile}`);
119 console.log('========================================\n');
120 console.log(`šŸ“Š Sites found: ${sites.length}/${wpConfigPaths.length}\n`);
121
122 if (sites.length > 0) {
123 console.log('Ready for migration!\n');
124 console.log('Next step: ./migration-scripts/migrate_wordpress_to_github.sh\n');
125 }
126}
127
128scanWordPressSites().catch(error => {
129 console.error('\nāŒ Error:', error.message);
130 process.exit(1);
131});