Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
allow to configure the directory where charts are published
  • Loading branch information
Christoph committed Jun 4, 2015
1 parent d4b8975 commit dd4b6ed
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 20 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,9 @@
CHANGELOG
---------

### 1.9.x
* the chart publish directory can now be configured

### 1.9.4
* allow setting of relative chart widths (e.g. 100%)
* reload chart iframe on ctrl-r
Expand Down
5 changes: 5 additions & 0 deletions config.template.yaml
Expand Up @@ -65,6 +65,11 @@ footer_links:
## libraries such as globalize.js.
# asset_domain: assets-datawrapper.s3.amazonaws.com

## This is the directory where locally published files will be stored.
## If you do not define this, the `charts` directory within datawrapper
## will be used.
# publish_directory: /full/path/to/where/charts/should/be/published/

## to use a custom home page in your installation
## uncomment the following setting and create a new
## Twig template (e.g. my-home.twig) in /templates/custom/
Expand Down
2 changes: 1 addition & 1 deletion lib/api/charts.php
Expand Up @@ -170,7 +170,7 @@ function if_chart_exists($id, $callback) {
$sizeLimit = 2 * 1024 * 1024; // byte

$uploader = new qqFileUploader($allowedExtensions, $sizeLimit);
$result = $uploader->handleUpload('../../charts/data/tmp/');
$result = $uploader->handleUpload(chart_publish_directory().'data/tmp/');

// check and correct file encoding
$detect_encoding = function($string) {
Expand Down
6 changes: 3 additions & 3 deletions lib/core/build/classes/datawrapper/Chart.php
Expand Up @@ -95,12 +95,12 @@ protected function uppercaseKeys($arr) {
* get the path where this charts data file is stored
*/
protected function getDataPath() {
$path = ROOT_PATH . 'charts/data/' . $this->getCreatedAt('Ym');
$path = chart_publish_directory() . 'data/' . $this->getCreatedAt('Ym');
return $path;
}

protected function getStaticPath() {
$path = ROOT_PATH . 'charts/static/' . $this->getID();
$path = chart_publish_directory() . 'static/' . $this->getID();
return $path;
}

Expand Down Expand Up @@ -285,7 +285,7 @@ public function publish() {
public function redirectPreviousVersions() {
$current_target = $this->getCDNPath();
$redirect_html = '<html><head><meta http-equiv="REFRESH" content="0; url=/'.$current_target.'"></head></html>';
$redirect_file = ROOT_PATH . 'charts/static/' . $this->getID() . '/redirect.html';
$redirect_file = chart_publish_directory() . 'static/' . $this->getID() . '/redirect.html';
file_put_contents($redirect_file, $redirect_html);
$files = array();
for ($v=0; $v < $this->getPublicVersion(); $v++) {
Expand Down
26 changes: 21 additions & 5 deletions lib/utils/chart_publish.php
Expand Up @@ -54,15 +54,15 @@ function _setPublishStatus($chart, $status) {
if (isset($_GLOBALS['dw-config']['memcache'])) {
$memcache->set('publish-status-' . $chart->getID(), round($status*100));
} else {
file_put_contents(ROOT_PATH . 'charts/tmp/publish-status-' . $chart->getID(), round($status*100));
file_put_contents(chart_publish_directory() . 'tmp/publish-status-' . $chart->getID(), round($status*100));
}
}

function _getPublishStatus($chart) {
if (isset($_GLOBALS['dw-config']['memcache'])) {
return $memcache->get('publish-status-' . $chart->getID());
} else {
$fn = ROOT_PATH . 'charts/tmp/publish-status-' . $chart->getID();
$fn = chart_publish_directory() . 'tmp/publish-status-' . $chart->getID();
if (!file_exists($fn)) return false;
return file_get_contents($fn);
}
Expand All @@ -73,12 +73,12 @@ function _clearPublishStatus($chart) {
global $memcache;
$memcache->delete('publish-status-' . $chart->getID());
} else {
unlink(ROOT_PATH . 'charts/tmp/publish-status-' . $chart->getID());
unlink(chart_publish_directory() . 'tmp/publish-status-' . $chart->getID());
}
}

function get_static_path($chart) {
$static_path = ROOT_PATH . "charts/static/" . $chart->getID();
$static_path = $chart->getStaticPath();
if (!is_dir($static_path)) {
mkdir($static_path);
}
Expand Down Expand Up @@ -113,7 +113,7 @@ function publish_html($user, $chart) {

function publish_js($user, $chart) {
$cdn_files = array();
$static_path = ROOT_PATH . 'charts/static/lib/';
$static_path = chart_publish_directory() . 'static/lib/';
$data = get_chart_content($chart, $user, false, '../');

// generate visualization script
Expand Down Expand Up @@ -271,3 +271,19 @@ function download($url, $outf) {
file_put_contents($outf, $html);
}
}

function chart_publish_directory() {
$dir = ROOT_PATH.'charts';

if (isset($_GLOBALS['dw-config']['publish_directory'])) {
$dir = $_GLOBALS['dw-config']['publish_directory'];
}

if (!is_dir($dir)) {
if (!@mkdir($dir, 0755, true)) {
throw new RuntimeException('Could not create chart publish directory "'.$dir.'". Please create it manually and make sure PHP can write to it.');
}
}

return rtrim(realpath($dir), DIRECTORY_SEPARATOR).DIRECTORY_SEPARATOR;
}
31 changes: 20 additions & 11 deletions lib/utils/check_server.php
Expand Up @@ -3,27 +3,36 @@
/* health check */

function check_path_permissions() {
$paths = array();
$rel = '..';
$paths[] = '/charts/static';
$paths[] = '/charts/data';
$paths[] = '/charts/images';
$paths[] = '/charts/data/tmp';
$paths[] = '/tmp';
$paths = array();
$rel = '..';
$publishRoot = chart_publish_directory();

$paths[] = $publishRoot.'static';
$paths[] = $publishRoot.'data';
$paths[] = $publishRoot.'images';
$paths[] = $publishRoot.'data/tmp';
$paths[] = ROOT_PATH.'tmp';

$err = array();
foreach ($paths as $path) {
if (!is_writable($rel . $path)) $err[] = $path;
if (!is_writable($path)) {
$err[] = $path;
}
}

if (count($err) > 0) {
$msg = '<h2>The following folders on your server need to be writable:</h2><ul>';

foreach ($paths as $path) {
# code...
$msg .= '<li><code>'.dirname(dirname(dirname(__FILE__))).$path.'</code></li>';
$msg .= '<li><code>'.htmlspecialchars($path, ENT_QUOTES, 'UTF-8').'</code></li>';
}

$msg .= '</ul>';
$msg .= 'Read more about <a href="http://codex.wordpress.org/Changing_File_Permissions#Using_an_FTP_Client">how to change file permissions</a>';

return $msg;
}

return '';
}

Expand Down Expand Up @@ -71,7 +80,7 @@ function check_database() {
return '<h2>Database is not initialized or corrupt</h2>'
. '<p>The database could be accessed but seems not be initialized correctly. '
. 'The following tables are missing:</p>'
. '<ul><li><code>' . join('</li></code><li><code>', $missingTables) . '</code></li></ul>'
. '<ul><li><code>' . implode('</li></code><li><code>', $missingTables) . '</code></li></ul>'
. '<p>Have you run the DB initialization in <code>lib/core/build/sql/schema.sql</code>?</p>';
}
return '';
Expand Down

0 comments on commit dd4b6ed

Please sign in to comment.