2014-02-27 69 views
0

我需要将约600个标签从旧项目迁移到Joomla,并且希望以编程方式执行此操作。我看了但没有发现有关如何实现的暗示。我发现了一些关于如何以编程方式向文章添加标签的建议(稍后可能会派上用场,但首先我需要拥有这些标签)。我想直接通过数据库查询来完成,但Joomla坚持要在任何地方使用嵌套表(标签表具有lft和rgt行)。我可以放心地忽略这些吗?或者它是否会破坏系统的某个位置?以编程方式向Joomla添加标签

感谢您提前提供任何帮助。

+0

的ID,您可能希望使用到Joomla API此。如果你不是很仔细,直接进入数据库很可能会破坏你的东西,并且检查Joomla是否会照顾到所有的副作用。 – Fge

+0

不,您不需要先添加标签,标签文章会在保存发生时自动检查具有相同名称的标签。问题是您有描述或图片或您需要添加的内容吗? – Elin

+0

这是否意味着,如果我以编程方式添加一个带有几个标签的文章,并且这些文章不在Joomla会自动添加它们的数据库中?这会很好,因为这会让事情变得更容易。 – Aurens

回答

2

这是一个CLI,我读取一个csv文件并从中创建标签。通过使用API​​,store()将创建正确的嵌套数据。这很粗糙,因为它只是用于测试标签API /性能,所以你可能想要清理它,特别是如果你有其他数据要放入CSV(或不同的数据源)。更新:它也有点旧,所以你可能需要导入cms和遗留库,因为在3.2中对store()进行了更改。这是关于为什么CLI应用程序在3.2版本中应该看看的文档wiki中的内容。

<?php 
/** 
* @package Joomla.Shell 
* 
* @copyright Copyright (C) 2005 - 2012 Open Source Matters, Inc. All rights reserved. 
* @license GNU General Public License version 2 or later; see LICENSE.txt 
*/ 

if (!defined('_JEXEC')) 
{ 
    // Initialize Joomla framework 
    define('_JEXEC', 1); 
} 

@ini_set('zend.ze1_compatibility_mode', '0'); 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 

// Load system defines 
if (file_exists(dirname(__DIR__) . '/defines.php')) 
{ 
    require_once dirname(__DIR__) . '/defines.php'; 
} 

if (!defined('JPATH_BASE')) 
{ 
    define('JPATH_BASE', dirname(__DIR__)); 
} 

if (!defined('_JDEFINES')) 
{ 
    require_once JPATH_BASE . '/includes/defines.php'; 
} 

// Get the framework. 
require_once JPATH_LIBRARIES . '/import.php'; 
// Get the framework. 
require_once JPATH_LIBRARIES . '/import.legacy.php'; 

// Bootstrap the CMS libraries. 
require_once JPATH_LIBRARIES . '/cms.php'; 

// Import the configuration. 
require_once JPATH_CONFIGURATION . '/configuration.php'; 

// System configuration. 
$config = new JConfig; 
// Include the JLog class. 
jimport('joomla.log.log'); 

// Add the logger. 
JLog::addLogger(
    // Pass an array of configuration options 
    array(
      // Set the name of the log file 
      'text_file' => 'test.log.php', 
      // (optional) you can change the directory 
      'text_file_path' => 'somewhere/logs' 
    ) 
); 

// start logging... 
JLog::add('Starting to log'); 


// Configure error reporting to maximum for CLI output. 
error_reporting(E_ALL); 
ini_set('display_errors', 1); 


/** 
* Create Tags 
* 
* @package Joomla.Shell 
* 
* @since 1.0 
*/ 
class Createtags extends JApplicationCli 
{ 

    public function __construct() 
    { 
     // Call the parent __construct method so it bootstraps the application class. 
     parent::__construct(); 
     require_once JPATH_CONFIGURATION.'/configuration.php'; 

     jimport('joomla.database.database'); 

     // System configuration. 
     $config = JFactory::getConfig(); 

     // Note, this will throw an exception if there is an error 
     // Creating the database connection. 
     $this->dbo = JDatabaseDriver::getInstance(
      array(
       'driver' => $config->get('dbtype'), 
       'host' => $config->get('host'), 
       'user' => $config->get('user'), 
       'password' => $config->get('password'), 
       'database' => $config->get('db'), 
       'prefix' => $config->get('dbprefix'), 
      ) 
     ); 

    } 

    /** 
    * Entry point for the script 
    * 
    * @return void 
    * 
    * @since 2.5 
    */ 
    public function doExecute() 
    { 


    if (($handle = fopen(JPATH_BASE."/cli/keyword.csv", "r")) !== FALSE) 
    { 
     fgetcsv($handle, 1000, ","); 
     while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
     { 

      $date = new JDate(); 
      include_once JPATH_BASE . '/administrator/components/com_tags/tables/tag.php'; 
      $table = new TagsTableTag(JFactory::getDbo()); 

      $table->title    = $data[0]; 
      $table->note    = ''; 
      $table->description   = ''; 
      $table->published   = 1; 
      $table->checked_out   = 0; 
      $table->checked_out_time = '0000-00-00 00:00:00'; 
      $table->created_user_id  = 42; 
      $table->created_time  = $date->toSql(); 
      $table->modified_user_id = 0; 
      $table->modified_time  = '0000-00-00 00:00:00'; 
      $table->hits    = 0; 
      $table->language   = 'en-GB'; 
      //$table->parent_id   = 1;// doesn't do anything 
      //$table->level    = 1;// doesn't do anything 

    //  $table->setLocation(0, 'last-child'); 

      $table->check(); 

      $table->store(); 
     } 

    fclose($handle); 
    } 
    } 
} 

if (!defined('JSHELL')) 
{ 
    JApplicationCli::getInstance('Createtags')->execute(); 
} 
+0

谢谢!适用于我所需要的而不需要更改任何内容,只是我的标签是从数据库中取出而不是从csv中取出。 – Aurens

1

如果你想使用的结构标签,只是去掉这一行:

$table->setLocation(parent_id, 'last-child'); 

其中PARENT_ID - 父标签节点

相关问题