Skip to content

Commit 4e1f6bd

Browse files
author
Artur (Seti) Łabudziński
committed
UTF8 partial support for url's in JIRA (Tested on Account Service with GroupNames in URLs).
1 parent 545d27e commit 4e1f6bd

File tree

5 files changed

+63
-14
lines changed

5 files changed

+63
-14
lines changed

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "setitch/php-jira-api-rest-client",
3-
"description": "Client for Jira REST Api written in PHP.",
3+
"description": "Client for Jira REST Api written in PHP. Now with partial support of UTF8 setting of JIRA",
44
"type": "library",
55
"keywords": ["jira", "jira-php", "jira-rest", "jira-api", "php", "library", "client"],
66
"support": {

src/Account/AccountService.php

+29-9
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ public function addGroupUser($name, $userName)
1515
]);
1616

1717
try {
18-
$ret = $this->exec($this->uri.'/user?groupname=' . $this->filterName($name).'', $json, 'POST');
18+
if (self::$isJIRAUtf8)
19+
$ret = $this->exec($this->uri.'/user?groupname=' . $this->filterName(urlencode($name), '+').'&useUnicode=true&characterEncoding=unicode', $json, 'POST');
20+
else
21+
$ret = $this->exec($this->uri.'/user?groupname=' . $this->filterName(urlencode($name), '+').'', $json, 'POST');
1922
} catch (\Jira\Api\Exception $e) {
2023
$code = $e -> getCode();
2124
$err = $e -> getMessage();
22-
2325
if (FALSE !== strpos($err, "Cannot add user. '$userName' does not exist"))
2426
return -1;
2527

@@ -42,7 +44,10 @@ public function removeGroupUser($name, $userName)
4244

4345
try
4446
{
45-
$ret = $this->exec($this->uri.'/user?username='.$userName.'&groupname=' . $this->filterName($name).'', null , 'DELETE');
47+
if (self::$isJIRAUtf8)
48+
$ret = $this->exec($this->uri.'/user?username='.$userName.'&groupname=' . $this->filterName(urlencode($name),'+').'&useUnicode=true&characterEncoding=unicode', null , 'DELETE');
49+
else
50+
$ret = $this->exec($this->uri.'/user?username='.$userName.'&groupname=' . $this->filterName(urlencode($name),'+').'', null , 'DELETE');
4651
}
4752
catch (\Jira\Api\Exception $e)
4853
{
@@ -65,13 +70,18 @@ public function removeGroupUser($name, $userName)
6570
*/
6671
public function getGroup($name)
6772
{
68-
$ret = $this->exec($this->uri.'?groupname=' . $this->filterName($name, '+') .'');
73+
if (self::$isJIRAUtf8)
74+
$ret = $this->exec($this->uri.'?groupname=' . str_replace(' ', '+', urlencode($name)).'&useUnicode=true&characterEncoding=unicode');
75+
else
76+
$ret = $this->exec($this->uri.'?groupname=' . str_replace(' ', '+', urlencode($name)));// .'&useUnicode=false&characterEncoding=unicode');
77+
// $ret = $this->exec($this->uri.'?groupname=' . ($this->filterName($name,'+')) .'&useUnicode=true&characterEncoding=UTF8');
78+
// $ret = $this->exec($this->uri, $json, 'POST');
6979
// $ret = $this->exec($this->uri.'/member?groupname=' . $name);
80+
//var_dump($ret);
7081

7182
$group = $this->json_mapper->map(
7283
json_decode($ret), new Group()
7384
);
74-
7585
return $group;
7686
}
7787

@@ -88,11 +98,14 @@ public function getGroupMembers($name)
8898
while ($page * $onPage < $max)
8999
{
90100
$json = json_encode([
91-
'groupname' => $this->filterName($name),
101+
// 'groupname' => $this->filterName($name),
92102
'maxResults' => $max,
93103
'startAt' => $onPage * $page,
94104
]);
95-
$ret = $this->exec($this->uri."?expand=users&maxResults=$max&startAt=".($onPage*$page).'&groupname=' . $this->filterName($name,'+'));
105+
if (self::$isJIRAUtf8)
106+
$ret = $this->exec($this->uri."?expand=users&maxResults=$max&startAt=".($onPage*$page).'&groupname=' . $this->filterName(urlencode($name),'+').'&useUnicode=true&characterEncoding=unicode');
107+
else
108+
$ret = $this->exec($this->uri."?expand=users&maxResults=$max&startAt=".($onPage*$page).'&groupname=' . $this->filterName(urlencode($name),'+'));
96109
// $ret = $this->exec($this->uri.'?groupname=' . $name);
97110
$ret = json_decode($ret);
98111
// print_r($ret);
@@ -122,8 +135,15 @@ public function createGroup($name)
122135
$json = json_encode([
123136
'name' => $this->filterName($name),
124137
]);
125-
$ret = $this->exec($this->uri, $json, 'POST');
126-
// $ret = $this->exec($this->uri.'/member?groupname=' . $name);
138+
var_dump($json);
139+
try {
140+
$ret = $this->exec($this->uri, $json, 'POST');
141+
} catch (\Exception $e) {
142+
if (stripos($e->getMessage(), 'A group or user with this name already exists.') !== 0) {
143+
die('a');
144+
}
145+
else throw $e;
146+
}
127147

128148
$group = $this->json_mapper->map(
129149
json_decode($ret), new Group()

src/Client.php

+21-4
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
*/
1818
class Client
1919
{
20+
static protected $isJIRAUtf8 = true;
21+
2022
static protected $jMapper = null;
2123
/**
2224
* Json Mapper
@@ -70,7 +72,7 @@ public function __construct(ConfigurationInterface $configuration = null)
7072
if ($this->configuration->getMapper())
7173
$this->json_mapper = $this->configuration->getMapper();
7274
else
73-
$this->json_mapper = new \Jira\Api\Mapper();
75+
$this->json_mapper = new \Jira\Mapper();
7476

7577
self::$jMapper = $this->json_mapper;
7678
// create logger
@@ -80,14 +82,27 @@ public function __construct(ConfigurationInterface $configuration = null)
8082
3,
8183
$this->convertLogLevel($configuration->getJiraLogLevel())
8284
));
85+
86+
self::$isJIRAUtf8 = $configuration -> getUtf8Support();
8387

8488
$this->http_response = 200;
8589
}
8690
static public function getMapper() { return self::$jMapper; }
8791

8892
public function filterName($name, $spaceEncode = false) {
93+
94+
8995
if ($spaceEncode) $name = str_replace(' ', $spaceEncode, $name);
90-
return ( iconv('utf-8', 'us-ascii//TRANSLIT', $name));
96+
$t = $name;
97+
try {
98+
// $t = iconv('CP1250', 'utf-8//TRANSLIT//IGNORE', $t);
99+
// $t = iconv('CP-1250', 'utf-8//TRANSLIT', $t);
100+
$name = $t;
101+
} catch (\Exception $e) {echo $e->getMessage();}
102+
return $name;
103+
// return urlencode( $name );
104+
return ( iconv('utf-8', 'us-ascii//TRANSLIT', $name) );
105+
// return ( iconv('utf-8', 'us-ascii//TRANSLIT', $name) );
91106
}
92107

93108
/**
@@ -149,10 +164,12 @@ public function exec($context, $post_data = null, $custom_request = null, $tries
149164
$this->log->addDebug("Curl [".($custom_request ? $custom_request : 'GET')."] $url JsonData=" . $post_data);
150165
// echo "Curl [".($custom_request ? $custom_request : 'GET')."] $url JsonData=" . $post_data, "\n";
151166
$ch = curl_init();
167+
curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8');
168+
152169
curl_setopt($ch, CURLOPT_TIMEOUT, 45); // Timeout na 30 sekund max
153170
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 15); // Timeout dla oczekiwania na połączenie na 30 sekund max
154171
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
155-
curl_setopt($ch, CURLOPT_URL, $url);
172+
curl_setopt($ch, CURLOPT_URL, utf8_encode($url));
156173
// post_data
157174
if (!is_null($post_data)) {
158175
// PUT REQUEST
@@ -415,4 +432,4 @@ public function getRealDate($dateObject = null, $timeZone = null) {
415432

416433
return $dateObject;
417434
}
418-
}
435+
}

src/Configuration/AbstractConfiguration.php

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ abstract class AbstractConfiguration implements \Jira\Api\Configuration\Configur
6565
protected $curlOptVerbose;
6666

6767
protected $mapper = null;
68+
69+
protected $utf8support = false;
6870

6971
/**
7072
* @return string
@@ -134,4 +136,9 @@ public function getMapper()
134136
{
135137
return $this->mapper;
136138
}
139+
140+
public function getUtf8Support()
141+
{
142+
return $this->utf8support;
143+
}
137144
}

src/Configuration/ConfigurationInterface.php

+5
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,9 @@ public function isCurlOptVerbose();
6868
* @return \JsonMapper\Interface
6969
*/
7070
public function getMapper();
71+
72+
/**
73+
* @return Boolean
74+
*/
75+
public function getUtf8Support();
7176
}

0 commit comments

Comments
 (0)