How-to: Running Concrete5 from PHP command line

Permalink 8 users found helpful
I'm currently writing a script which helps to export pages and files information from C5 to xml-file.

The idea is to run this script from PHP-CLI with max_execution_time=0 by default.

I wrote a concrete5_PHP-CLI.php file with all necessary commands to use CLI.
Move this file to your concrete5 site root folder.

It's not as simple as require index.php, because of new folder structure (there is an exit function in startup/updated_core_check).

<?php
#concrete5_PHP-CLI.php
define('DIR_BASE','.');
define('C5_ENVIRONMENT_ONLY', true);
require_once('config/site.php');
if ( !defined('REDIRECT_TO_BASE_URL') ){
  define('REDIRECT_TO_BASE_URL', false);
}
if ( defined('DIRNAME_APP_UPDATED') ){
  $GLOBALS['APP_UPDATED_PASSTHRU'] = true;  
  require('updates/' . DIRNAME_APP_UPDATED . '/concrete/dispatcher.php');
}
else{
  require('index.php');  
}


Example for using:
# pages.php
require('concrete5_PHP-CLI.php');
Loader::model('page_list');
$pl = new PageList();
$pages = $pl->get();
foreach($pages as $page){
  echo ($page->getCollectionName()."\n");
}


jc@jc-laptop:~/sites/concrete5$ php pages.php
jc@jc-laptop:~/sites/concrete5$ php pages.php >> output.txt

jcd
View Replies:
rgudinho replied on at Permalink Reply
rgudinho
This is a great script, I'd add just in case, if you want this script (pages.php in this example) to be runned exclusively over PHP-CLI, you can validate with:

<?php
if(defined('STDIN') )
  echo("Running from CLI");
else
  echo("Not Running from CLI");
?>
ijessup replied on at Permalink Reply
ijessup
Thanks jcd,
I've expanded just a bit on your code to support CRON tasks.

<?php
# ~/concrete5_PHP-CLI.php
defined('STDIN') or die(_("Access Denied. CLI Only"));
chdir(__DIR__);
define('DIR_BASE','.');
define('C5_ENVIRONMENT_ONLY', true);
require_once('config/site.php');
if ( !defined('REDIRECT_TO_BASE_URL') ){
  define('REDIRECT_TO_BASE_URL', false);
}
if ( defined('DIRNAME_APP_UPDATED') ){
  $GLOBALS['APP_UPDATED_PASSTHRU'] = true;  
  require('updates/' . DIRNAME_APP_UPDATED . '/concrete/dispatcher.php');
}
else{


<?php
# ~/cli/jobs.php
chdir(__DIR__);
require('../concrete5_PHP-CLI.php');
$_REQUEST['auth'] = Job::generateAuth();
$_REQUEST['jID'] = ''; //Run a specific job by its ID.
$_REQUEST['jHandle'] = ''; //Run a specfic job by ts Handle.
User::loginByUserID(1); //Not necessary for core jobs, but a privileged session may be required for some custom Jobs.
Loader::tool('jobs');
ntisithoj replied on at Permalink Reply
ntisithoj
here is my 5.7 version of same, in case you want to try teh same thing in 5.7... but I can't get User::loginByUserID(1); to work... this may be a bit ugly... fell free to make suggestions

<?php
define('DIR_BASE', dirname(dirname(__FILE__)) . '/web');
define('C5_ENVIRONMENT_ONLY', true);
require_once('../web/application/config/database.php');
if (!defined('REDIRECT_TO_BASE_URL')) {
    define('REDIRECT_TO_BASE_URL', false);
}
require '../web/concrete/bootstrap/configure.php';
require '../web/concrete/bootstrap/autoload.php';
require('../web/concrete/config/app.php');
$cms = require '../web/concrete/bootstrap/start.php';
use Concrete\Core\Job\Job;
use Concrete\Core\Page\Page;
if (!$cms->isRunThroughCommandLineInterface()) {
    $cms->shutdown();
TheRealSean replied on at Permalink Reply
TheRealSean
Just adding the link to the command line docs now we have some :)

http://documentation.concrete5.org/developers/appendix/cli-commands...