Zum Inhalt springen

cli und tx_impexp

Erstellt am 2. November 2011 · 1 Antwort · letzte Antwort am 3. November 2011

Tags: Frage

artus12 ·

Hallo,

ich versuche die Import-Funktion für .t3d-Dateien über die Kommandozeile verfügbar zu machen. Funktionen für das Aktivieren von Erweiterungen, einschließlich automatischer Aktualisierung der Datenbank, laufen bereits über dieses Skript.

Für den Import habe ich folgende Funktion hinzugefügt:

    function importT3d() {
	
	// get file name (absolute)
        $fileName = (string)$this->cli_args['_DEFAULT'][2];

	// get pid
        $pid = (int)$this->cli_args['_DEFAULT'][3];

	$import = t3lib_div::makeInstance('tx_impexp');
	$import->init(0, 'import');
	$import->enableLogging = true;

	if ($fileName && @is_file($fileName)) {	
		if ($import->loadFile($fileName, 1)) {
			$import->importData($pid);
			$this->cli_echo('Done');
		} else {
			$this->cli_echo('Loading file "'.$fileName.'" failed');
		}
	} else {
		$this->cli_echo('File "'.$fileName.'" not found');
	}
}

Problem ist, dass ein Auruf mit: [...] -f [...]/cli_dispatch.phpsh wee_shell import [...]/my.t3d 1 wortlos beendet wird. D.h. importData() wird zwar aufgrufen aber ohne jeglichen Effekt oder Fehlermeldung. Einen entsprechenden BE-Benutzer (_cli_wee_shell) für das CLI-Skript habe ich angelegt; bis auf die Import-Funktion, wo der Upload-Reiter im BE für diesen Benutzer nicht erscheint, sollte dieser Benutzer alle notwendigen Rechte besitzen.

Im Einsatz ist TYPO3 4.5.7 (blankpackage) mit templavoila 1.6.0-dev, static_info_tables 2.2.0 und fb_magento 1.1.0-beta.

Was mache ich falsch, worauf müsste ich noch achten?

Vielen Dank für eure Zeit.

ps: Grundsätzlich benötige ich eben ein brauchbares CLI für TYPO3 um meine ANT-Targets zu vereinfachen und flexibler zu gestalten. ANT benutze ich derzeit für das Deployment meiner Quellen und für die Grundinstallationen u.a. von TYPO3 einschließlich aller zusätzlichen und eigener Module, so das ich derzeit keine Installation mehr manuell durchführen muss und einen gesicherten Konfigurations-Zustand habe. Bisher funktioniert das auch ganz gut (ca. 6 Minuten für eine Installation), bis auf die zusätzlichen Daten in der Datenbank (Seitenbaum, Templates etc.). Frühere Aktionen mit entsprechenden SQL-Dumps werde ich künftig auch net mehr machen, da diese Methode zu unflexibel im Deployment und der Release-Erstellung ist.

artus12 ·

Ok ich habe das Problem lösen können, nachdem ich mir die Fehler des jeweiligen TCEMain, der von tx_impexp verwendet wird, hab ausgeben lassen. Das Problem war, dass ich zunächst das all Flag im Aufruf von loadFile net gesetzt hatte, und dann immer die falsche ID für die übergeordnete Seite angegeben hatte.

Das ganze Skript sieht derzeit so aus, falls es jemanden interessiert:

<?php
/*
 *  Copyright notice
*
*  (c) 2011 Anonymous <anonymous@somwhere.com>
*  All rights reserved
*
*  This script is part of the TYPO3 project. The TYPO3 project is
*  free software; you can redistribute it and/or modify
*  it under the terms of the GNU General Public License as published by
*  the Free Software Foundation; either version 2 of the License, or
*  (at your option) any later version.
*
*  The GNU General Public License can be found at
*  http://www.gnu.org/copyleft/gpl.html.
*  A copy is found in the textfile GPL.txt and important notices to the license
*  from the author is found in LICENSE.txt distributed with these scripts.
*
*
*  This script is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU General Public License for more details.
*
*  This copyright notice MUST APPEAR in all copies of the script!
*/

if (!defined('TYPO3_cliMode'))
die('You cannot run this script directly!');

