简体   繁体   English

以编程方式将标签添加到Joomla

[英]Adding tags to Joomla programmatically

I need to migrate about 600 tags from an old project to Joomla and would like to do that programmatically. 我需要将大约600个标签从一个旧项目迁移到Joomla,并希望以编程方式进行。 I have looked but not found any hints about how that could be achieved. 我看过但未发现有关如何实现的任何提示。 I found a few suggestions on how to add tags to an article programmatically (that might come in handy later, but first I need to have those tags). 我发现了一些有关如何以编程方式向文章添加标签的建议(以后可能会派上用场,但首先我需要这些标签)。 I thought about doing it straight via a database query but Joomla insists on using nested tables everywhere (tags table has a lft and rgt row). 我曾考虑过通过数据库查询直接完成此操作,但Joomla坚持在各处使用嵌套表(标签表具有lft和rgt行)。 Can I safely ignore those or does it break the system somewhere down the line? 我可以安全地忽略这些问题,还是会破坏系统的正常运转?

thanks for any help in advance. 感谢您的任何帮助。

This is a CLI I have that reads a csv file and creates tags from it. 我拥有的CLI可以读取csv文件并从中创建标签。 By using the API the store() will create the correct nesting data. 通过使用API​​,store()将创建正确的嵌套数据。 It's very rough because it was just for testing the tags api/performance so you may want to clean it up especially if you have other data you want to put into the csv (or a different data source). 这很粗糙,因为它仅用于测试api / performance标签,因此您可能需要清理它,尤其是当您要将其他数据放入csv(或其他数据源)时。 Update: also it's a little old so you probably need to import the cms and legacy libraries because of the changes that were made to store() in 3.2. 更新:它也有些旧,所以由于3.2中对store()所做的更改,您可能需要导入cms和旧版库。 The is something in the docs wiki about why CLI apps break in 3.2 that you should look at. 这是docs Wiki中有关CLI应用为何会闯入3.2的原因之一。

<?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();
}

If you want use structural tags, just uncomment this line: 如果要使用结构标记,只需取消注释以下行:

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

where parent_id - id of parent tag node 其中parent_id-父标记节点的ID

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM