# PHP CLI HTML Exporter Plan

## Overview
Create a PHP CLI tool to export SQLite data to HTML files with template support.

## Requirements Analysis

### Core Requirements
1. **PHP CLI instead of Node.js** - Use PHP 7.4+ for CLI execution
2. **Visual progress in CMD** - Show status messages during export
3. **"Export selesai" message** - Display on successful completion
4. **Bing image URL format** - Use `https://tse1.mm.bing.net/th?q=keyword&w=250&h=250&c=7`
5. **Valid URL validation** - Only use URLs with https://domain.com format
6. **Separate template files** - Templates not embedded in script
7. **Multiple template support** - Select template via command or config

### Data Schema (from export_sqlite.js analysis)
- Table: `posts`
- Fields:
  - `keyword` - Article title/keyword
  - `slug` - URL-safe slug
  - `images` - JSON array of image objects `{image, title, domain}`
  - `articles` - JSON array of article text paragraphs

## File Structure

```
ducktohtml/
├── export_html.php          # Main CLI entry point
├── templates/               # Template directory
│   ├── default/             # Default template (Tailwind CSS)
│   │   ├── config.php       # Template metadata
│   │   ├── header.php       # HTML head + body opening
│   │   ├── footer.php       # Body closing + inject scripts
│   │   ├── article.php      # Article paragraph layout
│   │   └── image.php        # Image display with caption
│   ├── gallery/             # Gallery-style template
│   │   ├── config.php
│   │   ├── header.php
│   │   ├── footer.php
│   │   ├── article.php
│   │   └── image.php
│   └── simple/              # Minimal template
│       ├── config.php
│       ├── header.php
│       ├── footer.php
│       ├── article.php
│       └── image.php
└── lib/                     # Core libraries
    ├── Database.php         # SQLite wrapper
    ├── Exporter.php         # Export orchestration
    ├── Template.php         # Template rendering
    └── Util.php             # Helpers (URL, text processing)
```

## Command Usage

```bash
# Basic usage with default template
php export_html.php <splitSize> <baseUrl>

# With specific template
php export_html.php <splitSize> <baseUrl> --template=default

# With all options
php export_html.php 50 "https://example.com" --template=default --verbose
```

## Template System Design

### Template Config (config.php)
```php
<?php
return [
    'name' => 'Default Template',
    'description' => 'Tailwind CSS based template',
    'version' => '1.0',
    'author' => 'Anggi',
    'css' => [
        'https://unpkg.com/tailwindcss@2.2.19/dist/tailwind.min.css'
    ],
    'inject' => [
        'header' => true,
        'footer' => true,
        'adsinarticle' => true
    ]
];
```

### Template Variables
Templates receive these variables:
- `$keyword` - Article title
- `$description` - Meta description
- `$mainImage` - Primary image URL
- `$images` - Array of image objects
- `$articles` - Array of article paragraphs
- `$relatedLinks` - Array of related article links
- `$ldJson` - Structured data JSON
- `$config` - Template configuration

## Image URL Processing

### Bing Image URL Generator
```php
function generateBingImageUrl(string $keyword): string {
    $encoded = urlencode($keyword);
    return "https://tse1.mm.bing.net/th?q={$encoded}&w=250&h=250&c=7";
}
```

### URL Validation
```php
function isValidUrl(string $url): bool {
    return filter_var($url, FILTER_VALIDATE_URL) !== false
        && preg_match('/^https:\/\//i', $url);
}
```

## Export Process Flow

```mermaid
flowchart TD
    A[Start: php export_html.php] --> B[Parse CLI Arguments]
    B --> C{Valid Arguments?}
    C -->|No| D[Show Usage Error]
    C -->|Yes| E[Load Template Config]
    E --> F[Scan data/ for SQLite files]
    F --> G{Found SQLite files?}
    G -->|No| H[Show: No SQLite files found]
    G -->|Yes| H1[Process Each SQLite File]
    H1 --> I[Connect to SQLite Database]
    I --> J[Query posts table]
    J --> K{Valid posts found?}
    K -->|No| L[Skip to next file]
    K -->|Yes| M[Chunk posts by splitSize]
    M --> N[Create output folder structure]
    N --> O[For each chunk: Generate HTML]
    O --> P[Write HTML files]
    P --> Q[Generate sitemap.xml]
    Q --> R{More files?}
    R -->|Yes| I
    R -->|No| S[Show Export Statistics]
    S --> T[Display: Export selesai]
```

## Progress Display

```
[INFO] Starting HTML export...
[INFO] Template: default
[INFO] Split size: 50
[INFO] Base URL: https://example.com

[INFO] Processing: Home1.sqlite
[INFO] Found 150 posts
[INFO] Creating 3 chunks (folders)
[INFO] Exporting folder_1/...
[INFO]   [====================] 100% - 50 files
[INFO] Exporting folder_2/...
[INFO]   [====================] 100% - 50 files
[INFO] Exporting folder_3/...
[INFO]   [====================] 100% - 50 files

[SUCCESS] Export completed!
[INFO] Total: 150 HTML files created in 3 folders
[INFO] Output: export/Home1/
[SUCCESS] Export selesai
```

## Implementation Steps

### Step 1: Create lib/Util.php
- URL validation functions
- Bing image URL generator
- Text cleaning functions
- String utilities

### Step 2: Create lib/Database.php
- SQLite connection wrapper
- Query execution methods
- Error handling

### Step 3: Create lib/Template.php
- Template loading
- Variable injection
- Template rendering

### Step 4: Create lib/Exporter.php
- Export orchestration
- File generation
- Sitemap creation
- Progress tracking

### Step 5: Create export_html.php
- CLI argument parsing
- Bootstrap the export
- Error handling

### Step 6: Create templates/default/
- Copy structure from Node.js export
- Adapt to PHP template system

### Step 7: Create templates/gallery/
- Alternative layout
- Images-focused design

### Step 8: Create templates/simple/
- Minimal template
- Basic HTML structure

### Step 9: Create export.bat
- Windows batch file for easy execution

### Step 10: Create README.md
- Documentation
- Usage guide
- Template creation guide

## Key Features

1. **Progress Bar** - Visual progress in CMD using character-based bar
2. **Color Output** - Different colors for info, success, error, warning
3. **Error Handling** - Graceful error handling with meaningful messages
4. **Template Inheritance** - Templates can extend base templates
5. **Inject Support** - Compatible with existing inject/ files
6. **LD-JSON Support** - Schema.org structured data
7. **Sitemap Generation** - Automatic sitemap.xml creation
8. **Related Links** - Auto-generate related articles section
