Skip to content

Commit

Permalink
re-think linkId->url conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
ssahara committed Jun 11, 2014
1 parent 94b234c commit 602cd5d
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 58 deletions.
8 changes: 8 additions & 0 deletions conf/default.php
@@ -0,0 +1,8 @@
<?php
/*
* DokuWiki Plugin Gview, default configuration settings
*
*/

$conf['alternative_mediadir'] = '/_media/';
$conf['debug'] = 0;
8 changes: 8 additions & 0 deletions conf/metadata.php
@@ -0,0 +1,8 @@
<?php
/*
* DokuWiki Plugin Gview, configuration metadata
*
*/

$meta['alternative_mediadir'] = array('string');
$meta['debug'] = array('onoff');
135 changes: 77 additions & 58 deletions syntax.php
Expand Up @@ -22,25 +22,88 @@
require_once DOKU_PLUGIN.'syntax.php';

class syntax_plugin_gview extends DokuWiki_Syntax_Plugin {

// URL of Google Docs Viwer Service
const URLgoogleViwer = 'https://docs.google.com/viewer';

public function getType() { return 'substition'; }
public function getPType() { return 'normal'; }
public function getSort() { return 305; }
public function connectTo($mode) {
$this->Lexer->addSpecialPattern('{{gview.*?\>.*?}}',$mode,'plugin_gview');
$this->Lexer->addSpecialPattern('{{gview.*?>.*?}}',$mode,'plugin_gview');

// trick pattern :-)
$this->Lexer->addSpecialPattern('{{obj:.*?\>.*?}}',$mode,'plugin_gview');
$this->Lexer->addSpecialPattern('{{obj:.*?>.*?}}',$mode,'plugin_gview');
}


/**
* show syntax in preview mode
* Resolve media URLs
* Create Media Link from DokuWiki media id considering $conf['userewrite'] value.
* @see function ml() in inc/common.php
*
* @param (string) $linkId mediaId
* @return (string) URL that does NOT contain DOKU_URL
*/
private function _show_usage() {
$syntax ='{{gview [size] [noembed] [noreference] > mediaID|title }}';
msg('Gview plugin usage: '.$syntax,-1);
private function _resolveMediaUrl($linkId = '') {
global $ACT, $ID, $conf;

// external URLs are always direct without rewriting
if(preg_match('#^https?://#i', $linkId)) { return $linkId; }

resolve_mediaid(getNS($ID), $linkId, $exists);
if (!$exists && ($ACT=='preview')) {
msg($this->getPluginName().': media file not exists: '.$linkId, -1);
return false;
}
// check access control
if (!media_ispublic($linkId) && ($ACT=='preview')) {
msg($this->getPluginName().': '.$linkId.' is NOT public!', 2);
}
// check MIME setting of DokuWiki - mime.conf/mime.local.conf
// Embedding will fail if the media file is to be force_download.
list($ext, $mtype, $force_download) = mimetype($linkId);
if (!$force_download) {
switch ($conf['userewrite']){
case 0: // No URL rewriting
$mediapath = 'lib/exe/fetch.php?media='.$linkId;
break;
case 1: // serverside rewiteing eg. .htaccess file
$mediapath = '_media/'.$linkId;
break;
case 2: // DokuWiki rewiteing
$mediapath = 'lib/exe/fetch.php/'.$linkId;
break;
}
} else {
// try alternative url to avoid download dialog.
//
// !!! EXPERIMENTAL : WEB SITE SPECIFIC FEATURE !!!
// we assume "DOKU_URL/_media" directory
// which physically mapped or linked to
// your DW_DATA_PATH/media directory.
// WebServer solution includes htpd.conf, IIS virtual directory.
// Symbolic link or Junction are Filesystem solution.
// Example:
// if linux: ln -s DW_DATA_PATH/media _media
// if iis6(Win2003S): linkd.exe _media DW_DATA_PATH/media
// if iis7(Win2008S): mklink.exe /d _media DW_DATA_PATH/media
//

$altMediaBaseDir = $this->getConf('alternative_mediadir');
if (empty($altMediaBaseDir)) $altMediaBaseDir ='/';
if ($linkId[0] == ':') $linkId = substr($linkId, 1);
$mediapath = $altMediaBaseDir . str_replace(':','/',$linkId);
if ($ACT=='preview') {
msg($this->getPluginName().': alternative url ('.$mediapath.') will be used for '.$linkId, 2);
}
}
// $mediapath does not contain "http://" and hostname
return $mediapath;
}



/**
* handle syntax
*/
Expand All @@ -57,7 +120,10 @@ public function handle($match, $state, $pos, &$handler){
//'border' => false,
);

list($params, $media) = explode('>',$match,2);
list($params, $media) = explode('>', trim($match,'{}'), 2);

// handle media parameters (linkId and title)
list($linkId, $title) = explode('|', $media, 2);

// handle viewer parameters
// split phrase of parameters by white space
Expand Down Expand Up @@ -107,17 +173,7 @@ public function handle($match, $state, $pos, &$handler){
}
}

// handle media parameters (ID and title)
$media = trim($media, ' {}');
if ((strpos($media,' ') !== false) && ($opts['class'] =='gview')) {
// likely wrong usage (older syntax used)
$this->_show_usage();
}

if (strpos($media,'|') !== false) {
list($media, $title) = explode('|',$media,2);
}
$opts['id'] = trim($media);
$opts['id'] = trim($linkId);
if (!empty($title)) $opts['title'] = trim($title);

return array($state, $opts);
Expand Down Expand Up @@ -145,48 +201,13 @@ public function render($mode, &$renderer, $data) {
return true;
}

/**
* Create Media Link from DokuWiki media id
* as to $conf['userewrite'] parameter.
* @see function ml() in inc/common.php
*/
private function _suboptimal_ml($id ='') {
global $conf;
// external URLs are always direct without rewriting
if(preg_match('#^(https?|ftp)://#i', $id)) {
return $id;
}
if ($id === '') $id = $conf['start'];
$id = idfilter($id);
$xlink = DOKU_URL;
if ($conf['userewrite'] == 1) {
// rewrite module enabled in your web server
$xlink .= $id;
} else {
// !!! EXPERIMENTAL : WEB SITE SPECIFIC FEATURE !!!
// otherwise, assume "DOKU_URL/_media" directory
// which physically mapped or linked to
// your DW_DATA_PATH/media directory.
// WebServer solution includes htpd.conf, IIS virtual directory.
// Symbolic link or Junction are Filesystem solution.
// Example:
// if linux: ln -s DW_DATA_PATH/media _media
// if iis6(Win2003S): linkd.exe _media DW_DATA_PATH/media
// if iis7(Win2008S): mklink.exe /d _media DW_DATA_PATH/media
//
$xlink .= '_media'; // should be configurable in admin panel?
$xlink .= str_replace(':','/',$id);
}
return $xlink;
}

/**
* Generate html for sytax {{obj:>}}
*/
private function _html_embed($opts) {

// make reference link
$url = $this->_suboptimal_ml($opts['id']);
$url = $this->_resolveMediaUrl($opts['id']);
$referencelink = '<a href="'.$url.'">'.urldecode($url).'</a>';

if (empty($opts['class'])) {
Expand Down Expand Up @@ -221,10 +242,8 @@ private function _html_embed($opts) {
*/
private function _html_embed_gview($opts) {

$viewerurl = 'http://docs.google.com/viewer';

// make reference link
$url = $this->_suboptimal_ml($opts['id']);
$url = DOKU_URL.$this->_resolveMediaUrl($opts['id']);
$referencelink = '<a href="'.$url.'">'.urldecode($url).'</a>';

$html = '<div class="obj_container_gview">'.NL;
Expand All @@ -236,7 +255,7 @@ private function _html_embed_gview($opts) {
$html.= sprintf($this->getLang('reference_msg'), $referencelink);
$html.= '</div>'.NL;
}
$html.= '<iframe src="'.$viewerurl;
$html.= '<iframe src="'.self::URLgoogleViwer;
$html.= '?url='.urlencode($url);
$html.= '&embedded=true"';
$html.= ' style="';
Expand Down

0 comments on commit 602cd5d

Please sign in to comment.