r72032 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72031‎ | r72032 | r72033 >
Date:17:02, 31 August 2010
Author:roberthl
Status:deferred (Comments)
Tags:
Comment:
Translate:
- Work on magic-export-2.php, now basically works for special page aliases. Concerned about memory usage, but need a large sample set to test.
Modified paths:
  • /trunk/extensions/Translate/scripts/magic-export-2.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/scripts/magic-export-2.php
@@ -3,6 +3,7 @@
44 * Script to export special page aliases and magic words of extensions.
55 *
66 * @author Robert Leverington <robert@rhl.me.uk>
 7+ *
78 * @copyright Copyright © 2010 Robert Leverington
89 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
910 * @file
@@ -45,6 +46,8 @@
4647 }
4748
4849 $langs = Cli::parseLanguageCodes( '*' );
 50+unset( $langs[array_search( 'en', $langs )] );
 51+$langs = array_merge( array( 'en' ), $langs );
4952 $groups = MessageGroups::singleton()->getGroups();
5053
5154 $type = $options['type'] ;
@@ -52,11 +55,9 @@
5356 // Open file handles.
5457 STDOUT( "Opening file handles..." );
5558 $handles = array();
56 -$keys = array(); error_reporting( E_ALL | E_STRICT );
 59+$messagesOld = array();
5760 foreach ( $groups as $group ) {
58 - if ( !$group instanceof ExtensionMessageGroup ) {
59 - continue;
60 - }
 61+ if ( !$group instanceof ExtensionMessageGroup ) continue;
6162
6263 if ( $type === 'special' ) {
6364 $filename = $group->getAliasFile();
@@ -64,21 +65,14 @@
6566 $filename = $group->getMagicFile();
6667 }
6768
68 - if ( $filename === null ) {
69 - continue;
70 - }
 69+ if ( $filename === null ) continue;
7170
7271 $file = "$wgTranslateExtensionDirectory/$filename";
73 - if ( !file_exists( $file ) ) {
74 - continue;
75 - }
 72+ if ( !file_exists( $file ) ) continue;
7673
7774 include( $file );
78 - if ( !isset( $aliases ) ) {
79 - continue;
80 - }
81 -
82 - $keys[$group->getId()] = array_keys( $aliases['en'] );
 75+ if( !isset( $aliases ) ) continue;
 76+ $messagesOld[$group->getId()] = $aliases;
8377 unset( $aliases );
8478
8579 $handles[$group->getId()] = fopen( $options['target'] . '/' . $filename, 'w' );
@@ -86,6 +80,26 @@
8781 STDOUT( "\t{$group->getId()}" );
8882 }
8983
 84+// Write header.
 85+foreach( $handles as $handle ) {
 86+ if( $type === 'special' ) {
 87+ fwrite( $handle, <<<EOT
 88+<?php
 89+
 90+/**
 91+ * Aliases for special pages
 92+ *
 93+ * @file
 94+ * @ingroup Extensions
 95+ */
 96+
 97+\$aliases = array();
 98+EOT
 99+);
 100+ } else {
 101+ }
 102+}
 103+
