use wcf\system\exception\IllegalLinkException;
use wcf\system\exception\UserInputException;
use wcf\system\WCF;
+use wcf\util\ArrayUtil;
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<string>
*/
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'];
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();
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,
'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,
'dbName' => $this->dbName,
'dbPrefix' => $this->dbPrefix,
'fileSystemPath' => $this->fileSystemPath,
- 'userMergeMode' => $this->userMergeMode
+ 'userMergeMode' => $this->userMergeMode,
+ 'additionalData' => $this->additionalData
));
}
}
* @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<integer>
*/
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()
*/
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()
*/
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);
}
/**
* Worker implementation for data import.
- *
+ *
* @author Marcel Werk
* @copyright 2001-2013 WoltLab GmbH
* @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
* @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()
*/
$this->exporter->exportData($this->parameters['objectType'], $this->loopCount);
}
-
+
/**
* @see wcf\system\worker\IWorker::getProceedURL()
*/