You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

162 lines
4.5KB

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