90104 foreach ( $langs as $l ) {
91105 switch ( $options['type'] ) {
92106 case 'special':
@@ -95,54 +109,63 @@
96110 $title = Title::newFromText( 'MediaWiki:Sp-translate-data-MagicWords/' . $l );
97111 break;
98112 default:
99 - STDERR( "Invalid type: must be one of: special, magic" );
100113 exit( 1 );
101114 }
102115
103 - if ( !$title || !$title->exists() ) {
 116+ if( !$title || !$title->exists() ) {
104117 STDOUT( "Skiping $l..." );
105 - continue;
 118+
 119+ $messagesNew = array();
106120 } else {
107121 STDOUT( "Processing $l..." );
108 - }
109122
110 - $article = new Article( $title );
111 - $data = $article->getContent();
 123+ $article = new Article( $title );
 124+ $data = $article->getContent();
112125
113 - // Parse message file.
114 - $segments = explode( "\n", $data );
115 - array_shift( $segments );
116 - array_shift( $segments );
117 - unset( $segments[count( $segments ) -1] );
118 - unset( $segments[count( $segments ) -1] );
119 -
120 - $messages = array();
121 -
122 - foreach ( $segments as $segment ) {
123 - $parts = explode( '=', $segment );
124 - $key = trim( array_shift( $parts ) );
125 - $translations = explode( ', ', implode( $parts ) );
126 - $messages[$key] = $translations;
 126+ // Parse message file.
 127+ $segments = explode( "\n", $data );
 128+ array_shift( $segments );
 129+ array_shift( $segments );
 130+ unset( $segments[count($segments)-1] );
 131+ unset( $segments[count($segments)-1] );
 132+ $messagesNew = array();
 133+ foreach( $segments as $segment ) {
 134+ $parts = explode( '=', $segment );
 135+ $key = trim( array_shift( $parts ) );
 136+ $translations = explode( ', ', implode( $parts ) );
 137+ $messagesNew[$key] = $translations;
 138+ }
127139 }
128140
129 - // Need to only provide the keys applicable to the file that is being written.
130 - foreach ( $handles as $group => $handle ) {
 141+ foreach( $handles as $group => $handle ) {
131142 STDOUT( "\t{$group}... " );
132 - $thismessages = $messages; // TODO: Reduce.
133 - $out = "\$aliases['{$group} '] = array(\n";
134 -
135 - foreach ( $thismessages as $key => $translations ) {
136 - $translations = implode( "', '", $translations );
137 - $out .= "\t'$key' => array( '$translations' ),\n";
 143+ $namesEn = LanguageNames::getNames( 'en' );
 144+ $namesNative = Language::getLanguageNames();
 145+ $messagesOut = array();
 146+ foreach( $messagesOld[$group]['en'] as $key => $message ) {
 147+ if( array_key_exists( $key, $messagesNew ) ) {
 148+ $messagesOut[$key] = $messagesNew[$key];
 149+ } elseif( isset( $messagesOld[$group][$l][$key] ) ) {
 150+ $messagesOut[$key] = $messagesOld[$group][$l][$key];
 151+ }
138152 }
139 -
140 - $out .= ");\n\n";
141 - fwrite( $handle, $out );
 153+ if( count( $messagesOut ) > 0 ) {
 154+ $out = "\n\n/** {$namesEn[$l]} ({$namesNative[$l]}) */\n\$aliases['{$l}'] = array(\n";
 155+ foreach( $messagesOut as $key => $translations ) {
 156+ foreach( $translations as $id => $translation ) {
 157+ $translations[$id] = addslashes( $translation );
 158+ }
 159+ $translations = implode( "', '", $translations );
 160+ $out .= "\t'$key' => array( '$translations' ),\n";
 161+ }
 162+ $out .= ");";
 163+ fwrite( $handle, $out );
 164+ }
142165 }
143166 }
144167
145168 // Close handles.
146169 STDOUT( "Closing file handles..." );
147 -foreach ( $handles as $group => $handle ) {
 170+foreach( $handles as $group => $handle ) {
148171 fclose( $handle );
149172 }

Comments

#Comment by Raymond (talk | contribs)   17:07, 31 August 2010
-	if ( !$group instanceof ExtensionMessageGroup ) {
-		continue;
-	}
+	if ( !$group instanceof ExtensionMessageGroup ) continue;

Is against our coding style, see http://www.mediawiki.org/wiki/Coding_style#Braceless_control_structures

#Comment by RobertL (talk | contribs)   17:13, 31 August 2010

This code was stylized while still a work in progress, so when I came to update there was a conflict - which I resolved by taking my working copy in full. Although it appears that way, I did not deliberately change the code style back and will re-run stylize.php when this code is complete.

#Comment by Nikerabbit (talk | contribs)   17:16, 31 August 2010

I'm not fully convinced that short single lines are so bad.

Status & tagging log