Advertisement
Guest User

_functions_evoupgrade.php for b2evolution v4.0.5

a guest
Sep 24th, 2011
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 106.08 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This file implements upgrading of DB tables
  4.  *
  5.  * b2evolution - {@link http://b2evolution.net/}
  6.  * Released under GNU GPL License - {@link http://b2evolution.net/about/license.html}
  7.  * @copyright (c)2003-2010 by Francois PLANQUE - {@link http://fplanque.net/}
  8.  *
  9.  * {@internal Open Source relicensing agreement:
  10.  * Daniel HAHLER grants Francois PLANQUE the right to license
  11.  * Daniel HAHLER's contributions to this file and the b2evolution project
  12.  * under any OSI approved OSS license (http://www.opensource.org/licenses/).
  13.  * }}
  14.  *
  15.  * @package install
  16.  */
  17. if( !defined('EVO_MAIN_INIT') ) die( 'Please, do not access this page directly.' );
  18.  
  19. load_funcs('_core/_param.funcs.php');
  20.  
  21.  
  22. /**
  23.  * Create a DB version checkpoint
  24.  *
  25.  * This is useful when the next operation might timeout or fail!
  26.  * The checkpoint will allow to restart the script and continue where it stopped
  27.  *
  28.  * @param string version of DB at checkpoint
  29.  */
  30. function set_upgrade_checkpoint( $version )
  31. {
  32.     global $DB, $script_start_time, $locale;
  33.  
  34.     echo "Creating DB schema version checkpoint at $version... ";
  35.  
  36.     if( $version < 8060 )
  37.     {
  38.         $query = 'UPDATE T_settings SET db_version = '.$version;
  39.     }
  40.     else
  41.     {
  42.         $query = "UPDATE T_settings
  43.                                 SET set_value = '$version'
  44.                                 WHERE set_name = 'db_version'";
  45.     }
  46.     $DB->query( $query );
  47.  
  48.  
  49.     $elapsed_time = time() - $script_start_time;
  50.  
  51.     echo "OK. (Elapsed upgrade time: $elapsed_time seconds)<br />\n";
  52.     flush();
  53.  
  54.     $max_exe_time = ini_get( 'max_execution_time' );
  55.     if( $max_exe_time && $elapsed_time > $max_exe_time - 10 )
  56.     { // Max exe time not disabled and we're recahing the end
  57.         echo 'We are reaching the time limit for this script. Please click <a href="index.php?locale='.$locale.'&amp;action=evoupgrade">continue</a>...';
  58.         // Dirty temporary solution:
  59.         exit(0);
  60.     }
  61. }
  62.  
  63.  
  64. /**
  65.  * @return boolean Does a given index key name exist in DB?
  66.  */
  67. function db_index_exists( $table, $index_name )
  68. {
  69.     global $DB;
  70.  
  71.     $index_name = strtolower($index_name);
  72.  
  73.     $DB->query('SHOW INDEX FROM '.$table);
  74.     while( $row = $DB->get_row() )
  75.     {
  76.         if( strtolower($row->Key_name) == $index_name )
  77.         {
  78.             return true;
  79.         }
  80.     }
  81.  
  82.     return false;
  83. }
  84.  
  85.  
  86. /**
  87.  * @param string Table name
  88.  * @param array Column names
  89.  * @return boolean Does a list of given column names exist in DB?
  90.  */
  91. function db_cols_exist( $table, $col_names )
  92. {
  93.     global $DB;
  94.  
  95.     foreach( $col_names as $k => $v )
  96.         $col_names[$k] = strtolower($v);
  97.  
  98.     foreach( $DB->get_results('SHOW COLUMNS FROM '.$table) as $row )
  99.         if( ($key = array_search(strtolower($row->Field), $col_names)) !== false )
  100.             unset( $col_names[$key] );
  101.  
  102.     return count($col_names) == 0;
  103. }
  104.  
  105. /**
  106.  * Drops a column, if it exists.
  107.  */
  108. function db_drop_col( $table, $col_name )
  109. {
  110.     global $DB;
  111.  
  112.     if( ! db_col_exists($table, $col_name) )
  113.         return false;
  114.  
  115.     $DB->query( 'ALTER TABLE '.$table.' DROP COLUMN '.$col_name );
  116. }
  117.  
  118. /**
  119.  * Add a column, if it does not already exist.
  120.  * If it exists already, a "ALTER TABLE" statement will get executed instead.
  121.  *
  122.  * @return boolean True if the column has been added, False if not.
  123.  */
  124. function db_add_col( $table, $col_name, $col_desc )
  125. {
  126.     global $DB;
  127.  
  128.     if( db_col_exists($table, $col_name) )
  129.     { // Column exists already, make sure it's the same.
  130.         $DB->query( 'ALTER TABLE '.$table.' MODIFY COLUMN '.$col_name.' '.$col_desc );
  131.         return false;
  132.     }
  133.  
  134.     $DB->query( 'ALTER TABLE '.$table.' ADD COLUMN '.$col_name.' '.$col_desc );
  135. }
  136.  
  137.  
  138. /**
  139.  * Add an INDEX. If another index with the same name already exists, it will
  140.  * get dropped before.
  141.  */
  142. function db_add_index( $table, $name, $def, $type = 'INDEX' )
  143. {
  144.     global $DB;
  145.     if( db_index_exists($table, $name) )
  146.     {
  147.         $DB->query( 'ALTER TABLE '.$table.' DROP INDEX '.$name );
  148.     }
  149.     $DB->query( 'ALTER TABLE '.$table.' ADD '.$type.' '.$name.' ('.$def.')' );
  150. }
  151.  
  152.  
  153. /**
  154.  * Check if a key item value already exists on database
  155.  */
  156. function db_key_exists( $table, $field_name, $field_value )
  157. {
  158.     global $DB;
  159.     return $DB->get_var( '
  160.         SELECT COUNT('.$field_name.')
  161.         FROM '.$table.'
  162.         WHERE '.$field_name.' = '.$field_value );
  163. }
  164.  
  165. /**
  166.  * Converts languages in a given table into according locales
  167.  *
  168.  * @param string name of the table
  169.  * @param string name of the column where lang is stored
  170.  * @param string name of the table's ID column
  171.  */
  172. function convert_lang_to_locale( $table, $columnlang, $columnID )
  173. {
  174.     global $DB, $locales, $default_locale;
  175.  
  176.     if( !preg_match('/[a-z]{2}-[A-Z]{2}(-.{1,14})?/', $default_locale) )
  177.     { // we want a valid locale
  178.         $default_locale = 'en-EU';
  179.     }
  180.  
  181.     echo 'Converting langs to locales for '. $table. '...<br />';
  182.  
  183.     // query given languages in $table
  184.     $query = "SELECT $columnID, $columnlang FROM $table";
  185.     $languagestoconvert = array();
  186.     foreach( $DB->get_results( $query, ARRAY_A ) as $row )
  187.     {
  188.         // remember the ID for that locale
  189.         $languagestoconvert[ $row[ $columnlang ] ][] = $row[ $columnID ];
  190.     }
  191.  
  192.     foreach( $languagestoconvert as $lkey => $lIDs)
  193.     { // converting the languages we've found
  194.         $converted = false;
  195.         echo '&nbsp; Converting lang \''. $lkey. '\' '; // (with IDs: '. implode( ', ', $lIDs ). ').. ';
  196.  
  197.         if( preg_match('/[a-z]{2}-[A-Z]{2}(-.{1,14})?/', $lkey) )
  198.         { // Already valid
  199.             echo 'nothing to update, already valid!<br />';
  200.             continue;
  201.         }
  202.  
  203.         if( (strlen($lkey) == 2) && ( substr( $default_locale, 0, 2 ) != $lkey ) )
  204.         { // we have an old two letter lang code to convert
  205.             // and it doesn't match the default locale
  206.             foreach( $locales as $newlkey => $v )
  207.             {  // loop given locales
  208.                 if( substr($newlkey, 0, 2) == strtolower($lkey) ) # TODO: check if valid/suitable
  209.                 {  // if language matches, update
  210.                     $converted = $DB->query( "
  211.                         UPDATE $table
  212.                            SET $columnlang = '$newlkey'
  213.                          WHERE $columnlang = '$lkey'" );
  214.                     echo 'to locale \''. $newlkey. '\'<br />';
  215.                     break;
  216.                 }
  217.             }
  218.         }
  219.  
  220.         if( !$converted )
  221.         { // we have nothing converted yet, setting default:
  222.             $DB->query( "UPDATE $table
  223.                                             SET $columnlang = '$default_locale'
  224.                                         WHERE $columnlang = '$lkey'" );
  225.             echo 'forced to default locale \''. $default_locale. '\'<br />';
  226.         }
  227.     }
  228.     echo "\n";
  229. }  // convert_lang_to_locale(-)
  230.  
  231.  
  232. /**
  233.  * upgrade_b2evo_tables(-)
  234.  */
  235. function upgrade_b2evo_tables()
  236. {
  237.     global $db_config, $tableprefix;
  238.     global $baseurl, $old_db_version, $new_db_version;
  239.     global $Group_Admins, $Group_Privileged, $Group_Bloggers, $Group_Users;
  240.     global $locales, $default_locale;
  241.     global $DB;
  242.     global $admin_url;
  243.  
  244.     // used for defaults, when upgrading to 1.6
  245.     global $use_fileupload, $fileupload_allowedtypes, $fileupload_maxk, $doubleCheckReferers;
  246.  
  247.     // new DB-delta functionality
  248.     global $schema_queries, $inc_path;
  249.  
  250.     // Load DB schema from modules
  251.     load_db_schema();
  252.  
  253.     load_funcs('_core/model/db/_upgrade.funcs.php');
  254.  
  255.  
  256.     echo '<p>'.T_('Checking DB schema version...').' ';
  257.     $old_db_version = get_db_version();
  258.  
  259.     if( empty($old_db_version) )
  260.     {
  261.         echo '<p><strong>OOPS! b2evolution doesn\'t seem to be installed yet.</strong></p>';
  262.         return;
  263.     }
  264.  
  265.     echo $old_db_version, ' : ';
  266.  
  267.     if( $old_db_version < 8000 ) debug_die( T_('This version is too old!') );
  268.     if( $old_db_version > $new_db_version ) debug_die( T_('This version is too recent! We cannot downgrade to the version you are trying to install...') );
  269.     echo "OK.<br />\n";
  270.  
  271.  
  272.     // Try to obtain some serious time to do some serious processing (5 minutes)
  273.     @set_time_limit( 300 );
  274.  
  275.  
  276.  
  277.     if( $old_db_version < 8010 )
  278.     {
  279.         echo 'Upgrading users table... ';
  280.         $query = "ALTER TABLE T_users
  281.                             MODIFY COLUMN user_pass CHAR(32) NOT NULL";
  282.         $DB->query( $query );
  283.         echo "OK.<br />\n";
  284.  
  285.         echo 'Upgrading blogs table... ';
  286.         $query = "ALTER TABLE T_blogs
  287.                             MODIFY COLUMN blog_lang VARCHAR(20) NOT NULL DEFAULT 'en_US',
  288.                             MODIFY COLUMN blog_longdesc TEXT NULL DEFAULT NULL";
  289.         $DB->query( $query );
  290.         echo "OK.<br />\n";
  291.  
  292.         echo 'Upgrading categories table... ';
  293.         $query = "ALTER TABLE T_categories
  294.                             ADD COLUMN cat_description VARCHAR(250) NULL DEFAULT NULL,
  295.                             ADD COLUMN cat_longdesc TEXT NULL DEFAULT NULL,
  296.                             ADD COLUMN cat_icon VARCHAR(30) NULL DEFAULT NULL";
  297.         $DB->query( $query );
  298.         echo "OK.<br />\n";
  299.  
  300.         echo 'Upgrading posts table... ';
  301.         $query = "ALTER TABLE {$tableprefix}posts
  302.                             MODIFY COLUMN post_lang VARCHAR(20) NOT NULL DEFAULT 'en_US',
  303.                             ADD COLUMN post_urltitle VARCHAR(50) NULL DEFAULT NULL AFTER post_title,
  304.                             ADD COLUMN post_url VARCHAR(250) NULL DEFAULT NULL AFTER post_urltitle,
  305.                             ADD COLUMN post_comments ENUM('disabled', 'open', 'closed') NOT NULL DEFAULT 'open' AFTER post_wordcount";
  306.         $DB->query( $query );
  307.         echo "OK.<br />\n";
  308.  
  309.         echo 'Generating wordcounts... ';
  310.         load_funcs('items/model/_item.funcs.php');
  311.         $query = "SELECT ID, post_content FROM {$tableprefix}posts WHERE post_wordcount IS NULL";
  312.         $i = 0;
  313.         foreach( $DB->get_results( $query, ARRAY_A ) as $row )
  314.         {
  315.             $query_update_wordcount = "UPDATE {$tableprefix}posts
  316.                                                                 SET post_wordcount = " . bpost_count_words($row['post_content']) . "
  317.                                                                 WHERE ID = " . $row['ID'];
  318.             $DB->query($query_update_wordcount);
  319.             $i++;
  320.         }
  321.         echo "OK. ($i rows updated)<br />\n";
  322.  
  323.         set_upgrade_checkpoint( '8010' );
  324.     }
  325.  
  326.  
  327.     if( $old_db_version < 8020 )
  328.     {
  329.         echo 'Encoding passwords... ';
  330.         $query = "UPDATE T_users
  331.                             SET user_pass = MD5(user_pass)";
  332.         $DB->query( $query );
  333.         echo "OK.<br />\n";
  334.  
  335.         set_upgrade_checkpoint( '8020' );
  336.     }
  337.  
  338.  
  339.     if( $old_db_version < 8030 )
  340.     {
  341.         echo 'Deleting unecessary logs... ';
  342.         $query = "DELETE FROM T_hitlog
  343.                             WHERE hit_ignore = 'badchar'";
  344.         $DB->query( $query );
  345.         echo "OK.<br />\n";
  346.  
  347.         echo 'Updating blog urls... ';
  348.         $query = "SELECT blog_ID, blog_siteurl FROM T_blogs";
  349.         $i = 0;
  350.         foreach( $DB->get_results( $query, ARRAY_A ) as $row )
  351.         {
  352.             $blog_ID = $row['blog_ID'];
  353.             $blog_siteurl = $row['blog_siteurl'];
  354.             // echo $blog_ID.':'.$blog_siteurl;
  355.             if( strpos( $blog_siteurl.'/', $baseurl ) !== 0 )
  356.             { // If not found at position 0
  357.                 echo ' <strong>WARNING: please check blog #', $blog_ID, ' manually.</strong><br /> ';
  358.                 continue;
  359.             }
  360.             // crop off the baseurl:
  361.             $blog_siteurl = evo_substr( $blog_siteurl.'/', evo_strlen($baseurl) );
  362.             // echo ' -> ', $blog_siteurl,'<br />';
  363.  
  364.             $query_update_blog = "UPDATE T_blogs SET blog_siteurl = '$blog_siteurl' WHERE blog_ID = $blog_ID";
  365.             // echo $query_update_blog, '<br />';
  366.             $DB->query( $query_update_blog );
  367.             $i++;
  368.         }
  369.         echo "OK. ($i rows updated)<br />\n";
  370.  
  371.         set_upgrade_checkpoint( '8030' );
  372.     }
  373.  
  374.  
  375.     if( $old_db_version < 8040 )
  376.     { // upgrade to 0.8.7
  377.         echo 'Creating table for Antispam Blackist... ';
  378.         $query = "CREATE TABLE T_antispam (
  379.             aspm_ID bigint(11) NOT NULL auto_increment,
  380.             aspm_string varchar(80) NOT NULL,
  381.             aspm_source enum( 'local','reported','central' ) NOT NULL default 'reported',
  382.             PRIMARY KEY aspm_ID (aspm_ID),
  383.             UNIQUE aspm_string (aspm_string)
  384.         )";
  385.         $DB->query( $query );
  386.         echo "OK.<br />\n";
  387.  
  388.         echo 'Creating default blacklist entries... ';
  389.         // This string contains antispam information that is obfuscated because some hosting
  390.         // companies prevent uploading PHP files containing "spam" strings.
  391.         // pre_dump(get_antispam_query());
  392.         $query = get_antispam_query();
  393.         $DB->query( $query );
  394.         echo "OK.<br />\n";
  395.  
  396.         echo 'Upgrading Settings table... ';
  397.         $query = "ALTER TABLE T_settings
  398.                             ADD COLUMN last_antispam_update datetime NOT NULL default '2000-01-01 00:00:00'";
  399.         $DB->query( $query );
  400.         echo "OK.<br />\n";
  401.  
  402.         set_upgrade_checkpoint( '8040' );
  403.     }
  404.  
  405.  
  406.     if( $old_db_version < 8050 )
  407.     { // upgrade to 0.8.9
  408.         echo 'Upgrading blogs table... ';
  409.         $query = "ALTER TABLE T_blogs
  410.                             ADD COLUMN blog_allowtrackbacks tinyint(1) NOT NULL default 1,
  411.                             ADD COLUMN blog_allowpingbacks tinyint(1) NOT NULL default 0,
  412.                             ADD COLUMN blog_pingb2evonet tinyint(1) NOT NULL default 0,
  413.                             ADD COLUMN blog_pingtechnorati tinyint(1) NOT NULL default 0,
  414.                             ADD COLUMN blog_pingweblogs tinyint(1) NOT NULL default 0,
  415.                             ADD COLUMN blog_pingblodotgs tinyint(1) NOT NULL default 0,
  416.                             ADD COLUMN blog_disp_bloglist tinyint NOT NULL DEFAULT 1";
  417.         $DB->query( $query );
  418.         echo "OK.<br />\n";
  419.  
  420.         // Create User Groups
  421.         global $Group_Admins, $Group_Privileged, $Group_Bloggers, $Group_Users;
  422.         echo 'Creating table for Groups... ';
  423.         $query = "CREATE TABLE T_groups (
  424.             grp_ID int(11) NOT NULL auto_increment,
  425.             grp_name varchar(50) NOT NULL default '',
  426.             grp_perm_admin enum('none','hidden','visible') NOT NULL default 'visible',
  427.             grp_perm_blogs enum('user','viewall','editall') NOT NULL default 'user',
  428.             grp_perm_stats enum('none','view','edit') NOT NULL default 'none',
  429.             grp_perm_spamblacklist enum('none','view','edit') NOT NULL default 'none',
  430.             grp_perm_options enum('none','view','edit') NOT NULL default 'none',
  431.             grp_perm_users enum('none','view','edit') NOT NULL default 'none',
  432.             grp_perm_templates TINYINT NOT NULL DEFAULT 0,
  433.             grp_perm_files enum('none','view','add','edit') NOT NULL default 'none',
  434.             PRIMARY KEY grp_ID (grp_ID)
  435.         )";
  436.         $DB->query( $query );
  437.         echo "OK.<br />\n";
  438.  
  439.         // This table needs to be created here for proper group insertion
  440.         task_begin( 'Creating table for Group Settings... ' );
  441.         $DB->query( "CREATE TABLE T_groups__groupsettings (
  442.             gset_grp_ID INT(11) UNSIGNED NOT NULL,
  443.             gset_name VARCHAR(30) NOT NULL,
  444.             gset_value VARCHAR(255) NULL,
  445.             PRIMARY KEY (gset_grp_ID, gset_name)
  446.         ) ENGINE = innodb" );
  447.         task_end();
  448.  
  449.         echo 'Creating default groups... ';
  450.         $Group_Admins = new Group(); // COPY !
  451.         $Group_Admins->set( 'name', 'Administrators' );
  452.         $Group_Admins->set( 'perm_admin', 'visible' );
  453.         $Group_Admins->set( 'perm_blogs', 'editall' );
  454.         $Group_Admins->set( 'perm_stats', 'edit' );
  455.         $Group_Admins->set( 'perm_spamblacklist', 'edit' );
  456.         $Group_Admins->set( 'perm_files', 'all' );
  457.         $Group_Admins->set( 'perm_options', 'edit' );
  458.         $Group_Admins->set( 'perm_templates', 1 );
  459.         $Group_Admins->set( 'perm_users', 'edit' );
  460.         $Group_Admins->dbinsert();
  461.  
  462.         $Group_Privileged = new Group(); // COPY !
  463.         $Group_Privileged->set( 'name', 'Privileged Bloggers' );
  464.         $Group_Privileged->set( 'perm_admin', 'visible' );
  465.         $Group_Privileged->set( 'perm_blogs', 'viewall' );
  466.         $Group_Privileged->set( 'perm_stats', 'view' );
  467.         $Group_Privileged->set( 'perm_spamblacklist', 'edit' );
  468.         $Group_Privileged->set( 'perm_files', 'add' );
  469.         $Group_Privileged->set( 'perm_options', 'view' );
  470.         $Group_Privileged->set( 'perm_templates', 0 );
  471.         $Group_Privileged->set( 'perm_users', 'view' );
  472.         $Group_Privileged->dbinsert();
  473.  
  474.         $Group_Bloggers = new Group(); // COPY !
  475.         $Group_Bloggers->set( 'name', 'Bloggers' );
  476.         $Group_Bloggers->set( 'perm_admin', 'visible' );
  477.         $Group_Bloggers->set( 'perm_blogs', 'user' );
  478.         $Group_Bloggers->set( 'perm_stats', 'none' );
  479.         $Group_Bloggers->set( 'perm_spamblacklist', 'view' );
  480.         $Group_Bloggers->set( 'perm_files', 'view' );
  481.         $Group_Bloggers->set( 'perm_options', 'none' );
  482.         $Group_Bloggers->set( 'perm_templates', 0 );
  483.         $Group_Bloggers->set( 'perm_users', 'none' );
  484.         $Group_Bloggers->dbinsert();
  485.  
  486.         $Group_Users = new Group(); // COPY !
  487.         $Group_Users->set( 'name', 'Basic Users' );
  488.         $Group_Users->set( 'perm_admin', 'none' );
  489.         $Group_Users->set( 'perm_blogs', 'user' );
  490.         $Group_Users->set( 'perm_stats', 'none' );
  491.         $Group_Users->set( 'perm_spamblacklist', 'none' );
  492.         $Group_Users->set( 'perm_files', 'none' );
  493.         $Group_Users->set( 'perm_options', 'none' );
  494.         $Group_Users->set( 'perm_templates', 0 );
  495.         $Group_Users->set( 'perm_users', 'none' );
  496.         $Group_Users->dbinsert();
  497.         echo "OK.<br />\n";
  498.  
  499.  
  500.         echo 'Creating table for Blog-User permissions... ';
  501.         $query = "CREATE TABLE T_coll_user_perms (
  502.             bloguser_blog_ID int(11) unsigned NOT NULL default 0,
  503.             bloguser_user_ID int(11) unsigned NOT NULL default 0,
  504.             bloguser_ismember tinyint NOT NULL default 0,
  505.             bloguser_perm_poststatuses set('published','deprecated','protected','private','draft') NOT NULL default '',
  506.             bloguser_perm_delpost tinyint NOT NULL default 0,
  507.             bloguser_perm_comments tinyint NOT NULL default 0,
  508.             bloguser_perm_cats tinyint NOT NULL default 0,
  509.             bloguser_perm_properties tinyint NOT NULL default 0,
  510.             bloguser_perm_media_upload tinyint NOT NULL default 0,
  511.             bloguser_perm_media_browse tinyint NOT NULL default 0,
  512.             bloguser_perm_media_change tinyint NOT NULL default 0,
  513.             PRIMARY KEY bloguser_pk (bloguser_blog_ID,bloguser_user_ID)
  514.         )";
  515.         $DB->query( $query );
  516.         echo "OK.<br />\n";
  517.         $tablegroups_isuptodate = true;
  518.         $tableblogusers_isuptodate = true;
  519.  
  520.         echo 'Creating user blog permissions... ';
  521.         // Admin: full rights for all blogs (look 'ma, doing a natural join! :>)
  522.         $query = "INSERT INTO T_coll_user_perms( bloguser_blog_ID, bloguser_user_ID, bloguser_ismember,
  523.                                 bloguser_perm_poststatuses, bloguser_perm_delpost, bloguser_perm_comments,
  524.                                 bloguser_perm_cats, bloguser_perm_properties)
  525.                             SELECT blog_ID, ID, 1, 'published,deprecated,protected,private,draft', 1, 1, 1, 1
  526.                             FROM T_users, T_blogs
  527.                             WHERE user_level = 10";
  528.         $DB->query( $query );
  529.  
  530.         // Normal users: basic rights for all blogs (can't stop doing joins :P)
  531.         $query = "INSERT INTO T_coll_user_perms( bloguser_blog_ID, bloguser_user_ID, bloguser_ismember,
  532.                                 bloguser_perm_poststatuses, bloguser_perm_delpost, bloguser_perm_comments,
  533.                                 bloguser_perm_cats, bloguser_perm_properties)
  534.                             SELECT blog_ID, ID, 1, 'published,protected,private,draft', 0, 1, 0, 0
  535.                             FROM T_users, T_blogs
  536.                             WHERE user_level > 0 AND user_level < 10";
  537.         $DB->query( $query );
  538.         echo "OK.<br />\n";
  539.  
  540.         echo 'Upgrading users table... ';
  541.         $query = "ALTER TABLE T_users
  542.                             ADD COLUMN user_notify tinyint(1) NOT NULL default 1,
  543.                             ADD COLUMN user_grp_ID int(4) NOT NULL default 1,
  544.                             MODIFY COLUMN user_idmode varchar(20) NOT NULL DEFAULT 'login',
  545.                             ADD KEY user_grp_ID (user_grp_ID)";
  546.         $DB->query( $query );
  547.         echo "OK.<br />\n";
  548.  
  549.         echo 'Assigning user groups... ';
  550.  
  551.         // Default is 1, so admins are already set.
  552.  
  553.         // Basic Users:
  554.         $query = "UPDATE T_users
  555.                             SET user_grp_ID = $Group_Users->ID
  556.                             WHERE user_level = 0";
  557.         $DB->query( $query );
  558.  
  559.         // Bloggers:
  560.         $query = "UPDATE T_users
  561.                             SET user_grp_ID = $Group_Bloggers->ID
  562.                             WHERE user_level > 0 AND user_level < 10";
  563.         $DB->query( $query );
  564.  
  565.         echo "OK.<br />\n";
  566.  
  567.         echo 'Upgrading settings table... ';
  568.         $query = "ALTER TABLE T_settings
  569.                             DROP COLUMN time_format,
  570.                             DROP COLUMN date_format,
  571.                             ADD COLUMN pref_newusers_grp_ID int unsigned DEFAULT 4 NOT NULL,
  572.                             ADD COLUMN pref_newusers_level tinyint unsigned DEFAULT 1 NOT NULL,
  573.                             ADD COLUMN pref_newusers_canregister tinyint unsigned DEFAULT 0 NOT NULL";
  574.         $DB->query( $query );
  575.         echo "OK.<br />\n";
  576.  
  577.         set_upgrade_checkpoint( '8050' );
  578.     }
  579.  
  580.  
  581.     if( $old_db_version < 8060 )
  582.     { // upgrade to 0.9
  583.         // Important check:
  584.         $stub_list = $DB->get_col( "
  585.             SELECT blog_stub
  586.               FROM T_blogs
  587.              GROUP BY blog_stub
  588.             HAVING COUNT(*) > 1" );
  589.         if( !empty($stub_list) )
  590.         {
  591.             echo '<div class="error"><p class="error">';
  592.             printf( T_("It appears that the following blog stub names are used more than once: ['%s']" ), implode( "','", $stub_list ) );
  593.             echo '</p><p>';
  594.             printf( T_("I can't upgrade until you make them unique. DB field: [%s]" ), $db_config['aliases']['T_blogs'].'.blog_stub' );
  595.             echo '</p></div>';
  596.             return false;
  597.         }
  598.  
  599.         // Create locales
  600.         echo 'Creating table for Locales... ';
  601.         $query = "CREATE TABLE T_locales (
  602.                 loc_locale varchar(20) NOT NULL default '',
  603.                 loc_charset varchar(15) NOT NULL default 'iso-8859-1',
  604.                 loc_datefmt varchar(10) NOT NULL default 'y-m-d',
  605.                 loc_timefmt varchar(10) NOT NULL default 'H:i:s',
  606.                 loc_name varchar(40) NOT NULL default '',
  607.                 loc_messages varchar(20) NOT NULL default '',
  608.                 loc_priority tinyint(4) UNSIGNED NOT NULL default '0',
  609.                 loc_enabled tinyint(4) NOT NULL default '1',
  610.                 PRIMARY KEY loc_locale( loc_locale )
  611.             ) COMMENT='saves available locales'";
  612.         $DB->query( $query );
  613.         echo "OK.<br />\n";
  614.  
  615.         echo 'Upgrading posts table... ';
  616.         $query = "UPDATE {$tableprefix}posts
  617.                             SET post_urltitle = NULL";
  618.         $DB->query( $query );
  619.  
  620.         $query = "ALTER TABLE {$tableprefix}posts
  621.                             CHANGE COLUMN post_date post_issue_date datetime NOT NULL default '1000-01-01 00:00:00',
  622.                             ADD COLUMN post_mod_date datetime NOT NULL default '1000-01-01 00:00:00'
  623.                                         AFTER post_issue_date,
  624.                             CHANGE COLUMN post_lang post_locale varchar(20) NOT NULL default 'en-EU',
  625.                             DROP COLUMN post_url,
  626.                             CHANGE COLUMN post_trackbacks post_url varchar(250) NULL default NULL,
  627.                             MODIFY COLUMN post_flags SET( 'pingsdone', 'imported' ),
  628.                             ADD COLUMN post_renderers VARCHAR(179) NOT NULL default 'default',
  629.                             DROP INDEX post_date,
  630.                             ADD INDEX post_issue_date( post_issue_date ),
  631.                             ADD UNIQUE post_urltitle( post_urltitle )";
  632.         $DB->query( $query );
  633.  
  634.         $query = "UPDATE {$tableprefix}posts
  635.                             SET post_mod_date = post_issue_date";
  636.         $DB->query( $query );
  637.         echo "OK.<br />\n";
  638.  
  639.         // convert given languages to locales
  640.         convert_lang_to_locale( "{$tableprefix}posts", 'post_locale', 'ID' );
  641.  
  642.         echo 'Upgrading blogs table... ';
  643.         $query = "ALTER TABLE T_blogs
  644.                             CHANGE blog_lang blog_locale varchar(20) NOT NULL default 'en-EU',
  645.                             CHANGE blog_roll blog_notes TEXT NULL,
  646.                             MODIFY COLUMN blog_default_skin VARCHAR(30) NOT NULL DEFAULT 'custom',
  647.                             DROP COLUMN blog_filename,
  648.                             ADD COLUMN blog_access_type VARCHAR(10) NOT NULL DEFAULT 'index.php' AFTER blog_locale,
  649.                             ADD COLUMN blog_force_skin tinyint(1) NOT NULL default 0 AFTER blog_default_skin,
  650.                             ADD COLUMN blog_in_bloglist tinyint(1) NOT NULL DEFAULT 1 AFTER blog_disp_bloglist,
  651.                             ADD COLUMN blog_links_blog_ID INT(4) NOT NULL DEFAULT 0,
  652.                             ADD UNIQUE KEY blog_stub (blog_stub)";
  653.         $DB->query( $query );
  654.  
  655.         $query = "UPDATE T_blogs
  656.                             SET blog_access_type = 'stub',
  657.                                     blog_default_skin = 'custom'";
  658.         $DB->query( $query );
  659.  
  660.         echo "OK.<br />\n";
  661.  
  662.         // convert given languages to locales
  663.         convert_lang_to_locale( 'T_blogs', 'blog_locale', 'blog_ID' );
  664.  
  665.  
  666.         echo 'Converting settings table... ';
  667.  
  668.         // get old settings
  669.         $query = 'SELECT * FROM T_settings';
  670.         $row = $DB->get_row( $query, ARRAY_A );
  671.  
  672.         #echo 'oldrow:<br />'; pre_dump($row);
  673.         $transform = array(
  674.             'posts_per_page' => array(5),      // note: moved to blogsettings in 2.0
  675.             'what_to_show' => array('posts'),  // note: moved to blogsettings in 2.0
  676.             'archive_mode' => array('monthly'),// note: moved to blogsettings in 2.0
  677.             'time_difference' => array(0),
  678.             'AutoBR' => array(0),
  679.             'last_antispam_update' => array('2000-01-01 00:00:00', 'antispam_last_update'),
  680.             'pref_newusers_grp_ID' => array($Group_Users->ID, 'newusers_grp_ID'),
  681.             'pref_newusers_level'  => array(1, 'newusers_level'),
  682.             'pref_newusers_canregister' => array(0, 'newusers_canregister'),
  683.         );
  684.  
  685.         $_trans = array();
  686.         foreach( $transform as $oldkey => $newarr )
  687.         {
  688.             $newname = ( isset($newarr[1]) ? $newarr[1] : $oldkey );
  689.             if( !isset( $row[$oldkey] ) )
  690.             {
  691.                 echo '&nbsp;&middot;Setting '.$oldkey.' not found, using defaults.<br />';
  692.                 $_trans[ $newname ] = $newarr[0];
  693.             }
  694.             else
  695.             {
  696.                 $_trans[ $newname ] = $row[$oldkey];
  697.             }
  698.         }
  699.  
  700.         // drop old table
  701.         $DB->query( 'DROP TABLE IF EXISTS T_settings' );
  702.  
  703.         // create new table
  704.         $DB->query(
  705.             'CREATE TABLE T_settings (
  706.                 set_name VARCHAR( 30 ) NOT NULL ,
  707.                 set_value VARCHAR( 255 ) NULL ,
  708.                 PRIMARY KEY ( set_name )
  709.             )');
  710.  
  711.         // insert defaults and use transformed settings
  712.         create_default_settings( $_trans );
  713.  
  714.         if( !isset( $tableblogusers_isuptodate ) )
  715.         {
  716.             echo 'Upgrading Blog-User permissions table... ';
  717.             $query = "ALTER TABLE T_coll_user_perms
  718.                                 ADD COLUMN bloguser_ismember tinyint NOT NULL default 0 AFTER bloguser_user_ID";
  719.             $DB->query( $query );
  720.  
  721.             // Any row that is created holds at least one permission,
  722.             // minimum permsission is to be a member, so we add that one too, to all existing rows.
  723.             $DB->query( "UPDATE T_coll_user_perms
  724.                                             SET bloguser_ismember = 1" );
  725.             echo "OK.<br />\n";
  726.         }
  727.  
  728.         echo 'Upgrading Comments table... ';
  729.         $query = "ALTER TABLE T_comments
  730.                             ADD COLUMN comment_author_ID int unsigned NULL default NULL AFTER comment_status,
  731.                             MODIFY COLUMN comment_author varchar(100) NULL,
  732.                             MODIFY COLUMN comment_author_email varchar(100) NULL,
  733.                             MODIFY COLUMN comment_author_url varchar(100) NULL,
  734.                             MODIFY COLUMN comment_author_IP varchar(23) NOT NULL default ''";
  735.         $DB->query( $query );
  736.         echo "OK.<br />\n";
  737.  
  738.         echo 'Upgrading Users table... ';
  739.         $query = "ALTER TABLE T_users ADD user_locale VARCHAR( 20 ) DEFAULT 'en-EU' NOT NULL AFTER user_yim";
  740.         $DB->query( $query );
  741.         echo "OK.<br />\n";
  742.  
  743.         set_upgrade_checkpoint( '8060' );
  744.     }
  745.  
  746.  
  747.     if( $old_db_version < 8062 )
  748.     { // upgrade to 0.9.0.4
  749.         echo "Checking for extra quote escaping in posts... ";
  750.         $query = "SELECT ID, post_title, post_content
  751.                                 FROM {$tableprefix}posts
  752.                              WHERE post_title LIKE '%\\\\\\\\\'%'
  753.                                     OR post_title LIKE '%\\\\\\\\\"%'
  754.                                     OR post_content LIKE '%\\\\\\\\\'%'
  755.                                     OR post_content LIKE '%\\\\\\\\\"%' ";
  756.         /* FP: the above looks overkill, but MySQL is really full of surprises...
  757.                         tested on 4.0.14-nt */
  758.         // echo $query;
  759.         $rows = $DB->get_results( $query, ARRAY_A );
  760.         if( $DB->num_rows )
  761.         {
  762.             echo 'Updating '.$DB->num_rows.' posts... ';
  763.             foreach( $rows as $row )
  764.             {
  765.                 // echo '<br />'.$row['post_title'];
  766.                 $query = "UPDATE {$tableprefix}posts
  767.                                     SET post_title = ".$DB->quote( stripslashes( $row['post_title'] ) ).",
  768.                                             post_content = ".$DB->quote( stripslashes( $row['post_content'] ) )."
  769.                                     WHERE ID = ".$row['ID'];
  770.                 // echo '<br />'.$query;
  771.                 $DB->query( $query );
  772.             }
  773.         }
  774.         echo "OK.<br />\n";
  775.  
  776.         set_upgrade_checkpoint( '8062' );
  777.     }
  778.  
  779.  
  780.     if( $old_db_version < 8064 )
  781.     { // upgrade to 0.9.0.6
  782.         cleanup_comment_quotes();
  783.  
  784.         set_upgrade_checkpoint( '8064' );
  785.     }
  786.  
  787.  
  788.     if( $old_db_version < 8066 )
  789.     {   // upgrade to 0.9.1
  790.         echo 'Adding catpost index... ';
  791.         $DB->query( 'ALTER TABLE T_postcats ADD UNIQUE catpost ( postcat_cat_ID, postcat_post_ID )' );
  792.         echo "OK.<br />\n";
  793.  
  794.         set_upgrade_checkpoint( '8066' );
  795.     }
  796.  
  797.  
  798.     if( $old_db_version < 8800 )
  799.     { // ---------------------------------- upgrade to 1.6 "phoenix ALPHA"
  800.  
  801.         echo 'Dropping old Hitlog table... ';
  802.         $DB->query( 'DROP TABLE IF EXISTS T_hitlog' );
  803.         echo "OK.<br />\n";
  804.  
  805.         // New tables:
  806.             echo 'Creating table for active sessions... ';
  807.             $DB->query( "CREATE TABLE T_sessions (
  808.                                             sess_ID        INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  809.                                             sess_key       CHAR(32) NULL,
  810.                                             sess_lastseen  DATETIME NOT NULL,
  811.                                             sess_ipaddress VARCHAR(15) NOT NULL DEFAULT '',
  812.                                             sess_user_ID   INT(10) DEFAULT NULL,
  813.                                             sess_agnt_ID   INT UNSIGNED NULL,
  814.                                             sess_data      TEXT DEFAULT NULL,
  815.                                             PRIMARY KEY( sess_ID )
  816.                                         )" );
  817.             echo "OK.<br />\n";
  818.  
  819.  
  820.             echo 'Creating user settings table... ';
  821.             $DB->query( "CREATE TABLE {$tableprefix}usersettings (
  822.                                             uset_user_ID INT(11) UNSIGNED NOT NULL,
  823.                                             uset_name    VARCHAR( 30 ) NOT NULL,
  824.                                             uset_value   VARCHAR( 255 ) NULL,
  825.                                             PRIMARY KEY ( uset_user_ID, uset_name )
  826.                                         )");
  827.             echo "OK.<br />\n";
  828.  
  829.  
  830.             echo 'Creating plugins table... ';
  831.             $DB->query( "CREATE TABLE T_plugins (
  832.                                             plug_ID        INT(11) UNSIGNED NOT NULL auto_increment,
  833.                                             plug_priority  INT(11) NOT NULL default 50,
  834.                                             plug_classname VARCHAR(40) NOT NULL default '',
  835.                                             PRIMARY KEY ( plug_ID )
  836.                                         )");
  837.             echo "OK.<br />\n";
  838.  
  839.  
  840.             echo 'Creating table for Post Statuses... ';
  841.             $query="CREATE TABLE {$tableprefix}poststatuses (
  842.                                             pst_ID   int(11) unsigned not null AUTO_INCREMENT,
  843.                                             pst_name varchar(30)      not null,
  844.                                             primary key ( pst_ID )
  845.                                         )";
  846.             $DB->query( $query );
  847.             echo "OK.<br />\n";
  848.  
  849.  
  850.             echo 'Creating table for Post Types... ';
  851.             $query="CREATE TABLE {$tableprefix}posttypes (
  852.                                             ptyp_ID   int(11) unsigned not null AUTO_INCREMENT,
  853.                                             ptyp_name varchar(30)      not null,
  854.                                             primary key (ptyp_ID)
  855.                                         )";
  856.             $DB->query( $query );
  857.             echo "OK.<br />\n";
  858.  
  859.  
  860.             echo 'Creating table for File Meta Data... ';
  861.             $DB->query( "CREATE TABLE T_files (
  862.                                          file_ID        int(11) unsigned  not null AUTO_INCREMENT,
  863.                                          file_root_type enum('absolute','user','group','collection') not null default 'absolute',
  864.                                          file_root_ID   int(11) unsigned  not null default 0,
  865.                                          file_path      varchar(255)      not null default '',
  866.                                          file_title     varchar(255),
  867.                                          file_alt       varchar(255),
  868.                                          file_desc      text,
  869.                                          primary key (file_ID),
  870.                                          unique file (file_root_type, file_root_ID, file_path)
  871.                                     )" );
  872.             echo "OK.<br />\n";
  873.  
  874.  
  875.             echo 'Creating table for base domains... ';
  876.             $DB->query( "CREATE TABLE T_basedomains (
  877.                                         dom_ID     INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  878.                                         dom_name   VARCHAR(250) NOT NULL DEFAULT '',
  879.                                         dom_status ENUM('unknown','whitelist','blacklist') NOT NULL DEFAULT 'unknown',
  880.                                         dom_type   ENUM('unknown','normal','searcheng','aggregator') NOT NULL DEFAULT 'unknown',
  881.                                         PRIMARY KEY (dom_ID),
  882.                                         UNIQUE dom_name (dom_name)
  883.                                     )" );   // fp> the unique key was only named in version 1.9. Crap. Put the name back here to save as many souls as possible. bulk has not upgraded from 0.9 yet :/
  884.             echo "OK.<br />\n";
  885.  
  886.         set_upgrade_checkpoint( '8820' );
  887.     }
  888.  
  889.  
  890.     if( $old_db_version < 8840 )
  891.     {
  892.  
  893.             echo 'Creating table for user agents... ';
  894.             $DB->query( "CREATE TABLE {$tableprefix}useragents (
  895.                                         agnt_ID        INT UNSIGNED NOT NULL AUTO_INCREMENT,
  896.                                         agnt_signature VARCHAR(250) NOT NULL,
  897.                                         agnt_type      ENUM('rss','robot','browser','unknown') DEFAULT 'unknown' NOT NULL,
  898.                                         PRIMARY KEY (agnt_ID) )" );
  899.             echo "OK.<br />\n";
  900.  
  901.  
  902.             echo 'Creating table for Hit-Logs... ';
  903.             $query = "CREATE TABLE T_hitlog (
  904.                                     hit_ID             INT(11) NOT NULL AUTO_INCREMENT,
  905.                                     hit_sess_ID        INT UNSIGNED,
  906.                                     hit_datetime       DATETIME NOT NULL,
  907.                                     hit_uri            VARCHAR(250) DEFAULT NULL,
  908.                                     hit_referer_type   ENUM('search','blacklist','referer','direct','spam') NOT NULL,
  909.                                     hit_referer        VARCHAR(250) DEFAULT NULL,
  910.                                     hit_referer_dom_ID INT UNSIGNED DEFAULT NULL,
  911.                                     hit_blog_ID        int(11) UNSIGNED NULL DEFAULT NULL,
  912.                                     hit_remote_addr    VARCHAR(40) DEFAULT NULL,
  913.                                     PRIMARY KEY (hit_ID),
  914.                                     INDEX hit_datetime ( hit_datetime ),
  915.                                     INDEX hit_blog_ID (hit_blog_ID)
  916.                                 )";
  917.             $DB->query( $query );
  918.             echo "OK.<br />\n";
  919.  
  920.  
  921.             echo 'Creating table for subscriptions... ';
  922.             $DB->query( "CREATE TABLE T_subscriptions (
  923.                                          sub_coll_ID     int(11) unsigned    not null,
  924.                                          sub_user_ID     int(11) unsigned    not null,
  925.                                          sub_items       tinyint(1)          not null,
  926.                                          sub_comments    tinyint(1)          not null,
  927.                                          primary key (sub_coll_ID, sub_user_ID)
  928.                                         )" );
  929.             echo "OK.<br />\n";
  930.  
  931.  
  932.             echo 'Creating table for blog-group permissions... ';
  933.             $DB->query( "CREATE TABLE T_coll_group_perms (
  934.                                             bloggroup_blog_ID int(11) unsigned NOT NULL default 0,
  935.                                             bloggroup_group_ID int(11) unsigned NOT NULL default 0,
  936.                                             bloggroup_ismember tinyint NOT NULL default 0,
  937.                                             bloggroup_perm_poststatuses set('published','deprecated','protected','private','draft') NOT NULL default '',
  938.                                             bloggroup_perm_delpost tinyint NOT NULL default 0,
  939.                                             bloggroup_perm_comments tinyint NOT NULL default 0,
  940.                                             bloggroup_perm_cats tinyint NOT NULL default 0,
  941.                                             bloggroup_perm_properties tinyint NOT NULL default 0,
  942.                                             bloggroup_perm_media_upload tinyint NOT NULL default 0,
  943.                                             bloggroup_perm_media_browse tinyint NOT NULL default 0,
  944.                                             bloggroup_perm_media_change tinyint NOT NULL default 0,
  945.                                             PRIMARY KEY bloggroup_pk (bloggroup_blog_ID,bloggroup_group_ID) )" );
  946.             echo "OK.<br />\n";
  947.  
  948.  
  949.         echo 'Upgrading blogs table... ';
  950.         $query = "ALTER TABLE T_blogs
  951.                             MODIFY COLUMN blog_ID int(11) unsigned NOT NULL auto_increment,
  952.                             MODIFY COLUMN blog_links_blog_ID INT(11) NULL DEFAULT NULL,
  953.                             CHANGE COLUMN blog_stub blog_urlname VARCHAR(255) NOT NULL DEFAULT 'urlname',
  954.                             ADD COLUMN blog_allowcomments VARCHAR(20) NOT NULL default 'post_by_post' AFTER blog_keywords,
  955.                             ADD COLUMN blog_allowblogcss TINYINT(1) NOT NULL default 1 AFTER blog_allowpingbacks,
  956.                             ADD COLUMN blog_allowusercss TINYINT(1) NOT NULL default 1 AFTER blog_allowblogcss,
  957.                             ADD COLUMN blog_stub VARCHAR(255) NOT NULL DEFAULT 'stub' AFTER blog_staticfilename,
  958.                             ADD COLUMN blog_commentsexpire INT(4) NOT NULL DEFAULT 0 AFTER blog_links_blog_ID,
  959.                             ADD COLUMN blog_media_location ENUM( 'default', 'subdir', 'custom', 'none' ) DEFAULT 'default' NOT NULL AFTER blog_commentsexpire,
  960.                             ADD COLUMN blog_media_subdir VARCHAR( 255 ) NOT NULL AFTER blog_media_location,
  961.                             ADD COLUMN blog_media_fullpath VARCHAR( 255 ) NOT NULL AFTER blog_media_subdir,
  962.                             ADD COLUMN blog_media_url VARCHAR(255) NOT NULL AFTER blog_media_fullpath,
  963.                             DROP INDEX blog_stub,
  964.                             ADD UNIQUE blog_urlname ( blog_urlname )";
  965.         $DB->query( $query );
  966.         echo "OK.<br />\n";
  967.  
  968.         set_upgrade_checkpoint( '8840' );
  969.     }
  970.  
  971.  
  972.     // sam2kb>fp: We need to make sure there are no values like "blog_a.php" in blog_urlname,
  973.     //          after this upgrade blog URLs look like $baseurl.'blog_a.php' which might be OK in 0.x version,
  974.     //          but this config will not work in b2evo 4. Blog URLs will be broken!
  975.     if( $old_db_version < 8850 )
  976.     {
  977.  
  978.         echo 'Updating relative URLs... ';
  979.         // We need to move the slashes to the end:
  980.         $query = "UPDATE T_blogs
  981.                                  SET blog_siteurl = CONCAT( SUBSTRING(blog_siteurl,2) , '/' )
  982.                              WHERE blog_siteurl LIKE '/%'";
  983.         $DB->query( $query );
  984.         echo "OK.<br />\n";
  985.  
  986.         echo 'Copying urlnames to stub names... ';
  987.         $query = 'UPDATE T_blogs
  988.                             SET blog_stub = blog_urlname';
  989.         $DB->query( $query );
  990.         echo "OK.<br />\n";
  991.  
  992.         set_upgrade_checkpoint( '8850' );
  993.     }
  994.  
  995.  
  996.     if( $old_db_version < 8855 )
  997.     {
  998.  
  999.         echo 'Upgrading posts table... ';
  1000.         $query = "ALTER TABLE {$tableprefix}posts
  1001.                             DROP COLUMN post_karma,
  1002.                             DROP COLUMN post_autobr,
  1003.                             DROP INDEX post_author,
  1004.                             DROP INDEX post_issue_date,
  1005.                             DROP INDEX post_category,
  1006.                             CHANGE COLUMN ID post_ID int(11) unsigned NOT NULL auto_increment,
  1007.                             CHANGE COLUMN post_author   post_creator_user_ID int(11) unsigned NOT NULL,
  1008.                             CHANGE COLUMN post_issue_date   post_datestart datetime NOT NULL,
  1009.                             CHANGE COLUMN post_mod_date post_datemodified datetime NOT NULL,
  1010.                             CHANGE COLUMN post_category post_main_cat_ID int(11) unsigned NOT NULL,
  1011.                             ADD post_parent_ID              int(11) unsigned NULL AFTER post_ID,
  1012.                             ADD post_lastedit_user_ID   int(11) unsigned NULL AFTER post_creator_user_ID,
  1013.                             ADD post_assigned_user_ID   int(11) unsigned NULL AFTER post_lastedit_user_ID,
  1014.                             ADD post_datedeadline       datetime NULL AFTER post_datestart,
  1015.                             ADD post_datecreated            datetime NULL AFTER post_datedeadline,
  1016.                             ADD post_pst_ID                     int(11) unsigned NULL AFTER post_status,
  1017.                             ADD post_ptyp_ID                    int(11) unsigned NULL AFTER post_pst_ID,
  1018.                             ADD post_views                      int(11) unsigned NOT NULL DEFAULT 0 AFTER post_flags,
  1019.                             ADD post_commentsexpire     datetime DEFAULT NULL AFTER post_comments,
  1020.                             ADD post_priority                   int(11) unsigned null,
  1021.                             ADD INDEX post_creator_user_ID( post_creator_user_ID ),
  1022.                             ADD INDEX post_parent_ID( post_parent_ID ),
  1023.                             ADD INDEX post_assigned_user_ID( post_assigned_user_ID ),
  1024.                             ADD INDEX post_datestart( post_datestart ),
  1025.                             ADD INDEX post_main_cat_ID( post_main_cat_ID ),
  1026.                             ADD INDEX post_ptyp_ID( post_ptyp_ID ),
  1027.                             ADD INDEX post_pst_ID( post_pst_ID ) ";
  1028.         $DB->query( $query );
  1029.         echo "OK.<br />\n";
  1030.  
  1031.         set_upgrade_checkpoint( '8855' );
  1032.     }
  1033.  
  1034.  
  1035.     if( $old_db_version < 8860 )
  1036.     {
  1037.         echo 'Updating post data... ';
  1038.         $query = "UPDATE {$tableprefix}posts
  1039.                             SET post_lastedit_user_ID = post_creator_user_ID,
  1040.                                     post_datecreated = post_datestart";
  1041.         $DB->query( $query );
  1042.         echo "OK.<br />\n";
  1043.  
  1044.  
  1045.         task_begin( 'Upgrading users table... ' );
  1046.         $DB->query( 'UPDATE T_users
  1047.                                       SET dateYMDhour = \'2000-01-01 00:00:00\'
  1048.                                     WHERE dateYMDhour = \'0000-00-00 00:00:00\'' );
  1049.         $DB->query( 'ALTER TABLE T_users
  1050.                             MODIFY COLUMN dateYMDhour DATETIME NOT NULL DEFAULT \'2000-01-01 00:00:00\',
  1051.                             CHANGE COLUMN ID user_ID int(11) unsigned NOT NULL auto_increment,
  1052.                             MODIFY COLUMN user_icq int(11) unsigned DEFAULT 0 NOT NULL,
  1053.                             ADD COLUMN user_showonline tinyint(1) NOT NULL default 1 AFTER user_notify' );
  1054.         task_end();
  1055.  
  1056.  
  1057.         set_upgrade_checkpoint( '8860' );
  1058.     }
  1059.  
  1060.  
  1061.     if( $old_db_version < 8900 )
  1062.     {
  1063.  
  1064.         echo 'Setting new defaults... ';
  1065.         $query = 'INSERT INTO T_settings (set_name, set_value)
  1066.                             VALUES
  1067.                                 ( "reloadpage_timeout", "300" ),
  1068.                                 ( "upload_enabled", "'.(isset($use_fileupload) ? (int)$use_fileupload : '1').'" ),
  1069.                                 ( "upload_allowedext", "'.(isset($fileupload_allowedtypes) ? $fileupload_allowedtypes : 'jpg gif png').'" ),
  1070.                                 ( "upload_maxkb", "'.(isset($fileupload_maxk) ? (int)$fileupload_maxk : '96').'" )
  1071.                             ';
  1072.         $DB->query( $query );
  1073.         // Replace "paged" mode with "posts" // note: moved to blogsettings in 2.0
  1074.         $DB->query( 'UPDATE T_settings
  1075.                                         SET set_value = "posts"
  1076.                                     WHERE set_name = "what_to_show"
  1077.                                       AND set_value = "paged"' );
  1078.         echo "OK.<br />\n";
  1079.  
  1080.  
  1081.         if( !isset( $tableblogusers_isuptodate ) )
  1082.         {   // We have created the blogusers table before and it's already clean!
  1083.             echo 'Altering table for Blog-User permissions... ';
  1084.             $DB->query( 'ALTER TABLE T_coll_user_perms
  1085.                                         MODIFY COLUMN bloguser_blog_ID int(11) unsigned NOT NULL default 0,
  1086.                                         MODIFY COLUMN bloguser_user_ID int(11) unsigned NOT NULL default 0,
  1087.                                         ADD COLUMN bloguser_perm_media_upload tinyint NOT NULL default 0,
  1088.                                         ADD COLUMN bloguser_perm_media_browse tinyint NOT NULL default 0,
  1089.                                         ADD COLUMN bloguser_perm_media_change tinyint NOT NULL default 0' );
  1090.             echo "OK.<br />\n";
  1091.         }
  1092.  
  1093.  
  1094.         task_begin( 'Altering comments table...' );
  1095.         $DB->query( 'UPDATE T_comments
  1096.                                       SET comment_date = \'2000-01-01 00:00:00\'
  1097.                                     WHERE comment_date = \'0000-00-00 00:00:00\'' );
  1098.         $DB->query( 'ALTER TABLE T_comments
  1099.                                     MODIFY COLUMN comment_date DATETIME NOT NULL DEFAULT \'2000-01-01 00:00:00\',
  1100.                                     MODIFY COLUMN comment_post_ID       int(11) unsigned NOT NULL default 0' );
  1101.         task_end();
  1102.  
  1103.         set_upgrade_checkpoint( '8900' );
  1104.     }
  1105.  
  1106.     if( $old_db_version < 9000 )
  1107.     {
  1108.         echo 'Altering Posts to Categories table... ';
  1109.         $DB->query( "ALTER TABLE T_postcats
  1110.                                     MODIFY COLUMN postcat_post_ID int(11) unsigned NOT NULL,
  1111.                                     MODIFY COLUMN postcat_cat_ID int(11) unsigned NOT NULL" );
  1112.         echo "OK.<br />\n";
  1113.  
  1114.  
  1115.         echo 'Altering Categories table... ';
  1116.         $DB->query( "ALTER TABLE T_categories
  1117.                                     MODIFY COLUMN cat_ID int(11) unsigned NOT NULL auto_increment,
  1118.                                     MODIFY COLUMN cat_parent_ID int(11) unsigned NULL,
  1119.                                     MODIFY COLUMN cat_blog_ID int(11) unsigned NOT NULL default 2" );
  1120.         echo "OK.<br />\n";
  1121.  
  1122.  
  1123.         echo 'Altering Locales table... ';
  1124.         $DB->query( 'ALTER TABLE T_locales
  1125.                                     ADD loc_startofweek TINYINT UNSIGNED NOT NULL DEFAULT 1 AFTER loc_timefmt' );
  1126.         echo "OK.<br />\n";
  1127.  
  1128.  
  1129.         if( !isset( $tablegroups_isuptodate ) )
  1130.         {   // We have created the groups table before and it's already clean!
  1131.             echo 'Altering Groups table... ';
  1132.             $DB->query( "ALTER TABLE T_groups
  1133.                                         ADD COLUMN grp_perm_admin enum('none','hidden','visible') NOT NULL default 'visible' AFTER grp_name,
  1134.                                         ADD COLUMN grp_perm_files enum('none','view','add','edit') NOT NULL default 'none'" );
  1135.             echo "OK.<br />\n";
  1136.         }
  1137.  
  1138.  
  1139.         echo 'Creating table for Post Links... ';
  1140.         $DB->query( "CREATE TABLE T_links (
  1141.                                     link_ID               int(11) unsigned  not null AUTO_INCREMENT,
  1142.                                     link_datecreated      datetime          not null,
  1143.                                     link_datemodified     datetime          not null,
  1144.                                     link_creator_user_ID  int(11) unsigned  not null,
  1145.                                     link_lastedit_user_ID int(11) unsigned  not null,
  1146.                                     link_item_ID          int(11) unsigned  NOT NULL,
  1147.                                     link_dest_item_ID     int(11) unsigned  NULL,
  1148.                                     link_file_ID          int(11) unsigned  NULL,
  1149.                                     link_ltype_ID         int(11) unsigned  NOT NULL default 1,
  1150.                                     link_external_url     VARCHAR(255)      NULL,
  1151.                                     link_title            TEXT              NULL,
  1152.                                     PRIMARY KEY (link_ID),
  1153.                                     INDEX link_item_ID( link_item_ID ),
  1154.                                     INDEX link_dest_item_ID (link_dest_item_ID),
  1155.                                     INDEX link_file_ID (link_file_ID)
  1156.                                 )" );
  1157.         echo "OK.<br />\n";
  1158.  
  1159.  
  1160.         echo 'Creating default Post Types... ';
  1161.         $DB->query( "
  1162.             INSERT INTO {$tableprefix}posttypes ( ptyp_ID, ptyp_name )
  1163.             VALUES ( 1, 'Post' ),
  1164.                    ( 2, 'Link' )" );
  1165.         echo "OK.<br />\n";
  1166.  
  1167.  
  1168.         set_upgrade_checkpoint( '9000' );
  1169.     }
  1170.  
  1171.  
  1172.     if( $old_db_version < 9100 )
  1173.     {   // 1.8 ALPHA
  1174.  
  1175.         echo 'Creating table for plugin events... ';
  1176.         $DB->query( '
  1177.             CREATE TABLE T_pluginevents(
  1178.                     pevt_plug_ID INT(11) UNSIGNED NOT NULL,
  1179.                     pevt_event VARCHAR(40) NOT NULL,
  1180.                     pevt_enabled TINYINT NOT NULL DEFAULT 1,
  1181.                     PRIMARY KEY( pevt_plug_ID, pevt_event )
  1182.                 )' );
  1183.         echo "OK.<br />\n";
  1184.  
  1185.  
  1186.         echo 'Altering Links table... ';
  1187.         $DB->query( 'ALTER TABLE T_links
  1188.                      CHANGE link_item_ID link_itm_ID INT( 11 ) UNSIGNED NOT NULL,
  1189.                      CHANGE link_dest_item_ID link_dest_itm_ID INT( 11 ) UNSIGNED NULL' );
  1190.         echo "OK.<br />\n";
  1191.  
  1192.  
  1193.         if( $old_db_version >= 9000 )
  1194.         { // sess_agnt_ID used in Phoenix-Alpha
  1195.             echo 'Altering sessions table... ';
  1196.             $query = "
  1197.                     ALTER TABLE T_sessions
  1198.                      DROP COLUMN sess_agnt_ID";
  1199.             $DB->query( $query );
  1200.             echo "OK.<br />\n";
  1201.         }
  1202.  
  1203.         echo 'Creating table for file types... ';
  1204.         $DB->query( '
  1205.                 CREATE TABLE T_filetypes (
  1206.                     ftyp_ID int(11) unsigned NOT NULL auto_increment,
  1207.                     ftyp_extensions varchar(30) NOT NULL,
  1208.                     ftyp_name varchar(30) NOT NULL,
  1209.                     ftyp_mimetype varchar(50) NOT NULL,
  1210.                     ftyp_icon varchar(20) default NULL,
  1211.                     ftyp_viewtype varchar(10) NOT NULL,
  1212.                     ftyp_allowed tinyint(1) NOT NULL default 0,
  1213.                     PRIMARY KEY (ftyp_ID)
  1214.                 )' );
  1215.         echo "OK.<br />\n";
  1216.  
  1217.         echo 'Creating default file types... ';
  1218.     // TODO: dh> shouldn't they get localized to the app's default locale?
  1219.         $DB->query( "INSERT INTO T_filetypes
  1220.                 (ftyp_ID, ftyp_extensions, ftyp_name, ftyp_mimetype, ftyp_icon, ftyp_viewtype, ftyp_allowed)
  1221.             VALUES
  1222.                 (1, 'gif', 'GIF image', 'image/gif', 'image2.png', 'image', 1),
  1223.                 (2, 'png', 'PNG image', 'image/png', 'image2.png', 'image', 1),
  1224.                 (3, 'jpg jpeg', 'JPEG image', 'image/jpeg', 'image2.png', 'image', 1),
  1225.                 (4, 'txt', 'Text file', 'text/plain', 'document.png', 'text', 1),
  1226.                 (5, 'htm html', 'HTML file', 'text/html', 'html.png', 'browser', 0),
  1227.                 (6, 'pdf', 'PDF file', 'application/pdf', 'pdf.png', 'browser', 1),
  1228.                 (7, 'doc', 'Microsoft Word file', 'application/msword', 'doc.gif', 'external', 1),
  1229.                 (8, 'xls', 'Microsoft Excel file', 'application/vnd.ms-excel', 'xls.gif', 'external', 1),
  1230.                 (9, 'ppt', 'Powerpoint', 'application/vnd.ms-powerpoint', 'ppt.gif', 'external', 1),
  1231.                 (10, 'pps', 'Slideshow', 'pps', 'pps.gif', 'external', 1),
  1232.                 (11, 'zip', 'ZIP archive', 'application/zip', 'zip.gif', 'external', 1),
  1233.                 (12, 'php php3 php4 php5 php6', 'PHP script', 'application/x-httpd-php', 'php.gif', 'text', 0),
  1234.                 (13, 'css', 'Style sheet', 'text/css', '', 'text', 1)
  1235.             " );
  1236.         echo "OK.<br />\n";
  1237.  
  1238.         echo 'Giving Administrator Group edit perms on files... ';
  1239.         $DB->query( 'UPDATE T_groups
  1240.                      SET grp_perm_files = "edit"
  1241.                      WHERE grp_ID = 1' );
  1242.         // Later versions give 'all' on install, but we won't upgrade to that for security.
  1243.         echo "OK.<br />\n";
  1244.  
  1245.         echo 'Giving Administrator Group full perms on media for all blogs... ';
  1246.         $DB->query( 'UPDATE T_coll_group_perms
  1247.                      SET bloggroup_perm_media_upload = 1,
  1248.                          bloggroup_perm_media_browse = 1,
  1249.                          bloggroup_perm_media_change = 1
  1250.                      WHERE bloggroup_group_ID = 1' );
  1251.         echo "OK.<br />\n";
  1252.  
  1253.  
  1254.         if( $old_db_version >= 9000 )
  1255.         { // Uninstall all ALPHA (potentially incompatible) plugins
  1256.             echo 'Uninstalling all existing plugins... ';
  1257.             $DB->query( 'DELETE FROM T_plugins WHERE 1=1' );
  1258.             echo "OK.<br />\n";
  1259.         }
  1260.  
  1261.         // NOTE: basic plugins get installed separatly for upgrade and install..
  1262.  
  1263.  
  1264.         set_upgrade_checkpoint( '9100' );
  1265.     }
  1266.  
  1267.  
  1268.     if( $old_db_version < 9190 ) // Note: changed from 9200, to include the block below, if DB is not yet on 1.8
  1269.     {   // 1.8 ALPHA (block #2)
  1270.         echo 'Altering Posts table... ';
  1271.         $DB->query( "ALTER TABLE {$tableprefix}posts
  1272.                      CHANGE post_comments post_comment_status ENUM('disabled', 'open', 'closed') NOT NULL DEFAULT 'open'" );
  1273.         echo "OK.<br />\n";
  1274.  
  1275.  
  1276.         set_upgrade_checkpoint( '9190' );
  1277.     }
  1278.  
  1279.  
  1280.     if( $old_db_version < 9192 )
  1281.     { // 1.8 ALPHA (block #3) - The payload that db_delta() handled before
  1282.  
  1283.         // This is a fix, which broke upgrade to 1.8 (from 1.6) in MySQL strict mode (inserted after 1.8 got released!):
  1284.         if( $DB->get_row( 'SHOW COLUMNS FROM T_hitlog LIKE "hit_referer_type"' ) )
  1285.         { // a niiiiiiiice extra check :p
  1286.             task_begin( 'Deleting all "spam" hitlog entries... ' );
  1287.             $DB->query( '
  1288.                     DELETE FROM T_hitlog
  1289.                      WHERE hit_referer_type = "spam"' );
  1290.             task_end();
  1291.         }
  1292.  
  1293.         task_begin( 'Upgrading users table... ' );
  1294.         $DB->query( 'ALTER TABLE T_users
  1295.                                         CHANGE COLUMN user_firstname user_firstname varchar(50) NULL,
  1296.                                         CHANGE COLUMN user_lastname user_lastname varchar(50) NULL,
  1297.                                         CHANGE COLUMN user_nickname user_nickname varchar(50) NULL,
  1298.                                         CHANGE COLUMN user_icq user_icq int(11) unsigned NULL,
  1299.                                         CHANGE COLUMN user_email user_email varchar(255) NOT NULL,
  1300.                                         CHANGE COLUMN user_url user_url varchar(255) NULL,
  1301.                                         CHANGE COLUMN user_ip user_ip varchar(15) NULL,
  1302.                                         CHANGE COLUMN user_domain user_domain varchar(200) NULL,
  1303.                                         CHANGE COLUMN user_browser user_browser varchar(200) NULL,
  1304.                                         CHANGE COLUMN user_aim user_aim varchar(50) NULL,
  1305.                                         CHANGE COLUMN user_msn user_msn varchar(100) NULL,
  1306.                                         CHANGE COLUMN user_yim user_yim varchar(50) NULL,
  1307.                                         ADD COLUMN user_allow_msgform TINYINT NOT NULL DEFAULT \'1\' AFTER user_idmode,
  1308.                                         ADD COLUMN user_validated TINYINT(1) NOT NULL DEFAULT 0 AFTER user_grp_ID' );
  1309.         task_end();
  1310.  
  1311.         task_begin( 'Creating blog settings...' );
  1312.         $DB->query( 'CREATE TABLE T_coll_settings (
  1313.                                                             cset_coll_ID INT(11) UNSIGNED NOT NULL,
  1314.                                                             cset_name    VARCHAR( 30 ) NOT NULL,
  1315.                                                             cset_value   VARCHAR( 255 ) NULL,
  1316.                                                             PRIMARY KEY ( cset_coll_ID, cset_name )
  1317.                                             )' );
  1318.         task_end();
  1319.         set_upgrade_checkpoint( '9192' );
  1320.     }
  1321.  
  1322.  
  1323.     if( $old_db_version < 9195 )
  1324.     {
  1325.         task_begin( 'Upgrading posts table... ' );
  1326.         $DB->query( 'ALTER TABLE '.$tableprefix.'posts
  1327.                                         CHANGE COLUMN post_content post_content         text NULL,
  1328.                                         CHANGE COLUMN post_url post_url                     VARCHAR(255) NULL DEFAULT NULL,
  1329.                                         CHANGE COLUMN post_renderers post_renderers     TEXT NOT NULL' );
  1330.         task_end();
  1331.  
  1332.         task_begin( 'Upgrading comments table... ' );
  1333.         $DB->query( 'ALTER TABLE T_comments
  1334.                                         CHANGE COLUMN comment_author_email comment_author_email varchar(255) NULL,
  1335.                                         CHANGE COLUMN comment_author_url comment_author_url varchar(255) NULL,
  1336.                                         ADD COLUMN comment_spam_karma TINYINT NULL AFTER comment_karma,
  1337.                                         ADD COLUMN comment_allow_msgform TINYINT NOT NULL DEFAULT 0 AFTER comment_spam_karma' );
  1338.         task_end();
  1339.  
  1340.         set_upgrade_checkpoint( '9195' );
  1341.     }
  1342.  
  1343.  
  1344.     if( $old_db_version < 9200 )
  1345.     {
  1346.         task_begin( 'Upgrading hitlog table... ' );
  1347.         $DB->query( 'ALTER TABLE T_hitlog
  1348.                                         CHANGE COLUMN hit_referer_type hit_referer_type   ENUM(\'search\',\'blacklist\',\'referer\',\'direct\') NOT NULL,
  1349.                                         ADD COLUMN hit_agnt_ID        INT UNSIGNED NULL AFTER hit_remote_addr' );
  1350.         task_end();
  1351.  
  1352.         task_begin( 'Upgrading post links table... ' );
  1353.         $DB->query( 'ALTER TABLE T_links
  1354.                                         ADD INDEX link_itm_ID( link_itm_ID ),
  1355.                                         ADD INDEX link_dest_itm_ID (link_dest_itm_ID)' );
  1356.         task_end();
  1357.  
  1358.         task_begin( 'Upgrading plugins table... ' );
  1359.         $DB->query( 'ALTER TABLE T_plugins
  1360.                                         CHANGE COLUMN plug_priority plug_priority        TINYINT NOT NULL default 50,
  1361.                                         ADD COLUMN plug_code            VARCHAR(32) NULL AFTER plug_classname,
  1362.                                         ADD COLUMN plug_apply_rendering ENUM( \'stealth\', \'always\', \'opt-out\', \'opt-in\', \'lazy\', \'never\' ) NOT NULL DEFAULT \'never\' AFTER plug_code,
  1363.                                         ADD COLUMN plug_version         VARCHAR(42) NOT NULL default \'0\' AFTER plug_apply_rendering,
  1364.                                         ADD COLUMN plug_status          ENUM( \'enabled\', \'disabled\', \'needs_config\', \'broken\' ) NOT NULL AFTER plug_version,
  1365.                                         ADD COLUMN plug_spam_weight     TINYINT UNSIGNED NOT NULL DEFAULT 1 AFTER plug_status,
  1366.                                         ADD UNIQUE plug_code( plug_code ),
  1367.                                         ADD INDEX plug_status( plug_status )' );
  1368.         task_end();
  1369.  
  1370.         task_begin( 'Creating plugin settings table... ' );
  1371.         $DB->query( 'CREATE TABLE T_pluginsettings (
  1372.                                                             pset_plug_ID INT(11) UNSIGNED NOT NULL,
  1373.                                                             pset_name VARCHAR( 30 ) NOT NULL,
  1374.                                                             pset_value TEXT NULL,
  1375.                                                             PRIMARY KEY ( pset_plug_ID, pset_name )
  1376.                                             )' );
  1377.         task_end();
  1378.  
  1379.         task_begin( 'Creating plugin user settings table... ' );
  1380.         $DB->query( 'CREATE TABLE T_pluginusersettings (
  1381.                                                             puset_plug_ID INT(11) UNSIGNED NOT NULL,
  1382.                                                             puset_user_ID INT(11) UNSIGNED NOT NULL,
  1383.                                                             puset_name VARCHAR( 30 ) NOT NULL,
  1384.                                                             puset_value TEXT NULL,
  1385.                                                             PRIMARY KEY ( puset_plug_ID, puset_user_ID, puset_name )
  1386.                                             )' );
  1387.         task_end();
  1388.  
  1389.         task_begin( 'Creating scheduled tasks table... ' );
  1390.         $DB->query( 'CREATE TABLE T_cron__task(
  1391.                                                  ctsk_ID              int(10) unsigned      not null AUTO_INCREMENT,
  1392.                                                  ctsk_start_datetime  datetime              not null,
  1393.                                                  ctsk_repeat_after    int(10) unsigned,
  1394.                                                  ctsk_name            varchar(50)           not null,
  1395.                                                  ctsk_controller      varchar(50)           not null,
  1396.                                                  ctsk_params          text,
  1397.                                                  primary key (ctsk_ID)
  1398.                                             )' );
  1399.         task_end();
  1400.  
  1401.         task_begin( 'Creating cron log table... ' );
  1402.         $DB->query( 'CREATE TABLE T_cron__log(
  1403.                                                              clog_ctsk_ID              int(10) unsigned   not null,
  1404.                                                              clog_realstart_datetime   datetime           not null,
  1405.                                                              clog_realstop_datetime    datetime,
  1406.                                                              clog_status               enum(\'started\',\'finished\',\'error\',\'timeout\') not null default \'started\',
  1407.                                                              clog_messages             text,
  1408.                                                              primary key (clog_ctsk_ID)
  1409.                                             )' );
  1410.         task_end();
  1411.  
  1412.         task_begin( 'Upgrading blogs table... ' );
  1413.         // blog_allowpingbacks is "DEFAULT 1" in the 0.9.0.11 dump.. - changed in 0.9.2?!
  1414.         $DB->query( 'ALTER TABLE T_blogs
  1415.                                         ALTER COLUMN blog_allowpingbacks SET DEFAULT 0,
  1416.                                     CHANGE COLUMN blog_media_subdir blog_media_subdir VARCHAR( 255 ) NULL,
  1417.                                         CHANGE COLUMN blog_media_fullpath blog_media_fullpath VARCHAR( 255 ) NULL,
  1418.                                         CHANGE COLUMN blog_media_url blog_media_url VARCHAR( 255 ) NULL' );
  1419.         task_end();
  1420.  
  1421.  
  1422.         set_upgrade_checkpoint( '9200' ); // at 1.8 "Summer Beta" release
  1423.     }
  1424.  
  1425.  
  1426.     // ____________________________ 1.9: ____________________________
  1427.  
  1428.     if( $old_db_version < 9290 )
  1429.     {
  1430.         echo 'Post-fix hit_referer_type == NULL... ';
  1431.         // If you've upgraded from 1.6 to 1.8 and it did not break because of strict mode, there are now NULL values for what "spam" was:
  1432.         $DB->query( '
  1433.                     DELETE FROM T_hitlog
  1434.                      WHERE hit_referer_type IS NULL' );
  1435.         echo "OK.<br />\n";
  1436.  
  1437.         echo 'Marking administrator accounts as validated... ';
  1438.         $DB->query( '
  1439.                 UPDATE T_users
  1440.                    SET user_validated = 1
  1441.                  WHERE user_grp_ID = 1' );
  1442.         echo "OK.<br />\n";
  1443.  
  1444.         echo 'Converting auto_prune_stats setting... ';
  1445.         $old_auto_prune_stats = $DB->get_var( '
  1446.                 SELECT set_value
  1447.                   FROM T_settings
  1448.                  WHERE set_name = "auto_prune_stats"' );
  1449.         if( ! is_null($old_auto_prune_stats) && $old_auto_prune_stats < 1 )
  1450.         { // This means it has been disabled before, so set auto_prune_stats_mode to "off"!
  1451.             $DB->query( '
  1452.                     REPLACE INTO T_settings ( set_name, set_value )
  1453.                      VALUES ( "auto_prune_stats_mode", "off" )' );
  1454.         }
  1455.         echo "OK.<br />\n";
  1456.  
  1457.         echo 'Converting time_difference from hours to seconds... ';
  1458.         $DB->query( 'UPDATE T_settings SET set_value = set_value*3600 WHERE set_name = "time_difference"' );
  1459.         echo "OK.<br />\n";
  1460.  
  1461.  
  1462.         echo 'Updating hitlog capabilities... ';
  1463.         $DB->query( '
  1464.                 ALTER TABLE '.$tableprefix.'useragents ADD INDEX agnt_type ( agnt_type )' );
  1465.         $DB->query( '
  1466.                 ALTER TABLE T_hitlog
  1467.                   CHANGE COLUMN hit_referer_type hit_referer_type ENUM(\'search\',\'blacklist\',\'referer\',\'direct\',\'self\',\'admin\') NOT NULL' );
  1468.         echo "OK.<br />\n";
  1469.  
  1470.         echo 'Updating plugin capabilities... ';
  1471.         $DB->query( '
  1472.                 ALTER TABLE T_plugins
  1473.                     MODIFY COLUMN plug_status ENUM( \'enabled\', \'disabled\', \'needs_config\', \'broken\' ) NOT NULL' );
  1474.         echo "OK.<br />\n";
  1475.  
  1476.         set_upgrade_checkpoint( '9290' );
  1477.     }
  1478.  
  1479.  
  1480.     if( $old_db_version < 9300 )
  1481.     {
  1482.         // This can be so long, it needs its own checkpoint protected block in case of failure
  1483.         echo 'Updating hitlog indexes... ';
  1484.         $DB->query( '
  1485.                 ALTER TABLE T_hitlog
  1486.                   ADD INDEX hit_agnt_ID        ( hit_agnt_ID ),
  1487.                   ADD INDEX hit_uri            ( hit_uri ),
  1488.                   ADD INDEX hit_referer_dom_ID ( hit_referer_dom_ID )
  1489.                 ' );
  1490.         echo "OK.<br />\n";
  1491.  
  1492.         set_upgrade_checkpoint( '9300' );
  1493.     }
  1494.  
  1495.  
  1496.     if( $old_db_version < 9310 )
  1497.     {
  1498.         echo 'Updating basedomains... ';
  1499.         $DB->query( '
  1500.                 UPDATE T_basedomains
  1501.                    SET dom_status = "unknown"' );       // someone has filled this up with junk blacklists before
  1502.         $DB->query( '
  1503.                 ALTER TABLE T_basedomains  ADD INDEX dom_type (dom_type)' );
  1504.         echo "OK.<br />\n";
  1505.  
  1506.         set_upgrade_checkpoint( '9310' );
  1507.     }
  1508.  
  1509.  
  1510.     if( $old_db_version < 9315 )
  1511.     {
  1512.         echo 'Altering locales table... ';
  1513.         $DB->query( "ALTER TABLE T_locales CHANGE COLUMN loc_datefmt loc_datefmt varchar(20) NOT NULL default 'y-m-d'" );
  1514.         $DB->query( "ALTER TABLE T_locales CHANGE COLUMN loc_timefmt loc_timefmt varchar(20) NOT NULL default 'H:i:s'" );
  1515.         echo "OK.<br />\n";
  1516.  
  1517.         echo 'Creating item prerendering cache table... ';
  1518.         $DB->query( "
  1519.                 CREATE TABLE {$tableprefix}item__prerendering(
  1520.                     itpr_itm_ID                   INT(11) UNSIGNED NOT NULL,
  1521.                     itpr_format                   ENUM('htmlbody', 'entityencoded', 'xml', 'text') NOT NULL,
  1522.                     itpr_renderers                TEXT NOT NULL,
  1523.                     itpr_content_prerendered      TEXT NULL,
  1524.                     itpr_datemodified             TIMESTAMP NOT NULL,
  1525.                     PRIMARY KEY (itpr_itm_ID, itpr_format)
  1526.                 )" );
  1527.         echo "OK.<br />\n";
  1528.  
  1529.         echo 'Altering plugins table... ';
  1530.         $DB->query( "ALTER TABLE T_plugins ADD COLUMN plug_name            VARCHAR(255) NULL default NULL AFTER plug_version" );
  1531.         $DB->query( "ALTER TABLE T_plugins ADD COLUMN plug_shortdesc       VARCHAR(255) NULL default NULL AFTER plug_name" );
  1532.         echo "OK.<br />\n";
  1533.  
  1534.         set_upgrade_checkpoint( '9315' );
  1535.     }
  1536.  
  1537.  
  1538.     if( $old_db_version < 9320 )
  1539.     { // Dropping hit_datetime because it's very slow on INSERT (dh)
  1540.         // This can be so long, it needs its own checkpoint protected block in case of failure
  1541.         if( db_index_exists( 'T_hitlog', 'hit_datetime' ) )
  1542.         { // only drop, if it still exists (may have been removed manually)
  1543.             echo 'Updating hitlog indexes... ';
  1544.             $DB->query( '
  1545.                     ALTER TABLE T_hitlog
  1546.                         DROP INDEX hit_datetime
  1547.                     ' );
  1548.             echo "OK.<br />\n";
  1549.         }
  1550.  
  1551.         set_upgrade_checkpoint( '9320' );
  1552.     }
  1553.  
  1554.  
  1555.     if( $old_db_version < 9326 )
  1556.     {
  1557.         echo 'Removing obsolete settings... ';
  1558.         $DB->query( 'DELETE FROM T_settings WHERE set_name = "upload_allowedext"' );
  1559.         echo "OK.<br />\n";
  1560.  
  1561.         echo 'Updating blogs... ';
  1562.         db_drop_col( 'T_blogs', 'blog_allowpingbacks' );
  1563.  
  1564.         // Remove and transform obsolete fields blog_pingb2evonet, blog_pingtechnorati, blog_pingweblogs, blog_pingblodotgs
  1565.         if( db_cols_exist( 'T_blogs', array('blog_pingb2evonet', 'blog_pingtechnorati', 'blog_pingweblogs', 'blog_pingblodotgs') ) )
  1566.         {
  1567.             foreach( $DB->get_results( '
  1568.                     SELECT blog_ID, blog_pingb2evonet, blog_pingtechnorati, blog_pingweblogs, blog_pingblodotgs
  1569.                         FROM T_blogs' ) as $row )
  1570.             {
  1571.                 $ping_plugins = $DB->get_var( 'SELECT cset_value FROM T_coll_settings WHERE cset_coll_ID = '.$row->blog_ID.' AND cset_name = "ping_plugins"' );
  1572.                 $ping_plugins = explode(',', $ping_plugins);
  1573.                 if( $row->blog_pingb2evonet )
  1574.                 {
  1575.                     $ping_plugins[] = 'ping_b2evonet';
  1576.                 }
  1577.                 if( $row->blog_pingtechnorati || $row->blog_pingweblogs || $row->blog_pingblodotgs )
  1578.                 { // if either one of the previous pingers was enabled, add ping-o-matic:
  1579.                     $ping_plugins[] = 'ping_pingomatic';
  1580.                 }
  1581.  
  1582.                 // Insert transformed/generated ping plugins collection setting:
  1583.                 $ping_plugins = array_unique($ping_plugins);
  1584.                 $DB->query( 'REPLACE INTO T_coll_settings
  1585.                         ( cset_coll_ID, cset_name, cset_value )
  1586.                         VALUES ( '.$row->blog_ID.', "ping_plugins", "'.implode( ',', $ping_plugins ).'" )' );
  1587.             }
  1588.             $DB->query( 'ALTER TABLE T_blogs
  1589.                     DROP COLUMN blog_pingb2evonet,
  1590.                     DROP COLUMN blog_pingtechnorati,
  1591.                     DROP COLUMN blog_pingweblogs,
  1592.                     DROP COLUMN blog_pingblodotgs' );
  1593.         }
  1594.         echo "OK.<br />\n";
  1595.  
  1596.  
  1597.         set_upgrade_checkpoint( '9326' );
  1598.     }
  1599.  
  1600.  
  1601.     if( $old_db_version < 9328 )
  1602.     {
  1603.         echo 'Updating posts... ';
  1604.         db_add_col( "{$tableprefix}posts", 'post_notifications_status',  'ENUM("noreq","todo","started","finished") NOT NULL DEFAULT "noreq" AFTER post_flags' );
  1605.         db_add_col( "{$tableprefix}posts", 'post_notifications_ctsk_ID', 'INT(10) unsigned NULL DEFAULT NULL AFTER post_notifications_status' );
  1606.         echo "OK.<br />\n";
  1607.         set_upgrade_checkpoint( '9328' );
  1608.     }
  1609.  
  1610.  
  1611.     if( $old_db_version < 9330 )
  1612.     {
  1613.         if( db_col_exists( "{$tableprefix}posts", 'post_flags') )
  1614.         {
  1615.             echo 'Updating post notifications... ';
  1616.             $DB->query( "
  1617.                 UPDATE {$tableprefix}posts
  1618.                      SET post_notifications_status = 'finished'
  1619.                  WHERE post_flags LIKE '%pingsdone%'" );
  1620.             db_drop_col( "{$tableprefix}posts", 'post_flags' );
  1621.             echo "OK.<br />\n";
  1622.         }
  1623.         set_upgrade_checkpoint( '9330' );
  1624.     }
  1625.  
  1626.  
  1627.     if( $old_db_version < 9340 )
  1628.     {
  1629.         echo 'Removing duplicate post link indexes... ';
  1630.         if( db_index_exists( 'T_links', 'link_item_ID' ) )
  1631.         { // only drop, if it still exists (may have been removed manually)
  1632.             $DB->query( '
  1633.                     ALTER TABLE T_links
  1634.                         DROP INDEX link_item_ID
  1635.                     ' );
  1636.         }
  1637.         if( db_index_exists( 'T_links', 'link_dest_item_ID' ) )
  1638.         { // only drop, if it still exists (may have been removed manually)
  1639.             $DB->query( '
  1640.                     ALTER TABLE T_links
  1641.                         DROP INDEX link_dest_item_ID
  1642.                     ' );
  1643.         }
  1644.         echo "OK.<br />\n";
  1645.  
  1646.         set_upgrade_checkpoint( '9340' );
  1647.     }
  1648.  
  1649.     // ____________________________ 1.10: ____________________________
  1650.  
  1651.     if( $old_db_version < 9345 )
  1652.     {
  1653.         echo 'Updating post table... ';
  1654.         $DB->query( "ALTER TABLE {$tableprefix}posts CHANGE COLUMN post_content post_content MEDIUMTEXT NULL" );
  1655.         echo "OK.<br />\n";
  1656.         set_upgrade_checkpoint( '9345' );
  1657.     }
  1658.  
  1659.     if( $old_db_version < 9346 )
  1660.     {
  1661.         echo 'Updating prerendering table... ';
  1662.         $DB->query( "ALTER TABLE {$tableprefix}item__prerendering CHANGE COLUMN itpr_content_prerendered itpr_content_prerendered MEDIUMTEXT NULL" );
  1663.         echo "OK.<br />\n";
  1664.         set_upgrade_checkpoint( '9346' );
  1665.     }
  1666.  
  1667.     if( $old_db_version < 9348 )
  1668.     {
  1669.         echo 'Updating sessions table... ';
  1670.         $DB->query( 'ALTER TABLE T_sessions CHANGE COLUMN sess_data sess_data MEDIUMBLOB DEFAULT NULL' );
  1671.         echo "OK.<br />\n";
  1672.         set_upgrade_checkpoint( '9348' );
  1673.     }
  1674.  
  1675.     if( $old_db_version < 9350 )
  1676.     {
  1677.         echo 'Updating hitlog table... ';
  1678.         $DB->query( 'ALTER TABLE T_hitlog CHANGE COLUMN hit_referer_type hit_referer_type   ENUM(\'search\',\'blacklist\',\'spam\',\'referer\',\'direct\',\'self\',\'admin\') NOT NULL' );
  1679.         echo "OK.<br />\n";
  1680.  
  1681.         set_upgrade_checkpoint( '9350' );
  1682.     }
  1683.  
  1684.  
  1685.     // TODO: "If a user has permission to edit a blog, he should be able to put files in the media folder for that blog." - see http://forums.b2evolution.net/viewtopic.php?p=36417#36417
  1686.     /*
  1687.     // blueyed>> I've came up with the following, but it's too generic IMHO
  1688.     if( $old_db_version < 9300 )
  1689.     {
  1690.         echo 'Setting automatic media perms on blogs (members can upload)... ';
  1691.         $users = $DB->query( '
  1692.                 UPDATE T_users
  1693.                    SET bloguser_perm_media_upload = 1
  1694.                  WHERE bloguser_ismember = 1' );
  1695.         echo "OK.<br />\n";
  1696.     }
  1697.     */
  1698.  
  1699.  
  1700.     // ____________________________ 2.0: ____________________________
  1701.  
  1702.     if( $old_db_version < 9406 )
  1703.     {
  1704.         echo 'Updating chapter url names... ';
  1705.         $DB->query( '
  1706.             ALTER TABLE T_categories
  1707.                 ADD COLUMN cat_urlname VARCHAR(255) NOT NULL' );
  1708.  
  1709.         // Create cat_urlname from cat_name:
  1710.         // TODO: Also use it for cafelog upgrade.
  1711.         load_funcs('locales/_charset.funcs.php');
  1712.         foreach( $DB->get_results('SELECT cat_ID, cat_name FROM T_categories') as $cat )
  1713.         {
  1714.             $cat_name = trim($cat->cat_name);
  1715.             if( strlen($cat_name) )
  1716.             {
  1717.                 // TODO: dh> pass locale (useful for transliteration). From main blog?
  1718.                 $cat_urlname = urltitle_validate('', $cat_name, $cat->cat_ID, false, 'cat_urlname', 'cat_ID', 'T_categories');
  1719.             }
  1720.             else
  1721.             {
  1722.                 $cat_urlname = 'c'.$cat->cat_ID;
  1723.             }
  1724.  
  1725.             $DB->query( '
  1726.                 UPDATE T_categories
  1727.                      SET cat_urlname = '.$DB->quote($cat_urlname).'
  1728.                  WHERE cat_ID = '.$cat->cat_ID );
  1729.         }
  1730.  
  1731.         $DB->query( '
  1732.             ALTER TABLE T_categories
  1733.                 ADD UNIQUE cat_urlname ( cat_urlname )' );
  1734.         echo "OK.<br />\n";
  1735.  
  1736.         echo 'Updating Settings... ';
  1737.         $DB->query( '
  1738.      UPDATE T_settings
  1739.         SET set_value = "disabled"
  1740.       WHERE set_name = "links_extrapath"
  1741.         AND set_value = 0' );
  1742.         $DB->query( '
  1743.      UPDATE T_settings
  1744.         SET set_value = "ymd"
  1745.       WHERE set_name = "links_extrapath"
  1746.         AND set_value <> 0' );
  1747.         echo "OK.<br />\n";
  1748.  
  1749.         set_upgrade_checkpoint( '9406' );
  1750.     }
  1751.  
  1752.  
  1753.     if( $old_db_version < 9407 )
  1754.     {
  1755.         echo 'Moving general settings to blog settings... ';
  1756.         $DB->query( 'REPLACE INTO T_coll_settings( cset_coll_ID, cset_name, cset_value )
  1757.                      SELECT blog_ID, set_name, set_value
  1758.                                      FROM T_blogs, T_settings
  1759.                                     WHERE set_name = "posts_per_page"
  1760.                                        OR set_name = "what_to_show"
  1761.                                        OR set_name = "archive_mode"' );
  1762.         $DB->query( 'DELETE FROM T_settings
  1763.                                     WHERE set_name = "posts_per_page"
  1764.                                        OR set_name = "what_to_show"
  1765.                                        OR set_name = "archive_mode"' );
  1766.         echo "OK.<br />\n";
  1767.  
  1768.         echo 'Upgrading blogs table... ';
  1769.         $query = "ALTER TABLE T_blogs
  1770.                             DROP COLUMN blog_force_skin";
  1771.         $DB->query( $query );
  1772.         echo "OK.<br />\n";
  1773.  
  1774.         echo 'Upgrading groups table... ';
  1775.         $query = "ALTER TABLE T_groups
  1776.                             CHANGE COLUMN grp_perm_files grp_perm_files enum('none','view','add','edit','all') NOT NULL default 'none'";
  1777.         $DB->query( $query );
  1778.         echo "OK.<br />\n";
  1779.  
  1780.         echo 'Upgrading files table... ';
  1781.         $query = "ALTER TABLE T_files
  1782.                             CHANGE COLUMN file_root_type file_root_type enum('absolute','user','group','collection','skins') not null default 'absolute'";
  1783.         $DB->query( $query );
  1784.         echo "OK.<br />\n";
  1785.  
  1786.         echo 'Updating file types... ';
  1787.         // Only change this if it's close enough to a default install (non customized)
  1788.         $DB->query( "UPDATE T_filetypes
  1789.                                         SET ftyp_viewtype = 'text'
  1790.                                     WHERE ftyp_ID = 12
  1791.                                         AND ftyp_extensions = 'php php3 php4 php5 php6'
  1792.                                         AND ftyp_mimetype ='application/x-httpd-php'
  1793.                                         AND ftyp_icon = 'php.gif'" );
  1794.         echo "OK.<br />\n";
  1795.  
  1796.         echo 'Remove obsolete user settings... ';
  1797.         $DB->query( 'DELETE FROM '.$tableprefix.'usersettings
  1798.                                     WHERE uset_name = "plugins_disp_avail"' );
  1799.         echo "OK.<br />\n";
  1800.  
  1801.         set_upgrade_checkpoint( '9407' );
  1802.     }
  1803.  
  1804.  
  1805.     if( $old_db_version < 9408 )
  1806.     {
  1807.         echo 'Creating skins table... ';
  1808.         $DB->query( 'CREATE TABLE T_skins__skin (
  1809.              skin_ID      int(10) unsigned      NOT NULL auto_increment,
  1810.              skin_name    varchar(32)           NOT NULL,
  1811.              skin_type    enum(\'normal\',\'feed\') NOT NULL default \'normal\',
  1812.              skin_folder  varchar(32)           NOT NULL,
  1813.              PRIMARY KEY skin_ID (skin_ID),
  1814.              UNIQUE skin_folder( skin_folder ),
  1815.              KEY skin_name( skin_name )
  1816.            )' );
  1817.         echo "OK.<br />\n";
  1818.  
  1819.         echo 'Creating skin containers table... ';
  1820.         $DB->query( 'CREATE TABLE T_skins__container (
  1821.              sco_skin_ID   int(10) unsigned      NOT NULL,
  1822.              sco_name      varchar(40)           NOT NULL,
  1823.              PRIMARY KEY (sco_skin_ID, sco_name)
  1824.            )' );
  1825.         echo "OK.<br />\n";
  1826.  
  1827.         echo 'Creating widgets table... ';
  1828.         $DB->query( 'CREATE TABLE T_widget (
  1829.                         wi_ID                   INT(10) UNSIGNED auto_increment,
  1830.                         wi_coll_ID    INT(11) UNSIGNED NOT NULL,
  1831.                         wi_sco_name   VARCHAR( 40 ) NOT NULL,
  1832.                         wi_order            INT(10) UNSIGNED NOT NULL,
  1833.                         wi_type       ENUM( \'core\', \'plugin\' ) NOT NULL DEFAULT \'core\',
  1834.                         wi_code       VARCHAR(32) NOT NULL,
  1835.                         wi_params     TEXT NULL,
  1836.                         PRIMARY KEY ( wi_ID ),
  1837.                         UNIQUE wi_order( wi_coll_ID, wi_sco_name, wi_order )
  1838.          )' );
  1839.         echo "OK.<br />\n";
  1840.  
  1841.         install_basic_skins();
  1842.  
  1843.         echo 'Updating blogs table... ';
  1844.         $DB->query( 'ALTER TABLE T_blogs
  1845.                                  ALTER COLUMN blog_allowtrackbacks SET DEFAULT 0,
  1846.                                     DROP COLUMN blog_default_skin,
  1847.                                      ADD COLUMN blog_owner_user_ID   int(11) unsigned NOT NULL default 1 AFTER blog_name,
  1848.                                      ADD COLUMN blog_skin_ID INT(10) UNSIGNED NOT NULL DEFAULT 1 AFTER blog_allowusercss' );
  1849.         echo "OK.<br />\n";
  1850.  
  1851.  
  1852.         install_basic_widgets();
  1853.  
  1854.         set_upgrade_checkpoint( '9408' );
  1855.     }
  1856.  
  1857.  
  1858.     if( $old_db_version < 9409 )
  1859.     {
  1860.         // Upgrade the blog access types:
  1861.         echo 'Updating blogs access types... ';
  1862.         $DB->query( 'UPDATE T_blogs
  1863.                                         SET blog_access_type = "absolute"
  1864.                                     WHERE blog_siteurl LIKE "http://%"
  1865.                                        OR blog_siteurl LIKE "https://%"' );
  1866.  
  1867.         $DB->query( 'UPDATE T_blogs
  1868.                                         SET blog_access_type = "relative",
  1869.                                                 blog_siteurl = CONCAT( blog_siteurl, blog_stub )
  1870.                                     WHERE blog_access_type = "stub"' );
  1871.  
  1872.         db_drop_col( 'T_blogs', 'blog_stub' );
  1873.  
  1874.         echo "OK.<br />\n";
  1875.  
  1876.  
  1877.         echo 'Updating columns... ';
  1878.         $DB->query( "ALTER TABLE T_groups CHANGE COLUMN grp_perm_stats grp_perm_stats enum('none','user','view','edit') NOT NULL default 'none'" );
  1879.  
  1880.         $DB->query( "ALTER TABLE T_coll_user_perms CHANGE COLUMN bloguser_perm_poststatuses bloguser_perm_poststatuses set('published','deprecated','protected','private','draft','redirected') NOT NULL default ''" );
  1881.  
  1882.         $DB->query( "ALTER TABLE T_coll_group_perms CHANGE COLUMN bloggroup_perm_poststatuses bloggroup_perm_poststatuses set('published','deprecated','protected','private','draft','redirected') NOT NULL default ''" );
  1883.  
  1884.         $DB->query( "ALTER TABLE {$tableprefix}posts CHANGE COLUMN post_status post_status enum('published','deprecated','protected','private','draft','redirected') NOT NULL default 'published'" );
  1885.         echo "OK.<br />\n";
  1886.  
  1887.         set_upgrade_checkpoint( '9409' );
  1888.     }
  1889.  
  1890.  
  1891.     if( $old_db_version < 9410 )
  1892.     {
  1893.         echo 'Updating columns... ';
  1894.         $DB->query( "ALTER TABLE T_comments CHANGE COLUMN comment_status comment_status ENUM('published','deprecated','protected','private','draft','redirected') DEFAULT 'published' NOT NULL" );
  1895.  
  1896.         $DB->query( "ALTER TABLE T_sessions CHANGE COLUMN sess_data sess_data MEDIUMBLOB DEFAULT NULL" );
  1897.  
  1898.         $DB->query( "ALTER TABLE T_hitlog CHANGE COLUMN hit_referer_type hit_referer_type ENUM('search','blacklist','spam','referer','direct','self','admin') NOT NULL" );
  1899.  
  1900.         echo "OK.<br />\n";
  1901.  
  1902.         set_upgrade_checkpoint( '9410' );
  1903.     }
  1904.  
  1905.  
  1906.     if( $old_db_version < 9411 )
  1907.     {
  1908.         echo 'Adding default Post Types... ';
  1909.         $DB->query( "
  1910.             REPLACE INTO {$tableprefix}posttypes ( ptyp_ID, ptyp_name )
  1911.             VALUES ( 1000, 'Page' ),
  1912.                          ( 2000, 'Reserved' ),
  1913.                          ( 3000, 'Reserved' ),
  1914.                          ( 4000, 'Reserved' ),
  1915.                          ( 5000, 'Reserved' ) " );
  1916.         echo "OK.<br />\n";
  1917.         set_upgrade_checkpoint( '9411' );
  1918.     }
  1919.  
  1920.  
  1921.     if( $old_db_version < 9412 )
  1922.     {
  1923.         echo 'Adding field for post excerpts... ';
  1924.         $DB->query( "ALTER TABLE {$tableprefix}posts ADD COLUMN post_excerpt  text NULL AFTER post_content" );
  1925.         echo "OK.<br />\n";
  1926.         set_upgrade_checkpoint( '9412' );
  1927.     }
  1928.  
  1929.     if( $old_db_version < 9414 )
  1930.     {
  1931.         echo "Renaming tables...";
  1932.         $DB->query( "RENAME TABLE {$tableprefix}item__prerendering TO T_items__prerendering" );
  1933.         $DB->query( "RENAME TABLE {$tableprefix}poststatuses TO T_items__status" );
  1934.         $DB->query( "RENAME TABLE {$tableprefix}posttypes TO T_items__type" );
  1935.         $DB->query( "RENAME TABLE {$tableprefix}posts TO T_items__item" );
  1936.         echo "OK.<br />\n";
  1937.  
  1938.         echo "Creating Tag tables...";
  1939.         $DB->query( "CREATE TABLE T_items__tag (
  1940.               tag_ID   int(11) unsigned not null AUTO_INCREMENT,
  1941.               tag_name varchar(50) not null,
  1942.               primary key (tag_ID),
  1943.               UNIQUE tag_name( tag_name )
  1944.             )" );
  1945.  
  1946.         $DB->query( "CREATE TABLE T_items__itemtag (
  1947.               itag_itm_ID int(11) unsigned NOT NULL,
  1948.               itag_tag_ID int(11) unsigned NOT NULL,
  1949.               PRIMARY KEY (itag_itm_ID, itag_tag_ID),
  1950.               UNIQUE tagitem ( itag_tag_ID, itag_itm_ID )
  1951.             )" );
  1952.         echo "OK.<br />\n";
  1953.  
  1954.         set_upgrade_checkpoint( '9414' );
  1955.     }
  1956.  
  1957.  
  1958.     if( $old_db_version < 9416 )
  1959.     {
  1960.         echo "Updating blogs table...";
  1961.         $DB->query( "ALTER TABLE T_blogs
  1962.                                     ADD COLUMN blog_advanced_perms  TINYINT(1) NOT NULL default 0 AFTER blog_owner_user_ID,
  1963.                                     DROP COLUMN blog_staticfilename" );
  1964.         $DB->query( "UPDATE T_blogs
  1965.                                       SET blog_advanced_perms = 1" );
  1966.         echo "OK.<br />\n";
  1967.  
  1968.         echo "Additionnal blog permissions...";
  1969.         $DB->query( "ALTER TABLE T_coll_user_perms
  1970.                                     ADD COLUMN bloguser_perm_admin tinyint NOT NULL default 0 AFTER bloguser_perm_properties,
  1971.                                     ADD COLUMN bloguser_perm_edit  ENUM('no','own','lt','le','all','redirected') NOT NULL default 'no' AFTER bloguser_perm_poststatuses" );
  1972.  
  1973.         $DB->query( "ALTER TABLE T_coll_group_perms
  1974.                                     ADD COLUMN bloggroup_perm_admin tinyint NOT NULL default 0 AFTER bloggroup_perm_properties,
  1975.                                     ADD COLUMN bloggroup_perm_edit  ENUM('no','own','lt','le','all','redirected') NOT NULL default 'no' AFTER bloggroup_perm_poststatuses" );
  1976.  
  1977.         // Preserve full admin perms:
  1978.         $DB->query( "UPDATE T_coll_user_perms
  1979.                                         SET bloguser_perm_admin = 1
  1980.                                     WHERE bloguser_perm_properties <> 0" );
  1981.         $DB->query( "UPDATE T_coll_group_perms
  1982.                                         SET bloggroup_perm_admin = 1
  1983.                                     WHERE bloggroup_perm_properties <> 0" );
  1984.  
  1985.         // Preserve full edit perms:
  1986.         $DB->query( "UPDATE T_coll_user_perms
  1987.                                         SET bloguser_perm_edit = 'all'" );
  1988.         $DB->query( "UPDATE T_coll_group_perms
  1989.                                         SET bloggroup_perm_edit = 'all'" );
  1990.  
  1991.         echo "OK.<br />\n";
  1992.  
  1993.         set_upgrade_checkpoint( '9416' );
  1994.     }
  1995.  
  1996.  
  1997.     if( $old_db_version < 9500 )
  1998.     {
  1999.         task_begin( 'Normalizing columns...' );
  2000.         $DB->query( 'ALTER TABLE T_blogs
  2001.                                         ALTER COLUMN blog_shortname SET DEFAULT \'\',
  2002.                                         ALTER COLUMN blog_tagline SET DEFAULT \'\',
  2003.                                         CHANGE COLUMN blog_description blog_description     varchar(250) NULL default \'\',
  2004.                                         ALTER COLUMN blog_siteurl SET DEFAULT \'\'' );
  2005.         task_end();
  2006.  
  2007.         task_begin( 'Normalizing dates...' );
  2008.         $DB->query( 'UPDATE T_users
  2009.                                         SET dateYMDhour = \'2000-01-01 00:00:00\'
  2010.                                     WHERE dateYMDhour = \'0000-00-00 00:00:00\'' );
  2011.         $DB->query( 'ALTER TABLE T_users
  2012.                                     MODIFY COLUMN dateYMDhour DATETIME NOT NULL DEFAULT \'2000-01-01 00:00:00\'' );
  2013.         $DB->query( 'UPDATE T_comments
  2014.                                         SET comment_date = \'2000-01-01 00:00:00\'
  2015.                                     WHERE comment_date = \'0000-00-00 00:00:00\'' );
  2016.         $DB->query( 'ALTER TABLE T_comments
  2017.                                     MODIFY COLUMN comment_date DATETIME NOT NULL DEFAULT \'2000-01-01 00:00:00\'' );
  2018.         task_end();
  2019.  
  2020.         task_begin( 'Normalizing cron jobs...' );
  2021.         $DB->query( 'UPDATE T_cron__task
  2022.                                         SET ctsk_controller = REPLACE(ctsk_controller, "cron/_", "cron/jobs/_" )
  2023.                                     WHERE ctsk_controller LIKE "cron/_%"' );
  2024.         task_end();
  2025.  
  2026.         task_begin( 'Extending comments table...' );
  2027.         $DB->query( 'ALTER TABLE T_comments
  2028.                                     ADD COLUMN comment_rating     TINYINT(1) NULL DEFAULT NULL AFTER comment_content,
  2029.                                     ADD COLUMN comment_featured   TINYINT(1) NOT NULL DEFAULT 0 AFTER comment_rating,
  2030.                                     ADD COLUMN comment_nofollow   TINYINT(1) NOT NULL DEFAULT 1 AFTER comment_featured;');
  2031.         task_end();
  2032.  
  2033.         set_upgrade_checkpoint( '9500' );
  2034.     }
  2035.  
  2036.  
  2037.     if( $old_db_version < 9600 )
  2038.     {   // 2.2.0
  2039.         task_begin( 'Creating global cache table...' );
  2040.         $DB->query( 'CREATE TABLE T_global__cache (
  2041.                                   cach_name VARCHAR( 30 ) NOT NULL ,
  2042.                                   cach_cache MEDIUMBLOB NULL ,
  2043.                                   PRIMARY KEY ( cach_name )
  2044.                                 )' );
  2045.         task_end();
  2046.  
  2047.         task_begin( 'Altering posts table...' );
  2048.         $DB->query( 'ALTER TABLE T_items__item
  2049.                                         MODIFY COLUMN post_datestart DATETIME NOT NULL DEFAULT \'2000-01-01 00:00:00\',
  2050.                                         MODIFY COLUMN post_datemodified DATETIME NOT NULL DEFAULT \'2000-01-01 00:00:00\',
  2051.                                         ADD COLUMN post_order    float NULL AFTER post_priority,
  2052.                                         ADD COLUMN post_featured tinyint(1) NOT NULL DEFAULT 0 AFTER post_order,
  2053.                                         ADD INDEX post_order( post_order )' );
  2054.         task_end();
  2055.  
  2056.         set_upgrade_checkpoint( '9600' );
  2057.     }
  2058.  
  2059.  
  2060.     if( $old_db_version < 9700 )
  2061.     {   // 2.3.2
  2062.       echo 'Creating PodCast Post Type... ';
  2063.         $DB->query( "
  2064.             REPLACE INTO T_items__type ( ptyp_ID, ptyp_name )
  2065.             VALUES ( 2000, 'Podcast' )" );
  2066.         echo "OK.<br />\n";
  2067.  
  2068.         // 2.4.0
  2069.       echo 'Adding additional group permissions... ';
  2070.         $DB->query( "
  2071.           ALTER TABLE T_groups
  2072.                     ADD COLUMN grp_perm_bypass_antispam         TINYINT(1)  NOT NULL DEFAULT 0        AFTER grp_perm_blogs,
  2073.                     ADD COLUMN grp_perm_xhtmlvalidation         VARCHAR(10) NOT NULL default 'always' AFTER grp_perm_bypass_antispam,
  2074.                     ADD COLUMN grp_perm_xhtmlvalidation_xmlrpc  VARCHAR(10) NOT NULL default 'always' AFTER grp_perm_xhtmlvalidation,
  2075.                     ADD COLUMN grp_perm_xhtml_css_tweaks        TINYINT(1)  NOT NULL DEFAULT 0        AFTER grp_perm_xhtmlvalidation_xmlrpc,
  2076.             ADD COLUMN grp_perm_xhtml_iframes           TINYINT(1)  NOT NULL DEFAULT 0        AFTER grp_perm_xhtml_css_tweaks,
  2077.             ADD COLUMN grp_perm_xhtml_javascript        TINYINT(1)  NOT NULL DEFAULT 0        AFTER grp_perm_xhtml_iframes,
  2078.                     ADD COLUMN grp_perm_xhtml_objects           TINYINT(1)  NOT NULL DEFAULT 0        AFTER grp_perm_xhtml_javascript " );
  2079.         echo "OK.<br />\n";
  2080.  
  2081.         set_upgrade_checkpoint( '9700' );
  2082.     }
  2083.  
  2084.  
  2085.     if( $old_db_version < 9800 )
  2086.     {   // 2.5.0
  2087.         echo 'Upgrading blogs table... ';
  2088.         db_drop_col( 'T_blogs', 'blog_commentsexpire' );
  2089.         echo "OK.<br />\n";
  2090.  
  2091.         echo 'Upgrading items table... ';
  2092.         $DB->query( "ALTER TABLE T_items__item
  2093.             CHANGE COLUMN post_urltitle post_urltitle VARCHAR(210) NULL DEFAULT NULL,
  2094.             CHANGE COLUMN post_order    post_order DOUBLE NULL,
  2095.             ADD COLUMN post_titletag  VARCHAR(255) NULL DEFAULT NULL AFTER post_urltitle,
  2096.             ADD COLUMN post_double1   DOUBLE NULL COMMENT 'Custom double value 1' AFTER post_priority,
  2097.             ADD COLUMN post_double2   DOUBLE NULL COMMENT 'Custom double value 2' AFTER post_double1,
  2098.             ADD COLUMN post_double3   DOUBLE NULL COMMENT 'Custom double value 3' AFTER post_double2,
  2099.             ADD COLUMN post_double4   DOUBLE NULL COMMENT 'Custom double value 4' AFTER post_double3,
  2100.             ADD COLUMN post_double5   DOUBLE NULL COMMENT 'Custom double value 5' AFTER post_double4,
  2101.             ADD COLUMN post_varchar1  VARCHAR(255) NULL COMMENT 'Custom varchar value 1' AFTER post_double5,
  2102.             ADD COLUMN post_varchar2  VARCHAR(255) NULL COMMENT 'Custom varchar value 2' AFTER post_varchar1,
  2103.             ADD COLUMN post_varchar3  VARCHAR(255) NULL COMMENT 'Custom varchar value 3' AFTER post_varchar2" );
  2104.         echo "OK.<br />\n";
  2105.  
  2106.         echo 'Creating keyphrase table... ';
  2107.         $query = "CREATE TABLE T_track__keyphrase (
  2108.            keyp_ID      INT UNSIGNED NOT NULL AUTO_INCREMENT,
  2109.            keyp_phrase  VARCHAR( 255 ) NOT NULL,
  2110.            PRIMARY KEY        ( keyp_ID ),
  2111.            UNIQUE keyp_phrase ( keyp_phrase )
  2112.          )";
  2113.         $DB->query( $query );
  2114.         echo "OK.<br />\n";
  2115.  
  2116.         echo 'Upgrading hitlog table... ';
  2117.         $query = "ALTER TABLE T_hitlog
  2118.              CHANGE COLUMN hit_ID hit_ID              INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  2119.              CHANGE COLUMN hit_datetime hit_datetime  DATETIME NOT NULL DEFAULT '2000-01-01 00:00:00',
  2120.              ADD COLUMN hit_keyphrase_keyp_ID         INT UNSIGNED DEFAULT NULL AFTER hit_referer_dom_ID,
  2121.              ADD INDEX hit_remote_addr ( hit_remote_addr ),
  2122.              ADD INDEX hit_sess_ID        ( hit_sess_ID )";
  2123.         $DB->query( $query );
  2124.         echo "OK.<br />\n";
  2125.  
  2126.         echo 'Upgrading sessions table... ';
  2127.         $DB->query( "ALTER TABLE T_sessions
  2128.             ALTER COLUMN sess_lastseen SET DEFAULT '2000-01-01 00:00:00',
  2129.             ADD COLUMN sess_hitcount  INT(10) UNSIGNED NOT NULL DEFAULT 1 AFTER sess_key,
  2130.             ADD KEY sess_user_ID (sess_user_ID)" );
  2131.         echo "OK.<br />\n";
  2132.  
  2133.         echo 'Creating goal tracking table... ';
  2134.     $DB->query( "CREATE TABLE T_track__goal(
  2135.                       goal_ID int(10) unsigned NOT NULL auto_increment,
  2136.                       goal_name varchar(50) default NULL,
  2137.                       goal_key varchar(32) default NULL,
  2138.                       goal_redir_url varchar(255) default NULL,
  2139.                       goal_default_value double default NULL,
  2140.                       PRIMARY KEY (goal_ID),
  2141.                       UNIQUE KEY goal_key (goal_key)
  2142.          )" );
  2143.  
  2144.     $DB->query( "CREATE TABLE T_track__goalhit (
  2145.                       ghit_ID int(10) unsigned NOT NULL auto_increment,
  2146.                       ghit_goal_ID    int(10) unsigned NOT NULL,
  2147.                       ghit_hit_ID     int(10) unsigned NOT NULL,
  2148.                       ghit_params     TEXT default NULL,
  2149.                       PRIMARY KEY  (ghit_ID),
  2150.                       KEY ghit_goal_ID (ghit_goal_ID),
  2151.                       KEY ghit_hit_ID (ghit_hit_ID)
  2152.         )" );
  2153.         echo "OK.<br />\n";
  2154.  
  2155.         set_upgrade_checkpoint( '9800' );
  2156.     }
  2157.  
  2158.  
  2159.     if( $old_db_version < 9900 )
  2160.     {   // 3.0 part 1
  2161.         task_begin( 'Updating keyphrases in hitlog table... ' );
  2162.         flush();
  2163.         load_class( 'sessions/model/_hit.class.php', 'Hit' );
  2164.         $sql = 'SELECT SQL_NO_CACHE hit_ID, hit_referer
  2165.                   FROM T_hitlog
  2166.                  WHERE hit_referer_type = "search"
  2167.                    AND hit_keyphrase_keyp_ID IS NULL'; // this line just in case we crashed in the middle, so we restart where we stopped
  2168.         $rows = $DB->get_results( $sql, OBJECT, 'get all search hits' );
  2169.         foreach( $rows as $row )
  2170.         {
  2171.             $keyphrase = Hit::extract_keyphrase_from_referer( $row->hit_referer );
  2172.             if( empty( $keyphrase ) )
  2173.             {
  2174.                 continue;
  2175.             }
  2176.  
  2177.             $DB->begin();
  2178.  
  2179.             $sql = 'SELECT keyp_ID
  2180.                       FROM T_track__keyphrase
  2181.                      WHERE keyp_phrase = '.$DB->quote($keyphrase);
  2182.             $keyp_ID = $DB->get_var( $sql, 0, 0, 'Get keyphrase ID' );
  2183.  
  2184.             if( empty( $keyp_ID ) )
  2185.             {
  2186.                 $sql = 'INSERT INTO T_track__keyphrase( keyp_phrase )
  2187.                         VALUES ('.$DB->quote($keyphrase).')';
  2188.                 $DB->query( $sql, 'Add new keyphrase' );
  2189.                 $keyp_ID = $DB->insert_id;
  2190.             }
  2191.  
  2192.             $DB->query( 'UPDATE T_hitlog
  2193.                             SET hit_keyphrase_keyp_ID = '.$keyp_ID.'
  2194.                           WHERE hit_ID = '.$row->hit_ID, 'Update hit' );
  2195.  
  2196.             $DB->commit();
  2197.             echo ". \n";
  2198.         }
  2199.         task_end();
  2200.  
  2201.         task_begin( 'Upgrading widgets table... ' );
  2202.         $DB->query( "ALTER TABLE T_widget
  2203.             CHANGE COLUMN wi_order wi_order INT(10) NOT NULL" );
  2204.         task_end();
  2205.  
  2206.         task_begin( 'Upgrading Files table... ' );
  2207.         $DB->query( "ALTER TABLE T_files
  2208.                                 CHANGE COLUMN file_root_type file_root_type enum('absolute','user','collection','shared','skins') not null default 'absolute'" );
  2209.         task_end();
  2210.  
  2211.         set_upgrade_checkpoint( '9900' );
  2212.     }
  2213.  
  2214.     if( $old_db_version < 9910 )
  2215.     {   // 3.0 part 2
  2216.  
  2217.         task_begin( 'Upgrading Blogs table... ' );
  2218.         $DB->query( "ALTER TABLE T_blogs CHANGE COLUMN blog_name blog_name varchar(255) NOT NULL default ''" );
  2219.         task_end();
  2220.  
  2221.         task_begin( 'Adding new Post Types...' );
  2222.         $DB->query( "
  2223.             REPLACE INTO T_items__type( ptyp_ID, ptyp_name )
  2224.             VALUES ( 1500, 'Intro-Main' ),
  2225.                          ( 1520, 'Intro-Cat' ),
  2226.                          ( 1530, 'Intro-Tag' ),
  2227.                          ( 1570, 'Intro-Sub' ),
  2228.                          ( 1600, 'Intro-All' ) " );
  2229.         task_end();
  2230.  
  2231.         task_begin( 'Updating User table' );
  2232.         $DB->query( "ALTER TABLE T_users
  2233.                                     ADD COLUMN user_avatar_file_ID int(10) unsigned default NULL AFTER user_validated" );
  2234.         task_end();
  2235.  
  2236.         task_begin( 'Creating table for User field definitions' );
  2237.         $DB->query( "CREATE TABLE T_users__fielddefs (
  2238.                 ufdf_ID int(10) unsigned NOT NULL,
  2239.                 ufdf_type char(8) NOT NULL,
  2240.                 ufdf_name varchar(255) collate latin1_general_ci NOT NULL,
  2241.                 PRIMARY KEY  (ufdf_ID)
  2242.             )" );
  2243.         task_end();
  2244.  
  2245.         task_begin( 'Creating default field definitions...' );
  2246.         $DB->query( "
  2247.         INSERT INTO T_users__fielddefs (ufdf_ID, ufdf_type, ufdf_name)
  2248.              VALUES ( 10000, 'email',    'MSN/Live IM'),
  2249.                             ( 10100, 'word',     'Yahoo IM'),
  2250.                             ( 10200, 'word',     'AOL AIM'),
  2251.                             ( 10300, 'number',   'ICQ ID'),
  2252.                             ( 40000, 'phone',    'Skype'),
  2253.                             ( 50000, 'phone',    'Main phone'),
  2254.                             ( 50100, 'phone',    'Cell phone'),
  2255.                             ( 50200, 'phone',    'Office phone'),
  2256.                             ( 50300, 'phone',    'Home phone'),
  2257.                             ( 60000, 'phone',    'Office FAX'),
  2258.                             ( 60100, 'phone',    'Home FAX'),
  2259.                             (100000, 'url',      'Website'),
  2260.                             (100100, 'url',      'Blog'),
  2261.                             (110000, 'url',      'Linkedin'),
  2262.                             (120000, 'url',      'Twitter'),
  2263.                             (130100, 'url',      'Facebook'),
  2264.                             (130200, 'url',      'Myspace'),
  2265.                             (140000, 'url',      'Flickr'),
  2266.                             (150000, 'url',      'YouTube'),
  2267.                             (160000, 'url',      'Digg'),
  2268.                             (160100, 'url',      'StumbleUpon'),
  2269.                             (200000, 'text',     'Role'),
  2270.                             (200100, 'text',     'Company/Org.'),
  2271.                             (200200, 'text',     'Division'),
  2272.                             (211000, 'text',     'VAT ID'),
  2273.                             (300000, 'text',     'Main address'),
  2274.                             (300300, 'text',     'Home address');" );
  2275.         task_end();
  2276.  
  2277.         task_begin( 'Creating table for User fields...' );
  2278.         $DB->query( "CREATE TABLE T_users__fields (
  2279.                 uf_ID      int(10) unsigned NOT NULL auto_increment,
  2280.               uf_user_ID int(10) unsigned NOT NULL,
  2281.               uf_ufdf_ID int(10) unsigned NOT NULL,
  2282.               uf_varchar varchar(255) NOT NULL,
  2283.               PRIMARY KEY (uf_ID)
  2284.             )" );
  2285.         task_end();
  2286.  
  2287.         set_upgrade_checkpoint( '9910' );
  2288.     }
  2289.  
  2290.     if( $old_db_version < 9920 )
  2291.     {   // 3.1
  2292.         task_begin( 'Upgrading Posts table... ' );
  2293.         // This is for old posts that may have a post type of NULL which should never happen. ptyp 1 is for regular posts
  2294.         $DB->query( "UPDATE T_items__item
  2295.                                         SET post_ptyp_ID = 1
  2296.                                     WHERE post_ptyp_ID IS NULL" );
  2297.         $DB->query( "ALTER TABLE T_items__item
  2298.                             CHANGE COLUMN post_ptyp_ID post_ptyp_ID int(10) unsigned NOT NULL DEFAULT 1" );
  2299.         task_end();
  2300.  
  2301.         task_begin( 'Upgrading Categories table... ' );
  2302.         $DB->query( "ALTER TABLE T_categories
  2303.             CHANGE COLUMN cat_name cat_name varchar(255) NOT NULL,
  2304.             CHANGE COLUMN cat_description cat_description varchar(255) NULL DEFAULT NULL" );
  2305.         db_add_col( 'T_categories', 'cat_order', 'int(11) NULL DEFAULT NULL AFTER cat_description' );
  2306.         db_add_index( 'T_categories', 'cat_order', 'cat_order' );
  2307.  
  2308.         $DB->query( "UPDATE T_categories
  2309.                     SET cat_order = cat_ID" );
  2310.         task_end();
  2311.  
  2312.         task_begin( 'Upgrading widgets table... ' );
  2313.         db_add_col( 'T_widget', 'wi_enabled', 'tinyint(1) NOT NULL DEFAULT 1 AFTER wi_order' );
  2314.         task_end();
  2315.     }
  2316.     if( $old_db_version < 9930 )
  2317.     {   // 3.1 continued
  2318.         task_begin( 'Updating item types...' );
  2319.         $DB->query( "
  2320.             REPLACE INTO T_items__type ( ptyp_ID, ptyp_name )
  2321.             VALUES ( 3000, 'Sidebar link' )" );
  2322.         echo "OK.<br />\n";
  2323.         task_end();
  2324.  
  2325.         task_begin( 'Updating items table...' );
  2326.         $DB->query( "ALTER TABLE T_items__item ENGINE=innodb" );    // fp> hum... this originally was a test :)
  2327.         task_end();
  2328.  
  2329.         task_begin( 'Creating versions table...' );
  2330.         $DB->query( "CREATE TABLE T_items__version (
  2331.                 iver_itm_ID        INT UNSIGNED NOT NULL ,
  2332.                 iver_edit_user_ID  INT UNSIGNED NOT NULL ,
  2333.                 iver_edit_datetime DATETIME NOT NULL ,
  2334.                 iver_status        ENUM('published','deprecated','protected','private','draft','redirected') NULL ,
  2335.                 iver_title         TEXT NULL ,
  2336.                 iver_content       MEDIUMTEXT NULL ,
  2337.                 INDEX iver_itm_ID ( iver_itm_ID )
  2338.                 ) ENGINE = innodb" );
  2339.         task_end();
  2340.  
  2341.         task_begin( 'Updating group permissions...' );
  2342.         $DB->query( "UPDATE T_groups
  2343.                                         SET grp_perm_xhtml_css_tweaks = 1
  2344.                                     WHERE grp_ID <= 3" );
  2345.         task_end();
  2346.  
  2347.         set_upgrade_checkpoint( '9930' );
  2348.     }
  2349.  
  2350.     if( $old_db_version < 9940 )
  2351.     {   // 3.2
  2352.         task_begin( 'Updating hitlog table...' );
  2353.         $DB->query( "ALTER TABLE T_hitlog ADD COLUMN hit_serprank INT UNSIGNED DEFAULT NULL AFTER hit_keyphrase_keyp_ID" );
  2354.         task_end();
  2355.  
  2356.         task_begin( 'Updating versions table...' );
  2357.         $DB->query( "ALTER TABLE T_items__version
  2358.                                 CHANGE COLUMN iver_edit_user_ID iver_edit_user_ID  INT UNSIGNED NULL" );
  2359.         task_end();
  2360.     }
  2361.  
  2362.     if( $old_db_version < 9950 )
  2363.     {   // 3.3
  2364.         task_begin( 'Altering Blogs table... ' );
  2365.         $DB->query( "ALTER TABLE T_blogs CHANGE COLUMN blog_shortname blog_shortname varchar(255) default ''" );
  2366.         task_end();
  2367.  
  2368.         task_begin( 'Altering default dates... ' );
  2369.         $DB->query( "ALTER TABLE T_links
  2370.      ALTER COLUMN link_datecreated SET DEFAULT '2000-01-01 00:00:00',
  2371.      ALTER COLUMN link_datemodified SET DEFAULT '2000-01-01 00:00:00'" );
  2372.         $DB->query( "ALTER TABLE T_cron__task
  2373.      ALTER COLUMN ctsk_start_datetime SET DEFAULT '2000-01-01 00:00:00'" );
  2374.         $DB->query( "ALTER TABLE T_cron__log
  2375.      ALTER COLUMN clog_realstart_datetime SET DEFAULT '2000-01-01 00:00:00'" );
  2376.         task_end();
  2377.  
  2378.         task_begin( 'Altering Items table... ' );
  2379.         $DB->query( "ALTER TABLE T_items__item
  2380.             ADD COLUMN post_metadesc VARCHAR(255) NULL DEFAULT NULL AFTER post_titletag,
  2381.             ADD COLUMN post_metakeywords VARCHAR(255) NULL DEFAULT NULL AFTER post_metadesc,
  2382.             ADD COLUMN post_editor_code VARCHAR(32) NULL COMMENT 'Plugin code of the editor used to edit this post' AFTER post_varchar3" );
  2383.         task_end();
  2384.  
  2385.         task_begin( 'Forcing AutoP posts to html editor...' );
  2386.         $DB->query( 'UPDATE T_items__item
  2387.                                             SET post_editor_code = "html"
  2388.                                         WHERE post_renderers = "default"
  2389.                                              OR post_renderers LIKE "%b2WPAutP%"' );
  2390.         task_end();
  2391.  
  2392.         set_upgrade_checkpoint( '9950' );
  2393.     }
  2394.  
  2395.     if( $old_db_version < 9960 )
  2396.     {   // 3.3
  2397.  
  2398.         echo "Renaming tables...";
  2399.         $DB->save_error_state();
  2400.         $DB->halt_on_error = false;
  2401.         $DB->show_errors = false;
  2402.         $DB->query( "ALTER TABLE {$tableprefix}users_fields RENAME TO T_users__fields" );
  2403.         $DB->restore_error_state();
  2404.         echo "OK.<br />\n";
  2405.  
  2406.         // fp> The following is more tricky to do with CHARACTER SET. During upgrade, we don't know what the admin actually wants.
  2407.         task_begin( 'Making sure all tables use desired storage ENGINE as specified in the b2evo schema...' );
  2408.         foreach( $schema_queries as $table_name=>$table_def )
  2409.         {
  2410.             if( $DB->query( 'SHOW TABLES LIKE \''.$table_name.'\'' )
  2411.                 && preg_match( '/\sENGINE\s*=\s*([a-z]+)/is', $table_def[1], $matches ) )
  2412.             {   // If the table exists and has an ENGINE definition:
  2413.                 echo $table_name.':'.$matches[1].'<br />';
  2414.                 $DB->query( "ALTER TABLE $table_name ENGINE = ".$matches[1] );
  2415.             }
  2416.         }
  2417.         task_end();
  2418.  
  2419.         set_upgrade_checkpoint( '9960' );
  2420.     }
  2421.  
  2422.     if( $old_db_version < 9970 )
  2423.     {   // 4.0 part 1
  2424.  
  2425.         // For create_default_currencies() and create_default_countries():
  2426.         require_once dirname(__FILE__).'/_functions_create.php';
  2427.  
  2428.         task_begin( 'Creating table for default currencies... ' );
  2429.         $DB->query( "CREATE TABLE T_currency (
  2430.                 curr_ID int(10) unsigned NOT NULL auto_increment,
  2431.                 curr_code char(3) NOT NULL,
  2432.                 curr_shortcut varchar(30) NOT NULL,
  2433.                 curr_name varchar(40) NOT NULL,
  2434.                 PRIMARY KEY curr_ID (curr_ID),
  2435.                 UNIQUE curr_code (curr_code)
  2436.             ) ENGINE = innodb" );
  2437.         task_end();
  2438.  
  2439.         create_default_currencies();
  2440.  
  2441.         task_begin( 'Creating table for default countries... ' );
  2442.         $DB->query( "CREATE TABLE T_country (
  2443.                 ctry_ID int(10) unsigned NOT NULL auto_increment,
  2444.                 ctry_code char(2) NOT NULL,
  2445.                 ctry_name varchar(40) NOT NULL,
  2446.                 ctry_curr_ID int(10) unsigned,
  2447.                 PRIMARY KEY ctry_ID (ctry_ID),
  2448.                 UNIQUE ctry_code (ctry_code)
  2449.             ) ENGINE = innodb" );
  2450.         task_end();
  2451.  
  2452.         create_default_countries();
  2453.  
  2454.         task_begin( 'Upgrading user permissions table... ' );
  2455.         $DB->query( "ALTER TABLE T_coll_user_perms
  2456.             ADD COLUMN bloguser_perm_page       tinyint NOT NULL default 0 AFTER bloguser_perm_media_change,
  2457.             ADD COLUMN bloguser_perm_intro      tinyint NOT NULL default 0 AFTER bloguser_perm_page,
  2458.             ADD COLUMN bloguser_perm_podcast    tinyint NOT NULL default 0 AFTER bloguser_perm_intro,
  2459.             ADD COLUMN bloguser_perm_sidebar    tinyint NOT NULL default 0 AFTER bloguser_perm_podcast" );
  2460.         task_end();
  2461.  
  2462.         task_begin( 'Upgrading group permissions table... ' );
  2463.         $DB->query( "ALTER TABLE T_coll_group_perms
  2464.             ADD COLUMN bloggroup_perm_page      tinyint NOT NULL default 0 AFTER bloggroup_perm_media_change,
  2465.             ADD COLUMN bloggroup_perm_intro     tinyint NOT NULL default 0 AFTER bloggroup_perm_page,
  2466.             ADD COLUMN bloggroup_perm_podcast   tinyint NOT NULL default 0 AFTER bloggroup_perm_intro,
  2467.             ADD COLUMN bloggroup_perm_sidebar   tinyint NOT NULL default 0 AFTER bloggroup_perm_podcast" );
  2468.         task_end();
  2469.  
  2470.         task_begin( 'Upgrading users table... ' );
  2471.         $DB->query( "ALTER TABLE T_users
  2472.             ADD COLUMN user_ctry_ID int(10) unsigned NULL AFTER user_avatar_file_ID" );
  2473.         task_end();
  2474.  
  2475.         // Creating tables for messaging module
  2476.  
  2477.         task_begin( 'Creating table for message threads... ' );
  2478.         $DB->query( "CREATE TABLE T_messaging__thread (
  2479.             thrd_ID int(10) unsigned NOT NULL auto_increment,
  2480.             thrd_title varchar(255) NOT NULL,
  2481.             thrd_datemodified datetime NOT NULL,
  2482.             PRIMARY KEY thrd_ID (thrd_ID)
  2483.         ) ENGINE = innodb" );
  2484.         task_end();
  2485.  
  2486.         task_begin( 'Creating table for messagee... ' );
  2487.         $DB->query( "CREATE TABLE T_messaging__message (
  2488.             msg_ID int(10) unsigned NOT NULL auto_increment,
  2489.             msg_author_user_ID int(10) unsigned NOT NULL,
  2490.             msg_datetime datetime NOT NULL,
  2491.             msg_thread_ID int(10) unsigned NOT NULL,
  2492.             msg_text text NULL,
  2493.             PRIMARY KEY msg_ID (msg_ID)
  2494.         ) ENGINE = innodb" );
  2495.         task_end();
  2496.  
  2497.         task_begin( 'Creating table for message thread statuses... ' );
  2498.         $DB->query( "CREATE TABLE T_messaging__threadstatus (
  2499.             tsta_thread_ID int(10) unsigned NOT NULL,
  2500.             tsta_user_ID int(10) unsigned NOT NULL,
  2501.             tsta_first_unread_msg_ID int(10) unsigned NULL,
  2502.             INDEX(tsta_user_ID)
  2503.         ) ENGINE = innodb" );
  2504.         task_end();
  2505.  
  2506.         task_begin( 'Creating table for messaging contacts... ' );
  2507.         $DB->query( "CREATE TABLE T_messaging__contact (
  2508.             mct_from_user_ID int(10) unsigned NOT NULL,
  2509.             mct_to_user_ID int(10) unsigned NOT NULL,
  2510.             mct_blocked tinyint(1) default 0,
  2511.             mct_last_contact_datetime datetime NOT NULL,
  2512.             PRIMARY KEY mct_PK (mct_from_user_ID, mct_to_user_ID)
  2513.         ) ENGINE = innodb" );
  2514.         task_end();
  2515.  
  2516.         task_begin( 'Upgrading skins table... ' );
  2517.         $DB->query( "ALTER TABLE T_skins__skin
  2518.                         MODIFY skin_type enum('normal','feed','sitemap') NOT NULL default 'normal'" );
  2519.         task_end();
  2520.  
  2521.         task_begin( 'Setting skin type of sitemap skin to "sitemap"... ' );
  2522.         $DB->query( "UPDATE T_skins__skin
  2523.                         SET skin_type = 'sitemap'
  2524.                         WHERE skin_folder = '_sitemap'" );
  2525.         task_end();
  2526.  
  2527.         // Creating table for pluggable permissions
  2528.  
  2529.         // This table gets created during upgrade to v0.8.9 at checkpoint 8050
  2530.         task_begin( 'Creating table for Group Settings... ' );
  2531.         $DB->query( "CREATE TABLE IF NOT EXISTS T_groups__groupsettings (
  2532.             gset_grp_ID INT(11) UNSIGNED NOT NULL,
  2533.             gset_name VARCHAR(30) NOT NULL,
  2534.             gset_value VARCHAR(255) NULL,
  2535.             PRIMARY KEY (gset_grp_ID, gset_name)
  2536.         ) ENGINE = innodb" );
  2537.         task_end();
  2538.  
  2539.         // Rename T_usersettings table to T_users__usersettings
  2540.         task_begin( 'Rename T_usersettings table to T_users__usersettings... ' );
  2541.         $DB->query( 'ALTER TABLE '.$tableprefix.'usersettings RENAME TO T_users__usersettings' );
  2542.         task_end();
  2543.  
  2544.         set_upgrade_checkpoint( '9970' );
  2545.     }
  2546.  
  2547.  
  2548.     if( $old_db_version < 9980 )
  2549.     {   // 4.0 part 2
  2550.  
  2551.         task_begin( 'Upgrading posts... ' );
  2552.         $DB->query( '
  2553.             UPDATE T_items__item
  2554.                SET post_datestart = FROM_UNIXTIME( FLOOR(UNIX_TIMESTAMP(post_datestart)/60)*60 )
  2555.              WHERE post_datestart > NOW()' );
  2556.         db_add_col( 'T_items__item', 'post_excerpt_autogenerated', 'TINYINT NULL DEFAULT NULL AFTER post_excerpt' );
  2557.         db_add_col( 'T_items__item', 'post_dateset', 'tinyint(1) NOT NULL DEFAULT 1 AFTER post_assigned_user_ID' );
  2558.         task_end();
  2559.  
  2560.         task_begin( 'Upgrading countries... ' );
  2561.         db_add_col( 'T_country', 'ctry_enabled', 'tinyint(1) NOT NULL DEFAULT 1 AFTER ctry_curr_ID' );
  2562.         task_end();
  2563.  
  2564.  
  2565.         task_begin( 'Upgrading links... ' );
  2566.  
  2567.         // Add link_position. Temporary allow NULL, set compatibility default, then do not allow NULL.
  2568.         // TODO: dh> actually, using "teaser" for the first link and "aftermore" for the rest would make more sense (and "aftermore" should get displayed with "no-more" posts anyway).
  2569.         //           Opinions? Could be heavy to transform this though..
  2570.         // fp> no, don't change past posts unexpectedly.
  2571.         db_add_col( 'T_links', 'link_position', "varchar(10) NULL AFTER link_title" );
  2572.         $DB->query( "UPDATE T_links SET link_position = 'teaser' WHERE link_position IS NULL" );
  2573.         db_add_col( 'T_links', 'link_position', "varchar(10) NOT NULL AFTER link_title" ); // change to NOT NULL
  2574.  
  2575.         // Add link_order. Temporary allow NULL, use order from ID, then do not allow NULL and add UNIQUE index.
  2576.         db_add_col( 'T_links', 'link_order', 'int(11) unsigned NULL AFTER link_position' );
  2577.         $DB->query( "UPDATE T_links SET link_order = link_ID WHERE link_order IS NULL" );
  2578.         db_add_col( 'T_links', 'link_order', 'int(11) unsigned NOT NULL AFTER link_position' ); // change to NOT NULL
  2579.         db_add_index( 'T_links', 'link_itm_ID_order', 'link_itm_ID, link_order', 'UNIQUE' );
  2580.  
  2581.         task_end();
  2582.  
  2583.         task_begin( 'Upgrading sessions... ' );
  2584.         $DB->query( "ALTER TABLE T_sessions CHANGE COLUMN sess_ipaddress sess_ipaddress VARCHAR(39) NOT NULL DEFAULT ''" );
  2585.         task_end();
  2586.  
  2587.         set_upgrade_checkpoint( '9980' );
  2588.     }
  2589.  
  2590.     if( $old_db_version < 9990 )
  2591.     {   // 4.0 part 3
  2592.  
  2593.         task_begin( 'Upgrading hitlog... ' );
  2594.  
  2595.         db_add_col( 'T_hitlog', 'hit_agent_type', "ENUM('rss','robot','browser','unknown') DEFAULT 'unknown' NOT NULL AFTER hit_remote_addr" );
  2596.  
  2597.         if( db_col_exists('T_hitlog', 'hit_agnt_ID') )
  2598.         {
  2599.             $DB->query( 'UPDATE T_hitlog, '.$tableprefix.'useragents
  2600.                             SET hit_agent_type = agnt_type
  2601.                           WHERE hit_agnt_ID = agnt_ID
  2602.                             AND agnt_type <> "unknown"' ); // We already have the unknown as default
  2603.             db_drop_col( 'T_hitlog', 'hit_agnt_ID' );
  2604.         }
  2605.         $DB->query( 'DROP TABLE IF EXISTS '.$tableprefix.'useragents' );
  2606.  
  2607.         task_end();
  2608.  
  2609.         set_upgrade_checkpoint( '9990' );
  2610.     }
  2611.  
  2612.     if( $old_db_version < 10000 )
  2613.     {   // 4.0 part 4
  2614.         // Integrate comment_secret
  2615.         task_begin( 'Extending Comment table... ' );
  2616.         db_add_col( 'T_comments', 'comment_secret', 'varchar(32) NULL default NULL' );
  2617.         task_end();
  2618.  
  2619.         // Create T_slug table and, Insert all slugs from T_items
  2620.         task_begin( 'Create Slugs table... ' );
  2621.         $DB->query( 'CREATE TABLE IF NOT EXISTS T_slug (
  2622.                         slug_ID int(10) unsigned NOT NULL auto_increment,
  2623.                         slug_title varchar(255) NOT NULL COLLATE ascii_bin,
  2624.                         slug_type char(6) NOT NULL DEFAULT "item",
  2625.                         slug_itm_ID int(11) unsigned,
  2626.                         PRIMARY KEY slug_ID (slug_ID),
  2627.                         UNIQUE  slug_title (slug_title)
  2628.                     ) ENGINE = innodb' );
  2629.         task_end();
  2630.  
  2631.         task_begin( 'Making sure all posts have a slug...' );
  2632.         // Get posts with empty urltitle:
  2633.         $sql = 'SELECT post_ID, post_title
  2634.                       FROM T_items__item
  2635.                      WHERE post_urltitle IS NULL OR post_urltitle = ""';
  2636.         $rows = $DB->get_results( $sql, OBJECT, 'Get posts with empty urltitle' );
  2637.         // Create URL titles when non existent:
  2638.         foreach( $rows as $row )
  2639.         {
  2640.             // TODO: dh> pass locale (useful for transliteration).
  2641.             $DB->query( 'UPDATE T_items__item
  2642.                               SET post_urltitle = "'.urltitle_validate( '', $row->post_title, 0 ).'"
  2643.                         WHERE post_ID = '.$row->post_ID, 'Set posts urltitle' );
  2644.         }
  2645.         task_end();
  2646.  
  2647.         task_begin( 'Populating Slugs table... ' );
  2648.         $DB->query( 'REPLACE INTO T_slug( slug_title, slug_type, slug_itm_ID)
  2649.                       SELECT post_urltitle, "item", post_ID
  2650.                                   FROM T_items__item' );
  2651.         task_end();
  2652.  
  2653.         task_begin( 'Add canonical and tiny slug IDs to post table...' );
  2654.         // modify post_urltitle column -> Not allow NULL value
  2655.         db_add_col( 'T_items__item', 'post_urltitle', 'VARCHAR(210) NOT NULL' );
  2656.         db_add_col( 'T_items__item', 'post_canonical_slug_ID', 'int(10) unsigned NULL default NULL after post_urltitle' );
  2657.         db_add_col( 'T_items__item', 'post_tiny_slug_ID', 'int(10) unsigned NULL default NULL after post_canonical_slug_ID' );
  2658.         task_end();
  2659.  
  2660.         task_begin( 'Upgrading posts...' );
  2661.         $DB->query( 'UPDATE T_items__item, T_slug
  2662.                           SET post_canonical_slug_ID = slug_ID
  2663.                         WHERE CONVERT( post_urltitle USING ASCII ) COLLATE ascii_bin = slug_title' );
  2664.         task_end();
  2665.  
  2666.         task_begin( 'Adding "help" slug...' );
  2667.         if( db_key_exists( 'T_slug', 'slug_title', '"help"' ) )
  2668.         {
  2669.             echo '<strong>Warning: "help" slug already exists!</strong><br /> ';
  2670.         }
  2671.         else
  2672.         {
  2673.             $DB->query( 'INSERT INTO T_slug( slug_title, slug_type )
  2674.                          VALUES( "help", "help" )', 'Add "help" slug' );
  2675.             task_end();
  2676.         }
  2677.  
  2678.         // fp> Next time we should use pluggable permissions instead.
  2679.         task_begin( 'Updgrading groups: Giving Administrators Group edit perms on slugs...' );
  2680.         db_add_col( 'T_groups', 'grp_perm_slugs', "enum('none','view','edit') NOT NULL default 'none'" );
  2681.         $DB->query( 'UPDATE T_groups
  2682.                      SET grp_perm_slugs = "edit"
  2683.                      WHERE grp_ID = 1' );
  2684.         task_end();
  2685.  
  2686.         task_begin( 'Upgrading settings table... ');
  2687.         $DB->query( 'UPDATE T_settings
  2688.                         SET set_value = 1
  2689.                       WHERE set_name = "fm_enable_roots_user"
  2690.                             AND set_value = 0' );
  2691.         task_end();
  2692.  
  2693.         // New perms for comment moderation depending on status:
  2694.         task_begin( 'Upgrading Blog-User permissions...' );
  2695.         db_add_col( 'T_coll_user_perms', 'bloguser_perm_draft_cmts', 'tinyint NOT NULL default 0 AFTER bloguser_perm_comments' );
  2696.         db_add_col( 'T_coll_user_perms', 'bloguser_perm_publ_cmts', 'tinyint NOT NULL default 0 AFTER bloguser_perm_comments' );
  2697.         db_add_col( 'T_coll_user_perms', 'bloguser_perm_depr_cmts', 'tinyint NOT NULL default 0 AFTER bloguser_perm_comments' );
  2698.  
  2699.         if( db_col_exists( 'T_coll_user_perms', 'bloguser_perm_comments' ) )
  2700.         { // if user had perm_comments he now gets all 3 new perms also:
  2701.             $DB->query( 'UPDATE T_coll_user_perms
  2702.                         SET bloguser_perm_draft_cmts = bloguser_perm_comments,
  2703.                             bloguser_perm_publ_cmts = bloguser_perm_comments,
  2704.                             bloguser_perm_depr_cmts = bloguser_perm_comments');
  2705.             db_drop_col( 'T_coll_user_perms', 'bloguser_perm_comments' );
  2706.         }
  2707.         task_end();
  2708.  
  2709.         task_begin( 'Upgrading Blog-Group permissions...' );
  2710.         db_add_col( 'T_coll_group_perms', 'bloggroup_perm_draft_cmts', 'tinyint NOT NULL default 0 AFTER bloggroup_perm_comments' );
  2711.         db_add_col( 'T_coll_group_perms', 'bloggroup_perm_publ_cmts', 'tinyint NOT NULL default 0 AFTER bloggroup_perm_comments' );
  2712.         db_add_col( 'T_coll_group_perms', 'bloggroup_perm_depr_cmts', 'tinyint NOT NULL default 0 AFTER bloggroup_perm_comments' );
  2713.  
  2714.         if( db_col_exists( 'T_coll_group_perms', 'bloggroup_perm_comments' ) )
  2715.         { // if group had perm_comments he now gets all 3 new perms also:
  2716.             $DB->query( 'UPDATE T_coll_group_perms
  2717.                         SET bloggroup_perm_draft_cmts = bloggroup_perm_comments,
  2718.                             bloggroup_perm_publ_cmts = bloggroup_perm_comments,
  2719.                             bloggroup_perm_depr_cmts = bloggroup_perm_comments');
  2720.             db_drop_col( 'T_coll_group_perms', 'bloggroup_perm_comments' );
  2721.         }
  2722.         task_end();
  2723.  
  2724.         task_begin( 'Upgrading messaging permissions...' );
  2725.         $DB->query( 'ALTER TABLE T_users ALTER COLUMN user_allow_msgform SET DEFAULT "2"' );
  2726.         $DB->query( 'UPDATE T_users
  2727.                     SET user_allow_msgform = 3
  2728.                     WHERE user_allow_msgform = 1');
  2729.         task_end();
  2730.  
  2731.         task_begin( 'Upgrading currency table...' );
  2732.         $DB->query( 'ALTER TABLE T_currency ADD COLUMN curr_enabled tinyint(1) NOT NULL DEFAULT 1 AFTER curr_name' );
  2733.         task_end();
  2734.  
  2735.         task_begin( 'Upgrading default blog access type for new blogs...' );
  2736.         $DB->query( 'ALTER TABLE T_blogs ALTER COLUMN blog_access_type SET DEFAULT "extrapath"' );
  2737.         task_end();
  2738.  
  2739.         task_begin( 'Upgrading tags table...' );
  2740.         $DB->query( 'ALTER TABLE T_items__tag CHANGE COLUMN tag_name tag_name varbinary(50) not null' );
  2741.         task_end();
  2742.  
  2743.         // fp> I don't understand why we need to carry this out "again" but I observed the installer barking on
  2744.         // this setting missing when upgrading from older 2.x versions. I figured it would be no big deal to do it twice...
  2745.         task_begin( 'Makin sure usersettings table is InnoDB...' );
  2746.         $DB->query( 'ALTER TABLE T_users__usersettings ENGINE=innodb' );
  2747.         task_end();
  2748.  
  2749.         // set_upgrade_checkpoint( '10000' );
  2750.     }
  2751.  
  2752.     /*
  2753.      * ADD UPGRADES HERE.
  2754.      *
  2755.      * ALL DB CHANGES MUST BE EXPLICITELY CARRIED OUT. DO NOT RELY ON SCHEMA UPDATES!
  2756.      * Schema updates do not survive after several incremental changes.
  2757.      *
  2758.      * NOTE: every change that gets done here, should bump {@link $new_db_version} (by 100).
  2759.      */
  2760.  
  2761.  
  2762.  
  2763.     /* Wait until we're sure and no longer experimental for that one...
  2764.     task_begin( 'Moving user data to fields' );
  2765.     // ICQ
  2766.     $DB->query( "INSERT INTO T_users__fields( uf_user_ID, uf_ufdf_ID, uf_varchar )
  2767.                              SELECT user_ID, 10300, user_icq
  2768.                                  FROM T_users
  2769.                                 WHERE user_msn IS NOT NULL AND TRIM(user_icq) <> ''" );
  2770.     // URL
  2771.     $DB->query( "INSERT INTO T_users__fields( uf_user_ID, uf_ufdf_ID, uf_varchar )
  2772.                              SELECT user_ID, 100000, user_url
  2773.                                  FROM T_users
  2774.                                 WHERE user_msn IS NOT NULL AND TRIM(user_url) <> ''" );
  2775.     // AIM
  2776.     $DB->query( "INSERT INTO T_users__fields( uf_user_ID, uf_ufdf_ID, uf_varchar )
  2777.                              SELECT user_ID, 10200, user_aim
  2778.                                  FROM T_users
  2779.                                 WHERE user_msn IS NOT NULL AND TRIM(user_aim) <> ''" );
  2780.     // MSN/live IM
  2781.     $DB->query( "INSERT INTO T_users__fields( uf_user_ID, uf_ufdf_ID, uf_varchar )
  2782.                              SELECT user_ID, 10000, user_msn
  2783.                                  FROM T_users
  2784.                                 WHERE user_msn IS NOT NULL AND TRIM(user_msn) <> ''" );
  2785.     // Yahoo IM
  2786.     $DB->query( "INSERT INTO T_users__fields( uf_user_ID, uf_ufdf_ID, uf_varchar )
  2787.                              SELECT user_ID, 10100, user_yim
  2788.                                  FROM T_users
  2789.                                 WHERE user_msn IS NOT NULL AND TRIM(user_yim) <> ''" );
  2790.     task_end();
  2791.     */
  2792.  
  2793.  
  2794.  
  2795.     // Just in case, make sure the db schema version is up to date at the end.
  2796.     if( $old_db_version != $new_db_version )
  2797.     { // Update DB schema version to $new_db_version
  2798.         set_upgrade_checkpoint( $new_db_version );
  2799.     }
  2800.  
  2801.  
  2802.  
  2803.     // This has to be at the end because plugin install may fail if the DB schema is not current (matching Plugins class).
  2804.     // Only new default plugins will be installed, based on $old_db_version.
  2805.     // dh> NOTE: if this fails (e.g. fatal error in one of the plugins), it will not get repeated
  2806.     install_basic_plugins( $old_db_version );
  2807.  
  2808.  
  2809.     /*
  2810.      * -----------------------------------------------
  2811.      * Check to make sure the DB schema is up to date:
  2812.      * -----------------------------------------------
  2813.      */
  2814.     $upgrade_db_deltas = array(); // This holds changes to make, if any (just all queries)
  2815.  
  2816.     global $debug;
  2817.  
  2818.     foreach( $schema_queries as $table => $query_info )
  2819.     {   // For each table in the schema, check diffs...
  2820.         if( $debug )
  2821.         {
  2822.             echo '<br />Checking table: '.$table.': ';
  2823.         }
  2824.         $updates = db_delta( $query_info[1], array('drop_column', 'drop_index'), false, true );
  2825.         if( empty($updates) )
  2826.         {
  2827.             if( $debug ) echo 'ok';
  2828.         }
  2829.         else
  2830.         {
  2831.             if( $debug ) echo 'NEEDS UPDATE!';
  2832.             foreach( $updates as $table => $queries )
  2833.             {
  2834.                 foreach( $queries as $qinfo )
  2835.                 {
  2836.                     foreach( $qinfo['queries'] as $query )
  2837.                     { // subqueries for this query (usually one, but may include required other queries)
  2838.                         $upgrade_db_deltas[] = $query;
  2839.                     }
  2840.                 }
  2841.             }
  2842.         }
  2843.     }
  2844.  
  2845.     if( $debug )
  2846.     {
  2847.         echo '<br />';
  2848.     }
  2849.  
  2850.     if( empty($upgrade_db_deltas) )
  2851.     {   // no upgrades needed:
  2852.         echo '<p>'.T_('The database schema is up to date.').'</p>';
  2853.     }
  2854.     else
  2855.     {   // Upgrades are needed:
  2856.  
  2857.         $confirmed_db_upgrade = param('confirmed', 'integer', 0); // force confirmation
  2858.         $upgrade_db_deltas_confirm_md5 = param( 'upgrade_db_deltas_confirm_md5', 'string', '' );
  2859.  
  2860.         if( ! $confirmed_db_upgrade )
  2861.         {
  2862.             if( ! empty($upgrade_db_deltas_confirm_md5) )
  2863.             { // received confirmation from form
  2864.                 if( $upgrade_db_deltas_confirm_md5 != md5( implode('', $upgrade_db_deltas) ) )
  2865.                 { // unlikely to happen
  2866.                     echo '<p class="error">'
  2867.                         .T_('The DB schema has been changed since confirmation.')
  2868.                         .'</p>';
  2869.                 }
  2870.                 else
  2871.                 {
  2872.                     $confirmed_db_upgrade = true;
  2873.                 }
  2874.             }
  2875.         }
  2876.  
  2877.         if( ! $confirmed_db_upgrade )
  2878.         {
  2879.             global $action, $locale, $form_action;
  2880.             load_class( '_core/ui/forms/_form.class.php', 'Form' );
  2881.  
  2882.             if( !empty( $form_action ) )
  2883.             {
  2884.                 $Form = new Form( $form_action, '', 'post' );
  2885.             }
  2886.             else
  2887.             {
  2888.                 $Form = new Form( NULL, '', 'post' );
  2889.             }
  2890.  
  2891.             $Form->begin_form( 'fform', T_('Upgrade database') );
  2892.  
  2893.             $Form->begin_fieldset();
  2894.             $Form->hidden( 'upgrade_db_deltas_confirm_md5', md5(implode( '', $upgrade_db_deltas )) );
  2895.             $Form->hidden( 'action', $action );
  2896.             $Form->hidden( 'locale', $locale );
  2897.  
  2898.  
  2899.             echo '<p>'.T_('The version number is correct, but we have detected changes in the database schema. This can happen with CVS versions...').'</p>';
  2900.  
  2901.             echo '<p>'.T_('The following database changes will be carried out. If you are not sure what this means, it will probably be alright.').'</p>';
  2902.  
  2903.             echo '<ul>';
  2904.             foreach( $upgrade_db_deltas as $l_delta )
  2905.             {
  2906.                 #echo '<li><code>'.nl2br($l_delta).'</code></li>';
  2907.                 echo '<li><pre>'.str_replace( "\t", '  ', $l_delta ).'</pre></li>';
  2908.             }
  2909.             echo '</ul>';
  2910.             $Form->submit( array( '', T_('Upgrade database!'), 'ActionButton' ) );
  2911.             $Form->end_form();
  2912.  
  2913.             return false;
  2914.         }
  2915.  
  2916.         // Alter DB to match DB schema:
  2917.         install_make_db_schema_current( true );
  2918.     }
  2919.  
  2920.     return true;
  2921. }
  2922.  
  2923.  
  2924. /*
  2925.  * $Log: _functions_evoupgrade.php,v $
  2926.  */
  2927. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement