File: update.php

package info (click to toggle)
webissues-server 0.8.3-2-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 392 kB
  • ctags: 610
  • sloc: php: 2,206; sql: 458; sh: 44; makefile: 9
file content (128 lines) | stat: -rw-r--r-- 4,433 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
/**************************************************************************
* This file is part of the WebIssues Server program
* Copyright (C) 2006 Michał Męciński
* Copyright (C) 2007-2008 WebIssues Team
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
**************************************************************************/

define( 'VERSION', '0.8.3' );

if( !include_once( 'config/config.inc.php' ) )
    die( '<p><strong>Fatal Error:</strong> The configuration file <tt>config/config.inc.php</tt> does not exist.</p>' );

require_once( 'include/common.inc.php' );

define( 'SCRIPT', 'update.php' );

$page_titles = array(
    'config' => 'Configuration',
    'tables' => 'Data Tables'
);

$action = $_POST[ 'action' ];
$page = '';
$body = '';
$title = 'Update';

$result = wi_update_execute();

function wi_update_execute()
{
    global $action, $page, $body;
    global $config;

    $page = 'config';

    if ( !wi_check_config() )
        return false;

    $page = 'tables';

    if ( !wi_table_exists( 'server' ) ) {
        $body .= "<p><strong>ERROR:</strong> The data tables were not found. Use <a href=\"setup.php\">setup.php</a> if you want to create new tables.</p>\n";
        return false;
    }

    $query = "SELECT db_version FROM {server}";

    $server_row = wi_query_row( $query );

    $db_version = $server_row[ 'db_version' ];

    if ( version_compare( $db_version, VERSION, '>' ) ) {
        $body .= "<p><strong>ERROR:</strong> Current database version ($db_version) is newer than this script.</p>\n";
        return false;
    }

    if ( version_compare( $db_version, VERSION, '<' ) ) {
        if ( $action != 'tables' ) {
            $body .= "<p>Server is configured correctly. Current database version is $db_version.</p>\n";
            $body .= "<p>The database tables will be updated.</p>\n";
            return true;
        }

        $engine = $config[ 'db_engine' ];

        // 0.8.1 -> 0.8.2
        // add support for file storage

        if ( ( $engine == 'mysql' || $engine == 'mysqli' ) && $db_version == '0.8.1' ) {
            $query = "ALTER TABLE {files} MODIFY file_data longblob";
            wi_query( $query );
            $query = "ALTER TABLE {files} ADD file_storage tinyint(4) NOT NULL default '0'";
            wi_query( $query );
        }

        if ( $engine == 'pgsql' && $db_version == '0.8.1' ) {
            $query = "ALTER TABLE {files} ALTER COLUMN file_data DROP NOT NULL";
            wi_query( $query );
            $query = "ALTER TABLE {files} ADD COLUMN file_storage smallint NOT NULL default '0'";
            wi_query( $query );
        }

        // 0.8.2 -> 0.8.3
        // MySQL: set utf8_bin collation for some columns (or the BINARY attribute for MySQL 4.0)

        if ( ( $engine == 'mysql' || $engine == 'mysqli' ) && version_compare( $db_version, '0.8.2', '<=' ) ) {
            $tables = array(
                '{attr_types}' => array( 'attr_name' ),
                '{folders}' => array( 'folder_name' ),
                '{issue_types}' => array( 'type_name' ),
                '{projects}' => array( 'project_name' ),
                '{users}' => array( 'user_login', 'user_name' )
            );
            if ( $engine == 'mysqli' || version_compare( mysql_get_server_info(), '4.1', '>=' ) )
                $type = "varchar(40) COLLATE utf8_bin NOT NULL default ''";
            else
                $type = "varchar(255) BINARY NOT NULL default ''";
            foreach ( $tables as $table => $columns ) {
                $modify = array();
                foreach ( $columns as $column )
                    $modify[] = "MODIFY $column $type";
                $query = "ALTER TABLE $table " . implode( ", ", $modify );
                wi_query( $query );
            }
        }

        // --------------

        $query = "UPDATE {server} SET db_version = %s";
        wi_query( $query, VERSION );

        $body .= "<p>Database tables were updated successfully.</p>\n";
    }

    $page = '';

    $body .= "<p>This server is configured and working correctly.</p>\n";
    $body .= "<p>Use the WebIssues Client application to access the server.</p>\n";

    return true;
}

require_once( 'include/wizard.inc.php' );