Skip to content

Commit

Permalink
Added ExportModel::FormatValue() convenience function.
Browse files Browse the repository at this point in the history
Added ExportModel::WriteBeginTable convenience function.
Added basic support for Vanilla 2 exports.
  • Loading branch information
tburry committed Dec 20, 2010
1 parent 6191cbf commit b8f29a4
Show file tree
Hide file tree
Showing 4 changed files with 2,098 additions and 15 deletions.
61 changes: 57 additions & 4 deletions class.exportmodel.php
Expand Up @@ -47,6 +47,9 @@ class ExportModel {
*/
public $Prefix = '';

/** @var string The path to the source of the export in the case where a file is being converted. */
public $SourcePath = '';

/**
* @var array Strucutes that define the format of the export tables.
*/
Expand Down Expand Up @@ -205,6 +208,8 @@ public function BeginExport($Path = '', $Source = '', $Header = array()) {
}
fwrite($fp, self::NEWLINE.self::NEWLINE);
$this->Comment('Export Started: '.date('Y-m-d H:i:s'));

return $fp;
}

/**
Expand Down Expand Up @@ -332,10 +337,6 @@ protected function _ExportTable($TableName, $Query, $Mappings = array()) {
$Value = NULL;
}

if ($TableName == 'Permission') {
$Foo = 'Bar';
}

// Check to see if there is a callback filter.
if (isset($Filters[$Field])) {
$Callback = $Filters[$Field];
Expand Down Expand Up @@ -400,6 +401,32 @@ static function FormatElapsed($Start, $End = NULL) {
return $Result;
}

static function FormatValue($Value) {
static $EscapeSearch = NULL; if ($EscapeSearch === NULL) $EscapeSearch = array(self::ESCAPE, self::DELIM, self::NEWLINE, self::QUOTE); // escape must go first
static $EscapeReplace = NULL; if ($EscapeReplace === NULL) $EscapeReplace = array(self::ESCAPE.self::ESCAPE, self::ESCAPE.self::DELIM, self::ESCAPE.self::NEWLINE, self::ESCAPE.self::QUOTE);

// Format the value for writing.
if (is_null($Value)) {
$Value = self::NULL;
} elseif (is_numeric($Value)) {
// Do nothing, formats as is.
} elseif (is_string($Value)) {
if($Mb && mb_detect_encoding($Value) != 'UTF-8')
$Value = utf8_encode($Value);

$Value = str_replace(array("\r\n", "\r"), array(self::NEWLINE, self::NEWLINE), $Value);
$Value = self::QUOTE
.str_replace($EscapeSearch, $EscapeReplace, $Value)
.self::QUOTE;
} elseif (is_bool($Value)) {
$Value = $Value ? 1 : 0;
} else {
// Unknown format.
$Value = self::NULL;
}
return $Value;
}

public function GetCharacterSet($Table) {
// First get the collation for the database.
$Data = $this->Query("show table status like ':_{$Table}';");
Expand Down Expand Up @@ -756,5 +783,31 @@ public function VerifySource($RequiredTables) {
return 'Missing required database tables: '.$MissingTables;
}
}

public function WriteBeginTable($fp, $TableName, $Columns) {
$TableHeader = '';

foreach($Columns as $Key => $Value) {
if (is_numeric($Key)) {
$Column = $Value;
$Type = '';
} else {
$Column = $Key;
$Type = $Value;
}

if(strlen($TableHeader) > 0)
$TableHeader .= self::DELIM;

if ($Type)
$TableHeader .= $Column.':'.$Type;
else
$TableHeader .= $Column;
}

fwrite($fp, 'Table: '.$TableName.self::NEWLINE);
fwrite($fp, $TableHeader.self::NEWLINE);

}
}
?>
42 changes: 39 additions & 3 deletions class.vanilla2.php
Expand Up @@ -13,10 +13,46 @@ class Vanilla2 extends ExportController {
protected $_SourceTables = array();

/**
* Forum-specific export format
* @param ExportModel $Ex
*/
protected function ForumExport($Ex) {

$Tables = array(
'Activity',
'Category',
'Comment',
'Conversation',
'ConversationMessage',
'Discussion',
'Media',
'Permission',
'Role',
'User',
'UserConversation',
'UserDiscussion',
'UserMeta',
'UserRole');

$Ex->BeginExport('', 'Vanilla 2.*', array('HashMethod' => 'Vanilla'));

foreach ($Tables as $TableName) {
$this->ExportTable($Ex, $TableName);
}

$Ex->EndExport();
}

/**
*
* @param ExportModel $Ex
* @param string $TableName
*/
protected function ExportTable($Ex, $TableName) {
// Make sure the table exists.
if (!$Ex->Exists($TableName))
return;

$Ex->ExportTable($TableName, "select * from :_{$TableName}");
}

}
}
?>
8 changes: 6 additions & 2 deletions index.php
@@ -1,6 +1,6 @@
<?php
define('APPLICATION', 'Porter');
define('APPLICATION_VERSION', '1.3.1');
define('APPLICATION_VERSION', '1.4a');
/**
* Vanilla 2 Exporter
* This script exports other forum databases to the Vanilla 2 import format.
Expand Down Expand Up @@ -29,11 +29,13 @@
/** @var array Supported forum packages: classname => array(name, prefix) */
$Supported = array(
'vanilla1' => array('name'=> 'Vanilla 1.*', 'prefix'=>'LUM_'),
'vanilla2' => array('name'=> 'Vanilla 2.*', 'prefix'=>'GDN_'),
'vbulletin' => array('name'=>'vBulletin 3.* and 4.*', 'prefix'=>'vb_'),
'phpbb2' => array('name'=>'phpBB 2.*', 'prefix' => 'phpbb_'),
'phpbb3' => array('name'=>'phpBB 3.*', 'prefix' => 'phpbb_'),
'bbPress' => array('name'=>'bbPress 1.*', 'prefix' => 'bb_'),
'SimplePress' => array('name'=>'SimpePress 1.*', 'prefix' => 'wp_')
'SimplePress' => array('name'=>'SimpePress 1.*', 'prefix' => 'wp_'),
'SMF' => array('name'=>'SMF (Simple Machines) 1.*', 'prefx' => 'smf_')
);

// Support Files
Expand All @@ -42,11 +44,13 @@
include('class.exportcontroller.php');

include('class.vanilla1.php');
include('class.vanilla2.php');
include('class.vbulletin.php');
include('class.phpbb2.php');
include('class.phpbb3.php');
include('class.bbpress.php');
include('class.simplepress.php');
include('class.smf.php');

// Make sure a default time zone is set
if (ini_get('date.timezone') == '')
Expand Down

0 comments on commit b8f29a4

Please sign in to comment.