简体   繁体   English

从 CLI 以编程方式添加 Joomla 文章

[英]Programmatically add a Joomla Article from CLI

I want to be able to add many articles programmatically in Joomla, from the command line using the cli feature in Joomla CMS.我希望能够使用 Joomla CMS 中的 cli 功能从命令行以编程方式在 Joomla 中添加许多文章。

I am basically using Create a Joomla!我基本上是在使用Create a Joomla! Article Programmatically but my script closes out after creating just one article with the error line 文章以编程方式,但我的脚本在仅创建一篇带有错误行的文章后关闭

Error displaying the error page: Application Instantiation Error:Application Instantiation Error显示错误页面时出错:应用程序实例化错误:应用程序实例化错误

This is the code that I am running from within the /cli folder in Joomla.这是我在 Joomla 的 /cli 文件夹中运行的代码。

I am using Joomla 3.4我正在使用 Joomla 3.4

<?php
const _JEXEC = 1;

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

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

require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';
require_once JPATH_CONFIGURATION . '/configuration.php';


class AddArticle extends JApplicationCli
{
    public function doExecute()
    {
        $count = 10;
        while ($count > 0) 
        {
            $count--;

            $jarticle                   = new stdClass();
            $jarticle->title            = 'New article added programmatically' . rand();

            $jarticle->introtext        = '<p>A programmatically created article</p>';

            $table = JTable::getInstance('content', 'JTable');
            $data = (array)$jarticle;

        // Bind data
            if (!$table->bind($data))
            {
                die('bind error');
                return false;
            }

        // Check the data.
            if (!$table->check())
            {
                die('check error');
                return false;
            }

        // Store the data.
            if (!$table->store())
            {
                die('store error');
                return false;
            }
    }
}
}

JApplicationCli::getInstance('AddArticle')->execute();

I was able to find the answer to this as it had been raised as an issue at github, so I am posting that solution here.我能够找到答案,因为它已在 github 上作为问题提出,因此我在此处发布该解决方案。

https://github.com/joomla/joomla-cms/issues/7028 https://github.com/joomla/joomla-cms/issues/7028

It is necessary to register the application like this, if the command line app uses JTable:如果命令行应用程序使用 JTable,则需要像这样注册应用程序:

class MakeSql extends JApplicationCli
{

   public function __construct() 
  {
     parent::__construct();
     JFactory::$application = $this; // this is necessary if using JTable
  }

  public function doExecute()
  {
      $db = JFactory::getDbo();
      // ... etc etc ...

I did this and it worked fine.我这样做了,效果很好。

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

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