Skip to content

Commit

Permalink
Closed #22 Export system: Generic export script
Browse files Browse the repository at this point in the history
  • Loading branch information
lolautruche committed Feb 14, 2012
1 parent a8f1d2c commit 47e2d2b
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 11 deletions.
99 changes: 91 additions & 8 deletions bin/php/export.php
@@ -1,11 +1,94 @@
<?php
use Disqus\Export\Processor as ExportProcessor,
Disqus\Export\Exporter\EzComments as EzCommentsExporter,
Disqus\Export\Formatter\DisqusWXR as DisqusFormatter;
/**
* Main export script
* @copyright Copyright (C) 2012 Bertrand Dunogier, Jérôme Vieilledent. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-3.0.txt GNU General Public License v3
*/
use Disqus\Export\Processor as ExportProcessor;

$processor = new ExportProcessor(
new EzCommentsExporter(),
new DisqusFormatter( true )
require 'autoload.php';

$cli = eZCLI::instance();
$cli->setUseStyles(true);
$script = eZScript::instance( array( 'description' => ( "Comment export script to be re-imported into the Disqus service\n"),
'use-session' => false,
'use-modules' => true,
'use-extensions' => true ) );

$script->startup();

// Options processing
$options = $script->getOptions(
'[exporter:][formatter:][options:][split]',
'',
array(
'exporter' => 'Exporter to use, as defined in disqus.ini',
'formatter' => 'Formatter to use. Default is DisqusWXR formatter',
'exporter-options' => 'Options for exporter. Should be something like --exporter-options="foo=bar,foo2=baz"',
'formatter-options' => 'Options for formatter. Should be something like --formatter-options="foo=bar,foo2=baz"',
'split' => 'Split the export into several files if needed'
)
);
$processor->export();
echo $processor->render();
$script->initialize();
$script->setUseDebugAccumulators( true );

try
{
$disqusINI = eZINI::instance( 'disqus.ini' );
$availableExporters = $disqusINI->variable( 'ExportSettings', 'Exporters' );
$availableFormatters = $disqusINI->variable( 'ExportSettings', 'Formatters' );

// Check exporter validity
if ( !isset( $options['exporter'] ) )
throw new Exception( 'You must provide an exporter!' );
if ( !isset( $availableExporters[$options['exporter']] ) )
throw new Exception( "Invalid exporter '{$options['exporter']}'. Valid exporters are: " . implode( ', ', array_keys( $availableExporters ) ) );
$exporter = new $availableExporters[$options['exporter']];

// Check formatter validity
if ( !isset( $options['formatter'] ) )
$options['formatter'] = 'disquswxr';
else if ( !isset( $availableFormatters[$options['formatter']] ) )
throw new Exception( "Invalid formatter '{$options['formatter']}'. Valid formatters are: " . implode( ', ', array_keys( $availableFormatters ) ) );
$formatter = new $availableFormatters[$options['formatter']];

// Now export
$cli->notice( "Now exporting with {$exporter->getName()} exporter and {$formatter->getName()} formatter..." );
$processor = new ExportProcessor( $exporter, $formatter );
$processor->export();

// Rendering or splitting ?
$exportDir = eZSys::varDirectory();
$exportBaseFilename = 'comments-export';
$exportFormat = $processor->getExportFormat();
if ( isset( $options['split'] ) )
{
$cli->notice( 'Now splitting final file.' );
$splittedData = $processor->split();
$cli->notice( 'Total size before splitting is ' . number_format( $splittedData->totalSize / 1024 / 1024, 2 ) . 'M' );
foreach ( $splittedData->stringArray as $i => $data )
{
$filename = "$exportBaseFilename.$i.$exportFormat";
$cli->notice( "Now rendering $exportDir/$filename" );
eZFile::create( "$filename", $exportDir, $data, true );
}
}
else
{
$cli->notice( "Now rendering to $exportDir/$exportBaseFilename.$exportFormat" );
eZFile::create( "$exportBaseFilename.$exportFormat", $exportDir, $processor->render(), true );
}

$memoryMax = memory_get_peak_usage();
$memoryMax = round( $memoryMax / 1024 / 1024, 2 ); // Convert in Megabytes
$cli->notice( 'Peak memory usage : '.$memoryMax . 'M' );

$script->shutdown();
}
catch( Exception $e )
{
$errCode = $e->getCode();
$errCode = $errCode != 0 ? $errCode : 1; // If an error has occured, script must terminate with a status other than 0
$cli->error( $e->getMessage() );
$script->shutdown( $errCode );
}
4 changes: 2 additions & 2 deletions classes/Disqus/Export/Exporter/EzComments.php
Expand Up @@ -48,9 +48,9 @@ public function __construct()
*
* @return string
*/
public function exporterName()
public function getName()
{
return 'eZ Comments exporter';
return 'eZ Comments';
}

/**
Expand Down
2 changes: 1 addition & 1 deletion classes/Disqus/Export/ExporterInterface.php
Expand Up @@ -19,7 +19,7 @@ interface ExporterInterface
*
* @return string
*/
public function exporterName();
public function getName();

/**
* Initializes the exporter.
Expand Down
21 changes: 21 additions & 0 deletions classes/Disqus/Export/Formatter/DisqusWXR.php
Expand Up @@ -62,6 +62,27 @@ public function initialize()
$this->xmlDoc->firstChild->appendChild( $this->channelTag );
}

/**
* Returns the formatter human readable name.
*
* @return string
*/
public function getName()
{
return 'Disqus WXR XML';
}

/**
* Returns export format.
* Will be use as export file suffix.
*
* @return string
*/
public function getFormat()
{
return 'xml';
}

/**
* Generates valid DOMDocument and appends its root <rss> tag
*
Expand Down
15 changes: 15 additions & 0 deletions classes/Disqus/Export/FormatterInterface.php
Expand Up @@ -51,4 +51,19 @@ public function getData();
* @return \Disqus\Export\SplitterInterface
*/
public function getSplitter();

/**
* Returns export format.
* Will be use as export file suffix.
*
* @return string
*/
public function getFormat();

/**
* Returns the formatter human readable name.
*
* @return string
*/
public function getName();
}
11 changes: 11 additions & 0 deletions classes/Disqus/Export/Processor.php
Expand Up @@ -82,4 +82,15 @@ public function split()
$splitter->split( $splittedData );
return $splittedData;
}

/**
* Returns export format.
* Will be use as export file suffix.
*
* @return string
*/
public function getExportFormat()
{
return $this->formatter->getFormat();
}
}
10 changes: 10 additions & 0 deletions settings/disqus.ini
Expand Up @@ -14,3 +14,13 @@ DevelopmentMode=disabled
# Export max file size, in bytes
# If export file exceeds this value, it will be splitted into several
ExportMaxSize=50000000

# Exporter classes
# Key is the identifier (used in the export script as --exporter option)
# Value is the exporter class FQN
# All exporters must implement Disqus\Export\ExporterInterface interface
Exporters[]
Exporters[ezcomments]=Disqus\Export\Exporter\EzComments

Formatters[]
Formatters[disquswxr]=Disqus\Export\Formatter\DisqusWXR

0 comments on commit 47e2d2b

Please sign in to comment.