summaryrefslogtreecommitdiff
path: root/admin/upgrades/1.0.1.php
blob: a17626e1cc3098d9c1a8cd708b13298859495a71 (plain)
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
<?php
/**
 * @version $Header$
 */
global $gBitInstaller;

$infoHash = array(
	'package'      => BOARDS_PKG_NAME,
	'version'      => str_replace( '.php', '', basename( __FILE__ )),
	'description'  => "Add boards sections and positioning.",
	'post_upgrade' => NULL,
);
$gBitInstaller->registerPackageUpgrade( $infoHash, array(

array( 'DATADICT' => array(
	array( 'CREATE' => array(
		'boards_sections' => "
			section_id I4 PRIMARY,
			section_title C(255)
		",	
	)),
	// insert new column
	array( 'ALTER' => array(
		'boards' => array(
			'section_id' => array( '`section_id`', 'I4' ),
			'pos' => array( '`pos`', 'I4' ),
	))),
	array( 'CREATEINDEX' => array(
		'boards_sections_idx'       => array( 'boards', 'section_id', array() ),
	)),
	array( 'CREATESEQUENCE' => array(
		'boards_sections_id_seq',
	)),
)),

array( 'PHP' => '
// Is package installed and enabled
global $gBitSystem;

$gBitSystem->verifyPackage( "boards" );

require_once( BOARDS_PKG_PATH."BitBoardTopic.php");

$oTopic = new BitBoardTopic();

// get a list of all the bad records
$list_query = "SELECT bt.*
            FROM `".BIT_DB_PREFIX."boards_topics` bt 
            INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id`= bt.`parent_id` ) 
            WHERE lc.content_type_guid != ?";

$bind_vars = array( "bitcomment" );

$max_records = 99999;

$map_errors = $oTopic->mDb->query( $list_query, $bind_vars, $max_records );

// fix everything 
// transaction will save us if something goes bad
$oTopic->StartTrans();

// expunge all the bad records we just got a list of
$expunge_query = "DELETE FROM `".BIT_DB_PREFIX."boards_topics` 
                    WHERE `parent_id` IN 
                    ( SELECT bt.`parent_id` 
                        FROM `".BIT_DB_PREFIX."boards_topics` bt 
                        INNER JOIN `".BIT_DB_PREFIX."liberty_content` lc ON ( lc.`content_id`= bt.`parent_id` ) 
                        WHERE lc.content_type_guid != ? )";
$oTopic->mDb->query( $expunge_query, $bind_vars );

// repopulate the records with the proper parent_id value
while( $topic = $map_errors->fetchRow() ) {
    $store_hash = $topic;
    $comment_query = "SELECT lcom.`content_id` FROM `".BIT_DB_PREFIX."liberty_comments` lcom WHERE lcom.`comment_id` = ?";
    // if the mapping isnt totally screwed up the parent id should work as a comment_id
    if( $comment_content_id = $oTopic->mDb->getOne( $comment_query, array( $topic["parent_id"] ) ) ){
        // just to be doublely safe, make sure the record doesnt already exist in the table
        if( !$oTopic->mDb->getOne( "SELECT parent_id FROM boards_topics WHERE parent_id = ?", $comment_content_id ) ){
            $store_hash["parent_id"] = $comment_content_id;
            // reinsert the topic
            if( $result = $oTopic->mDb->associateInsert( "boards_topics", $store_hash ) ){
                echo "Table boards_topic mapping repaired for topic/comment content id:" . $comment_content_id . "<br />";
            }
        }else{
            echo "Duplicate record for topic/comment content id:" . $comment_content_id . ", insertion ignored <br />";
        }
    }
}

$oTopic->CompleteTrans();
' ),


));
?>