Page 1 of 1

[SOLVED] Automatic XML module export script

Posted: Mon Jun 29, 2009 4:38 pm
by ikulis
Has anybody made an automatic shell script for generating XML files for modules basing on given module directory.

I could be PHP/bash/(any script language) script. I would be very grateful if anybody would share such script.

I know there is Export XML module, but work in CMSMS not in shell, that`s why I have posted the question.

Re: [SOLVED] Automatic XML module export script

Posted: Sat Aug 08, 2009 9:58 pm
by ikulis
I have created the PHP script for automatic XML module generation. Hope somebody will find is useful.
Writes the XML data to standard output.

Code: Select all

<?
// @author Szymon Lukaszczyk <szymon.lukaszczyk@gmail.com>
// @licence http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt
//
// CreateXMLPackage extracted from CMSMS core
// changed a little


// variables to edit
$path = "/var/www/cmsmadesimple/"; // add the path to working cmsms catalog
$module = "DownloadManager"; // specify the name of your module
$modulepath = $path."modules/".$module."/"; // specify the path to module sources

// do not edit things below unless you know what are you doing
require_once(realpath($path).'/include.php');
global $gCms;
$message = '';
$files = 0;

function CreateXMLPackage( &$modinstance, &$filecount, $dir )
{
  // get a file list
  $filecount = 0;
  $files = get_recursive_file_list( $dir, array ( '^\.svn' ,'^CVS$','^\#.*\#$','~$','\.bak$'));

  $xmltxt  = '<?xml version="1.0" encoding="ISO-8859-1"?>';
  $xmltxt .= "\n<!DOCTYPE module [
  <!ELEMENT module (dtdversion,name,version,description*,help*,about*,requires*,file+)>
  <!ELEMENT dtdversion (#PCDATA)>
  <!ELEMENT name (#PCDATA)>
  <!ELEMENT version (#PCDATA)>
  <!ELEMENT mincmsversion (#PCDATA)>
  <!ELEMENT description (#PCDATA)>
  <!ELEMENT help (#PCDATA)>
  <!ELEMENT about (#PCDATA)>
  <!ELEMENT requires (requiredname,requiredversion)>
  <!ELEMENT requiredname (#PCDATA)>
  <!ELEMENT requiredversion (#PCDATA)>
  <!ELEMENT file (filename,isdir,data)>
  <!ELEMENT filename (#PCDATA)>
  <!ELEMENT isdir (#PCDATA)>
  <!ELEMENT data (#PCDATA)>
]>\n";
  $xmltxt .= "<module>\n";
	  $xmltxt .= "	<dtdversion>".MODULE_DTD_VERSION."</dtdversion>\n";
  $xmltxt .= "	<name>".$modinstance->GetName()."</name>\n";
  $xmltxt .= "	<version>".$modinstance->GetVersion()."</version>\n";
  $xmltxt .= "  <mincmsversion>".$modinstance->MinimumCMSVersion()."</mincmsversion>\n";
  $xmltxt .= "	<help>".base64_encode($modinstance->GetHelpPage())."</help>\n";
  $xmltxt .= "	<about>".base64_encode($modinstance->GetAbout())."</about>\n";
  $desc = $modinstance->GetAdminDescription();
  if( $desc != '' )
	{
	  $xmltxt .= "	<description>".$desc."</description>\n";
	}
  $depends = $modinstance->GetDependencies();
  foreach( $depends as $key=>$val )
	{
	  $xmltxt .= "	<requires>\n";
		  $xmltxt .= "	  <requiredname>$key</requiredname>\n";
		  $xmltxt .= "	  <requiredversion>$val</requiredversion>\n";
	  $xmltxt .= "	</requires>\n";
	}
  foreach( $files as $file )
	{
	  // strip off the beginning
	  if (substr($file,0,strlen($dir)) == $dir)
		  {
		  $file = substr($file,strlen($dir));
		  }
	  if( $file == '' ) continue;

	  $xmltxt .= "	<file>\n";
	  $filespec = $dir.DIRECTORY_SEPARATOR.$file;
	  $xmltxt .= "	  <filename>$file</filename>\n";
	  if( @is_dir( $filespec ) )
	{
	  $xmltxt .= "	  <isdir>1</isdir>\n";
	}
	  else
	{
	  $xmltxt .= "	  <isdir>0</isdir>\n";
	  $data = base64_encode(file_get_contents($filespec));
	  $xmltxt .= "	  <data><![CDATA[".$data."]]></data>\n";
	}

	  $xmltxt .= "	</file>\n";
	  ++$filecount;
	}
	  $xmltxt .= "</module>\n";
  return $xmltxt;
}

$object = $gCms->modules[$module]['object'];
$xmltxt = CreateXMLPackage($object,$files,$modulepath);


if( $files == 0 )
{
  echo "<p class=\"error\">".lang('errornofilesexported')."</p>";
}
else 
{

  echo $xmltxt;
}

?>

Re: [SOLVED] Automatic XML module export script

Posted: Sat Aug 08, 2009 10:01 pm
by NaN
Did you ever hit the XML button in "Extensions->Modules" ?

Re: [SOLVED] Automatic XML module export script

Posted: Sat Aug 08, 2009 10:02 pm
by ikulis
NaN wrote:
Did you ever hit the XML button in "Extensions->Modules" ?
Read the first post ...

Re: [SOLVED] Automatic XML module export script

Posted: Sat Aug 08, 2009 10:06 pm
by calguy1000
Use wget
build a script to login to the admin
and request the appropriate url.

It's not difficult for experienced shell script developers.
I have created scripts for use in cron to create NMS jobs, and send them.

Re: [SOLVED] Automatic XML module export script

Posted: Sat Aug 08, 2009 10:39 pm
by ikulis
I`m using this as a part of bash script, which also generates the zip and bzip2 archives.

