示例#1
0
<?php

/*
 * compares current database to the release database template and makes
 * updates as needed
 *
 * @author Stephen Billard
 * @Copyright 2016 by Stephen L Billard for use in {@link https://github.com/ZenPhoto20/ZenPhoto20 ZenPhoto20}
 *
 */
$dbSoftware = db_software();
$indexComments = version_compare($dbSoftware['version'], '5.5.0') >= 0;
$database = $orphans = array();
foreach (getDBTables() as $table) {
    $tablecols = db_list_fields($table);
    foreach ($tablecols as $key => $datum) {
        //remove don't care fields
        unset($datum['Collation']);
        unset($datum['Key']);
        unset($datum['Extra']);
        unset($datum['Privileges']);
        $database[$table]['fields'][$datum['Field']] = $datum;
    }
    $indices = array();
    $sql = 'SHOW KEYS FROM ' . prefix($table);
    $result = query_full_array($sql);
    foreach ($result as $index) {
        if ($index['Key_name'] !== 'PRIMARY') {
            $indices[$index['Key_name']][] = $index;
        }
    }
示例#2
0
 /**
  *
  * This method establishes the current set of database fields. It will add the
  * fields to the database if they are not already present. Fields from previous
  * constructor calls that are no longer in the list will be removed from the
  * database (along with any data associated with them.)
  *
  * @param array $newfields
  */
 function constructor($me, $newfields)
 {
     $database = array();
     foreach (getDBTables() as $table) {
         $tablecols = db_list_fields($table);
         foreach ($tablecols as $key => $datum) {
             $database[$table][$datum['Field']] = $datum;
         }
     }
     $current = $fields = $searchDefault = array();
     if (extensionEnabled($me)) {
         //need to update the database tables.
         foreach ($newfields as $newfield) {
             $table = $newfield['table'];
             $name = $newfield['name'];
             if (!($existng = isset($database[$table][$name]))) {
                 if (isset($newfield['searchDefault']) && $newfield['searchDefault']) {
                     $searchDefault[] = $name;
                 }
             }
             if (is_null($newfield['type'])) {
                 if ($name == 'tags') {
                     setOption('adminTagsTab', 1);
                 }
             } else {
                 switch (strtolower($newfield['type'])) {
                     default:
                         $dbType = strtoupper($newfield['type']);
                         break;
                     case 'int':
                         $dbType = strtoupper($newfield['type']) . '(' . min(255, $newfield['size']) . ')';
                         if (isset($newfield['attribute'])) {
                             $dbType .= ' ' . $newfield['attribute'];
                             unset($newfield['attribute']);
                         }
                         break;
                     case 'varchar':
                         $dbType = strtoupper($newfield['type']) . '(' . min(255, $newfield['size']) . ')';
                         break;
                 }
                 if ($existng) {
                     if (strtoupper($database[$table][$name]['Type']) != $dbType || empty($database[$table][$name]['Comment'])) {
                         $cmd = ' CHANGE `' . $name . '`';
                     } else {
                         $cmd = NULL;
                     }
                     unset($database[$table][$name]);
                 } else {
                     $cmd = ' ADD COLUMN';
                 }
                 $sql = 'ALTER TABLE ' . prefix($newfield['table']) . $cmd . ' `' . $name . '` ' . $dbType;
                 if (isset($newfield['attribute'])) {
                     $sql .= ' ' . $newfield['attribute'];
                 }
                 if (isset($newfield['default'])) {
                     $sql .= ' DEFAULT ' . $newfield['default'];
                 }
                 $sql .= " COMMENT 'optional_{$me}'";
                 if ((!$cmd || setupQuery($sql)) && in_array($newfield['table'], array('albums', 'images', 'news', 'news_categories', 'pages'))) {
                     $fields[] = strtolower($newfield['name']);
                 }
                 $current[$newfield['table']][$newfield['name']] = $dbType;
             }
         }
         setOption(get_class($this) . '_addedFields', serialize($current));
         if (!empty($searchDefault)) {
             $fieldExtenderMutex = new zpMutex('fE');
             $fieldExtenderMutex->lock();
             $engine = new SearchEngine();
             $set_fields = $engine->allowedSearchFields();
             $set_fields = array_unique(array_merge($set_fields, $searchDefault));
             setOption('search_fields', implode(',', $set_fields));
             $fieldExtenderMutex->unlock();
         }
     } else {
         purgeOption(get_class($this) . '_addedFields');
     }
     foreach ($database as $table => $fields) {
         //drop fields no longer defined
         foreach ($fields as $field => $orphaned) {
             if ($orphaned['Comment'] == "optional_{$me}") {
                 $sql = 'ALTER TABLE ' . prefix($table) . ' DROP `' . $field . '`';
                 setupQuery($sql);
             }
         }
     }
 }