1af1904f9SAndreas Gohr<?php 2af1904f9SAndreas Gohr/** 3af1904f9SAndreas Gohr * Translation Plugin: Simple multilanguage plugin 4af1904f9SAndreas Gohr * 5af1904f9SAndreas Gohr * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6af1904f9SAndreas Gohr * @author Andreas Gohr <andi@splitbrain.org> 7af1904f9SAndreas Gohr */ 8af1904f9SAndreas Gohr 9af1904f9SAndreas Gohr// must be run within Dokuwiki 10af1904f9SAndreas Gohrif(!defined('DOKU_INC')) die(); 11af1904f9SAndreas Gohr 12af1904f9SAndreas Gohrclass helper_plugin_translation extends DokuWiki_Plugin { 13af1904f9SAndreas Gohr var $trans = array(); 14af1904f9SAndreas Gohr var $tns = ''; 157c54a0a6SAndreas Gohr var $defaultlang = ''; 16af1904f9SAndreas Gohr 17af1904f9SAndreas Gohr /** 18af1904f9SAndreas Gohr * Initialize 19af1904f9SAndreas Gohr */ 20af1904f9SAndreas Gohr function helper_plugin_translation(){ 217c54a0a6SAndreas Gohr global $conf; 22af1904f9SAndreas Gohr require_once(DOKU_INC.'inc/pageutils.php'); 23af1904f9SAndreas Gohr require_once(DOKU_INC.'inc/utf8.php'); 24af1904f9SAndreas Gohr 25af1904f9SAndreas Gohr // load wanted translation into array 26af1904f9SAndreas Gohr $this->trans = strtolower(str_replace(',',' ',$this->getConf('translations'))); 27af1904f9SAndreas Gohr $this->trans = array_unique(array_filter(explode(' ',$this->trans))); 28af1904f9SAndreas Gohr sort($this->trans); 297c54a0a6SAndreas Gohr 307c54a0a6SAndreas Gohr // get default translation 317c54a0a6SAndreas Gohr if(!$conf['lang_before_translation']){ 327c54a0a6SAndreas Gohr $dfl = $conf['lang']; 337c54a0a6SAndreas Gohr } else { 347c54a0a6SAndreas Gohr $dfl = $conf['lang_before_translation']; 357c54a0a6SAndreas Gohr } 367c54a0a6SAndreas Gohr if(in_array($dfl,$this->trans)){ 377c54a0a6SAndreas Gohr $this->defaultlang = $dfl; 387c54a0a6SAndreas Gohr }else{ 397c54a0a6SAndreas Gohr $this->defaultlang = ''; 40af1904f9SAndreas Gohr array_unshift($this->trans,''); 417c54a0a6SAndreas Gohr } 427c54a0a6SAndreas Gohr 43af1904f9SAndreas Gohr 44af1904f9SAndreas Gohr $this->tns = cleanID($this->getConf('translationns')); 45af1904f9SAndreas Gohr if($this->tns) $this->tns .= ':'; 46af1904f9SAndreas Gohr } 47af1904f9SAndreas Gohr 48af1904f9SAndreas Gohr /** 49af1904f9SAndreas Gohr * return some info 50af1904f9SAndreas Gohr */ 51af1904f9SAndreas Gohr function getInfo(){ 52af1904f9SAndreas Gohr return confToHash(dirname(__FILE__).'/info.txt'); 53af1904f9SAndreas Gohr } 54af1904f9SAndreas Gohr 55af1904f9SAndreas Gohr /** 56af1904f9SAndreas Gohr * Check if the given ID is a translation and return the language code. 57af1904f9SAndreas Gohr */ 58af1904f9SAndreas Gohr function getLangPart($id){ 59af1904f9SAndreas Gohr $rx = '/^'.$this->tns.'('.join('|',$this->trans).'):/'; 60af1904f9SAndreas Gohr if(preg_match($rx,$id,$match)){ 61af1904f9SAndreas Gohr return $match[1]; 62af1904f9SAndreas Gohr } 63af1904f9SAndreas Gohr return ''; 64af1904f9SAndreas Gohr } 65af1904f9SAndreas Gohr 66af1904f9SAndreas Gohr /** 677053cd66SAndreas Gohr * Returns the browser language if it matches with one of the configured 687053cd66SAndreas Gohr * languages 697053cd66SAndreas Gohr */ 707053cd66SAndreas Gohr function getBrowserLang(){ 717053cd66SAndreas Gohr $rx = '/(^|,|:|;|-)('.join('|',$this->trans).')($|,|:|;|-)/i'; 727053cd66SAndreas Gohr if(preg_match($rx,$_SERVER['HTTP_ACCEPT_LANGUAGE'],$match)){ 737053cd66SAndreas Gohr return strtolower($match[2]); 747053cd66SAndreas Gohr } 757053cd66SAndreas Gohr return false; 767053cd66SAndreas Gohr } 777053cd66SAndreas Gohr 787053cd66SAndreas Gohr 797053cd66SAndreas Gohr /** 807c54a0a6SAndreas Gohr * Returns the ID and name to the wanted translation, empty 817c54a0a6SAndreas Gohr * $lng is default lang 82af1904f9SAndreas Gohr */ 83af1904f9SAndreas Gohr function buildTransID($lng,$idpart){ 84af1904f9SAndreas Gohr global $conf; 85af1904f9SAndreas Gohr global $saved_conf; 86af1904f9SAndreas Gohr if($lng){ 87af1904f9SAndreas Gohr $link = ':'.$this->tns.$lng.':'.$idpart; 88af1904f9SAndreas Gohr $name = $lng; 89af1904f9SAndreas Gohr }else{ 90af1904f9SAndreas Gohr $link = ':'.$this->tns.$idpart; 91af1904f9SAndreas Gohr if(!$conf['lang_before_translation']){ 92af1904f9SAndreas Gohr $name = $conf['lang']; 93af1904f9SAndreas Gohr } else { 94af1904f9SAndreas Gohr $name = $conf['lang_before_translation']; 95af1904f9SAndreas Gohr } 96af1904f9SAndreas Gohr } 97af1904f9SAndreas Gohr return array($link,$name); 98af1904f9SAndreas Gohr } 99af1904f9SAndreas Gohr 1001469199dSAndreas Gohr /** 101*84877e9bSAndreas Gohr * Check if current ID should be translated and any GUI 102*84877e9bSAndreas Gohr * should be shown 103*84877e9bSAndreas Gohr */ 104*84877e9bSAndreas Gohr function istranslatable($id,$checkact=true){ 105*84877e9bSAndreas Gohr global $ACT; 106*84877e9bSAndreas Gohr 107*84877e9bSAndreas Gohr if($checkact && $ACT != 'show') return false; 108*84877e9bSAndreas Gohr if($this->tns && strpos($id,$this->tns) !== 0) return false; 109*84877e9bSAndreas Gohr $skiptrans = trim($this->getConf('skiptrans')); 110*84877e9bSAndreas Gohr if($skiptrans && preg_match('/'.$skiptrans.'/ui',':'.$id)) return false; 111*84877e9bSAndreas Gohr $meta = p_get_metadata($id); 112*84877e9bSAndreas Gohr if($meta['plugin']['translation']['notrans']) return false; 113*84877e9bSAndreas Gohr 114*84877e9bSAndreas Gohr return true; 115*84877e9bSAndreas Gohr } 116*84877e9bSAndreas Gohr 117*84877e9bSAndreas Gohr /** 1181469199dSAndreas Gohr * Displays the available and configured translations. Needs to be placed in the template. 1191469199dSAndreas Gohr */ 1201469199dSAndreas Gohr function showTranslations(){ 1211469199dSAndreas Gohr global $ID; 1221469199dSAndreas Gohr global $conf; 1231469199dSAndreas Gohr global $INFO; 1241469199dSAndreas Gohr 125*84877e9bSAndreas Gohr if(!$this->istranslatable($ID)) return; 126*84877e9bSAndreas Gohr 127*84877e9bSAndreas Gohr $this->checkage(); 1281469199dSAndreas Gohr 1291469199dSAndreas Gohr $rx = '/^'.$this->tns.'(('.join('|',$this->trans).'):)?/'; 1301469199dSAndreas Gohr $idpart = preg_replace($rx,'',$ID); 1311469199dSAndreas Gohr 1321469199dSAndreas Gohr $out = '<div class="plugin_translation">'; 1331469199dSAndreas Gohr $out .= '<span>'.$this->getLang('translations'); 1341469199dSAndreas Gohr if($this->getConf('about')){ 1351469199dSAndreas Gohr $out .= '<sup>'.html_wikilink($this->getConf('about'),'?').'</sup>'; 1361469199dSAndreas Gohr } 1371469199dSAndreas Gohr $out .= ':</span> '; 1381469199dSAndreas Gohr 1391469199dSAndreas Gohr if($this->getConf('dropdown')){ // use dropdown 1401469199dSAndreas Gohr if($INFO['exists']){ 1411469199dSAndreas Gohr $class = 'wikilink1'; 1421469199dSAndreas Gohr }else{ 1431469199dSAndreas Gohr $class = 'wikilink2'; 1441469199dSAndreas Gohr } 1451469199dSAndreas Gohr $out .= '<form action="'.wl().'" id="translation__dropdown">'; 1461469199dSAndreas Gohr $out .= '<select name="id" class="'.$class.'">'; 1471469199dSAndreas Gohr foreach($this->trans as $t){ 1481469199dSAndreas Gohr list($link,$name) = $this->buildTransID($t,$idpart); 1491469199dSAndreas Gohr $link = cleanID($link); 1501469199dSAndreas Gohr if($ID == $link){ 1511469199dSAndreas Gohr $sel = ' selected="selected"'; 1521469199dSAndreas Gohr }else{ 1531469199dSAndreas Gohr $sel = ''; 1541469199dSAndreas Gohr } 1551469199dSAndreas Gohr if(page_exists($link,'',false)){ 1561469199dSAndreas Gohr $class = 'wikilink1'; 1571469199dSAndreas Gohr }else{ 1581469199dSAndreas Gohr $class = 'wikilink2'; 1591469199dSAndreas Gohr } 1601469199dSAndreas Gohr $out .= '<option value="'.$link.'"'.$sel.' class="'.$class.'">'.hsc($name).'</option>'; 1611469199dSAndreas Gohr } 1621469199dSAndreas Gohr $out .= '</select>'; 1631469199dSAndreas Gohr $out .= '<input name="go" type="submit" value="→" />'; 1641469199dSAndreas Gohr $out .= '</form>'; 1651469199dSAndreas Gohr }else{ // use list 1661469199dSAndreas Gohr $out .= '<ul>'; 1671469199dSAndreas Gohr foreach($this->trans as $t){ 1681469199dSAndreas Gohr list($link,$name) = $this->buildTransID($t,$idpart); 1691469199dSAndreas Gohr $out .= ' <li><div class="li">'.html_wikilink($link,$name).'</div></li>'; 1701469199dSAndreas Gohr } 1711469199dSAndreas Gohr $out .= '</ul>'; 1721469199dSAndreas Gohr } 1731469199dSAndreas Gohr 1741469199dSAndreas Gohr $out .= '</div>'; 1751469199dSAndreas Gohr 1761469199dSAndreas Gohr return $out; 1771469199dSAndreas Gohr } 178af1904f9SAndreas Gohr 179*84877e9bSAndreas Gohr /** 180*84877e9bSAndreas Gohr * Checks if the current page is a translation of a page 181*84877e9bSAndreas Gohr * in the default language. Displays a notice when it is 182*84877e9bSAndreas Gohr * older than the original page. Tries to lin to a diff 183*84877e9bSAndreas Gohr * with changes on the original since the translation 184*84877e9bSAndreas Gohr */ 185*84877e9bSAndreas Gohr function checkage(){ 186*84877e9bSAndreas Gohr global $ID; 187*84877e9bSAndreas Gohr global $INFO; 188*84877e9bSAndreas Gohr if(!$this->getConf('checkage')) return; 189*84877e9bSAndreas Gohr if(!$INFO['exists']) return; 190*84877e9bSAndreas Gohr $lng = $this->getLangPart($ID); 191*84877e9bSAndreas Gohr if($lng == $this->defaultlang) return; 192af1904f9SAndreas Gohr 193*84877e9bSAndreas Gohr $rx = '/^'.$this->tns.'(('.join('|',$this->trans).'):)?/'; 194*84877e9bSAndreas Gohr $idpart = preg_replace($rx,'',$ID); 195*84877e9bSAndreas Gohr 196*84877e9bSAndreas Gohr // compare modification times 197*84877e9bSAndreas Gohr list($orig,$name) = $this->buildTransID($this->defaultlang,$idpart); 198*84877e9bSAndreas Gohr $origfn = wikiFN($orig); 199*84877e9bSAndreas Gohr if($INFO['lastmod'] >= @filemtime($origfn) ) return; 200*84877e9bSAndreas Gohr 201*84877e9bSAndreas Gohr // get revision from before translation 202*84877e9bSAndreas Gohr $orev = 0; 203*84877e9bSAndreas Gohr $revs = getRevisions($orig,0,100); 204*84877e9bSAndreas Gohr foreach($revs as $rev){ 205*84877e9bSAndreas Gohr if($rev < $INFO['lastmod']){ 206*84877e9bSAndreas Gohr $orev = $rev; 207*84877e9bSAndreas Gohr break; 208*84877e9bSAndreas Gohr } 209*84877e9bSAndreas Gohr } 210*84877e9bSAndreas Gohr 211*84877e9bSAndreas Gohr // build the message and display it 212*84877e9bSAndreas Gohr $msg = sprintf($this->getLang('outdated'),wl($orig)); 213*84877e9bSAndreas Gohr if($orev){ 214*84877e9bSAndreas Gohr $msg .= sprintf(' '.$this->getLang('diff'), 215*84877e9bSAndreas Gohr wl($orig,array('do'=>'diff','rev'=>$orev))); 216*84877e9bSAndreas Gohr } 217*84877e9bSAndreas Gohr msg($msg,2); 218*84877e9bSAndreas Gohr } 219af1904f9SAndreas Gohr} 220