Index: trunk/extensions/Translate/scripts/list-mwext-i18n-files.php |
— | — | @@ -18,6 +18,7 @@ |
19 | 19 | } |
20 | 20 | require_once( "$IP/maintenance/Maintenance.php" ); |
21 | 21 | |
| 22 | +/// Script which lists required i18n files for mediawiki extensions. |
22 | 23 | class MWExtFileList extends Maintenance { |
23 | 24 | public function __construct() { |
24 | 25 | parent::__construct(); |
Index: trunk/extensions/Translate/scripts/tm-export.php |
— | — | @@ -17,6 +17,7 @@ |
18 | 18 | } |
19 | 19 | require_once( "$IP/maintenance/Maintenance.php" ); |
20 | 20 | |
| 21 | +/// Script to bootstrap translatetoolkit translation memory |
21 | 22 | class TMExport extends Maintenance { |
22 | 23 | public function __construct() { |
23 | 24 | parent::__construct(); |
Index: trunk/extensions/Translate/TranslateUtils.php |
— | — | @@ -9,9 +9,10 @@ |
10 | 10 | */ |
11 | 11 | |
12 | 12 | /** |
13 | | - * @todo Needs documentation. |
| 13 | + * Essentially random collection of helper functions, similar to GlobalFunctions.php. |
14 | 14 | */ |
15 | 15 | class TranslateUtils { |
| 16 | + /// @todo Get rid of this constant. |
16 | 17 | const MSG = 'translate-'; |
17 | 18 | |
18 | 19 | /** |
— | — | @@ -24,9 +25,7 @@ |
25 | 26 | public static function title( $message, $code ) { |
26 | 27 | global $wgContLang; |
27 | 28 | |
28 | | - /** |
29 | | - * Cache some amount of titles for speed. |
30 | | - */ |
| 29 | + // Cache some amount of titles for speed. |
31 | 30 | static $cache = array(); |
32 | 31 | |
33 | 32 | if ( !isset( $cache[$message] ) ) { |
— | — | @@ -40,6 +39,12 @@ |
41 | 40 | } |
42 | 41 | } |
43 | 42 | |
| 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 | + */ |
44 | 49 | public static function figureMessage( $text ) { |
45 | 50 | $pos = strrpos( $text, '/' ); |
46 | 51 | $code = substr( $text, $pos + 1 ); |
— | — | @@ -48,6 +53,13 @@ |
49 | 54 | return array( $key, $code ); |
50 | 55 | } |
51 | 56 | |
| 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 | + */ |
52 | 64 | public static function getMessageContent( $key, $language, |
53 | 65 | $namespace = NS_MEDIAWIKI ) { |
54 | 66 | |
— | — | @@ -58,10 +70,12 @@ |
59 | 71 | } |
60 | 72 | |
61 | 73 | /** |
62 | | - * Fetches contents for titles in given namespace |
| 74 | + * Fetches contents for pagenames in given namespace without side effects. |
63 | 75 | * |
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. |
66 | 80 | */ |
67 | 81 | public static function getContents( $titles, $namespace ) { |
68 | 82 | $dbr = wfGetDB( DB_SLAVE ); |
— | — | @@ -93,8 +107,8 @@ |
94 | 108 | * Fetches recent changes for titles in given namespaces |
95 | 109 | * |
96 | 110 | * @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. |
99 | 113 | * @return \array List of recent changes. |
100 | 114 | */ |
101 | 115 | public static function translationChanges( $hours = 24, $bots = false, $ns = null ) { |
— | — | @@ -119,9 +133,7 @@ |
120 | 134 | |
121 | 135 | $res = $dbr->query( $sql, __METHOD__ ); |
122 | 136 | |
123 | | - /** |
124 | | - * Fetch results, prepare a batch link existence check query. |
125 | | - */ |
| 137 | + // Fetch results, prepare a batch link existence check query. |
126 | 138 | $rows = array(); |
127 | 139 | while ( $row = $dbr->fetchObject( $res ) ) { |
128 | 140 | $rows[] = $row; |
— | — | @@ -133,10 +145,23 @@ |
134 | 146 | |
135 | 147 | /* Some other helpers for ouput*/ |
136 | 148 | |
| 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 | + */ |
137 | 155 | public static function selector( $name, $options ) { |
138 | 156 | return Xml::tags( 'select', array( 'name' => $name, 'id' => $name ), $options ); |
139 | 157 | } |
140 | 158 | |
| 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 | + */ |
141 | 166 | public static function simpleSelector( $name, $items, $selected ) { |
142 | 167 | $options = array(); |
143 | 168 | |
— | — | @@ -148,6 +173,13 @@ |
149 | 174 | return self::selector( $name, implode( "\n", $options ) ); |
150 | 175 | } |
151 | 176 | |
| 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 | + */ |
152 | 184 | public static function getLanguageName( $code, $native = false, $language = 'en' ) { |
153 | 185 | if ( !$native && is_callable( array( 'LanguageNames', 'getNames' ) ) ) { |
154 | 186 | $languages = LanguageNames::getNames( $language , |
— | — | @@ -163,21 +195,15 @@ |
164 | 196 | |
165 | 197 | $parts1 = isset( $parts[1] ) ? $parts[1] : ''; |
166 | 198 | |
167 | | - /** |
168 | | - * @todo Add missing scripts that are in use (deva, arab). |
169 | | - */ |
| 199 | + /// @todo Add missing scripts that are in use (deva, arab). |
170 | 200 | switch ( $parts1 ) { |
171 | 201 | case 'latn': |
172 | | - /** |
173 | | - * @todo i18n. |
174 | | - */ |
| 202 | + /// @todo i18n. |
175 | 203 | $suffix = ' (Latin)'; |
176 | 204 | unset( $parts[1] ); |
177 | 205 | break; |
178 | 206 | case 'cyrl': |
179 | | - /** |
180 | | - * @todo i18n. |
181 | | - */ |
| 207 | + /// @todo i18n. |
182 | 208 | $suffix = ' (Cyrillic)'; |
183 | 209 | unset( $parts[1] ); |
184 | 210 | break; |
— | — | @@ -187,6 +213,12 @@ |
188 | 214 | return isset( $languages[$code] ) ? $languages[$code] . $suffix : false; |
189 | 215 | } |
190 | 216 | |
| 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 | + */ |
191 | 223 | public static function languageSelector( $language, $selectedId ) { |
192 | 224 | if ( is_callable( array( 'LanguageNames', 'getNames' ) ) ) { |
193 | 225 | $languages = LanguageNames::getNames( $language, |
— | — | @@ -207,16 +239,21 @@ |
208 | 240 | return $selector->getHTML(); |
209 | 241 | } |
210 | 242 | |
| 243 | + /// \array Cached message index. |
211 | 244 | static $mi = null; |
212 | 245 | |
| 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 | + */ |
213 | 252 | public static function messageKeyToGroup( $namespace, $key ) { |
214 | 253 | if ( self::$mi === null ) { |
215 | 254 | self::messageIndex(); |
216 | 255 | } |
217 | 256 | |
218 | | - /** |
219 | | - * Performance hotspot. |
220 | | - */ |
| 257 | + // Performance hotspot. |
221 | 258 | # $normkey = self::normaliseKey( $namespace, $key ); |
222 | 259 | $normkey = str_replace( " ", "_", strtolower( "$namespace:$key" ) ); |
223 | 260 | |
— | — | @@ -229,14 +266,18 @@ |
230 | 267 | return $group; |
231 | 268 | } |
232 | 269 | |
| 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 | + */ |
233 | 276 | public static function messageKeyToGroups( $namespace, $key ) { |
234 | 277 | if ( self::$mi === null ) { |
235 | 278 | self::messageIndex(); |
236 | 279 | } |
237 | 280 | |
238 | | - /** |
239 | | - * Performance hotspot. |
240 | | - */ |
| 281 | + // Performance hotspot. |
241 | 282 | # $normkey = self::normaliseKey( $namespace, $key ); |
242 | 283 | $normkey = str_replace( " ", "_", strtolower( "$namespace:$key" ) ); |
243 | 284 | |
— | — | @@ -247,10 +288,21 @@ |
248 | 289 | } |
249 | 290 | } |
250 | 291 | |
| 292 | + /** |
| 293 | + * Converts page name and namespace to message index format. |
| 294 | + * @param $namespace \int |
| 295 | + * @param $key \string |
| 296 | + * @return \string |
| 297 | + */ |
251 | 298 | public static function normaliseKey( $namespace, $key ) { |
252 | 299 | return str_replace( " ", "_", strtolower( "$namespace:$key" ) ); |
253 | 300 | } |
254 | 301 | |
| 302 | + |
| 303 | + /** |
| 304 | + * Opens and returns the message index. |
| 305 | + * @return \array or \type{false} |
| 306 | + */ |
255 | 307 | public static function messageIndex() { |
256 | 308 | $filename = self::cacheFile( 'translate_messageindex.cdb' ); |
257 | 309 | |
— | — | @@ -271,6 +323,13 @@ |
272 | 324 | return $keyToGroup; |
273 | 325 | } |
274 | 326 | |
| 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 | + */ |
275 | 334 | public static function fieldset( $legend, $contents, $attributes = array() ) { |
276 | 335 | return Xml::openElement( 'fieldset', $attributes ) . |
277 | 336 | Xml::tags( 'legend', null, $legend ) . $contents . |
— | — | @@ -296,6 +355,9 @@ |
297 | 356 | return $msg; |
298 | 357 | } |
299 | 358 | |
| 359 | + /** |
| 360 | + * Injects extension css (only once). |
| 361 | + */ |
300 | 362 | public static function injectCSS() { |
301 | 363 | static $done = false; |
302 | 364 | |
— | — | @@ -306,7 +368,6 @@ |
307 | 369 | $done = true; |
308 | 370 | |
309 | 371 | global $wgOut; |
310 | | - |
311 | 372 | $wgOut->addExtensionStyle( self::assetPath( 'Translate.css' ) ); |
312 | 373 | } |
313 | 374 | |
— | — | @@ -322,6 +383,9 @@ |
323 | 384 | |
324 | 385 | /** |
325 | 386 | * 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. |
326 | 390 | */ |
327 | 391 | public static function cacheFile( $filename ) { |
328 | 392 | global $wgTranslateCacheDirectory, $wgCacheDirectory; |
— | — | @@ -337,26 +401,24 @@ |
338 | 402 | return "$dir/$filename"; |
339 | 403 | } |
340 | 404 | |
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 | | - } |
351 | 405 | } |
352 | 406 | |
353 | 407 | /** |
354 | | - * @todo Needs documentation. |
| 408 | + * Yet another class for building html selectors. |
355 | 409 | */ |
356 | 410 | class HTMLSelector { |
| 411 | + /// \list{String} \<option> elements. |
357 | 412 | private $options = array(); |
| 413 | + /// \string The selected value. |
358 | 414 | private $selected = false; |
| 415 | + /// \array Extra html attributes. |
359 | 416 | private $attributes = array(); |
360 | | - |
| 417 | + |
| 418 | + /** |
| 419 | + * @param $name \string |
| 420 | + * @param $id \string Default false. |
| 421 | + * @param $selected \string Default false. |
| 422 | + */ |
361 | 423 | public function __construct( $name = false, $id = false, $selected = false ) { |
362 | 424 | if ( $name ) { |
363 | 425 | $this->setAttribute( 'name', $name ); |
— | — | @@ -371,20 +433,38 @@ |
372 | 434 | } |
373 | 435 | } |
374 | 436 | |
| 437 | + /** |
| 438 | + * Set selected value. |
| 439 | + * @param $selected \string Default false. |
| 440 | + */ |
375 | 441 | public function setSelected( $selected ) { |
376 | 442 | $this->selected = $selected; |
377 | 443 | } |
378 | 444 | |
| 445 | + /** |
| 446 | + * Set html attribute. |
| 447 | + * @param $name \string Attribute name. |
| 448 | + * @param $value \string Attribute value. |
| 449 | + */ |
379 | 450 | public function setAttribute( $name, $value ) { |
380 | 451 | $this->attributes[$name] = $value; |
381 | 452 | } |
382 | 453 | |
| 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 | + */ |
383 | 460 | public function addOption( $name, $value = false, $selected = false ) { |
384 | 461 | $selected = $selected ? $selected : $this->selected; |
385 | 462 | $value = $value ? $value : $name; |
386 | 463 | $this->options[] = Xml::option( $name, $value, $value === $selected ); |
387 | 464 | } |
388 | 465 | |
| 466 | + /** |
| 467 | + * @return \string Html for the selector. |
| 468 | + */ |
389 | 469 | public function getHTML() { |
390 | 470 | return Xml::tags( 'select', $this->attributes, implode( "\n", $this->options ) ); |
391 | 471 | } |