插件导入导出的命令运行方法

导入/导出插件提供了将数据传入和传出 OJS 和 OMP 的工具,建议新手不要用命令行的方式操作以免出现未知错误。

插件导入导出的命令运行方法直入主题,输入命令:
$ php tools/importExport.php ExampleImportExportPlugin import filename.csv

import(‘lib.pkp.classes.plugins.ImportExportPlugin’);
class ExampleImportExportPlugin extends ImportExportPlugin {
/**
* @copydoc ImportExportPlugin::usage()
*/
public function usage($scriptName) {
echo “Usage: ” . $scriptName . ” ” . $this->getName() . ” [command]\n”;
echo “Commands:\n”;
echo ” import [filename]”;
echo ” export [filename]”;
}

/**
* @copydoc ImportExportPlugin::executeCLI()
*/
public function executeCLI($scriptName, &$args) {
$command = array_shift($args);
$filename = array_shift($args);

if ($command === ‘import’) {
$data = file_get_contents($filename);
/* now import the data */

} elseif ($command === ‘export’) {
$fp = fopen($filename, ‘wt’);
fputcsv($fp, [/* export dataset */]);
fclose($fp);
}
}
}

每个导入/导出插件在“工具>导入/导出”菜单下都有自己的页面。这可用于显示导入和导出选项,并提供用于生成导出的 UI。

import(‘lib.pkp.classes.plugins.ImportExportPlugin’);
class ExampleImportExportPlugin extends ImportExportPlugin {
/**
* @copydoc ImportExportPlugin::register()
*/
public function display($args, $request) {
parent::display($args, $request);

// Use the path to determine which action
// should be taken.
$path = array_shift($args);
switch ($path) {

// Deliver a CSV file for download
case ‘exportAll’:
header(‘content-type: text/comma-separated-values’);
header(‘content-disposition: attachment; filename=example-‘ . date(‘Ymd’) . ‘.csv’);
$fp = fopen(‘php://output’, ‘wt’);
fputcsv($fp, [/* export dataset */]);
fclose($fp);
break;

// When no path is requested, display a page with
// export options and a form to kick off the
// `exportAll` path.
default:
$templateMgr = TemplateManager::getManager($request);
$templateMgr->display($this->getTemplateResource(‘export.tpl’));
}
}
}

使用模板中的 smarty 功能将表单提交到其中一个导入或导出路径。{plugin_url …}

<form method=”POST” action=”{plugin_url path=”exportAll”}”>
<button type=”submit”>Export All</button>
</form>