1<?php 2/** 3 * Translation Plugin: Simple multilanguage plugin 4 * 5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html) 6 * @author Andreas Gohr <andi@splitbrain.org> 7 */ 8 9// must be run within Dokuwiki 10if(!defined('DOKU_INC')) die(); 11 12class helper_plugin_translation extends DokuWiki_Plugin { 13 var $trans = array(); 14 var $tns = ''; 15 var $defaultlang = ''; 16 var $LN = array(); // hold native names 17 var $opts = array(); // display options 18 19 /** 20 * Initialize 21 */ 22 function helper_plugin_translation(){ 23 global $conf; 24 require_once(DOKU_INC.'inc/pageutils.php'); 25 require_once(DOKU_INC.'inc/utf8.php'); 26 27 // load wanted translation into array 28 $this->trans = strtolower(str_replace(',',' ',$this->getConf('translations'))); 29 $this->trans = array_unique(array_filter(explode(' ',$this->trans))); 30 sort($this->trans); 31 32 // load language names 33 $this->LN = confToHash(dirname(__FILE__).'/lang/langnames.txt'); 34 35 // display options 36 $this->opts = $this->getConf('display'); 37 $this->opts = explode(',',$this->opts); 38 $this->opts = array_map('trim',$this->opts); 39 $this->opts = array_fill_keys($this->opts, true); 40 41 // get default translation 42 if(!$conf['lang_before_translation']){ 43 $dfl = $conf['lang']; 44 } else { 45 $dfl = $conf['lang_before_translation']; 46 } 47 if(in_array($dfl,$this->trans)){ 48 $this->defaultlang = $dfl; 49 }else{ 50 $this->defaultlang = ''; 51 array_unshift($this->trans,''); 52 } 53 54 $this->tns = cleanID($this->getConf('translationns')); 55 if($this->tns) $this->tns .= ':'; 56 } 57 58 /** 59 * Check if the given ID is a translation and return the language code. 60 */ 61 function getLangPart($id){ 62 list($lng) = $this->getTransParts($id); 63 return $lng; 64 } 65 66 /** 67 * Check if the given ID is a translation and return the language code and 68 * the id part. 69 */ 70 function getTransParts($id){ 71 $rx = '/^'.$this->tns.'('.join('|',$this->trans).'):(.*)/'; 72 if(preg_match($rx,$id,$match)){ 73 return array($match[1],$match[2]); 74 } 75 return array('',$id); 76 } 77 78 /** 79 * Returns the browser language if it matches with one of the configured 80 * languages 81 */ 82 function getBrowserLang(){ 83 $rx = '/(^|,|:|;|-)('.join('|',$this->trans).')($|,|:|;|-)/i'; 84 if(preg_match($rx,$_SERVER['HTTP_ACCEPT_LANGUAGE'],$match)){ 85 return strtolower($match[2]); 86 } 87 return false; 88 } 89 90 /** 91 * Returns the ID and name to the wanted translation, empty 92 * $lng is default lang 93 */ 94 function buildTransID($lng,$idpart){ 95 global $conf; 96 if($lng){ 97 $link = ':'.$this->tns.$lng.':'.$idpart; 98 $name = $lng; 99 }else{ 100 $link = ':'.$this->tns.$idpart; 101 $name = $this->realLC(''); 102 } 103 return array($link,$name); 104 } 105 106 /** 107 * Returns the real language code, even when an empty one is given 108 * (eg. resolves th default language) 109 */ 110 function realLC($lc){ 111 global $conf; 112 if($lc){ 113 return $lc; 114 }elseif(!$conf['lang_before_translation']){ 115 return $conf['lang']; 116 } else { 117 return $conf['lang_before_translation']; 118 } 119 } 120 121 /** 122 * Check if current ID should be translated and any GUI 123 * should be shown 124 */ 125 function istranslatable($id,$checkact=true){ 126 global $ACT; 127 128 if($checkact && $ACT != 'show') return false; 129 if($this->tns && strpos($id,$this->tns) !== 0) return false; 130 $skiptrans = trim($this->getConf('skiptrans')); 131 if($skiptrans && preg_match('/'.$skiptrans.'/ui',':'.$id)) return false; 132 $meta = p_get_metadata($id); 133 if($meta['plugin']['translation']['notrans']) return false; 134 135 return true; 136 } 137 138 /** 139 * Return the (localized) about link 140 */ 141 function showAbout() { 142 global $ID; 143 global $conf; 144 global $INFO; 145 146 $about = $this->getConf('about'); 147 if($this->getConf('localabout')){ 148 list($lc,$idpart) = $this->getTransParts($about); 149 list($about,$name) = $this->buildTransID($conf['lang'],$idpart); #FIXME conf[lang] is wrong 150 $about = cleanID($about); 151 } 152 153 $out = ''; 154 $out .= '<sup>'; 155 $out .= html_wikilink($about,'?'); 156 $out .= '</sup>'; 157 158 return $out; 159 } 160 161 /** 162 * Displays the available and configured translations. Needs to be placed in the template. 163 */ 164 function showTranslations(){ 165 global $ID; 166 global $conf; 167 global $INFO; 168 169 if(!$this->istranslatable($ID)) return; 170 $this->checkage(); 171 172 list($lc,$idpart) = $this->getTransParts($ID); 173 $lang = $this->realLC($lc); 174 175 176 $out = '<div class="plugin_translation">'; 177 178 //show title and about 179 if (isset($this->opts['title'])){ 180 $out .= '<span>'.$this->getLang('translations'); 181 if ($this->getConf('about')) $out .= $this->showAbout(); 182 $out .= ':</span> '; 183 if(isset($this->opts['twolines'])) $out .= '<br />'; 184 } 185 186 // open wrapper 187 if($this->getConf('dropdown')){ 188 // select needs its own styling 189 if($INFO['exists']){ 190 $class = 'wikilink1'; 191 }else{ 192 $class = 'wikilink2'; 193 } 194 if(isset($this->opts['flag'])){ 195 $flag = DOKU_BASE.'lib/plugins/translation/flags/'.hsc($lang).'.gif'; 196 } 197 $out .= '<form action="'.wl().'" id="translation__dropdown">'; 198 if($flag) $out .= '<img src="'.$flag.'" alt="'.hsc($lang).'" height="11" class="'.$class.'" /> '; 199 $out .= '<select name="id" class="'.$class.'">'; 200 }else{ 201 $out .= '<ul>'; 202 } 203 204 // insert items 205 foreach($this->trans as $t){ 206 $out .= $this->getTransItem($t, $idpart); 207 } 208 209 // close wrapper 210 if($this->getConf('dropdown')){ 211 $out .= '</select>'; 212 $out .= '<input name="go" type="submit" value="→" />'; 213 $out .= '</form>'; 214 }else{ 215 $out .= '</ul>'; 216 } 217 218 // show about if not already shown 219 if (!isset($this->opts['title']) && $this->getConf('about')) { 220 $out .= ' '; 221 $out .= $this->showAbout(); 222 } 223 224 $out .= '</div>'; 225 226 return $out; 227 } 228 229 /** 230 * Create the link or option for a single translation 231 * 232 * @param $lc string The language code 233 * @param $idpart string The ID of the translated page 234 * @returns string The item 235 */ 236 function getTransItem($lc, $idpart) { 237 global $ID; 238 global $conf; 239 240 list($link,$lang) = $this->buildTransID($lc,$idpart); 241 $link = cleanID($link); 242 243 244 // class 245 if(page_exists($link,'',false)){ 246 $class = 'wikilink1'; 247 }else{ 248 $class = 'wikilink2'; 249 } 250 251 // local language name 252 if ($this->LN[$lang]){ 253 $localname = $this->LN[$lang]; 254 } else{ 255 $localname = $lang; 256 } 257 258 // current? 259 if($ID == $link){ 260 $sel = ' selected="selected"'; 261 $class .= ' cur'; 262 }else{ 263 $sel = ''; 264 } 265 266 // flag 267 if(isset($this->opts['flag'])){ 268 $flag = DOKU_BASE.'lib/plugins/translation/flags/'.hsc($lang).'.gif'; 269 $style = ' style="background-image: url(\''.$flag.'\')"'; 270 $class .= ' flag'; 271 } 272 273 // what to display as name 274 if(isset($this->opts['name'])){ 275 $display = hsc($localname); 276 if(isset($this->opts['langcode'])) $display .= ' ('.hsc($lang).')'; 277 }elseif(isset($this->opts['langcode'])){ 278 $display = hsc($lang); 279 }else{ 280 $display = ' '; 281 } 282 283 // prepare output 284 $out = ''; 285 if($this->getConf('dropdown')){ 286 $out .= '<option class="'.$class.'" title="'.hsc($localname).'" value="'.$link.'"'.$sel.$style.'>'; 287 $out .= $display; 288 $out .= '</option>'; 289 }else{ 290 $out .= '<li><div class="li">'; 291 $out .= '<a href='.wl($link).' class="'.$class.'" title="'.hsc($localname).'">'; 292 if($flag) $out .= '<img src="'.$flag.'" alt="'.hsc($lang).'" height="11" />'; 293 $out .= $display; 294 $out .= '</a>'; 295 $out .= '</div></li>'; 296 } 297 298 return $out; 299 } 300 301 /** 302 * Checks if the current page is a translation of a page 303 * in the default language. Displays a notice when it is 304 * older than the original page. Tries to lin to a diff 305 * with changes on the original since the translation 306 */ 307 function checkage(){ 308 global $ID; 309 global $INFO; 310 if(!$this->getConf('checkage')) return; 311 if(!$INFO['exists']) return; 312 $lng = $this->getLangPart($ID); 313 if($lng == $this->defaultlang) return; 314 315 $rx = '/^'.$this->tns.'(('.join('|',$this->trans).'):)?/'; 316 $idpart = preg_replace($rx,'',$ID); 317 318 // compare modification times 319 list($orig,$name) = $this->buildTransID($this->defaultlang,$idpart); 320 $origfn = wikiFN($orig); 321 if($INFO['lastmod'] >= @filemtime($origfn) ) return; 322 323 // get revision from before translation 324 $orev = 0; 325 $revs = getRevisions($orig,0,100); 326 foreach($revs as $rev){ 327 if($rev < $INFO['lastmod']){ 328 $orev = $rev; 329 break; 330 } 331 } 332 333 // see if the found revision still exists 334 if($orev && !page_exists($orig,$orev)) $orev=0; 335 336 // build the message and display it 337 $orig = cleanID($orig); 338 $msg = sprintf($this->getLang('outdated'),wl($orig)); 339 if($orev){ 340 $msg .= sprintf(' '.$this->getLang('diff'), 341 wl($orig,array('do'=>'diff','rev'=>$orev))); 342 } 343 344 echo '<div class="notify">'.$msg.'</div>'; 345 } 346} 347