Explicitly initialize anonModule to null.
[htmlpurifier.git] / maintenance / update-freshmeat.php
blob5295c04300d32394e7b85b2dc009017d95855f25
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) {
43 if ($username && $password) {
44 $this->login($username, $password);
48 /**
49 * Performs a raw XML RPC call to self::URL
51 protected function call($method, $params) {
52 $request = xmlrpc_encode_request($method, $params, $this->encodeOptions);
53 $ch = curl_init();
54 curl_setopt($ch, CURLOPT_URL, self::URL);
55 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
56 curl_setopt($ch, CURLOPT_TIMEOUT, 1);
57 curl_setopt($ch, CURLOPT_HTTPHEADER, array(
58 'Content-type: text/xml',
59 'Content-length: ' . strlen($request)
60 ));
61 curl_setopt($ch, CURLOPT_POSTFIELDS, $request);
62 $data = curl_exec($ch);
63 if ($errno = curl_errno($ch)) {
64 throw new Exception("Curl error [$errno]: " . curl_error($ch));
65 } else {
66 curl_close($ch);
67 return xmlrpc_decode($data);
71 /**
72 * Performs an XML RPC call to Freshmeat.
73 * @param $name Name of method to call, can be methodName or method_name
74 * @param $args Arguments of call, in form array('key1', 'val1', 'key2' ...)
76 public function __call($name, $args) {
77 $method = $this->camelToUnderscore($name);
78 $params = array();
79 if ($this->sid) $params['SID'] = $this->sid;
80 if (isset($this->signatures[$method])) {
81 for ($i = 0, $c = count($this->signatures[$method]); $i < $c; $i++) {
82 $params[$this->signatures[$method][$i]] = $args[$i];
84 } else {
85 for ($i = 0, $c = count($args); $i + 1 < $c; $i += 2) {
86 $params[$args[$i]] = $args[$i + 1];
89 $result = $this->call($method, $params);
90 switch ($method) {
91 case 'login':
92 $this->sid = $result['SID'];
93 break;
94 case 'logout':
95 $this->sid = null;
96 break;
98 if ($this->chatty) print_r($result);
99 return $result;
103 * Munge methodName to method_name
105 private function camelToUnderscore($name) {
106 $method = '';
107 for ($i = 0, $c = strlen($name); $i < $c; $i++) {
108 $v = $name[$i];
109 if (ctype_lower($v)) $method .= $v;
110 else $method .= '_' . strtolower($v);
112 return $method;
116 * Automatically logout at end of scope
118 public function __destruct() {
119 if ($this->sid) $this->logout();
124 $rpc = new XmlRpc_Freshmeat($argv[1], $argv[2]);
125 $rpc->chatty = true;
127 $project = 'htmlpurifier';
128 $branch = 'Default';
129 $version = file_get_contents('../VERSION');
131 $result = $rpc->fetchRelease($project, $branch, $version);
132 if (!isset($result['faultCode'])) {
133 echo "Freshmeat release already exists.\n";
134 exit(0);
137 $changes = strtr(file_get_contents('../WHATSNEW'), array("\r" => '', "\n" => ' '));
138 $focus = (int) trim(file_get_contents('../FOCUS'));
140 if (strlen($changes) > 600) {
141 echo "WHATSNEW entry is too long.\n";
142 exit(1);
145 $rpc->publishRelease(
146 'project_name', $project,
147 'branch_name', $branch,
148 'version', $version,
149 'changes', $changes,
150 'release_focus', $focus,
151 'url_tgz', "http://htmlpurifier.org/releases/htmlpurifier-$version.tar.gz",
152 'url_zip', "http://htmlpurifier.org/releases/htmlpurifier-$version.zip",
153 'url_changelog', "http://htmlpurifier.org/svnroot/htmlpurifier/tags/$version/NEWS"
156 // vim: et sw=4 sts=4