For wget u need webserver running, here it is not needed.

Re: [SOLVED] Automatic XML module export script

Posted: Sat Aug 08, 2009 10:53 pm
by calguy1000
An excerpt from my BASH script:

Code: Select all

# do the login
_postdata="username=${username}&password=${password}&loginsubmit=Submit"
wget -o log -O outfile.1 --save-cookies ${_cookies} --keep-session-cookies --post-data=$_postdata ${sitename}/admin/login.php 
_x=`grep -c 'password incorrect' outfile.1`
if [ $_x -gt 0 ]; then
  echo "FATAL: Authentication error";
  cd $_owd
  rm -rf $_tmpdir 2>/dev/null
  exit 1
fi


# Get the session key
_line=`cat $_cookies | grep ${keyname}`
if [ ${line:-notset} != notset ]; then
  _sessionkey=`cat $_cookies | grep ${keyname} | tr '\t' , | cut -d, -f7`
else
  _sessionkey=`cat log | grep ^Location | cut -d= -f2 | cut -d" " -f1`
fi


# Create an NMS job
_jn=`echo $_jobname | tr -s " " _`
_postdata="m1_messagelist=${message}&m1_listlist=${list}&m1_jobname=${_jn}"
wget -a log -O outfile.2 --load-cookies ${_cookies} --post-data=${_postdata} ${sitename}/admin/moduleinterface.php?${keyname}=${_sessionkey}\&mact=NMS,m1_,do_create_edit_job,0 

Re: [SOLVED] Automatic XML module export script

Posted: Sat Aug 08, 2009 11:41 pm
by blast2007
And the full code is...

Code: Select all

#!/bin/bash
username="yourusernamehere"
password="yourpasswordhere"
sitename="http://yoursitenamehere.com"
_cookies="/tmp/cookie"
keyname="sp_"
# we want to get XML of AjaxMadeSimple but you can use what you want
module="AjaxMadeSimple"


# do the login
_postdata="username=${username}&password=${password}&loginsubmit=Submit"

wget -o log -O outfile.1 --save-cookies ${_cookies} --keep-session-cookies --post-data=$_postdata ${sitename}/admin/login.php
_x=`grep -c 'password incorrect' outfile.1`

if [ $_x -gt 0 ]; then
  echo "FATAL: Authentication error";
  cd $_owd
  rm -rf $_tmpdir 2>/dev/null
  exit 1
fi


# Get the session key
_line=`cat $_cookies | grep ${keyname}`
if [ ${line:-notset} != notset ]; then
  _sessionkey=`cat $_cookies | grep ${keyname} | tr '\t' , | cut -d, -f7`
else
  _sessionkey=`cat log | grep ^Location | cut -d= -f2 | cut -d" " -f1`
fi

wget -a log -O $module.xml --load-cookies ${_cookies} ${sitename}/admin/listmodules.php?${keyname}=${_sessionkey}\&action=exportxml\&module=$module
thanks calguy1000 +++
regards
blast


P.S. pay attention at ^Location string because it's in english version only of wget

P.S.2 This example could be also used for automated (scheduled) backups from command line of our systems...