This commit is contained in:
mutoo 2013-12-12 10:37:19 +08:00
commit 074f511a65
3 changed files with 396 additions and 0 deletions

138
GitHubGit/Action.php Normal file
View File

@ -0,0 +1,138 @@
<?php
class GitHubGit_Action extends Widget_Abstract_Contents implements Widget_Interface_Do
{
public function action()
{
/** get added files */
// GitHub webhooks
// https://help.github.com/articles/post-receive-hooks
$payload = ltrim(urldecode(file_get_contents('php://input')), 'payload=');
$data = json_decode($payload, true); //without `true`, json_decode will return object instead of array
$repository_url = $data['repository']['url'];
$commits = $data['commits'];
foreach ($commits as $commit) {
foreach ($commit['added'] as $added_file) {
$added_files[] = $added_file;
}
}
/** login */
$master = $this->db->fetchRow($this->db->select()->from('table.users')
->where('group = ?', 'administrator')
->order('uid', Typecho_Db::SORT_ASC)
->limit(1));
if (empty($master)) {
return false;
} else if (!$this->user->simpleLogin($master['uid'])) {
return false;
}
/** add article */
if (isset($added_files) && is_array($added_files)) {
foreach ($added_files as $added_file) {
$input = array(
'do' => 'publish',
'allowComment' => $this->options->defaultAllowComment,
'allowPing' => $this->options->defaultAllowPing,
'allowFeed' => $this->options->defaultAllowFeed
);
list($slug) = explode('.', basename($added_file));
$input['slug'] = $slug;
$post = $this->db->fetchRow($this->db->select()
->from('table.contents')->where('slug = ?', $slug)->limit(1));
if (!empty($post)) {
if ('post' != $post['type']) {
return false;
} else {
$input['cid'] = $post['cid'];
}
}
$input['category'] = 'default';
$input['title'] = pathinfo($added_file)['filename'];
$url = preg_replace('#https://#', 'https://raw.', $repository_url) . '/master/' . $added_file;
$input['text'] = file_get_contents($url);
if ($input) {
$this->widget('Widget_Contents_Post_Edit', NULL, $input, false)->action();
}
}
}
}
}
<?php
class GitHubGit_Action extends Widget_Abstract_Contents implements Widget_Interface_Do
{
public function action()
{
/** get added files */
// GitHub webhooks
// https://help.github.com/articles/post-receive-hooks
$payload = ltrim(urldecode(file_get_contents('php://input')), 'payload=');
$data = json_decode($payload, true); //without `true`, json_decode will return object instead of array
$repository_url = $data['repository']['url'];
$commits = $data['commits'];
foreach ($commits as $commit) {
foreach ($commit['added'] as $added_file) {
$added_files[] = $added_file;
}
}
/** login */
$master = $this->db->fetchRow($this->db->select()->from('table.users')
->where('group = ?', 'administrator')
->order('uid', Typecho_Db::SORT_ASC)
->limit(1));
if (empty($master)) {
return false;
} else if (!$this->user->simpleLogin($master['uid'])) {
return false;
}
/** add article */
if (isset($added_files) && is_array($added_files)) {
foreach ($added_files as $added_file) {
$input = array(
'do' => 'publish',
'allowComment' => $this->options->defaultAllowComment,
'allowPing' => $this->options->defaultAllowPing,
'allowFeed' => $this->options->defaultAllowFeed
);
list($slug) = explode('.', basename($added_file));
$input['slug'] = $slug;
$post = $this->db->fetchRow($this->db->select()
->from('table.contents')->where('slug = ?', $slug)->limit(1));
if (!empty($post)) {
if ('post' != $post['type']) {
return false;
} else {
$input['cid'] = $post['cid'];
}
}
$input['category'] = 'default';
$input['title'] = pathinfo($added_file)['filename'];
$url = preg_replace('#https://#', 'https://raw.', $repository_url) . '/master/' . $added_file;
$input['text'] = file_get_contents($url);
if ($input) {
$this->widget('Widget_Contents_Post_Edit', NULL, $input, false)->action();
}
}
}
}
}

207
GitHubGit/Plugin.php Normal file
View File

