Php 以编程方式向Joomla添加标记
我需要将大约600个标记从一个旧项目迁移到Joomla,并希望以编程方式实现这一点。我已经看过了,但没有找到任何关于如何实现这一目标的线索。我发现了一些关于如何以编程方式将标记添加到文章中的建议(这可能会在以后派上用场,但首先我需要这些标记)。我曾想过直接通过数据库查询来实现,但Joomla坚持在任何地方都使用嵌套表(tags表有一个lft和rgt行)。我能安全地忽略这些吗?或者它会在某个地方破坏系统吗Php 以编程方式向Joomla添加标记,php,joomla,Php,Joomla,我需要将大约600个标记从一个旧项目迁移到Joomla,并希望以编程方式实现这一点。我已经看过了,但没有找到任何关于如何实现这一目标的线索。我发现了一些关于如何以编程方式将标记添加到文章中的建议(这可能会在以后派上用场,但首先我需要这些标记)。我曾想过直接通过数据库查询来实现,但Joomla坚持在任何地方都使用嵌套表(tags表有一个lft和rgt行)。我能安全地忽略这些吗?或者它会在某个地方破坏系统吗 感谢您的帮助。这是我的CLI,它可以读取csv文件并从中创建标记。通过使用API,store
感谢您的帮助。这是我的CLI,它可以读取csv文件并从中创建标记。通过使用API,store()将创建正确的嵌套数据。它非常粗糙,因为它只是为了测试标记api/性能,所以您可能需要清理它,特别是如果您有其他数据要放入csv(或其他数据源)。更新:而且它有点旧,所以您可能需要导入cms和遗留库,因为在3.2中对store()进行了更改。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();
}
这是我拥有的一个CLI,用于读取csv文件并从中创建标记。通过使用API,store()将创建正确的嵌套数据。它非常粗糙,因为它只是为了测试标记api/性能,所以您可能需要清理它,特别是如果您有其他数据要放入csv(或其他数据源)。更新:而且它有点旧,所以您可能需要导入cms和遗留库,因为在3.2中对store()进行了更改。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();
}
如果要使用结构标记,只需取消对此行的注释:
$table->setLocation(parent_id, 'last-child');
其中parent_id-parent tag node的id如果要使用结构标记,只需取消注释此行:
$table->setLocation(parent_id, 'last-child');
其中parent_id-parent tag node的id您可能希望为此使用Joomla API。如果您不仔细检查Joomla以处理所有副作用,直接插入数据库很可能会破坏内容。不,您不需要先添加标签,在保存时为文章添加标签将自动检查具有相同名称的标签。问题是您有描述、图像或需要添加的内容吗?这是否意味着,如果我以编程方式添加一篇带有一些标记的文章,而这些标记不在数据库中,Joomla会自动添加它们?这将是非常好的,因为这将使事情变得更加容易。您可能希望使用JoomlaAPI来实现这一点。如果您不仔细检查Joomla以处理所有副作用,直接插入数据库很可能会破坏内容。不,您不需要先添加标签,在保存时为文章添加标签将自动检查具有相同名称的标签。问题是您有描述、图像或需要添加的内容吗?这是否意味着,如果我以编程方式添加一篇带有一些标记的文章,而这些标记不在数据库中,Joomla会自动添加它们?那太好了,因为那会让事情变得更容易。谢谢!适用于我需要的内容,无需更改任何内容,除了我的标签来自db而不是csv。谢谢!适用于我需要的内容,无需更改任何内容,除了我的标签来自db而不是csv。