Skip to content

Instantly share code, notes, and snippets.

@Mark-H
Last active November 4, 2025 13:31
Show Gist options
  • Select an option

  • Save Mark-H/4303088 to your computer and use it in GitHub Desktop.

Select an option

Save Mark-H/4303088 to your computer and use it in GitHub Desktop.

Revisions

  1. Mark-H revised this gist Jul 26, 2013. 1 changed file with 7 additions and 1 deletion.
    8 changes: 7 additions & 1 deletion modcli.php
    Original file line number Diff line number Diff line change
    @@ -5,9 +5,15 @@
    * It enables you to run any core or third party processor from the command line, passing it options as you go along.
    *
    * @author Mark Hamstra <hello@markhamstra.com>
    * @version 0.1.0-pl, 2012-12-16
    * @version 0.1.1-pl, 2013-07-26
    * @license GPL v2
    */

    // Make sure we're on the command line.
    if (realpath($_SERVER['argv'][0]) != __FILE__)
    {
    die('This is CLI script! You can not run it from the web!');
    }

    define('MODCLI_VERSION','0.1.0-pl');

  2. Mark-H renamed this gist Dec 30, 2012. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. Mark-H revised this gist Dec 16, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -8,9 +8,9 @@ up the console or terminal, and start firing some commands at it.

    ## Syntax

    php modcli.php <processor> [field=value [field=value]..]
    php modcli.php processor [field=value [field=value]..]

    The <processor> needs to be a valid processor in your core/model/modx/processors/ directory, minus the extension.
    The _processor_ needs to be a valid processor in your core/model/modx/processors/ directory, minus the extension.
    Specifying field=value pairs will pass these on as properties to the processor, just like using AJAX in the manager would.

    By specifying the "debug" keyword anywhere in the command, you will get to see a bit more of information. By specifying a
  4. Mark-H revised this gist Dec 16, 2012. 1 changed file with 14 additions and 2 deletions.
    16 changes: 14 additions & 2 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -113,5 +113,17 @@ List the packages in the manager, starting at #2 and only returning one result.
    ````
    ## Limitations
    Currently, the properties only support a single word. Multi-word properties are on the roadmap but need to be implemented.
    ## Limitations, bugs & other things
    Currently, the properties only support a single word (every space denotes a next field). Multi-word properties are on the roadmap but need to be implemented..
    ### Using PHP on the commandline when running MAMP (Mac)
    When using modCLI on Mac running MAMP, you may get weird errors about PDO::CONNECT and what not with a number of processors.
    This is because it is using the Mac-included PHP binary, and not the one from MAMP. To get past this issue, add the following
    lines to your ~/.bash_profile (Of course updating the path depending on your version of both MAMP and PHP):
    alias mphp='/Applications/MAMP/bin/php/php5.4.4/bin/php'
    and restarting your terminal or typing `. ~/.bash_profile` to load in the new configuration.
    After you did that you can start using the `mphp` command instead of `php` in the above examples, which should then work.
    You can test if the binary is properly loaded with `mphp --version`.
  5. Mark-H revised this gist Dec 16, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -70,6 +70,7 @@ Create a new resource with a pagetitle and content under resource 6, this time w
    ```
    List the packages in the manager, starting at #2 and only returning one result.
    php modcli.php workspace/packages/getlist limit=1 start=2
    ````
  6. Mark-H revised this gist Dec 16, 2012. 1 changed file with 4 additions and 6 deletions.
    10 changes: 4 additions & 6 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -19,11 +19,10 @@ a processor belonging to a different package.

    ## Examples

    Input:
    Create a new resource with a pagetitle and content under resource 6.

    php modcli.php resource/create pagetitle=Awesome! parent=6 content=Awesomer!

    Output:
    ````
    # Running modCLI 0.1.0-pl in modcli.php
    # Processor: resource/create
    @@ -35,11 +34,11 @@ Output:
    > Object retrieved:
    > id => 20
    ````
    Input:

    Create a new resource with a pagetitle and content under resource 6, this time with debug enabled.

    php modcli.php resource/create pagetitle=Awesome! parent=6 content=Awesomer! debug

    Output:
    ````
    # Running modCLI 0.1.0-pl in modcli.php
    # Debug is enabled.
    @@ -70,10 +69,9 @@ Output:
    > id => 21
    ```
    Input:
    List the packages in the manager, starting at #2 and only returning one result.
    php modcli.php workspace/packages/getlist limit=1 start=2
    Output:
    ````
    # Running modCLI 0.1.0-pl in modcli.php
    # Processor: workspace/packages/getlist
  7. Mark-H revised this gist Dec 16, 2012. 1 changed file with 0 additions and 16 deletions.
    16 changes: 0 additions & 16 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -65,22 +65,6 @@ Output:
    )
    > Raw response: Array
    (
    [success] => 1
    [message] =>
    [total] => 0
    [errors] => Array
    (
    )
    [object] => Array
    (
    [id] => 21
    )
    )
    > Response type: get
    > Object retrieved:
    > id => 21
  8. Mark-H revised this gist Dec 16, 2012. 2 changed files with 56 additions and 4 deletions.
    3 changes: 0 additions & 3 deletions modcli.php
    Original file line number Diff line number Diff line change
    @@ -105,9 +105,6 @@ function formatValue(&$value, $maxLength = 200, $prefix = '>', $prePad = 6, $pre
    if ($result instanceof modProcessorResponse) {
    // Get the raw response
    $response = $result->getResponse();
    if ($debug) {
    echo "> Raw response: " . print_r($response, true) . "\n";
    }
    // If it's not an array yet, it may be a JSON collection. Try that.
    if (!is_array($response)) {
    $responseFromJSON = $modx->fromJSON($response);
    57 changes: 56 additions & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -19,8 +19,11 @@ a processor belonging to a different package.

    ## Examples

    Input:

    php modcli.php resource/create pagetitle=Awesome! parent=6 content=Awesomer!


    Output:
    ````
    # Running modCLI 0.1.0-pl in modcli.php
    # Processor: resource/create
    @@ -32,9 +35,61 @@ a processor belonging to a different package.
    > Object retrieved:
    > id => 20
    ````
    Input:

    php modcli.php resource/create pagetitle=Awesome! parent=6 content=Awesomer! debug

    Output:
    ````
    # Running modCLI 0.1.0-pl in modcli.php
    # Debug is enabled.
    # Processor: resource/create
    # Properties:
    # pagetitle => Awesome!
    # parent => 6
    # content => Awesomer!
    > Raw response: Array
    (
    [success] => 1
    [message] =>
    [total] => 0
    [errors] => Array
    (
    )
    [object] => Array
    (
    [id] => 21
    )
    )
    > Raw response: Array
    (
    [success] => 1
    [message] =>
    [total] => 0
    [errors] => Array
    (
    )
    [object] => Array
    (
    [id] => 21
    )
    )
    > Response type: get
    > Object retrieved:
    > id => 21
    ```
    Input:
    php modcli.php workspace/packages/getlist limit=1 start=2
    Output:
    ````
    # Running modCLI 0.1.0-pl in modcli.php
    # Processor: workspace/packages/getlist
  9. Mark-H revised this gist Dec 16, 2012. 1 changed file with 48 additions and 44 deletions.
    92 changes: 48 additions & 44 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -21,54 +21,58 @@ a processor belonging to a different package.

    php modcli.php resource/create pagetitle=Awesome! parent=6 content=Awesomer!

    > \# Running modCLI 0.1.0-pl in modcli.php
    > \# Processor: resource/create
    > \# Properties:
    > \# pagetitle => Awesome!
    > \# parent => 6
    > \# content => Awesomer!
    >
    > > Object retrieved:
    > > id => 20
    ````
    # Running modCLI 0.1.0-pl in modcli.php
    # Processor: resource/create
    # Properties:
    # pagetitle => Awesome!
    # parent => 6
    # content => Awesomer!
    > Object retrieved:
    > id => 20
    ````

    php modcli.php workspace/packages/getlist limit=1 start=2

    > \# Running modCLI 0.1.0-pl in modcli.php
    > \# Processor: workspace/packages/getlist
    > \# Properties:
    > \# limit => 1
    > \# start => 2
    >
    > > Amount of Results: 8
    ````
    # Running modCLI 0.1.0-pl in modcli.php
    # Processor: workspace/packages/getlist
    # Properties:
    # limit => 1
    # start => 2
    > Amount of Results: 8
    > Result 1:
    > > signature => login-1.8.1-pl
    > > created => Sep 07, 2012 12:59 AM
    > > updated => Sep 07, 2012 01:01 AM
    > > installed => Sep 07, 2012 01:01 AM
    > > state => 0
    > > workspace => 1
    > > provider => 0
    > > disabled =>
    > > source => login-1.8.1-pl.transport.zip
    > > manifest =>
    > > package_name => login
    > > version_major => 1
    > > version_minor => 8
    > > version_patch => 1
    > > release => pl
    > > release_index => 0
    > > provider_name =>
    > > name => login
    > > version => 1.8.1
    > > iconaction => icon-uninstall
    > > textaction => Uninstall
    > > readme =>
    > > --------------------<br />
    > > Snippet: Login<br />
    > > --------------------<br />
    > > Version: 1.7<br />
    > > Sin...
    > > updateable =>
    > signature => login-1.8.1-pl
    > created => Sep 07, 2012 12:59 AM
    > updated => Sep 07, 2012 01:01 AM
    > installed => Sep 07, 2012 01:01 AM
    > state => 0
    > workspace => 1
    > provider => 0
    > disabled =>
    > source => login-1.8.1-pl.transport.zip
    > manifest =>
    > package_name => login
    > version_major => 1
    > version_minor => 8
    > version_patch => 1
    > release => pl
    > release_index => 0
    > provider_name =>
    > name => login
    > version => 1.8.1
    > iconaction => icon-uninstall
    > textaction => Uninstall
    > readme =>
    > --------------------<br />
    > Snippet: Login<br />
    > --------------------<br />
    > Version: 1.7<br />
    > Sin...
    > updateable =>
    ````


    ## Limitations
  10. Mark-H revised this gist Dec 16, 2012. 1 changed file with 31 additions and 32 deletions.
    63 changes: 31 additions & 32 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -28,9 +28,8 @@ a processor belonging to a different package.
    > \# parent => 6
    > \# content => Awesomer!
    >
    > \> Object retrieved:
    >
    > \> id => 20
    > > Object retrieved:
    > > id => 20
    php modcli.php workspace/packages/getlist limit=1 start=2

    @@ -40,36 +39,36 @@ a processor belonging to a different package.
    > \# limit => 1
    > \# start => 2
    >
    > \> Amount of Results: 8
    > > Amount of Results: 8
    > Result 1:
    > \> signature => login-1.8.1-pl
    > \> created => Sep 07, 2012 12:59 AM
    > \> updated => Sep 07, 2012 01:01 AM
    > \> installed => Sep 07, 2012 01:01 AM
    > \> state => 0
    > \> workspace => 1
    > \> provider => 0
    > \> disabled =>
    > \> source => login-1.8.1-pl.transport.zip
    > \> manifest =>
    > \> package_name => login
    > \> version_major => 1
    > \> version_minor => 8
    > \> version_patch => 1
    > \> release => pl
    > \> release_index => 0
    > \> provider_name =>
    > \> name => login
    > \> version => 1.8.1
    > \> iconaction => icon-uninstall
    > \> textaction => Uninstall
    > \> readme =>
    > \> --------------------<br />
    > \> Snippet: Login<br />
    > \> --------------------<br />
    > \> Version: 1.7<br />
    > \> Sin...
    > \> updateable =>
    > > signature => login-1.8.1-pl
    > > created => Sep 07, 2012 12:59 AM
    > > updated => Sep 07, 2012 01:01 AM
    > > installed => Sep 07, 2012 01:01 AM
    > > state => 0
    > > workspace => 1
    > > provider => 0
    > > disabled =>
    > > source => login-1.8.1-pl.transport.zip
    > > manifest =>
    > > package_name => login
    > > version_major => 1
    > > version_minor => 8
    > > version_patch => 1
    > > release => pl
    > > release_index => 0
    > > provider_name =>
    > > name => login
    > > version => 1.8.1
    > > iconaction => icon-uninstall
    > > textaction => Uninstall
    > > readme =>
    > > --------------------<br />
    > > Snippet: Login<br />
    > > --------------------<br />
    > > Version: 1.7<br />
    > > Sin...
    > > updateable =>

    ## Limitations
  11. Mark-H revised this gist Dec 16, 2012. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -29,6 +29,7 @@ a processor belonging to a different package.
    > \# content => Awesomer!
    >
    > \> Object retrieved:
    >
    > \> id => 20
    php modcli.php workspace/packages/getlist limit=1 start=2
  12. Mark-H revised this gist Dec 16, 2012. 1 changed file with 48 additions and 41 deletions.
    89 changes: 48 additions & 41 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -10,58 +10,65 @@ up the console or terminal, and start firing some commands at it.

    php modcli.php <processor> [field=value [field=value]..]

    The <processor> needs to be a valid processor in your core/model/modx/processors/ directory, minus the extension.
    Specifying field=value pairs will pass these on as properties to the processor, just like using AJAX in the manager would.

    By specifying the "debug" keyword anywhere in the command, you will get to see a bit more of information. By specifying a
    processors_path=/absolute/path/to/processors/ you can override the path to look in for processors, for example to hit up
    a processor belonging to a different package.

    ## Examples

    php modcli.php resource/create pagetitle=Awesome! parent=6 content=Awesomer!

    > \# Running modCLI 0.1.0-pl in modcli.php
    > # Processor: resource/create
    > # Properties:
    > # pagetitle => Awesome!
    > # parent => 6
    > # content => Awesomer!
    > \# Processor: resource/create
    > \# Properties:
    > \# pagetitle => Awesome!
    > \# parent => 6
    > \# content => Awesomer!
    >
    > > Object retrieved:
    > > id => 20
    > \> Object retrieved:
    > \> id => 20
    php modcli.php workspace/packages/getlist limit=1 start=2

    > # Running modCLI 0.1.0-pl in modcli.php
    > # Processor: workspace/packages/getlist
    > # Properties:
    > # limit => 1
    > # start => 2
    > \# Running modCLI 0.1.0-pl in modcli.php
    > \# Processor: workspace/packages/getlist
    > \# Properties:
    > \# limit => 1
    > \# start => 2
    >
    > > Amount of Results: 8
    > \> Amount of Results: 8
    > Result 1:
    > > signature => login-1.8.1-pl
    > > created => Sep 07, 2012 12:59 AM
    > > updated => Sep 07, 2012 01:01 AM
    > > installed => Sep 07, 2012 01:01 AM
    > > state => 0
    > > workspace => 1
    > > provider => 0
    > > disabled =>
    > > source => login-1.8.1-pl.transport.zip
    > > manifest =>
    > > package_name => login
    > > version_major => 1
    > > version_minor => 8
    > > version_patch => 1
    > > release => pl
    > > release_index => 0
    > > provider_name =>
    > > name => login
    > > version => 1.8.1
    > > iconaction => icon-uninstall
    > > textaction => Uninstall
    > > readme =>
    > > --------------------<br />
    > > Snippet: Login<br />
    > > --------------------<br />
    > > Version: 1.7<br />
    > > Sin...
    > > updateable =>
    > \> signature => login-1.8.1-pl
    > \> created => Sep 07, 2012 12:59 AM
    > \> updated => Sep 07, 2012 01:01 AM
    > \> installed => Sep 07, 2012 01:01 AM
    > \> state => 0
    > \> workspace => 1
    > \> provider => 0
    > \> disabled =>
    > \> source => login-1.8.1-pl.transport.zip
    > \> manifest =>
    > \> package_name => login
    > \> version_major => 1
    > \> version_minor => 8
    > \> version_patch => 1
    > \> release => pl
    > \> release_index => 0
    > \> provider_name =>
    > \> name => login
    > \> version => 1.8.1
    > \> iconaction => icon-uninstall
    > \> textaction => Uninstall
    > \> readme =>
    > \> --------------------<br />
    > \> Snippet: Login<br />
    > \> --------------------<br />
    > \> Version: 1.7<br />
    > \> Sin...
    > \> updateable =>

    ## Limitations
  13. Mark-H revised this gist Dec 16, 2012. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -14,7 +14,7 @@ up the console or terminal, and start firing some commands at it.

    php modcli.php resource/create pagetitle=Awesome! parent=6 content=Awesomer!

    > # Running modCLI 0.1.0-pl in modcli.php
    > \# Running modCLI 0.1.0-pl in modcli.php
    > # Processor: resource/create
    > # Properties:
    > # pagetitle => Awesome!
  14. Mark-H revised this gist Dec 16, 2012. 1 changed file with 68 additions and 0 deletions.
    68 changes: 68 additions & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,68 @@
    # modCLI

    modCLI is a wrapper for the MODX Revolution Processors, allowing you to pretty much do anything from the commandline
    that you would normally do within the manager.

    To use modCLI, simply download the modcli.php file and put it in the MODX_BASE_PATH of your installation. Next open
    up the console or terminal, and start firing some commands at it.

    ## Syntax

    php modcli.php <processor> [field=value [field=value]..]

    ## Examples

    php modcli.php resource/create pagetitle=Awesome! parent=6 content=Awesomer!

    > # Running modCLI 0.1.0-pl in modcli.php
    > # Processor: resource/create
    > # Properties:
    > # pagetitle => Awesome!
    > # parent => 6
    > # content => Awesomer!
    >
    > > Object retrieved:
    > > id => 20
    php modcli.php workspace/packages/getlist limit=1 start=2

    > # Running modCLI 0.1.0-pl in modcli.php
    > # Processor: workspace/packages/getlist
    > # Properties:
    > # limit => 1
    > # start => 2
    >
    > > Amount of Results: 8
    > Result 1:
    > > signature => login-1.8.1-pl
    > > created => Sep 07, 2012 12:59 AM
    > > updated => Sep 07, 2012 01:01 AM
    > > installed => Sep 07, 2012 01:01 AM
    > > state => 0
    > > workspace => 1
    > > provider => 0
    > > disabled =>
    > > source => login-1.8.1-pl.transport.zip
    > > manifest =>
    > > package_name => login
    > > version_major => 1
    > > version_minor => 8
    > > version_patch => 1
    > > release => pl
    > > release_index => 0
    > > provider_name =>
    > > name => login
    > > version => 1.8.1
    > > iconaction => icon-uninstall
    > > textaction => Uninstall
    > > readme =>
    > > --------------------<br />
    > > Snippet: Login<br />
    > > --------------------<br />
    > > Version: 1.7<br />
    > > Sin...
    > > updateable =>

    ## Limitations
    Currently, the properties only support a single word. Multi-word properties are on the roadmap but need to be implemented.
  15. Mark-H revised this gist Dec 16, 2012. 1 changed file with 100 additions and 100 deletions.
    200 changes: 100 additions & 100 deletions modcli.php
    Original file line number Diff line number Diff line change
    @@ -17,8 +17,8 @@
    * @param int $padLength
    */
    function formatKey(&$key, $padLength = 15) {
    if (strlen($key) > 15) $key = substr($key, 0, 13) . "..";
    $key = str_pad($key, 15);
    if (strlen($key) > 15) $key = substr($key, 0, 13) . "..";
    $key = str_pad($key, 15);
    }
    /**
    * Format values (passed by reference) limited to $maxLength characters, with a $prefix and $prePad amount of spaces.
    @@ -29,13 +29,13 @@ function formatKey(&$key, $padLength = 15) {
    * @param bool $preBreakIfMultiline
    */
    function formatValue(&$value, $maxLength = 200, $prefix = '>', $prePad = 6, $preBreakIfMultiline = true) {
    if (strlen($value) > $maxLength) {
    $value = substr($value, 0, $maxLength - 3) . "...";
    }
    $value = str_replace("\n", "\n" . $prefix . str_repeat(' ', $prePad), $value);
    if ($preBreakIfMultiline && substr_count($value, "\n") > 0) {
    $value = "\n" . $prefix . str_repeat(' ', $prePad) . $value;
    }
    if (strlen($value) > $maxLength) {
    $value = substr($value, 0, $maxLength - 3) . "...";
    }
    $value = str_replace("\n", "\n" . $prefix . str_repeat(' ', $prePad), $value);
    if ($preBreakIfMultiline && substr_count($value, "\n") > 0) {
    $value = "\n" . $prefix . str_repeat(' ', $prePad) . $value;
    }
    }


    @@ -59,14 +59,14 @@ function formatValue(&$value, $maxLength = 200, $prefix = '>', $prePad = 6, $pre
    // Show some infos.
    echo "# Running modCLI ".MODCLI_VERSION." in {$argv[0]}\n";
    if ($debug) {
    echo "# Debug is enabled.\n";
    echo "# Debug is enabled.\n";
    }

    // Make sure we have a processor to call, otherwise display a bit of help.
    if (empty($processor) || in_array($processor, array('--help', '-help', '-h', '-?'))) {
    echo "modCLI Usage: \n\tphp {$argv[0]} <processor> [[field=value] [field=value] ...]
    echo "modCLI Usage: \n\tphp {$argv[0]} <processor> [[field=value] [field=value] ...]
    \t\t where <processor> is a valid core processor.\n\n";
    exit(1);
    exit(1);
    }

    // Show processor name to user.
    @@ -75,26 +75,26 @@ function formatValue(&$value, $maxLength = 200, $prefix = '>', $prePad = 6, $pre
    // Allow overriding of the processors_path; this allows running 3rd party processors too.
    $options = array();
    if (isset($scriptProperties['processors_path']) && !empty($scriptProperties['processors_path'])) {
    $options['processors_path'] = $scriptProperties['processors_path'];
    echo "# Processor Path: {$options['processors_path']} \n";
    $options['processors_path'] = $scriptProperties['processors_path'];
    echo "# Processor Path: {$options['processors_path']} \n";
    }

    // Show the properties we are passing (if any).
    if (!empty($scriptProperties)) {
    echo "# Properties: \n";
    foreach ($scriptProperties as $key => $value) {
    // Get rid of some unneeded properties
    if (in_array($key, array('processors_path', 'debug'))) {
    unset($scriptProperties[$key]);
    continue;
    }

    formatKey($key);
    formatValue($value, 200, '#');

    // Show the property on screen.
    echo "# {$key} => {$value} \n";
    }
    echo "# Properties: \n";
    foreach ($scriptProperties as $key => $value) {
    // Get rid of some unneeded properties
    if (in_array($key, array('processors_path', 'debug'))) {
    unset($scriptProperties[$key]);
    continue;
    }

    formatKey($key);
    formatValue($value, 200, '#');

    // Show the property on screen.
    echo "# {$key} => {$value} \n";
    }
    }
    echo "\n";

    @@ -103,79 +103,79 @@ function formatValue(&$value, $maxLength = 200, $prefix = '>', $prePad = 6, $pre

    // If the $result is a modProcessorResponse, it was a valid processor. Otherwise not so much.
    if ($result instanceof modProcessorResponse) {
    // Get the raw response
    $response = $result->getResponse();
    if ($debug) {
    echo "> Raw response: " . print_r($response, true) . "\n";
    }
    // If it's not an array yet, it may be a JSON collection. Try that.
    if (!is_array($response)) {
    $responseFromJSON = $modx->fromJSON($response);
    if ($responseFromJSON !== false) {
    $response = $responseFromJSON;
    }
    }

    if ($debug) {
    echo "> Raw response: " . print_r($response, true) . "\n";
    }

    // Dealing with a "get" processor.
    if ($result->hasObject()) {
    if ($debug) echo "> Response type: get \n";

    // Get the returned object.
    $object = $result->getObject();
    echo "> Object retrieved: \n";

    // Output object properties
    foreach ($object as $key => $value) {
    formatKey($key);
    formatValue($value, 200, '>', 8);
    echo "> {$key} => {$value}\n";
    }
    }

    // Dealing with a "getlist" processor
    elseif (isset($response['total']) && is_numeric($response['total']) && isset($response['results'])) {
    if ($debug) echo "> Response type: getlist \n";
    echo "> Amount of Results: {$response['total']} \n";

    foreach ($response['results'] as $idx => $item) {
    // Increase idx by 1 to make it more human-like.
    $idx++;
    echo "> Result {$idx}:\n";
    foreach ($item as $key => $value) {
    // Nicely format the key.
    formatKey($key);
    formatValue($value, 100, '>', 8);
    echo "> {$key} => {$value}\n";
    }
    }
    }

    // Dealing with a more "do-y" kind of processor with success or error
    else {
    if ($debug) echo "> Response type: do-y stuff \n";

    if ($result->isError()) {
    echo "Uh oh, something went wrong. \n";

    // Show errors if we have them.
    $fieldErrors = $result->getAllErrors();
    if (!empty($fieldErrors)) {
    echo "Errors:\n";
    foreach ($fieldErrors as $error) {
    echo " {$error} \n";
    }
    }
    echo "\n";
    exit(1);
    }
    }
    // Get the raw response
    $response = $result->getResponse();
    if ($debug) {
    echo "> Raw response: " . print_r($response, true) . "\n";
    }
    // If it's not an array yet, it may be a JSON collection. Try that.
    if (!is_array($response)) {
    $responseFromJSON = $modx->fromJSON($response);
    if ($responseFromJSON !== false) {
    $response = $responseFromJSON;
    }
    }

    if ($debug) {
    echo "> Raw response: " . print_r($response, true) . "\n";
    }

    // Dealing with a "get" processor.
    if ($result->hasObject()) {
    if ($debug) echo "> Response type: get \n";

    // Get the returned object.
    $object = $result->getObject();
    echo "> Object retrieved: \n";

    // Output object properties
    foreach ($object as $key => $value) {
    formatKey($key);
    formatValue($value, 200, '>', 8);
    echo "> {$key} => {$value}\n";
    }
    }

    // Dealing with a "getlist" processor
    elseif (isset($response['total']) && is_numeric($response['total']) && isset($response['results'])) {
    if ($debug) echo "> Response type: getlist \n";
    echo "> Amount of Results: {$response['total']} \n";

    foreach ($response['results'] as $idx => $item) {
    // Increase idx by 1 to make it more human-like.
    $idx++;
    echo "> Result {$idx}:\n";
    foreach ($item as $key => $value) {
    // Nicely format the key.
    formatKey($key);
    formatValue($value, 100, '>', 8);
    echo "> {$key} => {$value}\n";
    }
    }
    }

    // Dealing with a more "do-y" kind of processor with success or error
    else {
    if ($debug) echo "> Response type: do-y stuff \n";

    if ($result->isError()) {
    echo "Uh oh, something went wrong. \n";

    // Show errors if we have them.
    $fieldErrors = $result->getAllErrors();
    if (!empty($fieldErrors)) {
    echo "Errors:\n";
    foreach ($fieldErrors as $error) {
    echo " {$error} \n";
    }
    }
    echo "\n";
    exit(1);
    }
    }
    } else {
    echo "Error: Processor not found. \n\n";
    exit (1);
    echo "Error: Processor not found. \n\n";
    exit (1);
    }
    echo "\n";
    exit(0);
  16. Mark-H revised this gist Dec 16, 2012. 1 changed file with 167 additions and 13 deletions.
    180 changes: 167 additions & 13 deletions modcli.php
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,181 @@
    <?php
    /**
    * modCLI is a command line interface for MODX Revolution Processors.
    *
    * It enables you to run any core or third party processor from the command line, passing it options as you go along.
    *
    * @author Mark Hamstra <hello@markhamstra.com>
    * @version 0.1.0-pl, 2012-12-16
    * @license GPL v2
    */

    define('MODCLI_VERSION','0.1.0-pl');

    /**
    * Format keys (passed by reference) padded to $padLength with spaces and ellepsis.
    * @param $key
    * @param int $padLength
    */
    function formatKey(&$key, $padLength = 15) {
    if (strlen($key) > 15) $key = substr($key, 0, 13) . "..";
    $key = str_pad($key, 15);
    }
    /**
    * Format values (passed by reference) limited to $maxLength characters, with a $prefix and $prePad amount of spaces.
    * @param $value
    * @param int $maxLength
    * @param string $prefix
    * @param int $prePad
    * @param bool $preBreakIfMultiline
    */
    function formatValue(&$value, $maxLength = 200, $prefix = '>', $prePad = 6, $preBreakIfMultiline = true) {
    if (strlen($value) > $maxLength) {
    $value = substr($value, 0, $maxLength - 3) . "...";
    }
    $value = str_replace("\n", "\n" . $prefix . str_repeat(' ', $prePad), $value);
    if ($preBreakIfMultiline && substr_count($value, "\n") > 0) {
    $value = "\n" . $prefix . str_repeat(' ', $prePad) . $value;
    }
    }


    // include modX
    define('MODX_API_MODE', true);

    // Full path to the index
    require_once('/path/to/modx/public_html/index.php');
    require_once(dirname(__FILE__) . '/index.php');
    $modx= new modX();
    $modx->initialize('mgr');
    $modx->getService('error','error.modError', '', '');

    /* Ensure log entries are echo-ed straight to the console */
    $modx->setLogTarget('ECHO');
    $modx->setLogLevel(modX::LOG_LEVEL_INFO);

    // Get the cli options
    $scriptProperties = array();
    $processor = $argv[1];
    parse_str(implode('&', array_slice($argv, 2)), $scriptProperties);
    $debug = (in_array('debug', $argv));

    // Show some infos.
    echo "# Running modCLI ".MODCLI_VERSION." in {$argv[0]}\n";
    if ($debug) {
    echo "# Debug is enabled.\n";
    }

    // Make sure we have a processor to call, otherwise display a bit of help.
    if (empty($processor) || in_array($processor, array('--help', '-help', '-h', '-?'))) {
    echo "modCLI Usage: \n\tphp {$argv[0]} <processor> [[field=value] [field=value] ...]
    \t\t where <processor> is a valid core processor.\n\n";
    exit(1);
    }

    // Show processor name to user.
    echo "# Processor: {$processor}\n";

    // Allow overriding of the processors_path; this allows running 3rd party processors too.
    $options = array();
    $action = $argv[1];
    parse_str(implode('&', array_slice($argv, 2)), $options);
    if (isset($scriptProperties['processors_path']) && !empty($scriptProperties['processors_path'])) {
    $options['processors_path'] = $scriptProperties['processors_path'];
    echo "# Processor Path: {$options['processors_path']} \n";
    }

    // hand over the weaponry
    if (empty($action)) {
    exit('Missing action!');
    // Show the properties we are passing (if any).
    if (!empty($scriptProperties)) {
    echo "# Properties: \n";
    foreach ($scriptProperties as $key => $value) {
    // Get rid of some unneeded properties
    if (in_array($key, array('processors_path', 'debug'))) {
    unset($scriptProperties[$key]);
    continue;
    }

    formatKey($key);
    formatValue($value, 200, '#');

    // Show the property on screen.
    echo "# {$key} => {$value} \n";
    }
    }
    echo "\n";

    // Run the processor.
    $result = $modx->runProcessor($processor, $scriptProperties, $options);

    // If the $result is a modProcessorResponse, it was a valid processor. Otherwise not so much.
    if ($result instanceof modProcessorResponse) {
    // Get the raw response
    $response = $result->getResponse();
    if ($debug) {
    echo "> Raw response: " . print_r($response, true) . "\n";
    }
    // If it's not an array yet, it may be a JSON collection. Try that.
    if (!is_array($response)) {
    $responseFromJSON = $modx->fromJSON($response);
    if ($responseFromJSON !== false) {
    $response = $responseFromJSON;
    }
    }

    if ($debug) {
    echo "> Raw response: " . print_r($response, true) . "\n";
    }

    // Dealing with a "get" processor.
    if ($result->hasObject()) {
    if ($debug) echo "> Response type: get \n";

    // Get the returned object.
    $object = $result->getObject();
    echo "> Object retrieved: \n";

    // Output object properties
    foreach ($object as $key => $value) {
    formatKey($key);
    formatValue($value, 200, '>', 8);
    echo "> {$key} => {$value}\n";
    }
    }

    // Dealing with a "getlist" processor
    elseif (isset($response['total']) && is_numeric($response['total']) && isset($response['results'])) {
    if ($debug) echo "> Response type: getlist \n";
    echo "> Amount of Results: {$response['total']} \n";

    foreach ($response['results'] as $idx => $item) {
    // Increase idx by 1 to make it more human-like.
    $idx++;
    echo "> Result {$idx}:\n";
    foreach ($item as $key => $value) {
    // Nicely format the key.
    formatKey($key);
    formatValue($value, 100, '>', 8);
    echo "> {$key} => {$value}\n";
    }
    }
    }

    // Dealing with a more "do-y" kind of processor with success or error
    else {
    if ($debug) echo "> Response type: do-y stuff \n";

    if ($result->isError()) {
    echo "Uh oh, something went wrong. \n";

    $result = $modx->runProcessor($action, $options);
    $msg = $result->getMessage();
    if ($result->isError()) {
    exit ('Failed to make you a sandwich: '.$msg);
    // Show errors if we have them.
    $fieldErrors = $result->getAllErrors();
    if (!empty($fieldErrors)) {
    echo "Errors:\n";
    foreach ($fieldErrors as $error) {
    echo " {$error} \n";
    }
    }
    echo "\n";
    exit(1);
    }
    }
    } else {
    echo "Error: Processor not found. \n\n";
    exit (1);
    }
    echo 'Done. '.$msg;
    echo "\n";
    exit(0);
  17. Mark-H revised this gist Dec 16, 2012. No changes.
  18. Mark-H created this gist Dec 16, 2012.
    27 changes: 27 additions & 0 deletions modcli.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    <?php

    // include modX
    define('MODX_API_MODE', true);

    // Full path to the index
    require_once('/path/to/modx/public_html/index.php');
    $modx= new modX();
    $modx->initialize('mgr');

    // Get the cli options
    $options = array();
    $action = $argv[1];
    parse_str(implode('&', array_slice($argv, 2)), $options);

    // hand over the weaponry
    if (empty($action)) {
    exit('Missing action!');
    }

    $result = $modx->runProcessor($action, $options);
    $msg = $result->getMessage();
    if ($result->isError()) {
    exit ('Failed to make you a sandwich: '.$msg);
    }
    echo 'Done. '.$msg;
    exit(0);