@ -0,0 +1,207 @@
<?php
/**
* GitHub git 同步文章
*
* @package GitHub Git Transmit
* @author weakish
* @version 0.0.1
* @dependence 10.6.24-*
* @link http://typecho.org
*/
class GitHubGit_Plugin implements Typecho_Plugin_Interface
{
// Use your own random code.
// Keep your code secret!
// Anyone knowing the code can post articles on your blog!
const github_git = 'curtseyingpiddlesMiguelyeshivahsclarinettists';
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
if (false == Typecho_Http_Client::get()) {
throw new Typecho_Plugin_Exception(_t('对不起, 您的主机不支持 php-curl 扩展而且没有打开 allow_url_fopen 功能, 无法正常使用此功能'));
}
Helper::addAction(github_git, 'GitHubGit_Action');
return _t('请在插件设置里设置 GitHub 的Git参数') . $error;
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
Helper::removeAction(github_git);
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$basePath = new Typecho_Widget_Helper_Form_Element_Text('basePath', NULL, '/_posts',
_t('Git目录'), _t('填写需要监控的Git目录')); // 默认为Jekyll的_posts目录
$form->addInput($basePath->addRule('required', _t('必须填写数据库用户名')));
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
}
<?php
/**
* GitHub git 同步文章
*
* @package GitHub Git Transmit
* @author weakish
* @version 0.0.1
* @dependence 10.6.24-*
* @link http://typecho.org
*/
class GitHubGit_Plugin implements Typecho_Plugin_Interface
{
// Use your own random code.
// Keep your code secret!
// Anyone knowing the code can post articles on your blog!
const github_git = 'curtseyingpiddlesMiguelyeshivahsclarinettists';
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
if (false == Typecho_Http_Client::get()) {
throw new Typecho_Plugin_Exception(_t('对不起, 您的主机不支持 php-curl 扩展而且没有打开 allow_url_fopen 功能, 无法正常使用此功能'));
}
Helper::addAction(github_git, 'GitHubGit_Action');
return _t('请在插件设置里设置 GitHub 的Git参数') . $error;
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
Helper::removeAction(github_git);
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$basePath = new Typecho_Widget_Helper_Form_Element_Text('basePath', NULL, '/_posts',
_t('Git目录'), _t('填写需要监控的Git目录')); // 默认为Jekyll的_posts目录
$form->addInput($basePath->addRule('required', _t('必须填写数据库用户名')));
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
}
<?php
/**
* GitHub git 同步文章
*
* @package GitHub Git Transmit
* @author weakish
* @version 0.0.1
* @dependence 10.6.24-*
* @link http://typecho.org
*/
class GitHubGit_Plugin implements Typecho_Plugin_Interface
{
// Use your own random code.
// Keep your code secret!
// Anyone knowing the code can post articles on your blog!
const github_git = 'curtseyingpiddlesMiguelyeshivahsclarinettists';
/**
* 激活插件方法,如果激活失败,直接抛出异常
*
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function activate()
{
if (false == Typecho_Http_Client::get()) {
throw new Typecho_Plugin_Exception(_t('对不起, 您的主机不支持 php-curl 扩展而且没有打开 allow_url_fopen 功能, 无法正常使用此功能'));
}
Helper::addAction(github_git, 'GitHubGit_Action');
return _t('请在插件设置里设置 GitHub 的Git参数') . $error;
}
/**
* 禁用插件方法,如果禁用失败,直接抛出异常
*
* @static
* @access public
* @return void
* @throws Typecho_Plugin_Exception
*/
public static function deactivate()
{
Helper::removeAction(github_git);
}
/**
* 获取插件配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form 配置面板
* @return void
*/
public static function config(Typecho_Widget_Helper_Form $form)
{
$basePath = new Typecho_Widget_Helper_Form_Element_Text('basePath', NULL, '/_posts',
_t('Git目录'), _t('填写需要监控的Git目录')); // 默认为Jekyll的_posts目录
$form->addInput($basePath->addRule('required', _t('必须填写数据库用户名')));
}
/**
* 个人用户的配置面板
*
* @access public
* @param Typecho_Widget_Helper_Form $form
* @return void
*/
public static function personalConfig(Typecho_Widget_Helper_Form $form){}
}

51
GitHubGit/README.md Normal file
View File

@ -0,0 +1,51 @@
Configuration
-------------
### configure security code
Open `Plugin.php` in your editor, find this line:
```php
const github_git = 'curtseyingpiddlesMiguelyeshivahsclarinettists' ;
```
Change `github_git` value to something else.
You can make up anything you like, but please use a long and hard to guess one.
If the value of `github_git` be guessed by someone else, they can post articles to your blog, if they know how to add GitHub web hooks.
After editing, save `Plugin.php`.
### setup GitHub web hook
In your repository, click `Settings` -> `Service Hooks` -> `WebHook URLs`, add the action url, e.g.
```
http://typecho.example.com/action/curtseyingpiddlesMiguelyeshivahsclarinettists
```
Install
-------
Same as other typecho plugins.
That is:
- Upload the `GitHubGit` diretory to `usr/plugins` of your typecho directory.
- Enable the plugin at your dashboard.
Usage
-----
- Add new posts in your git repository.
- Commit and push to GitHub.
Done. Your new post will be published in typecho automatically:
- Use your file name as post title.
- Use your file content as post text.
- Under the default category (which you can change it later).
Note: if you want some format, you need to use html tags.
Markdown support may be added in future version.