3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * @package Joomla.Cli * * @copyright (C) 2012 Open Source Matters, Inc. <https://www.joomla.org> * @license GNU General Public License version 2 or later; see LICENSE.txt */ /** * A command line cron job to bulk import JSON data into Joomla articles */ // Initialize Joomla framework const _JEXEC = 1; // Load system defines if (file_exists(__DIR__ . '/defines.php')) { require_once __DIR__ . '/defines.php'; } if (!defined('_JDEFINES')) { define('JPATH_BASE', __DIR__); require_once JPATH_BASE . '/includes/defines.php'; } // Get the framework. require_once JPATH_LIBRARIES . '/import.legacy.php'; // Bootstrap the CMS libraries. require_once JPATH_LIBRARIES . '/cms.php'; class BulkArticles extends JApplicationCli { public function doExecute(): void { $this->out(JText::_('Start')); // Fetch the JSON data and Run the insertArticle bulk data import to articles function $curlOpts = [ CURLOPT_URL => 'https://api.dtn.com/publishing/news/articles?categoryId=16%2C17%2C18&limit=10&maxAge=30&apikey=placeholder', CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => '', CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => 'GET', ]; try { $fetchedArticles = $this->curlToObject($curlOpts); if ($fetchedArticles) { $this->out(JText::_('Fetched Articles: ' . count($fetchedArticles))); } $insertedIds = $this->insertArticles($fetchedArticles); if ($insertedIds) { $this->out(JText::_('Inserted IDs: ' . implode(', ', $insertedIds))); } } catch (exception $e) { $this->out(JText::_($e->getMessage())); } $this->out(JText::_('Complete')); } /** * @throws Exception */ private function curlToObject(array $curlOpts): array { $ch = curl_init(); curl_setopt_array($ch, $curlOpts); $result = curl_exec($ch); $error = curl_error($ch); // Get the last error curl_close($ch); if ($error) { throw new Exception($error); } return json_decode($result); } /** * @throws Exception */ private function insertArticles(array $articles): array { require_once JPATH_ADMINISTRATOR . '/components/com_content/models/article.php'; $articleModel = new ContentModelArticle([]); foreach ($articles as $article) { $articleData = [ 'id' => 0, 'catid' => 8, 'title' => ucwords(strtolower($article->title)), 'alias' => JFilterOutput::stringURLSafe($article->title), 'introtext' => strip_tags($article->storySummary), 'fulltext' => strip_tags($article->content), 'state' => 1, 'access' => 2, 'created_by' => 332, 'language' => '*', 'rules' => [ 'core.edit.delete' => [], 'core.edit.edit' => [], 'core.edit.state' => [] ], ]; if (!$articleModel->save($articleData)) { throw new Exception($articleModel->getError()); } else { $newIds[] = $articleModel->getItem()->id; } } return $newIds ?? []; } } JApplicationCli::getInstance('BulkArticles')->execute();
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 6
Branch analysis from position: 5
2 jumps found. (Code = 43) Position 1 = 9, Position 2 = 16
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
Branch analysis from position: 6
filename:       /in/0kYDU
function name:  (null)
number of ops:  29
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   15     0  E >   DECLARE_CONST                                            '_JEXEC', 1
   18     1        INIT_FCALL                                               'file_exists'
          2        SEND_VAL                                                 '%2Fin%2Fdefines.php'
          3        DO_ICALL                                         $0      
          4      > JMPZ                                                     $0, ->6
   19     5    >   INCLUDE_OR_EVAL                                          '%2Fin%2Fdefines.php', REQUIRE_ONCE
   22     6    >   DEFINED                                          ~2      '_JDEFINES'
          7        BOOL_NOT                                         ~3      ~2
          8      > JMPZ                                                     ~3, ->16
   23     9    >   INIT_FCALL                                               'define'
         10        SEND_VAL                                                 'JPATH_BASE'
         11        SEND_VAL                                                 '%2Fin'
         12        DO_ICALL                                                 
   24    13        FETCH_CONSTANT                                   ~5      'JPATH_BASE'
         14        CONCAT                                           ~6      ~5, '%2Fincludes%2Fdefines.php'
         15        INCLUDE_OR_EVAL                                          ~6, REQUIRE_ONCE
   28    16    >   FETCH_CONSTANT                                   ~8      'JPATH_LIBRARIES'
         17        CONCAT                                           ~9      ~8, '%2Fimport.legacy.php'
         18        INCLUDE_OR_EVAL                                          ~9, REQUIRE_ONCE
   31    19        FETCH_CONSTANT                                   ~11     'JPATH_LIBRARIES'
         20        CONCAT                                           ~12     ~11, '%2Fcms.php'
         21        INCLUDE_OR_EVAL                                          ~12, REQUIRE_ONCE
   33    22        DECLARE_CLASS                                            'bulkarticles', 'japplicationcli'
  117    23        INIT_STATIC_METHOD_CALL                                  'JApplicationCli', 'getInstance'
         24        SEND_VAL_EX                                              'BulkArticles'
         25        DO_FCALL                                      0  $14     
         26        INIT_METHOD_CALL                                         $14, 'execute'
         27        DO_FCALL                                      0          
         28      > RETURN                                                   1

