CiviCRM Community Forums (archive)

*

News:

Have a question about CiviCRM?
Get it answered quickly at the new
CiviCRM Stack Exchange Q+A site

This forum was archived on 25 November 2017. Learn more.
How to get involved.
What to do if you think you've found a bug.



  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • How to install an extension from a script
Pages: [1]

Author Topic: How to install an extension from a script  (Read 1471 times)

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
How to install an extension from a script
May 18, 2013, 01:32:21 pm
Is it possible to install an extension from inside a script? Such as from PHP using an API? Or using a drush-style command?  Or is the only option to use the  user interface and click the "Install" link?

(I am running 4.2.7 and 4.3.3)
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: How to install an extension from a script
May 19, 2013, 10:58:26 am
Yup. In Civi 4.3, you can use the "Extension" API. The most useful actions are probably:

 * "download": Download and install an extension. This API expects an argument called "key" (the name of the extension) and optionally "url" (the URL of the zip file; determined automatically using civicrm.org's extension directory)
 * "install"/"disable"/"enable"/"uninstall": These APIs expect an argument called "key" (a single extension name) or "keys" (a list of extension names)

(Note: Most of the actions were also available in Civi 4.2, but the "install" action was responsible for downloading as well as local installation. These were split apart for clarity in 4.3.)

Code: [Select]
## Ex: Download & install via drush
drush cvapi extension.download key=org.example.first

## Ex: Download & install an unpublished pre-release via drush
drush cvapi extension.download key=org.example.first url=http://www.example.org/first-1.0-alpha1.zip

## Ex: Install existing extensions via drush
drush cvapi extension.install keys=org.example.first,org.example.second,org.example.third

## Ex: Install existing extensions via PHP
$result = civicrm_api('Extension', 'install', array(
  'version' => 3,
  'keys' => array(
    'org.example.first',
    'org.example.second',
    'org.example.third',
  ),
));

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: How to install an extension from a script
May 19, 2013, 11:10:38 am
4.3 also introduced the extension.refresh API which (a) scans the file system for extension info.xml files and (b) downloads the latest list of available extensions from civicrm.org.

More details about the API can be found by skimming https://github.com/civicrm/civicrm-core/blob/master/api/v3/Extension.php

SarahG (FountainTribe)

  • Ask me questions
  • ****
  • Posts: 782
  • Karma: 29
  • CiviCRM version: 4.4.7
  • CMS version: Drupal 6, Drupal 7
  • MySQL version: 5.5
  • PHP version: 5.3
Re: How to install an extension from a script
May 20, 2013, 06:35:51 am
Thanks for the help. BTW:  I was using the API explorer ( /civicrm/api/explorer ) on my local installation to investigate which methods were available, but the "install" method was not available for "Extension".   Does the explorer only list standard methods that are available for all/most entities?

Also, the only way I can tell to check the version of an existing, installed extension is to scan the "info.xml" file in the file system. Is there a better way? (I do not want to install/update an extension if its already installed and at the latest version.)
Did I help you? Please donate to the Civi-Make-It-Happen campaign  CiviCRM for mobile devices! 

totten

  • Administrator
  • Ask me questions
  • *****
  • Posts: 695
  • Karma: 64
Re: How to install an extension from a script
May 20, 2013, 04:24:40 pm
Yes, I think the explorer only handles the common actions (create/get/delete). :(

For getting the version, it basically requires hitting the filesystem to parse info.xml. The internal code which does that can be reached via:

Code: [Select]
$info = CRM_Extension_System::singleton()
  ->getMapper()
  ->keyToInfo('com.example.mymodule');

In 4.3, there's some commented code in civicrm_api3_extension_get (api/v3/Extension.php) which will cause the API to return the metadata from info.xml. I don't recall why it's disabled -- my guesses:

 * I was afraid it would be more time-consuming. (A simple "extension.get" requires parsing all the info.xml files.)
 * We'll hopefully adopt composer.json as the metadata format sometime -- in which case we might become liable for maintaining backward compatibility in the API.

If neither of those seem like significant issues to anyone besides me, then we could enable the functionality.

xavier

  • Forum Godess / God
  • I’m (like) Lobo ;)
  • *****
  • Posts: 4453
  • Karma: 161
    • Tech To The People
  • CiviCRM version: yes probably
  • CMS version: drupal
Re: How to install an extension from a script
May 20, 2013, 11:05:08 pm
Quote from: totten on May 20, 2013, 04:24:40 pm
Yes, I think the explorer only handles the common actions (create/get/delete). :(

It does.

As you are aware, you can alter the input field and change the action manually if you know its name, but doesn't help for the discovery.

I did try to implement a getaction, managed to get a list of all the civicrm_api3_entity_xxx functions and use it to populate, but didn't find a way to get the functions in the Entity/Action.php files nor the generic actions in the time I allowed myself to this feature.

epg, something to tackle at a sprint?

X+
-Hackathon and data journalism about the European parliament 24-26 jan. Watch out the result

Pages: [1]
  • CiviCRM Community Forums (archive) »
  • Discussion »
  • Extensions (Moderators: mathieu, totten, kasiawaka) »
  • How to install an extension from a script

This forum was archived on 2017-11-26.