Known2Ghost/export.php

122 lines
3.5 KiB
PHP
Raw Permalink Normal View History

2017-02-27 14:07:17 +01:00
<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
2017-02-27 14:07:17 +01:00
require_once(dirname(__FILE__) . '/Idno/start.php');
include 'HTML-To-Markdown.php';
2017-02-28 22:26:09 +01:00
$garray = [];
$garray['data'] = [];
$garray['data']['posts'] = [];
$garray['data']['tags'] = [];
$garray['data']['posts_tags'] = [];
$garray['data']['users'] = [];
function metas(&$garray) {
$garray['meta'] = [
'exported_on' => date( 'r' ),
'version' => '000',
];
2017-02-27 14:07:17 +01:00
}
2017-02-28 22:26:09 +01:00
function posts(&$garray) {
$posts = Idno\Common\Entity::getFromAll([], [], 99999);
$slug_number = 0;
$_post_tags = [];
$i = 1;
foreach($posts as $post) {
if ($post instanceof Idno\Entities\Notification) {
continue;
}
2017-02-27 14:07:17 +01:00
$post_markdown = new HTML_To_Markdown( $post->attributes['body'] );
2017-02-27 14:07:17 +01:00
2017-02-28 22:26:09 +01:00
$_post_tags[] = [];
$garray['data']['posts'][] = array(
'id' => $i,
'title' => substr( ( empty( $post->attributes['title'] ) ) ? $post->attributes['body'] : $post->attributes['title'], 0, 150 ),
'slug' => $post->attributes['slug'],
'markdown' => $post_markdown->output(),
'html' => $post->attributes['body'],
'image' => null,
'featured' => 0,
2017-02-28 22:32:40 +01:00
'page' => 0,
2017-02-28 22:26:09 +01:00
'status' => isset($post->attributes['publish_status']) ? substr( $post->attributes['publish_status'], 0, 150 ) : 'published',
'language' => 'fr_FR',
'meta_title' => null,
'meta_description' => null,
'author_id' => 1,
'created_at' => _get_json_date( $post->created ),
'created_by' => 1,
'updated_at' => _get_json_date( $post->updated ),
'updated_by' => 1,
'published_at' => _get_json_date( $post->created ),
'published_by' => 1,
);
$slug_number += 1;
$i++;
}
$garray['data']['posts_tags'] = $_post_tags;
// cleanup
unset( $posts_args );
unset( $posts );
unset( $slug_number );
unset( $_post_tags );
unset( $tags );
unset( $tag );
unset( $s );
}
2017-02-27 14:07:17 +01:00
function _get_json_date( $date ) {
2017-02-28 22:26:09 +01:00
return date( 'r', $date );
}
2017-02-27 14:07:17 +01:00
2017-02-28 22:26:09 +01:00
function populate_data(&$garray) {
// attaches metadata
metas($garray);
// populates posts
posts($garray);
2017-02-27 14:07:17 +01:00
}
/**
* Sends necessary headers and whatnot to allow to download file
*/
function download_file($garray) {
2017-02-27 14:07:17 +01:00
2017-02-28 22:26:09 +01:00
populate_data($garray);
$filedir = dirname(__FILE__).'/Uploads';
$filename = 'known2ghost_export_' . time() . '.json';
if ( ! is_writable( $filedir ) ) {
echo ( "<p>Uploads directory is not writable, can't save the Ghost json file :/</p><p>Generated by the Ghost plugin version {$this->version}.</p>");
}
$handle = fopen( $filedir . '/' . $filename, 'w' );
$content = json_encode( $garray );
fwrite( $handle, $content );
fclose( $handle );
header( 'Content-Description: File Transfer' );
header( 'Content-Type: application/octet-stream' );
header( 'Content-Disposition: attachment; filename='.$filename );
header( 'Content-Transfer-Encoding: binary' );
header( 'Expires: 0' );
header( 'Cache-Control: must-revalidate' );
header( 'Pragma: public' );
header( 'Content-Length: ' . filesize( $filedir . '/' . $filename ) );
flush();
readfile( $filedir . '/' . $filename );
exit;
2017-02-27 14:07:17 +01:00
}
2017-02-28 22:26:09 +01:00
download_file($garray);