Changeset 67


Ignore:
Timestamp:
03/11/2009 10:41:24 AM (3 years ago)
Author:
ofer
Message:

Updated database structure a bit, need to discuss some code there as I added a few TODOs

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/WordPress/plugin/transposh/transposh.php

    r64 r67  
    4242 
    4343//Database version 
    44 define("DB_VERSION", "1.0"); 
     44define("DB_VERSION", "1.01"); 
    4545 
    4646//Constant used as key in options database 
     
    287287        return; 
    288288    } 
    289      
     289 
    290290 
    291291    $overlib_dir = "$plugin_url/js/overlibmws"; 
     
    588588function setup_db() 
    589589{ 
    590  
    591     global $wpdb; 
     590    logger("Enter " . __METHOD__  ); 
     591        global $wpdb; 
    592592    require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); 
    593593 
    594     $table_name = $wpdb->prefix . TRANSLATIONS_TABLE; 
    595  
    596     if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) 
    597     { 
     594    $installed_ver = get_option(TRANSPOSH_DB_VERSION); 
     595 
     596    if( $installed_ver != DB_VERSION ) { 
     597                $table_name = $wpdb->prefix . TRANSLATIONS_TABLE; 
     598 
     599    //if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) 
     600    //{ 
    598601        logger("Attempting to create table $table_name", 0); 
    599         $sql = "CREATE TABLE " . $table_name . " (original VARCHAR(256) NOT NULL, 
    600                                                   lang CHAR(5) NOT NULL, 
    601                                                   translated VARCHAR(256), 
    602                                                   PRIMARY KEY (original, lang)) "; 
     602        $sql = "CREATE TABLE $table_name (original VARCHAR(256) NOT NULL, 
     603                                lang CHAR(5) NOT NULL, 
     604                                translated VARCHAR(256), 
     605                                source TINYINT NOT NULL, 
     606                                PRIMARY KEY (original, lang)) "; 
    603607 
    604608 
     
    606610 
    607611        //Verify that newly created table is ready for use. 
    608         $insert = "INSERT INTO " . $table_name . " (original, translated, lang) " . 
    609         "VALUES ('Hello','Hi There','zz')"; 
    610  
    611         $result = $wpdb->query($insert); 
    612  
    613         if($result === FALSE) 
    614         { 
    615             logger("Error failed to create $table_name !!!", 0); 
    616         } 
    617         else 
    618         { 
    619             logger("Table $table_name was created successfuly", 0); 
    620             add_option(TRANSPOSH_DB_VERSION, DB_VERSION); 
    621         } 
    622     } 
    623  
    624     $table_name = $wpdb->prefix . TRANSLATIONS_LOG; 
    625  
    626     if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) 
    627     { 
     612        //$insert = "INSERT INTO " . $table_name . " (original, translated, lang) " . 
     613        //"VALUES ('Hello','Hi There','zz')"; 
     614 
     615        //$result = $wpdb->query($insert); 
     616 
     617        //if($result === FALSE) 
     618        //{ 
     619            //logger("Error failed to create $table_name !!!", 0); 
     620        //} 
     621        //else 
     622        //{ 
     623                // logger("Table $table_name was created successfuly", 0); 
     624        //} 
     625    //} 
     626 
     627        $table_name = $wpdb->prefix . TRANSLATIONS_LOG; 
     628 
     629    //if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) 
     630    //{ 
    628631        logger("Attempting to create table $table_name", 0); 
    629632        $sql = "CREATE TABLE " . $table_name . " (original VARCHAR(256) NOT NULL, 
     
    636639 
    637640        dbDelta($sql); 
     641        // TODO: check 
     642                update_option(TRANSPOSH_DB_VERSION, DB_VERSION); 
    638643    } 
    639644 
     
    731736    logger("Enter " . __METHOD__, 3); 
    732737 
     738    $db_version = get_option(TRANSPOSH_DB_VERSION); 
     739 
     740    if ($db_version != DB_VERSION) 
     741    { 
     742        setup_db(); 
     743        //$admin_msg = "Translation database version ($db_version) is not comptabile with this plugin (". DB_VERSION . ")  <br>"; 
     744 
     745        logger("Updating database in plugin loaded", 0); 
     746        //Some error occured - notify admin and deactivate plugin 
     747        //add_action('admin_notices', 'plugin_install_error'); 
     748    } 
     749 
     750    // TODO: This is wrong? and also deactivation fails 
    733751    if (get_option(TRANSPOSH_DB_VERSION) == NULL) 
    734752    { 
     
    740758    } 
    741759 
    742     $db_version = get_option(TRANSPOSH_DB_VERSION); 
    743  
    744     if ($db_version != DB_VERSION) 
    745     { 
    746         $admin_msg = "Translation database version ($db_version) is not comptabile with this plugin (". DB_VERSION . ")  <br>"; 
    747  
    748         logger("Messsage to admin: $admin_msg", 0); 
    749         //Some error occured - notify admin and deactivate plugin 
    750         add_action('admin_notices', 'plugin_install_error'); 
    751     } 
    752 } 
    753  
    754 /* 
    755  * Gets the plugin name to be used in activation/decativation hooks.  
     760} 
     761 
     762/* 
     763 * Gets the plugin name to be used in activation/decativation hooks. 
    756764 * Keep only the file name and its containing directory. Don't use the full 
    757  * path as it will break when using symbollic links.  
     765 * path as it will break when using symbollic links. 
    758766 */ 
    759767function get_plugin_name() 
    760768{ 
    761         $file = __FILE__;        
     769        $file = __FILE__; 
    762770        $file = str_replace('\\','/',$file); // sanitize for Win32 installs 
    763771        $file = preg_replace('|/+|','/', $file); // remove any duplicate slash 
    764          
     772 
    765773        //keep only the file name and its parent directory 
    766774        $file = preg_replace('/.*\/([^\/]+\/[^\/]+)$/', '$1', $file); 
Note: See TracChangeset for help on using the changeset viewer.