Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Todd's generic CSV utility & tips

  • Loading branch information...
commit 1b45af9cd3ffccf5870c06292dfade09f54d4aa7 1 parent ed59555
@linc linc authored
Showing with 102 additions and 3 deletions.
  1. +7 −1 README
  2. +93 −0 class.csv.php
  3. +1 −0  index.php
  4. +1 −2  make.php
View
8 README
@@ -6,4 +6,10 @@ Don't directly use this code to export your forum.
Use the compiled package at vanillaforums.org instead.
If you are modifying the code for your own purposes, don't modify vanilla2export.php directly.
-Modify the other source files and then run or browse to make.php and it will generate a new copy of vanilla2export.php.
+Modify the other source files and then run or browse to make.php and it will generate a new copy of vanilla2export.php.
+
+
+Tips
+-----
+1) You can include a custom file in your build by calling make.php?type=ClassName
+2) New export classes must extend either ExportController (default) or CsvController (CSV-based exporters).
View
93 class.csv.php
@@ -0,0 +1,93 @@
+<?php
+/**
+ * Utility additions for CSV-based exporter tools.
+ *
+ * @copyright Vanilla Forums Inc. 2011
+ * @license http://opensource.org/licenses/gpl-2.0.php GNU GPL2
+ * @package VanillaPorter
+ */
+
+/**
+ * Adds CSV utility methods for CSV-based exporters.
+ *
+ * @package VanillaPorter
+ */
+abstract class CsvController extends ExportController {
+ /**
+ * Import a CSV table into a database.
+ *
+ * @param string $Path
+ * @param string $TableName
+ * @param array $ColumnInfo
+ */
+ public function ImportCsv($Path, $TableName, $ColumnInfo = array()) {
+ $this->_DefineCsvTable($Path, $TableName, $ColumnInfo);
+
+ $this->Ex->Query("truncate table `$TableName`;");
+
+ $QPath = mysql_escape_string($Path);
+
+ $Sql = "load data infile '$QPath' into table $TableName
+ character set utf8
+ columns terminated by ','
+ optionally enclosed by '\"'
+ lines terminated by ',\\n'
+ ignore 1 lines";
+ $this->Ex->Query($Sql);
+ }
+
+ /**
+ * Build database tables based on information we can glean.
+ *
+ * @param string $Path
+ * @param string $TableName
+ * @param array $ColumnInfo
+ */
+ protected function _DefineCsvTable($Path, $TableName, $ColumnInfo = array()) {
+ // Grab the header information.
+ $fp = fopen($Path, 'rb');
+ $HeaderLine = fgets($fp);
+ fclose($fp);
+
+ $ColumnNames = explode(',', $HeaderLine);
+
+ // Loop through the columns and buld up a tabledef.
+ $Defs = array();
+ foreach ($ColumnNames as $ColumnName) {
+ $ColumnName = trim($ColumnName);
+ if (!$ColumnName) {
+ continue;
+ }
+
+ // Check for duplicate filenames.
+ $ColumnName2 = $ColumnName;
+ for($i = 1; isset($Defs[$ColumnName2]); $i++) {
+ $ColumnName2 = "DUP{$i}_{$ColumnName}";
+ }
+ $ColumnName = $ColumnName2;
+
+ $Defs[$ColumnName] = $ColumnName.' varchar(200)'; // default column def.
+
+ // Check to see if there is a more specific column definition.
+ foreach ($ColumnInfo as $Expr => $ColumnDef) {
+ if ($Expr == $ColumnName) {
+ $Defs[$ColumnName] = $ColumnName.' '.$ColumnDef;
+ break;
+ } elseif ($Expr[0] == $Expr[strlen($Expr) - 1]) {
+ if (preg_match($Expr, $ColumnName)) {
+ $Defs[$ColumnName] = $ColumnName.' '.$ColumnDef;
+ break;
+ }
+ }
+ }
+ }
+
+ // Drop the table.
+ $this->Ex->Query("drop table if exists `$TableName`");
+
+ // Create the table.
+ $CreateDef = "create table `$TableName` (\n".implode(",\n", $Defs).')';
+ $this->Ex->Query($CreateDef, TRUE);
+ }
+}
+?>
View
1  index.php
@@ -42,6 +42,7 @@
include('class.exportmodel.php');
include('views.php');
include('class.exportcontroller.php');
+include('class.csv.php');
include('class.vanilla1.php');
include('class.vanilla2.php');
View
3  make.php
@@ -4,8 +4,7 @@
</head>
<body>
<pre><?php
- /** Make the final vanilla2export.php file from the other sources.
- */
+ // Make the final vanilla2export.php file from the other sources.
// Open the file.
$Path = dirname(__FILE__).'/vanilla2export.php';
Please sign in to comment.
Something went wrong with that request. Please try again.