From fe0f49963165b63ac50699aefea5adf2844db401 Mon Sep 17 00:00:00 2001 From: NurPech Date: Sun, 14 Jul 2013 02:51:53 +0200 Subject: [PATCH] added support for additional data in data exporter --- .../lib/acp/form/DataImportForm.class.php | 88 +++++++++++-------- .../exporter/AbstractExporter.class.php | 43 +++++---- .../lib/system/worker/ImportWorker.class.php | 30 +++---- 3 files changed, 89 insertions(+), 72 deletions(-) diff --git a/wcfsetup/install/files/lib/acp/form/DataImportForm.class.php b/wcfsetup/install/files/lib/acp/form/DataImportForm.class.php index 081fdc14a8..34db5c504a 100644 --- a/wcfsetup/install/files/lib/acp/form/DataImportForm.class.php +++ b/wcfsetup/install/files/lib/acp/form/DataImportForm.class.php @@ -6,6 +6,7 @@ use wcf\system\database\DatabaseException; use wcf\system\exception\IllegalLinkException; use wcf\system\exception\UserInputException; use wcf\system\WCF; +use wcf\util\ArrayUtil; use wcf\util\StringUtil; /** @@ -19,142 +20,148 @@ use wcf\util\StringUtil; * @category Community Framework */ class DataImportForm extends AbstractForm { + /** + * additional data + * @var array + */ + public $additionalData = array(); + /** * @see wcf\page\AbstractPage::$activeMenuItem */ public $activeMenuItem = 'wcf.acp.menu.link.maintenance.import'; - + /** * @see wcf\page\AbstractPage::$neededPermissions */ public $neededPermissions = array('admin.system.canImportData'); - + /** * list of available exporters * @var array */ public $exporters = array(); - + /** * exporter name * @var string */ public $exporterName = ''; - + /** * exporter object * @var wcf\system\exporter\IExporter */ public $exporter = null; - + /** * list of available importers * @var array */ public $importers = array(); - + /** * list of supported data types * @var array */ public $supportedData = array(); - + /** * selected data types * @var array */ public $selectedData = array(); - + /** * database host name * @var string */ public $dbHost = ''; - + /** * database user name * @var string */ public $dbUser = ''; - + /** * database password * @var string */ public $dbPassword = ''; - + /** * database name * @var string */ public $dbName = ''; - + /** * database table prefix * @var string */ public $dbPrefix = ''; - + /** * file system path * @var string */ public $fileSystemPath = ''; - + /** * user merge mode * @var integer */ public $userMergeMode = 2; - + /** * @see wcf\page\IPage::readParameters() */ public function readParameters() { parent::readParameters(); - + // get available exporters/importers $this->exporters = ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.exporter'); $this->importers = array_keys(ObjectTypeCache::getInstance()->getObjectTypes('com.woltlab.wcf.importer')); - + if (isset($_REQUEST['exporterName'])) { $this->exporterName = $_REQUEST['exporterName']; if (!isset($this->exporters[$this->exporterName])) { throw new IllegalLinkException(); } - + $this->exporter = $this->exporters[$this->exporterName]->getProcessor(); $this->supportedData = $this->exporter->getSupportedData(); - + // remove unsupported data foreach ($this->supportedData as $key => $subData) { if (!in_array($key, $this->importers)) { unset($this->supportedData[$key]); continue; } - + foreach ($subData as $key2 => $value) { if (!in_array($value, $this->importers)) { unset($this->supportedData[$key][$key2]); } } } - + // get default database prefix if (!count($_POST)) { $this->dbPrefix = $this->exporter->getDefaultDatabasePrefix(); } } } - + /** * @see wcf\form\IForm::readFormParameters() */ public function readFormParameters() { parent::readFormParameters(); - + if (isset($_POST['selectedData']) && is_array($_POST['selectedData'])) $this->selectedData = $_POST['selectedData']; - + if (isset($_POST['dbHost'])) $this->dbHost = StringUtil::trim($_POST['dbHost']); if (isset($_POST['dbUser'])) $this->dbUser = StringUtil::trim($_POST['dbUser']); if (isset($_POST['dbPassword'])) $this->dbPassword = $_POST['dbPassword']; @@ -162,16 +169,17 @@ class DataImportForm extends AbstractForm { if (isset($_POST['dbPrefix'])) $this->dbPrefix = StringUtil::trim($_POST['dbPrefix']); if (isset($_POST['fileSystemPath'])) $this->fileSystemPath = StringUtil::trim($_POST['fileSystemPath']); if (isset($_POST['userMergeMode'])) $this->userMergeMode = intval($_POST['userMergeMode']); + if (isset($_POST['additionalData'])) $this->additionalData = ArrayUtil::trim($_POST['additionalData']); } - + /** * @see wcf\form\IForm::validate() */ public function validate() { parent::validate(); - - $this->exporter->setData($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName, $this->dbPrefix, $this->fileSystemPath); - + + $this->exporter->setData($this->dbHost, $this->dbUser, $this->dbPassword, $this->dbName, $this->dbPrefix, $this->fileSystemPath, $this->additionalData); + // validate database Access try { $this->exporter->validateDatabaseAccess(); @@ -180,32 +188,32 @@ class DataImportForm extends AbstractForm { WCF::getTPL()->assign('exception', $e); throw new UserInputException('database'); } - + // validate selected data if (!$this->exporter->validateSelectedData($this->selectedData)) { throw new UserInputException('selectedData'); } - + // validate file access if (!$this->exporter->validateFileAccess()) { throw new UserInputException('fileSystemPath'); } - + // validate user merge mode if ($this->userMergeMode < 1 || $this->userMergeMode > 3) { $this->userMergeMode = 2; } } - + /** * @see wcf\form\IForm::save() */ public function save() { parent::save(); - + // get queue $queue = $this->exporter->getQueue(); - + // save import data WCF::getSession()->register('importData', array( 'exporterName' => $this->exporterName, @@ -215,18 +223,19 @@ class DataImportForm extends AbstractForm { 'dbName' => $this->dbName, 'dbPrefix' => $this->dbPrefix, 'fileSystemPath' => $this->fileSystemPath, - 'userMergeMode' => $this->userMergeMode + 'userMergeMode' => $this->userMergeMode, + 'additionalData' => $this->additionalData )); - + WCF::getTPL()->assign('queue', $queue); } - + /** * @see wcf\page\IPage::assignVariables() */ public function assignVariables() { parent::assignVariables(); - + WCF::getTPL()->assign(array( 'exporter' => $this->exporter, 'importers' => $this->importers, @@ -240,7 +249,8 @@ class DataImportForm extends AbstractForm { 'dbName' => $this->dbName, 'dbPrefix' => $this->dbPrefix, 'fileSystemPath' => $this->fileSystemPath, - 'userMergeMode' => $this->userMergeMode + 'userMergeMode' => $this->userMergeMode, + 'additionalData' => $this->additionalData )); } } diff --git a/wcfsetup/install/files/lib/system/exporter/AbstractExporter.class.php b/wcfsetup/install/files/lib/system/exporter/AbstractExporter.class.php index 8a9a6d7499..a30a9695d7 100644 --- a/wcfsetup/install/files/lib/system/exporter/AbstractExporter.class.php +++ b/wcfsetup/install/files/lib/system/exporter/AbstractExporter.class.php @@ -16,99 +16,106 @@ use wcf\util\FileUtil; * @category Community Framework */ abstract class AbstractExporter implements IExporter { + /** + * additional data + * @var array + */ + public $additionalData = array(); + /** * database host name * @var string */ protected $databaseHost = ''; - + /** * database username * @var string */ protected $databaseUser = ''; - + /** * database password * @var string */ protected $databasePassword = ''; - + /** * database name * @var string */ protected $databaseName = ''; - + /** * table prefix * @var string */ protected $databasePrefix = ''; - + /** * file system path * @var string */ protected $fileSystemPath = ''; - + /** * database connection * @var wcf\system\database\Database */ protected $database = null; - + /** * object type => method names * @var array */ protected $methods = array(); - + /** * limits for items per run * @var array */ protected $limits = array(); - + /** * default limit for items per run * @var integer */ protected $defaultLimit = 1000; - + /** * @see wcf\system\exporter\IExporter::setData() */ - public function setData($databaseHost, $databaseUser, $databasePassword, $databaseName, $databasePrefix, $fileSystemPath) { + public function setData($databaseHost, $databaseUser, $databasePassword, $databaseName, $databasePrefix, $fileSystemPath, $additionalData) { $this->databaseHost = $databaseHost; $this->databaseUser = $databaseUser; $this->databasePassword = $databasePassword; $this->databaseName = $databaseName; $this->databasePrefix = $databasePrefix; $this->fileSystemPath = ($fileSystemPath ? FileUtil::addTrailingSlash($fileSystemPath) : ''); + $this->additionalData = $additionalData; } - + /** * @see wcf\system\exporter\IExporter::init() */ public function init() { $this->database = new MySQLDatabase($this->databaseHost, $this->databaseUser, $this->databasePassword, $this->databaseName, 0); } - + /** * @see wcf\system\exporter\IExporter::validateDatabaseAccess() */ public function validateDatabaseAccess() { $this->init(); } - + /** * @see wcf\system\exporter\IExporter::getDefaultDatabasePrefix() */ public function getDefaultDatabasePrefix() { return ''; } - + /** * @see wcf\system\exporter\IExporter::countLoops() */ @@ -116,12 +123,12 @@ abstract class AbstractExporter implements IExporter { if (!isset($this->methods[$objectType]) || !method_exists($this, 'count'.$this->methods[$objectType])) { throw new SystemException("unknown object type '".$objectType."' given"); } - + $count = call_user_func(array($this, 'count'.$this->methods[$objectType])); $limit = (isset($this->limits[$objectType]) ? $this->limits[$objectType] : $this->defaultLimit); return ceil($count / $limit); } - + /** * @see wcf\system\exporter\IExporter::exportData() */ @@ -129,7 +136,7 @@ abstract class AbstractExporter implements IExporter { if (!isset($this->methods[$objectType]) || !method_exists($this, 'export'.$this->methods[$objectType])) { throw new SystemException("unknown object type '".$objectType."' given"); } - + $limit = (isset($this->limits[$objectType]) ? $this->limits[$objectType] : $this->defaultLimit); call_user_func(array($this, 'export'.$this->methods[$objectType]), $loopCount * $limit, $limit); } diff --git a/wcfsetup/install/files/lib/system/worker/ImportWorker.class.php b/wcfsetup/install/files/lib/system/worker/ImportWorker.class.php index aa48b34c77..6ce635ec15 100644 --- a/wcfsetup/install/files/lib/system/worker/ImportWorker.class.php +++ b/wcfsetup/install/files/lib/system/worker/ImportWorker.class.php @@ -7,7 +7,7 @@ use wcf\system\WCF; /** * Worker implementation for data import. - * + * * @author Marcel Werk * @copyright 2001-2013 WoltLab GmbH * @license GNU Lesser General Public License @@ -21,62 +21,62 @@ class ImportWorker extends AbstractWorker { * @var array */ protected $importData = null; - + /** * exporter object * @var wcf\system\exporter\IExporter */ protected $exporter = null; - + /** * @see wcf\system\worker\IWorker::validate() */ public function validate() { WCF::getSession()->checkPermissions(array('admin.system.canImportData')); - + if (!isset($this->parameters['objectType'])) { throw new SystemException("parameter 'objectType' missing"); } - + // get import data $this->importData = WCF::getSession()->getVar('importData'); if ($this->importData === null) { throw new SystemException("import data missing"); } - + // get exporter $this->exporter = ObjectTypeCache::getInstance()->getObjectTypeByName('com.woltlab.wcf.exporter', $this->importData['exporterName'])->getProcessor(); - + // set data - $this->exporter->setData($this->importData['dbHost'], $this->importData['dbUser'], $this->importData['dbPassword'], $this->importData['dbName'], $this->importData['dbPrefix'], $this->importData['fileSystemPath']); + $this->exporter->setData($this->importData['dbHost'], $this->importData['dbUser'], $this->importData['dbPassword'], $this->importData['dbName'], $this->importData['dbPrefix'], $this->importData['fileSystemPath'], $this->importData['additionalData']); $this->exporter->init(); - + // set user merge mode ImportHandler::getInstance()->setUserMergeMode($this->importData['userMergeMode']); } - + /** * @see wcf\system\worker\AbstractWorker::countObjects() */ protected function countObjects() { $this->count = $this->exporter->countLoops($this->parameters['objectType']); } - + /** * @see wcf\system\worker\IWorker::getProgress() */ public function getProgress() { $this->countObjects(); - + if (!$this->count) { return 100; } - + $progress = (($this->loopCount + 1) / $this->count) * 100; if ($progress > 100) $progress = 100; return round($progress, 0); } - + /** * @see wcf\system\worker\IWorker::execute() */ @@ -87,7 +87,7 @@ class ImportWorker extends AbstractWorker { $this->exporter->exportData($this->parameters['objectType'], $this->loopCount); } - + /** * @see wcf\system\worker\IWorker::getProceedURL() */ -- 2.20.1