Class BulkArticles:
Function doexecute:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 29, Position 2 = 37
Branch analysis from position: 29
2 jumps found. (Code = 43) Position 1 = 42, Position 2 = 53
Branch analysis from position: 42
1 jumps found. (Code = 42) Position 1 = 63
Branch analysis from position: 63
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 53
Branch analysis from position: 37
Found catch point at position: 54
Branch analysis from position: 54
2 jumps found. (Code = 107) Position 1 = 55, Position 2 = -2
Branch analysis from position: 55
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0kYDU
function name:  doExecute
number of ops:  70
compiled vars:  !0 = $curlOpts, !1 = $fetchedArticles, !2 = $insertedIds, !3 = $e
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   37     0  E >   INIT_METHOD_CALL                                         'out'
          1        INIT_STATIC_METHOD_CALL                                  'JText', '_'
          2        SEND_VAL_EX                                              'Start'
          3        DO_FCALL                                      0  $4      
          4        SEND_VAR_NO_REF_EX                                       $4
          5        DO_FCALL                                      0          
   41     6        FETCH_CONSTANT                                   ~6      'CURLOPT_URL'
          7        INIT_ARRAY                                       ~7      'https%3A%2F%2Fapi.dtn.com%2Fpublishing%2Fnews%2Farticles%3FcategoryId%3D16%252C17%252C18%26limit%3D10%26maxAge%3D30%26apikey%3Dplaceholder', ~6
   42     8        FETCH_CONSTANT                                   ~8      'CURLOPT_RETURNTRANSFER'
   41     9        ADD_ARRAY_ELEMENT                                ~7      <true>, ~8
   43    10        FETCH_CONSTANT                                   ~9      'CURLOPT_ENCODING'
         11        ADD_ARRAY_ELEMENT                                ~7      '', ~9
   44    12        FETCH_CONSTANT                                   ~10     'CURLOPT_MAXREDIRS'
         13        ADD_ARRAY_ELEMENT                                ~7      10, ~10
   45    14        FETCH_CONSTANT                                   ~11     'CURLOPT_TIMEOUT'
         15        ADD_ARRAY_ELEMENT                                ~7      0, ~11
   46    16        FETCH_CONSTANT                                   ~12     'CURLOPT_FOLLOWLOCATION'
   41    17        ADD_ARRAY_ELEMENT                                ~7      <true>, ~12
   47    18        FETCH_CONSTANT                                   ~13     'CURLOPT_HTTP_VERSION'
         19        FETCH_CONSTANT                                   ~14     'CURL_HTTP_VERSION_1_1'
         20        ADD_ARRAY_ELEMENT                                ~7      ~14, ~13
   48    21        FETCH_CONSTANT                                   ~15     'CURLOPT_CUSTOMREQUEST'
         22        ADD_ARRAY_ELEMENT                                ~7      'GET', ~15
   40    23        ASSIGN                                                   !0, ~7
   51    24        INIT_METHOD_CALL                                         'curlToObject'
         25        SEND_VAR_EX                                              !0
         26        DO_FCALL                                      0  $17     
         27        ASSIGN                                                   !1, $17
   52    28      > JMPZ                                                     !1, ->37
   53    29    >   INIT_METHOD_CALL                                         'out'
         30        INIT_STATIC_METHOD_CALL                                  'JText', '_'
         31        COUNT                                            ~19     !1
         32        CONCAT                                           ~20     'Fetched+Articles%3A+', ~19
         33        SEND_VAL_EX                                              ~20
         34        DO_FCALL                                      0  $21     
         35        SEND_VAR_NO_REF_EX                                       $21
         36        DO_FCALL                                      0          
   55    37    >   INIT_METHOD_CALL                                         'insertArticles'
         38        SEND_VAR_EX                                              !1
         39        DO_FCALL                                      0  $23     
         40        ASSIGN                                                   !2, $23
   56    41      > JMPZ                                                     !2, ->53
   57    42    >   INIT_METHOD_CALL                                         'out'
         43        INIT_STATIC_METHOD_CALL                                  'JText', '_'
         44        INIT_FCALL                                               'implode'
         45        SEND_VAL                                                 '%2C+'
         46        SEND_VAR                                                 !2
         47        DO_ICALL                                         $25     
         48        CONCAT                                           ~26     'Inserted+IDs%3A+', $25
         49        SEND_VAL_EX                                              ~26
         50        DO_FCALL                                      0  $27     
         51        SEND_VAR_NO_REF_EX                                       $27
         52        DO_FCALL                                      0          
         53    > > JMP                                                      ->63
   59    54  E > > CATCH                                       last         'exception'
   60    55    >   INIT_METHOD_CALL                                         'out'
         56        INIT_STATIC_METHOD_CALL                                  'JText', '_'
         57        INIT_METHOD_CALL                                         !3, 'getMessage'
         58        DO_FCALL                                      0  $29     
         59        SEND_VAR_NO_REF_EX                                       $29
         60        DO_FCALL                                      0  $30     
         61        SEND_VAR_NO_REF_EX                                       $30
         62        DO_FCALL                                      0          
   62    63    >   INIT_METHOD_CALL                                         'out'
         64        INIT_STATIC_METHOD_CALL                                  'JText', '_'
         65        SEND_VAL_EX                                              'Complete'
         66        DO_FCALL                                      0  $32     
         67        SEND_VAR_NO_REF_EX                                       $32
         68        DO_FCALL                                      0          
   63    69      > RETURN                                                   null

