<?php
/*
Plugin Name: isamToInnoDB
Description: Converts Isam to InnoDB table. Rerunning will rebuild the indexes.
Author: Jeff Shields
Author URI: http://yaadev.com
Version: 1.0.0
Date: Feb 15, 2018
Requires at least: 3.12

 */

// chceck if in command line mode => DO NOT RUN
if (inCLI()) {
    return;
}

pluginAction_addHandlerAndLink('Convert tables to INNODB', 'convertIsamTableToInnoDB', 'admins');

/**
 * convertIsamTableToInnoDB
 * @return void
 */
function convertIsamTableToInnoDB()
{
    $adminUI = _initizationAdminUI();

    // page title
    $adminUI['PAGE_TITLE'] = [
        t("Plugins") => '?menu=admin&action=plugins',
        t("Convert Table Engine"),
        t("Results") => '?_pluginAction=' . __FUNCTION__,
    ];


    // Alter Table cmsb_about ENGINE=INNODB;
    $msg = "<h2>Convert tables to InnoDB</h2>";
    $sql = "SHOW TABLE STATUS WHERE 1";
    $tables = mysqli()->query($sql) or die("MySQL Error: " .mysqli()->error. "\n");
    foreach ($tables as $table) {
        $msg .= $table['Name'] . " => '" . $table['Engine']. "' " ;
        $alterSql = "Alter Table ".$table['Name']. " ENGINE=INNODB;";
        $result = mysqli()->query($alterSql) or die("MySQL Error: " .mysqli()->error. "\n");
        $msg .= " to InnoDB " . EOL;
    }

    $adminUI['CONTENT'] .= $msg;

    $adminUI['CONTENT'] .= "Finihed";
    // show page
    adminUI($adminUI);
    exit;
}

/**
 * _initizationAdminUI initializes the adminUI array
 *
 * sets up array,
 * inializes the 'CONTENT'
 * inializes the 'ADVANCED_ACTIONS'
 * @return array adminUI
 */
function _initizationAdminUI()
{
    $adminUI = [];
    $adminUI['CONTENT'] = "";
    // advanced actions
    $adminUI['ADVANCED_ACTIONS'] = [
    'Convert Tables' => '?_pluginAction=yaaApp_setTableToInnoDB',
    ];
    return $adminUI;
}

//eof
