Merge pull request #60 from sylfabre/patch-1
[htmlpurifier.git] / maintenance / update-freshmeat.php
blob4d73c76ae1f0a6fc73f75fc808e425e0be5811f6
1 #!/usr/bin/php
2 <?php
4 chdir(dirname(__FILE__));
5 require_once 'common.php';
6 assertCli();
8 /**
9 * @file
10 * Updates Freshmeat's HTML Purifier with the latest information via XML RPC.
13 class XmlRpc_Freshmeat
16 const URL = 'http://freshmeat.net/xmlrpc/';
18 public $chatty = false;
20 public $encodeOptions = array(
21 'encoding' => 'utf-8',
24 /**
25 * This array defines shortcut method signatures for dealing with simple
26 * XML RPC methods. More complex ones (publish_release) should use the named parameter
27 * syntax.
29 public $signatures = array(
30 'login' => array('username', 'password'),
31 'fetch_branch_list' => array('project_name'),
32 'fetch_release' => array('project_name', 'branch_name', 'version'),
33 'withdraw_release' => array('project_name', 'branch_name', 'version'),
36 protected $sid = null;
38 /**
39 * @param $username Username to login with
40 * @param $password Password to login with
42 public function __construct($username = null, $password = null)
44 if ($username && $password) {
45 $this->login($username, $password);
49 /**
50 * Performs a raw XML RPC call to self::URL
52 protected function call($method, $params)
54 $request = xmlrpc_encode_request($method, $params, $this->encodeOptions);
55 $ch = curl_init();
56 curl_setopt($ch, CURLOPT_URL, self::URL);
57 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
58 curl_setopt($ch, CURLOPT_TIMEOUT, 1);
59 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
60 'Content-type: text/xml',
61 'Content-length: ' . strlen($request)
62 ));
63 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
64 $data = curl_exec($ch);
65 if ($errno = curl_errno($ch)) {
66 throw new Exception("Curl error [$errno]: " . curl_error($ch));
67 } else {
68 curl_close($ch);
69 return xmlrpc_decode($data);
73 /**
74 * Performs an XML RPC call to Freshmeat.
75 * @param $name Name of method to call, can be methodName or method_name
76 * @param $args Arguments of call, in form array('key1', 'val1', 'key2' ...)
78 public function __call($name, $args)
80 $method = $this->camelToUnderscore($name);
81 $params = array();
82 if ($this->sid) $params['SID'] = $this->sid;
83 if (isset($this->signatures[$method])) {
84 for ($i = 0, $c = count($this->signatures[$method]); $i < $c; $i++) {
85 $params[$this->signatures[$method][$i]] = $args[$i];
87 } else {
88 for ($i = 0, $c = count($args); $i + 1 < $c; $i += 2) {
89 $params[$args[$i]] = $args[$i + 1];
92 $result = $this->call($method, $params);
93 switch ($method) {
94 case 'login':
95 $this->sid = $result['SID'];
96 break;
97 case 'logout':
98 $this->sid = null;
99 break;
101 if ($this->chatty) print_r($result);
102 return $result;
106 * Munge methodName to method_name
108 private function camelToUnderscore($name)
110 $method = '';
111 for ($i = 0, $c = strlen($name); $i < $c; $i++) {
112 $v = $name[$i];
113 if (ctype_lower($v)) $method .= $v;
114 else $method .= '_' . strtolower($v);
116 return $method;
120 * Automatically logout at end of scope
122 public function __destruct()
124 if ($this->sid) $this->logout();
129 $rpc = new XmlRpc_Freshmeat($argv[1], $argv[2]);
130 $rpc->chatty = true;
132 $project = 'htmlpurifier';
133 $branch = 'Default';
134 $version = file_get_contents('../VERSION');
136 $result = $rpc->fetchRelease($project, $branch, $version);
137 if (!isset($result['faultCode'])) {
138 echo "Freshmeat release already exists.\n";
139 exit(0);
142 $changes = strtr(file_get_contents('../WHATSNEW'), array("\r" => '', "\n" => ' '));
143 $focus = (int) trim(file_get_contents('../FOCUS'));
145 if (strlen($changes) > 600) {
146 echo "WHATSNEW entry is too long.\n";
147 exit(1);
150 $rpc->publishRelease(
151 'project_name', $project,
152 'branch_name', $branch,
153 'version', $version,
154 'changes', $changes,
155 'release_focus', $focus,
156 'url_tgz', "http://htmlpurifier.org/releases/htmlpurifier-$version.tar.gz",
157 'url_zip', "http://htmlpurifier.org/releases/htmlpurifier-$version.zip",
158 'url_changelog', "http://htmlpurifier.org/svnroot/htmlpurifier/tags/$version/NEWS"
161 // vim: et sw=4 sts=4