End of function doexecute

Function curltoobject:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 20, Position 2 = 24
Branch analysis from position: 20
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 24
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/0kYDU
function name:  curlToObject
number of ops:  31
compiled vars:  !0 = $curlOpts, !1 = $ch, !2 = $result, !3 = $error
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   68     0  E >   RECV                                             !0      
   70     1        INIT_FCALL_BY_NAME                                       'curl_init'
          2        DO_FCALL                                      0  $4      
          3        ASSIGN                                                   !1, $4
   71     4        INIT_FCALL_BY_NAME                                       'curl_setopt_array'
          5        SEND_VAR_EX                                              !1
          6        SEND_VAR_EX                                              !0
          7        DO_FCALL                                      0          
   72     8        INIT_FCALL_BY_NAME                                       'curl_exec'
          9        SEND_VAR_EX                                              !1
         10        DO_FCALL                                      0  $7      
         11        ASSIGN                                                   !2, $7
   73    12        INIT_FCALL_BY_NAME                                       'curl_error'
         13        SEND_VAR_EX                                              !1
         14        DO_FCALL                                      0  $9      
         15        ASSIGN                                                   !3, $9
   74    16        INIT_FCALL_BY_NAME                                       'curl_close'
         17        SEND_VAR_EX                                              !1
         18        DO_FCALL                                      0          
   75    19      > JMPZ                                                     !3, ->24
   76    20    >   NEW                                              $12     'Exception'
         21        SEND_VAR_EX                                              !3
         22        DO_FCALL                                      0          
         23      > THROW                                         0          $12
   78    24    >   INIT_FCALL                                               'json_decode'
         25        SEND_VAR                                                 !2
         26        DO_ICALL                                         $14     
         27        VERIFY_RETURN_TYPE                                       $14
         28      > RETURN                                                   $14
   79    29*       VERIFY_RETURN_TYPE                                       
         30*     > RETURN                                                   null

End of function curltoobject