// Include basis cli class
require_once(PATH_t3lib.'class.t3lib_cli.php');

require_once (PATH_site.'typo3/sysext/impexp/class.tx_impexp.php');

/**
 * tx_weeshell command line interface
 *
 * @author Anonymous <anonymous@somewhere.com>
 * @package TYPO3
 * @subpackage wee_shell
 * @todo Formating outputs
 */
class tx_weeshell_cli extends t3lib_cli {

	var $prefixId      = 'tx_weeshell_cli';
	var $scriptRelPath = 'cli/class.tx_weeshell_cli.php';
	var $extKey        = 'wee_shell';

	/**
	 * Constructor
	 *
	 * @return tx_weeshell_cli
	 */
	function tx_weeshell_cli() {
		// Running parent class constructor
		parent::t3lib_cli();

		// Setting help texts:
		$this->cli_help['name'] = 'Wee Shell';
		$this->cli_help['synopsis'] = '###OPTIONS###';
		$this->cli_help['description'] = "Basic CLI for TYPO3";
		$this->cli_help['examples'] = '/.../cli_dispatch.phpsh '.$this->extKey.' <task> <args>';
		$this->cli_help['author'] = '(c) 2011 Anonymous <anonymous@somewhere.com>';
	}

	/**
	 * Register handler for a hook
	 *
	 * @param array $hook
	 * @param string $handler Method name
	 * @return tx_weeshell_cli
	 */
	function registerHandler(&$hook, $handler) {
		$hook[] = 'EXT:wee_shell/cli/class.tx_weeshell_cli.php:&tx_weeshell_cli->'.$handler;
	}

	/**
	 * Main entry point
	 *
	 * @param array Command line arguments
	 * @return string
	 */
	function cli_main($argv) {

		// validate input
		$this->cli_validateArgs();
			
		// get task (function)
		$task = (string)$this->cli_args['_DEFAULT'][1];

		if (!$task) {
			$this->cli_help();
			exit;
		}

		// select called function
		switch ($task) {
			case 'enable':
				$this->enableExtension();
				break;
			case 'import':
				$this->importT3d();
				break;
			default:
				$this->cli_echo('Unknown task "'.$task.'"');
		}
	}

	/**
	 * Enable extension
	 *
	 * @todo Error handling
	 * @return void
	 */
	function enableExtension() {
		// get extension key
		$key = (string)$this->cli_args['_DEFAULT'][2];

		t3lib_div::makeInstance('tx_em_Connection_ExtDirectServer')->enableExtension($key);
	}

	/**
	 * Import T3D File
	 *
	 * @return void
	 */
	function importT3d() {
		global $GLOBALS;

		$hooks = &$GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['ext/impexp/class.tx_impexp.php'];

		// get file path
		$fileName = (string)$this->cli_args['_DEFAULT'][2];

		// get pid
		$pid = (int)$this->cli_args['_DEFAULT'][3];

		$import = t3lib_div::makeInstance('tx_impexp');
		$import->init(0, 'import');
		$import->enableLogging = true;

		if ($fileName && @is_file($fileName))	{
			if ($import->loadFile($fileName,true)) {
				$this->cli_echo('Importing "'.$fileName.'"');

				$this->registerHandler($hooks['after_writeRecordsPages'], 'showErrors');
				$this->registerHandler($hooks['after_writeRecordsPagesOrder'], 'showErrors');
				$this->registerHandler($hooks['after_writeRecordsRecords'], 'showErrors');
				$this->registerHandler($hooks['after_writeRecordsRecordsOrder'], 'showErrors');

				$import->importData($pid);
			} else {
				$this->cli_echo('Can\'t load file "'.$fileName.'"');
			}
		} else {
			$this->cli_echo('File "'.$fileName.'" not found');
		}
	}

	function showErrors(&$params, &$reference) {
		if ($params['tce'] && $params['tce']->errorLog) {
			foreach ($params['tce']->errorLog as $error) {
				$this->cli_echo($error);
			}
		}
	}
}

// Call the functionality
$cleanerObj = t3lib_div::makeInstance('tx_weeshell_cli');
$cleanerObj->cli_main($_SERVER['argv']);

?>