r107233 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107232‎ | r107233 | r107234 >
Date:23:08, 24 December 2011
Author:jarry1250
Status:ok (Comments)
Tags:
Comment:
Fix compat with latest SVN; add image preview; improve the range of SVGs that can be handled.
Modified paths:
  • /trunk/extensions/TranslateSvg/SpecialTranslateSvg.php (modified) (history)
  • /trunk/extensions/TranslateSvg/ext.translateSvg.core.js (modified) (history)

Diff [purge]

Index: trunk/extensions/TranslateSvg/SpecialTranslateSvg.php
@@ -14,6 +14,7 @@
1515 private $number = 0;
1616 private $added = array();
1717 private $modified = array();
 18+ private $thumb = null;
1819
1920 function __construct() {
2021 parent::__construct( 'TranslateSvg' );
@@ -33,27 +34,30 @@
3435 } else {
3536 $file = wfFindFile( $title );
3637
37 - if ( $file && $file->exists() ){
 38+ if ( $file && $file->exists() ){
3839 $this->svg = new DOMDocument( '1.0' );
39 - $this->svg->load( $file->getPath() );
 40+ $this->svg->load( $file->getLocalRefPath() );
4041 $this->xpath = new DOMXpath( $this->svg );
4142 $this->xpath->registerNamespace( 'svg', 'http://www.w3.org/2000/svg' );
42 - if( !$this->makeTranslationReady() ){
43 - die( "Could not be made translation ready." ); //TODO: internationalise.
44 - }
45 - $this->extractTranslations();
46 - $this->tidyTranslations();
47 - $params = $request->getQueryValues();
48 - if( count( $params ) > 2 && isset( $params['title'] ) && isset( $params['file'] ) ){
49 - unset( $params['title'] );
50 - $filename = $params['file'];
51 - unset( $params['file'] );
52 - $this->updateTranslations( $params );
53 - $this->updateSVG();
54 - $this->saveSVG( $file->getPath(), $filename );
55 - $file->purgeThumbnails();
 43+ if( $this->makeTranslationReady() ){
 44+ $this->extractTranslations();
 45+ $this->tidyTranslations();
 46+ $params = $request->getQueryValues();
 47+ if( count( $params ) > 2 && isset( $params['title'] ) && isset( $params['file'] ) ){
 48+ unset( $params['title'] );
 49+ $filename = $params['file'];
 50+ unset( $params['file'] );
 51+ $this->updateTranslations( $params );
 52+ $this->updateSVG();
 53+ $this->saveSVG( $file->getLocalRefPath(), $filename );
 54+ $file->purgeThumbnails();
 55+ } else {
 56+ $this->thumb = Linker::makeThumbLinkObj( $title, $file, $label = '', '',
 57+ $align = 'right', array( 'width' => 250, 'height' => 250) );
 58+ $this->printTranslations( $file->getName() );
 59+ }
5660 } else {
57 - $this->printTranslations( $file->getName() );
 61+ $this->getOutput()->addHTML( '<p>This file could not be translated, sorry.</p>' );
5862 }
5963 } else {
6064 $this->getOutput()->setStatusCode( 404 );
@@ -91,7 +95,7 @@
9296 $ancestorswitches = $this->xpath->query( "ancestor::svg:switch", $text );
9397 if( $ancestorswitches->length == 0 ){
9498 $switch = $this->svg->createElement( 'switch' );
95 - $text->parentNode->appendChild( $switch );
 99+ $text->parentNode->insertBefore( $switch, $text );
96100 $switch->appendChild( $text ); //Move node into sibling <switch> element
97101 } else {
98102 if( $text->parentNode->nodeName !== "switch" ){
@@ -111,7 +115,25 @@
112116 //Repair by trimming excess <tspan>s
113117 $attrs = ( $child->hasAttributes() ) ? $child->attributes : array();
114118 foreach ($attrs as $num => $attr){
115 - $text->setAttribute( $attr->name, $attr->value );
 119+ $parentattr = trim( $text->getAttribute( $attr->name ) );
 120+ switch( $attr->name ){
 121+ case 'x':
 122+ case 'y':
 123+ case '':
 124+ $text->setAttribute( $attr->name, $attr->value ); //Overwrite
 125+ break;
 126+ case 'style':
 127+ $merged = $parentattr;
 128+ if( substr( $merged, -1 ) !== ';' ){
 129+ $merged .= ';';
 130+ }
 131+ $merged .= $attr->value;
 132+ break;
 133+ case 'id':
 134+ break; //Ignore
 135+ default:
 136+ return false;
 137+ }
116138 }
117139 $text->appendChild( $child->childNodes->item( 0 ) );
118140 $text->removeChild( $child );
@@ -166,8 +188,14 @@
167189 */
168190 function printTranslations( $filename ){
169191 global $wgScript;
 192+
 193+ $languages = Language::getLanguageNames();
 194+ $languages['fallback'] = wfMsg( 'translatesvg-fallbackdesc');
 195+ $languages['qqq'] = wfMsg( 'translatesvg-qqqdesc' );
 196+
170197 $this->getOutput()->addModules( 'ext.translateSvg' );
171 - $html = Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'specialtranslatesvg' ) ) .
 198+ $html = $this->thumb .
 199+ Html::openElement( 'form', array( 'method' => 'get', 'action' => $wgScript, 'id' => 'specialtranslatesvg' ) ) .
172200 Html::hidden( 'file', $filename ) .
173201 Html::hidden( 'title', $this->getTitle()->getPrefixedText() );
174202
@@ -227,7 +255,7 @@
228256 return $this->translations[$language][$num];
229257 } else {
230258 $fallback = $this->getFallback( $num );
231 - if( preg_match( '/^[0-9 .,]+$/', trim( $fallback['text'] ) ) ){
 259+ if( preg_match( '/^[0-9 .,-]+$/', trim( $fallback['text'] ) ) ){
232260 return $fallback;
233261 } else {
234262 return array( 'text' => '', $fallback['x'], $fallback['y'] );
@@ -257,9 +285,13 @@
258286 foreach( $params as $name=>$value ){
259287 list( $lang, $num, $param ) = explode( '-', $name );
260288 if( !isset( $this->translations[ $lang ][ $num ] ) ){
261 - $this->translations[ $lang ][ $num ] = $this->getFallback( $num );
 289+ if( $lang !== 'qqq' ){
 290+ $this->translations[ $lang ][ $num ] = $this->getFallback( $num );
 291+ } else {
 292+ $this->translations[ $lang ][ $num ] = array();
 293+ }
262294 }
263 - $this->translations[ $lang ][ $num ][$param] = $value;
 295+ $this->translations[ $lang ][ $num ][ $param ] = $value;
264296 }
265297
266298 $reverse = array();
Index: trunk/extensions/TranslateSvg/ext.translateSvg.core.js
@@ -91,7 +91,7 @@
9292
9393 function getExisting( fallback ){
9494 //No need to check for an existing translation since we're creating a new language box
95 - if( fallback.match( /^[0-9 .,]+$/ ) ){
 95+ if( fallback.match( /^[0-9 .,-]+$/ ) ){
9696 return fallback;
9797 } else {
9898 return '';

Comments

#Comment by Nikerabbit (talk | contribs)   11:18, 25 December 2011

You should consider using early returns to reduce the indenting:

if ( some prerequisite ) {
  display error();
  return;
}

Sometimes you need to split the methods into smaller ones to utilize that pattern.

Status & tagging log