Function insertarticles:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 60
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 60
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 47, Position 2 = 54
Branch analysis from position: 47
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 54
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 60
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 60
filename:       /in/0kYDU
function name:  insertArticles
number of ops:  67
compiled vars:  !0 = $articles, !1 = $articleModel, !2 = $article, !3 = $articleData, !4 = $newIds
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   84     0  E >   RECV                                             !0      
   86     1        FETCH_CONSTANT                                   ~5      'JPATH_ADMINISTRATOR'
          2        CONCAT                                           ~6      ~5, '%2Fcomponents%2Fcom_content%2Fmodels%2Farticle.php'
          3        INCLUDE_OR_EVAL                                          ~6, REQUIRE_ONCE
   87     4        NEW                                              $8      'ContentModelArticle'
          5        SEND_VAL_EX                                              <array>
          6        DO_FCALL                                      0          
          7        ASSIGN                                                   !1, $8
   89     8      > FE_RESET_R                                       $11     !0, ->60
          9    > > FE_FETCH_R                                               $11, !2, ->60
   91    10    >   INIT_ARRAY                                       ~12     0, 'id'
   92    11        ADD_ARRAY_ELEMENT                                ~12     8, 'catid'
   93    12        INIT_FCALL                                               'ucwords'
         13        INIT_FCALL                                               'strtolower'
         14        FETCH_OBJ_R                                      ~13     !2, 'title'
         15        SEND_VAL                                                 ~13
         16        DO_ICALL                                         $14     
         17        SEND_VAR                                                 $14
         18        DO_ICALL                                         $15     
         19        ADD_ARRAY_ELEMENT                                ~12     $15, 'title'
   94    20        INIT_STATIC_METHOD_CALL                                  'JFilterOutput', 'stringURLSafe'
         21        CHECK_FUNC_ARG                                           
         22        FETCH_OBJ_FUNC_ARG                               $16     !2, 'title'
         23        SEND_FUNC_ARG                                            $16
         24        DO_FCALL                                      0  $17     
         25        ADD_ARRAY_ELEMENT                                ~12     $17, 'alias'
   95    26        INIT_FCALL                                               'strip_tags'
         27        FETCH_OBJ_R                                      ~18     !2, 'storySummary'
         28        SEND_VAL                                                 ~18
         29        DO_ICALL                                         $19     
         30        ADD_ARRAY_ELEMENT                                ~12     $19, 'introtext'
   96    31        INIT_FCALL                                               'strip_tags'
         32        FETCH_OBJ_R                                      ~20     !2, 'content'
         33        SEND_VAL                                                 ~20
         34        DO_ICALL                                         $21     
         35        ADD_ARRAY_ELEMENT                                ~12     $21, 'fulltext'
   97    36        ADD_ARRAY_ELEMENT                                ~12     1, 'state'
   98    37        ADD_ARRAY_ELEMENT                                ~12     2, 'access'
   99    38        ADD_ARRAY_ELEMENT                                ~12     332, 'created_by'
  100    39        ADD_ARRAY_ELEMENT                                ~12     '%2A', 'language'
   91    40        ADD_ARRAY_ELEMENT                                ~12     <array>, 'rules'
   90    41        ASSIGN                                                   !3, ~12
  107    42        INIT_METHOD_CALL                                         !1, 'save'
         43        SEND_VAR_EX                                              !3
         44        DO_FCALL                                      0  $23     
         45        BOOL_NOT                                         ~24     $23
         46      > JMPZ                                                     ~24, ->54
  108    47    >   NEW                                              $25     'Exception'
         48        INIT_METHOD_CALL                                         !1, 'getError'
         49        DO_FCALL                                      0  $26     
         50        SEND_VAR_NO_REF_EX                                       $26
         51        DO_FCALL                                      0          
         52      > THROW                                         0          $25
         53*       JMP                                                      ->59
  110    54    >   INIT_METHOD_CALL                                         !1, 'getItem'
         55        DO_FCALL                                      0  $29     
         56        FETCH_OBJ_R                                      ~30     $29, 'id'
         57        ASSIGN_DIM                                               !4
         58        OP_DATA                                                  ~30
   89    59      > JMP                                                      ->9
         60    >   FE_FREE                                                  $11
  113    61        COALESCE                                         ~31     !4
         62        QM_ASSIGN                                        ~31     <array>
         63        VERIFY_RETURN_TYPE                                       ~31
         64      > RETURN                                                   ~31
  114    65*       VERIFY_RETURN_TYPE                                       
         66*     > RETURN                                                   null

End of function insertarticles

End of class BulkArticles.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
134.73 ms | 1417 KiB | 27 Q