r72434 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r72433‎ | r72434 | r72435 >
Date:14:33, 5 September 2010
Author:nikerabbit
Status:ok
Tags:
Comment:
Documentation updates
Modified paths:
  • /trunk/extensions/Translate/TranslateUtils.php (modified) (history)
  • /trunk/extensions/Translate/scripts/list-mwext-i18n-files.php (modified) (history)
  • /trunk/extensions/Translate/scripts/tm-export.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/scripts/list-mwext-i18n-files.php
@@ -18,6 +18,7 @@
1919 }
2020 require_once( "$IP/maintenance/Maintenance.php" );
2121
 22+/// Script which lists required i18n files for mediawiki extensions.
2223 class MWExtFileList extends Maintenance {
2324 public function __construct() {
2425 parent::__construct();
Index: trunk/extensions/Translate/scripts/tm-export.php
@@ -17,6 +17,7 @@
1818 }
1919 require_once( "$IP/maintenance/Maintenance.php" );
2020
 21+/// Script to bootstrap translatetoolkit translation memory
2122 class TMExport extends Maintenance {
2223 public function __construct() {
2324 parent::__construct();
Index: trunk/extensions/Translate/TranslateUtils.php
@@ -9,9 +9,10 @@
1010 */
1111
1212 /**
13 - * @todo Needs documentation.
 13+ * Essentially random collection of helper functions, similar to GlobalFunctions.php.
1414 */
1515 class TranslateUtils {
 16+ /// @todo Get rid of this constant.
1617 const MSG = 'translate-';
1718
1819 /**
@@ -24,9 +25,7 @@
2526 public static function title( $message, $code ) {
2627 global $wgContLang;
2728
28 - /**
29 - * Cache some amount of titles for speed.
30 - */
 29+ // Cache some amount of titles for speed.
3130 static $cache = array();
3231
3332 if ( !isset( $cache[$message] ) ) {
@@ -40,6 +39,12 @@
4140 }
4241 }
4342
 43+ /**
 44+ * Splits page name into message key and language code.
 45+ * @param $text \string
 46+ * @return \type{Tuple{String,String}} Key and language code.
 47+ * @todo Handle names without slash.
 48+ */
4449 public static function figureMessage( $text ) {
4550 $pos = strrpos( $text, '/' );
4651 $code = substr( $text, $pos + 1 );
@@ -48,6 +53,13 @@
4954 return array( $key, $code );
5055 }
5156
 57+ /**
 58+ * Loads page content *without* side effects.
 59+ * @param $key \string Message key.
 60+ * @param $language \string Language code.
 61+ * @param $namespace \ing Namespace number.
 62+ * @return \types{\string,\null} The contents or null.
 63+ */
5264 public static function getMessageContent( $key, $language,
5365 $namespace = NS_MEDIAWIKI ) {
5466
@@ -58,10 +70,12 @@
5971 }
6072
6173 /**
62 - * Fetches contents for titles in given namespace
 74+ * Fetches contents for pagenames in given namespace without side effects.
6375 *
64 - * @param $titles Mixed String or array of titles.
65 - * @param $namespace Mixed The number of the namespace to look in for.
 76+ * @param $titles \types{String,\list{String}} Database page names.
 77+ * @param $namespace \int The number of the namespace.
 78+ * @return \arrayof{\String,\type{Tuple{String,String}}} Tuples of page
 79+ * text and last author indexed by page name.
6680 */
6781 public static function getContents( $titles, $namespace ) {
6882 $dbr = wfGetDB( DB_SLAVE );
@@ -93,8 +107,8 @@
94108 * Fetches recent changes for titles in given namespaces
95109 *
96110 * @param $hours \int Number of hours.
97 - * @param $bots \bool Should bot edits be included.
98 - * @param $ns \array Array of namespace IDs.
 111+ * @param $bots \bool Should bot edits be included.
 112+ * @param $ns \list{Integer} List of namespace IDs.
99113 * @return \array List of recent changes.
100114 */
101115 public static function translationChanges( $hours = 24, $bots = false, $ns = null ) {
@@ -119,9 +133,7 @@
120134
121135 $res = $dbr->query( $sql, __METHOD__ );
122136
123 - /**
124 - * Fetch results, prepare a batch link existence check query.
125 - */
 137+ // Fetch results, prepare a batch link existence check query.
126138 $rows = array();
127139 while ( $row = $dbr->fetchObject( $res ) ) {
128140 $rows[] = $row;
@@ -133,10 +145,23 @@
134146
135147 /* Some other helpers for ouput*/
136148
 149+ /**
 150+ * Makes a selector from name and options.
 151+ * @param $name \string
 152+ * @param $options \List{String} Html \<option> elements.
 153+ * @return \string Html.
 154+ */
137155 public static function selector( $name, $options ) {
138156 return Xml::tags( 'select', array( 'name' => $name, 'id' => $name ), $options );
139157 }
140158
 159+ /**
 160+ * Makes a selector from name and options.
 161+ * @param $name \string
 162+ * @param $options \list{String} The name and value of options.
 163+ * @param $selected \string The default selected value.
 164+ * @return \string Html.
 165+ */
141166 public static function simpleSelector( $name, $items, $selected ) {
142167 $options = array();
143168
@@ -148,6 +173,13 @@
149174 return self::selector( $name, implode( "\n", $options ) );
150175 }
151176
 177+ /**
 178+ * Returns a localised language name.
 179+ * @param $code \string Language code.
 180+ * @param $native \string Use only native names.
 181+ * @param $language \string Language code of language the the name should be in.
 182+ * @return \string Best-effort localisation of wanted language name.
 183+ */
152184 public static function getLanguageName( $code, $native = false, $language = 'en' ) {
153185 if ( !$native && is_callable( array( 'LanguageNames', 'getNames' ) ) ) {
154186 $languages = LanguageNames::getNames( $language ,
@@ -163,21 +195,15 @@
164196
165197 $parts1 = isset( $parts[1] ) ? $parts[1] : '';
166198
167 - /**
168 - * @todo Add missing scripts that are in use (deva, arab).
169 - */
 199+ /// @todo Add missing scripts that are in use (deva, arab).
170200 switch ( $parts1 ) {
171201 case 'latn':
172 - /**
173 - * @todo i18n.
174 - */
 202+ /// @todo i18n.
175203 $suffix = ' (Latin)';
176204 unset( $parts[1] );
177205 break;
178206 case 'cyrl':
179 - /**
180 - * @todo i18n.
181 - */
 207+ /// @todo i18n.
182208 $suffix = ' (Cyrillic)';
183209 unset( $parts[1] );
184210 break;
@@ -187,6 +213,12 @@
188214 return isset( $languages[$code] ) ? $languages[$code] . $suffix : false;
189215 }
190216
 217+ /**
 218+ * Returns a language selector.
 219+ * @param $language \string Language code of the language the names should
 220+ * be localised to.
 221+ * @param $selectedId \string The language code that is selected by default.
 222+ */
191223 public static function languageSelector( $language, $selectedId ) {
192224 if ( is_callable( array( 'LanguageNames', 'getNames' ) ) ) {
193225 $languages = LanguageNames::getNames( $language,
@@ -207,16 +239,21 @@
208240 return $selector->getHTML();
209241 }
210242
 243+ /// \array Cached message index.
211244 static $mi = null;
212245
 246+ /**
 247+ * Returns the primary group message belongs to.
 248+ * @param $namespace \int
 249+ * @param $key \string
 250+ * @return \types{\String,\null} Group id or null.
 251+ */
213252 public static function messageKeyToGroup( $namespace, $key ) {
214253 if ( self::$mi === null ) {
215254 self::messageIndex();
216255 }
217256
218 - /**
219 - * Performance hotspot.
220 - */
 257+ // Performance hotspot.
221258 # $normkey = self::normaliseKey( $namespace, $key );
222259 $normkey = str_replace( " ", "_", strtolower( "$namespace:$key" ) );
223260
@@ -229,14 +266,18 @@
230267 return $group;
231268 }
232269
 270+ /**
 271+ * Returns the all the groups message belongs to.
 272+ * @param $namespace \int
 273+ * @param $key \string
 274+ * @return \list{String} Possibly empty list of group ids.
 275+ */
233276 public static function messageKeyToGroups( $namespace, $key ) {
234277 if ( self::$mi === null ) {
235278 self::messageIndex();
236279 }
237280
238 - /**
239 - * Performance hotspot.
240 - */
 281+ // Performance hotspot.
241282 # $normkey = self::normaliseKey( $namespace, $key );
242283 $normkey = str_replace( " ", "_", strtolower( "$namespace:$key" ) );
243284
@@ -247,10 +288,21 @@
248289 }
249290 }
250291
 292+ /**
 293+ * Converts page name and namespace to message index format.
 294+ * @param $namespace \int
 295+ * @param $key \string
 296+ * @return \string
 297+ */
251298 public static function normaliseKey( $namespace, $key ) {
252299 return str_replace( " ", "_", strtolower( "$namespace:$key" ) );
253300 }
254301
 302+
 303+ /**
 304+ * Opens and returns the message index.
 305+ * @return \array or \type{false}
 306+ */
255307 public static function messageIndex() {
256308 $filename = self::cacheFile( 'translate_messageindex.cdb' );
257309
@@ -271,6 +323,13 @@
272324 return $keyToGroup;
273325 }
274326
 327+ /**
 328+ * Constructs a fieldset with contents.
 329+ * @param $legend \string Raw html.
 330+ * @param $contents \string Raw html.
 331+ * @param $attributes \array Html attributes for the fieldset.
 332+ * @return \string Html.
 333+ */
275334 public static function fieldset( $legend, $contents, $attributes = array() ) {
276335 return Xml::openElement( 'fieldset', $attributes ) .
277336 Xml::tags( 'legend', null, $legend ) . $contents .
@@ -296,6 +355,9 @@
297356 return $msg;
298357 }
299358
 359+ /**
 360+ * Injects extension css (only once).
 361+ */
300362 public static function injectCSS() {
301363 static $done = false;
302364
@@ -306,7 +368,6 @@
307369 $done = true;
308370
309371 global $wgOut;
310 -
311372 $wgOut->addExtensionStyle( self::assetPath( 'Translate.css' ) );
312373 }
313374
@@ -322,6 +383,9 @@
323384
324385 /**
325386 * Gets the path for cache files
 387+ * @param $filename \string
 388+ * @return \string Full path.
 389+ * @throws \type{MWException} If cache directory is not configured.
326390 */
327391 public static function cacheFile( $filename ) {
328392 global $wgTranslateCacheDirectory, $wgCacheDirectory;
@@ -337,26 +401,24 @@
338402 return "$dir/$filename";
339403 }
340404
341 - public static function snippet( &$text, $length = 10 ) {
342 - global $wgContLang;
343 -
344 - $snippet = preg_replace( "/[^\p{L}]/u", ' ', $text );
345 - $snippet = preg_replace( "/ {2,}/u", ' ', $snippet );
346 - $snippet = $wgContLang->truncate( $snippet, $length, '' );
347 - $snippet = str_replace( ' ', '_', trim( $snippet ) );
348 -
349 - return $snippet;
350 - }
351405 }
352406
353407 /**
354 - * @todo Needs documentation.
 408+ * Yet another class for building html selectors.
355409 */
356410 class HTMLSelector {
 411+ /// \list{String} \<option> elements.
357412 private $options = array();
 413+ /// \string The selected value.
358414 private $selected = false;
 415+ /// \array Extra html attributes.
359416 private $attributes = array();
360 -
 417+
 418+ /**
 419+ * @param $name \string
 420+ * @param $id \string Default false.
 421+ * @param $selected \string Default false.
 422+ */
361423 public function __construct( $name = false, $id = false, $selected = false ) {
362424 if ( $name ) {
363425 $this->setAttribute( 'name', $name );
@@ -371,20 +433,38 @@
372434 }
373435 }
374436
 437+ /**
 438+ * Set selected value.
 439+ * @param $selected \string Default false.
 440+ */
375441 public function setSelected( $selected ) {
376442 $this->selected = $selected;
377443 }
378444
 445+ /**
 446+ * Set html attribute.
 447+ * @param $name \string Attribute name.
 448+ * @param $value \string Attribute value.
 449+ */
379450 public function setAttribute( $name, $value ) {
380451 $this->attributes[$name] = $value;
381452 }
382453
 454+ /**
 455+ * Add an option.
 456+ * @param $name \string Display name.
 457+ * @param $value \string Option value. Uses $name if not given.
 458+ * @param $selected \string Default selected value. Uses object value if not given.
 459+ */
383460 public function addOption( $name, $value = false, $selected = false ) {
384461 $selected = $selected ? $selected : $this->selected;
385462 $value = $value ? $value : $name;
386463 $this->options[] = Xml::option( $name, $value, $value === $selected );
387464 }
388465
 466+ /**
 467+ * @return \string Html for the selector.
 468+ */
389469 public function getHTML() {
390470 return Xml::tags( 'select', $this->attributes, implode( "\n", $this->options ) );
391471 }

Status & tagging log