联动加入redmine的wik
<?
php
error_reporting(E_ERROR);
date_default_timezone_set('Asia/Shanghai');
$red_server = "http://redmine.meilishuo.com/projects";
$project_name = "opupdate";
$wiki_name = "故障纪录";
$url = $red_server . "/$project_name/wiki/wiki.json";
create_wiki($url, "故障记录-" . date('Y-m'), $wiki_name, $message);
function create_wiki( $url, $title, $wiki_name, $message) {
$level = preg_split('/\//', $title);
for ( $i = 0; $i <= count($level); $i++) {
$suffix = ".json";
if ( $i == 0 ) {
$url = preg_replace('/wiki.*/i', "wiki/$wiki_name", $url);
}
if ( ! strstr($url, $suffix)) {
$url .= $suffix;
}
$page = get_page_content($url, "GET", "");
$org = json_decode( $page["content"] );
$org_content = $org->{'wiki_page'}->{'text'};
if ( 0 == $i ) {
$check_str = $wiki_name;
$parent = "Wiki";
} else {
$parent = $i == 1 ? $wiki_name : $level[($i-1)];
$check_str = $level[$i];
}
$check_result = strstr($org->{'wiki_page'}->{'text'}, $check_str);
if ( ! $check_result && 0 == $i ) {
$new_content = "h1. $check_str\r\n\r\nh2. [[$level[$i]]]";
} elseif ( count($level) == $i ) {
$new_content = "h3. " . strftime("%Y-%m-%d %H:%M:%S") . " : \r\n\r\n$message";
} elseif ( ! $check_result ) {
$last = $level[($i-1)];
$new_content = "h1. $last\r\n\r\nh2. [[$level[$i]]]";
}
$org_content .= "\r\n\r\n$new_content ";
$org->{'wiki_page'}->{'text'} = $org_content;
$org->{'wiki_page'}->{'parent_title'} = $parent;
$new = json_encode($org);
if ( isset($new_content) ) {
$response = get_page_content($url, "PUT", $new);
if ( 200 == $response['http_code']) {
print_r("Wiki : $url updated.\n$new\n");
} elseif ( 201 == $response['http_code']) {
print_r("Wiki : $url created.\n");
} else {
print_r("Failed to update wiki : $url\t" . $response['http_code'] . "\n");
}
}
$url = preg_replace('/wiki.*/i', "wiki/$level[$i]", $url);
}
}
$page = get_page_content($url, "GET", "");
$org = json_decode( $page["content"] );
if ( ! strstr($org->{'wiki_page'}->{'text'}, $wiki_name) ) {
$org_content = $org->{'wiki_page'}->{'text'};
$new_content = "* [[$wiki_name]]";
$org_content .= "\r\n\r\n$new_content ";
$org->{'wiki_page'}->{'text'} = $org_content;
$new = json_encode($org);
$response = get_page_content($url, "PUT", $new);
if ( 201 == $response['http_code']) {
printf("Wiki : $wiki_name created.\n");
}
}
function get_page_content( $url, $func, $curl_data ) {
$header = array(
"X-Redmine-API-Key: 5b01xxxxxxxddddddddddddddddddd",
"Content-Type: application/json"
);
$options = array(
CURLOPT_HTTPHEADER => $header,
CURLOPT_RETURNTRANSFER => true, // return web page
CURLOPT_HEADER => false, // don't return headers
CURLOPT_FOLLOWLOCATION => true, // follow redirects
CURLOPT_ENCODING => "", // handle all encodings
CURLOPT_USERAGENT => "Release Shell", // who am i
CURLOPT_AUTOREFERER => true, // set referer on redirect
CURLOPT_CONNECTTIMEOUT => 10, // timeout on connect
CURLOPT_TIMEOUT => 10, // timeout on response
CURLOPT_MAXREDIRS => 10, // stop after 10 redirects
CURLOPT_CUSTOMREQUEST => $func,
CURLOPT_VERBOSE => 0, //
CURLOPT_POSTFIELDS => $curl_data // this are my post vars
);
if ( "POST" == $func ) {
$post_opt = array(
CURLOPT_POST => 1, // i am sending post data
);
$options = array_merge($options, $post_opt);
}
$ch = curl_init($url);
curl_setopt_array($ch,$options);
$content = curl_exec($ch);
$err = curl_errno($ch);
$errmsg = curl_error($ch) ;
$header = curl_getinfo($ch);
curl_close($ch);
$header['content'] = $content;
$header['errmsg'] = $errmsg;
return $header;
}