Merge "Replace deprecated wfMsg* calls with Message class calls."
[lhc/web/wiklou.git] / maintenance / language / checkLanguage.inc
index 52281b5..11b00e1 100644 (file)
@@ -1,12 +1,34 @@
 <?php
 /**
+ * Helper class for checkLanguage.php script.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
  * @ingroup MaintenanceLanguage
  */
 
+/**
+ * @ingroup MaintenanceLanguage
+ */
 class CheckLanguageCLI {
        protected $code  = null;
        protected $level = 2;
        protected $doLinks = false;
+       protected $linksPrefix = '';
        protected $wikiCode = 'en';
        protected $checkAll = false;
        protected $output = 'plain';
@@ -19,12 +41,12 @@ class CheckLanguageCLI {
 
        /**
         * Constructor.
-        * @param $options Options for script.
+        * @param $options array Options for script.
         */
        public function __construct( Array $options ) {
                if ( isset( $options['help'] ) ) {
                        echo $this->help();
-                       exit();
+                       exit(1);
                }
 
                if ( isset( $options['lang'] ) ) {
@@ -42,6 +64,10 @@ class CheckLanguageCLI {
                $this->includeExif = !isset( $options['noexif'] );
                $this->checkAll = isset( $options['all'] );
 
+               if ( isset( $options['prefix'] ) ) {
+                       $this->linksPrefix = $options['prefix'];
+               }
+
                if ( isset( $options['wikilang'] ) ) {
                        $this->wikiCode = $options['wikilang'];
                }
@@ -68,7 +94,7 @@ class CheckLanguageCLI {
 
        /**
         * Get the default checks.
-        * @return A list of the default checks.
+        * @return array A list of the default checks.
         */
        protected function defaultChecks() {
                return array(
@@ -81,7 +107,7 @@ class CheckLanguageCLI {
 
        /**
         * Get the checks which check other things than messages.
-        * @return A list of the non-message checks.
+        * @return array A list of the non-message checks.
         */
        protected function nonMessageChecks() {
                return array(
@@ -92,7 +118,7 @@ class CheckLanguageCLI {
 
        /**
         * Get the checks that can easily be treated by non-speakers of the language.
-        * @return A list of the easy checks.
+        * @return Array A list of the easy checks.
         */
        protected function easyChecks() {
                return array(
@@ -103,7 +129,7 @@ class CheckLanguageCLI {
 
        /**
         * Get all checks.
-        * @return An array of all check names mapped to their function names.
+        * @return array An array of all check names mapped to their function names.
         */
        protected function getChecks() {
                return array(
@@ -131,7 +157,7 @@ class CheckLanguageCLI {
 
        /**
         * Get total count for each check non-messages check.
-        * @return An array of all check names mapped to a two-element array:
+        * @return array An array of all check names mapped to a two-element array:
         * function name to get the total count and language code or null
         * for checked code.
         */
@@ -150,7 +176,7 @@ class CheckLanguageCLI {
 
        /**
         * Get all check descriptions.
-        * @return An array of all check names mapped to their descriptions.
+        * @return array An array of all check names mapped to their descriptions.
         */
        protected function getDescriptions() {
                return array(
@@ -178,28 +204,35 @@ class CheckLanguageCLI {
 
        /**
         * Get help.
-        * @return The help string.
+        * @return string The help string.
         */
        protected function help() {
                return <<<ENDS
 Run this script to check a specific language file, or all of them.
 Command line settings are in form --parameter[=value].
 Parameters:
-       * lang: Language code (default: the installation default language).
-       * all: Check all customized languages.
-       * help: Show this help.
-       * level: Show the following display level (default: 2).
-       * links: Link the message values (default off).
-       * wikilang: For the links, what is the content language of the wiki to display the output in (default en).
-       * whitelist: Do only the following checks (form: code,code).
-       * blacklist: Don't do the following checks (form: code,code).
-       * easy: Do only the easy checks, which can be treated by non-speakers of the language.
-       * noexif: Don't check for EXIF messages (a bit hard and boring to translate), if you know that they are currently not translated and want to focus on other problems (default off).
+       --help: Show this help.
+       --lang: Language code (default: the installation default language).
+       --all: Check all customized languages.
+       --level: Show the following display level (default: 2):
+               * 0: Skip the checks (useful for checking syntax).
+               * 1: Show only the stub headers and number of wrong messages, without list of messages.
+               * 2: Show only the headers and the message keys, without the message values.
+               * 3: Show both the headers and the complete messages, with both keys and values.
+       --links: Link the message values (default off).
+       --prefix: prefix to add to links.
+       --wikilang: For the links, what is the content language of the wiki to display the output in (default en).
+       --noexif: Do not check for EXIF messages (a bit hard and boring to translate), if you know
+               that they are currently not translated and want to focus on other problems (default off).
+       --whitelist: Do only the following checks (form: code,code).
+       --blacklist: Do not do the following checks (form: code,code).
+       --easy: Do only the easy checks, which can be treated by non-speakers of the language.
+
 Check codes (ideally, all of them should result 0; all the checks are executed by default (except language-specific check blacklists in checkLanguage.inc):
        * untranslated: Messages which are required to translate, but are not translated.
        * duplicate: Messages which translation equal to fallback
        * obsolete: Messages which are untranslatable or do not exist, but are translated.
-       * variables: Messages without variables which should be used, or with variables which shouldn't be used.
+       * variables: Messages without variables which should be used, or with variables which should not be used.
        * empty: Empty messages and messages that contain only -.
        * whitespace: Messages which have trailing whitespace.
        * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
@@ -214,11 +247,6 @@ Check codes (ideally, all of them should result 0; all the checks are executed b
        * magic-case: Magic words whose translation changes the case-sensitivity of the original English word.
        * special: Special page names that were not translated.
        * special-old: Special page names which do not exist.
-Display levels (default: 2):
-       * 0: Skip the checks (useful for checking syntax).
-       * 1: Show only the stub headers and number of wrong messages, without list of messages.
-       * 2: Show only the headers and the message keys, without the message values.
-       * 3: Show both the headers and the complete messages, with both keys and values.
 
 ENDS;
        }
@@ -267,7 +295,7 @@ ENDS;
 
        /**
         * Get the check blacklist.
-        * @return The list of checks which should not be executed.
+        * @return array The list of checks which should not be executed.
         */
        protected function getCheckBlacklist() {
                global $checkBlacklist;
@@ -276,23 +304,23 @@ ENDS;
 
        /**
         * Check a language.
-        * @param $code The language code.
-        * @return The results.
+        * @param $code string The language code.
+        * @return array The results.
         */
        protected function checkLanguage( $code ) {
                # Syntax check only
+               $results = array();
                if ( $this->level === 0 ) {
                        $this->L->getMessages( $code );
-                       return;
+                       return $results;
                }
 
-               $results = array();
                $checkFunctions = $this->getChecks();
                $checkBlacklist = $this->getCheckBlacklist();
                foreach ( $this->checks as $check ) {
                        if ( isset( $checkBlacklist[$code] ) &&
                                in_array( $check, $checkBlacklist[$code] ) ) {
-                               $result[$check] = array();
+                               $results[$check] = array();
                                continue;
                        }
 
@@ -308,17 +336,17 @@ ENDS;
 
        /**
         * Format a message key.
-        * @param $key The message key.
-        * @param $code The language code.
-        * @return The formatted message key.
+        * @param $key string The message key.
+        * @param $code string The language code.
+        * @return string The formatted message key.
         */
        protected function formatKey( $key, $code ) {
                if ( $this->doLinks ) {
                        $displayKey = ucfirst( $key );
                        if ( $code == $this->wikiCode ) {
-                               return "[[MediaWiki:$displayKey|$key]]";
+                               return "[[{$this->linksPrefix}MediaWiki:$displayKey|$key]]";
                        } else {
-                               return "[[MediaWiki:$displayKey/$code|$key]]";
+                               return "[[{$this->linksPrefix}MediaWiki:$displayKey/$code|$key]]";
                        }
                } else {
                        return $key;
@@ -327,7 +355,6 @@ ENDS;
 
        /**
         * Output the checks results as plain text.
-        * @return The checks results as plain text.
         */
        protected function outputText() {
                foreach ( $this->results as $code => $results ) {
@@ -373,12 +400,10 @@ ENDS;
 
        /**
         * Output the checks results as wiki text.
-        * @return The checks results as wiki text.
         */
        function outputWiki() {
-               global $wgContLang, $IP;
                $detailText = '';
-               $rows[] = '! Language !! Code !! Total !! ' . implode( ' !! ', $this->checks );
+               $rows[] = '! Language !! Code !! Total !! ' . implode( ' !! ', array_diff( $this->checks, $this->nonMessageChecks() ) );
                foreach ( $this->results as $code => $results ) {
                        $detailTextForLang = "==$code==\n";
                        $numbers = array();
@@ -412,13 +437,13 @@ ENDS;
                                # Don't list languages without problems
                                continue;
                        }
-                       $language = $wgContLang->getLanguageName( $code );
+                       $language = Language::fetchLanguageName( $code );
                        $rows[] = "| $language || $code || $problems || " . implode( ' || ', $numbers );
                }
 
                $tableRows = implode( "\n|-\n", $rows );
 
-               $version = SpecialVersion::getVersion( $IP );
+               $version = SpecialVersion::getVersion( 'nodb' );
                echo <<<EOL
 '''Check results are for:''' <code>$version</code>
 
@@ -434,11 +459,11 @@ EOL;
 
        /**
         * Check if there are any results for the checks, in any language.
-        * @return True if there are any results, false if not.
+        * @return bool True if there are any results, false if not.
         */
        protected function isEmpty() {
-               foreach( $this->results as $code => $results ) {
-                       foreach( $results as $check => $messages ) {
+               foreach( $this->results as $results ) {
+                       foreach( $results as $messages ) {
                                if( !empty( $messages ) ) {
                                        return false;
                                }
@@ -448,18 +473,21 @@ EOL;
        }
 }
 
+/**
+ * @ingroup MaintenanceLanguage
+ */
 class CheckExtensionsCLI extends CheckLanguageCLI {
        private $extensions;
 
        /**
         * Constructor.
-        * @param $options Options for script.
-        * @param $extension The extension name (or names).
+        * @param $options array Options for script.
+        * @param $extension string The extension name (or names).
         */
        public function __construct( Array $options, $extension ) {
                if ( isset( $options['help'] ) ) {
                        echo $this->help();
-                       exit();
+                       exit(1);
                }
 
                if ( isset( $options['lang'] ) ) {
@@ -538,7 +566,7 @@ class CheckExtensionsCLI extends CheckLanguageCLI {
 
        /**
         * Get the default checks.
-        * @return A list of the default checks.
+        * @return array A list of the default checks.
         */
        protected function defaultChecks() {
                return array(
@@ -549,7 +577,7 @@ class CheckExtensionsCLI extends CheckLanguageCLI {
 
        /**
         * Get the checks which check other things than messages.
-        * @return A list of the non-message checks.
+        * @return array A list of the non-message checks.
         */
        protected function nonMessageChecks() {
                return array();
@@ -557,7 +585,7 @@ class CheckExtensionsCLI extends CheckLanguageCLI {
 
        /**
         * Get the checks that can easily be treated by non-speakers of the language.
-        * @return A list of the easy checks.
+        * @return arrayA list of the easy checks.
         */
        protected function easyChecks() {
                return array(
@@ -567,7 +595,7 @@ class CheckExtensionsCLI extends CheckLanguageCLI {
 
        /**
         * Get help.
-        * @return The help string.
+        * @return string The help string.
         */
        protected function help() {
                return <<<ENDS
@@ -587,7 +615,7 @@ Check codes (ideally, all of them should result 0; all the checks are executed b
        * untranslated: Messages which are required to translate, but are not translated.
        * duplicate: Messages which translation equal to fallback
        * obsolete: Messages which are untranslatable, but translated.
-       * variables: Messages without variables which should be used, or with variables which shouldn't be used.
+       * variables: Messages without variables which should be used, or with variables which should not be used.
        * empty: Empty messages.
        * whitespace: Messages which have trailing whitespace.
        * xhtml: Messages which are not well-formed XHTML (checks only few common errors).
@@ -612,7 +640,7 @@ ENDS;
 
        /**
         * Check a language and show the results.
-        * @param $code The language code.
+        * @param $code string The language code.
         */
        protected function checkLanguage( $code ) {
                foreach( $this->extensions as $extension ) {
@@ -645,25 +673,40 @@ ENDS;
 # Blacklist some checks for some languages
 $checkBlacklist = array(
 #'code'        => array( 'check1', 'check2' ... )
+'az'           => array( 'plural' ),
+'bo'           => array( 'plural' ),
+'dz'           => array( 'plural' ),
+'id'           => array( 'plural' ),
+'fa'           => array( 'plural' ),
 'gan'          => array( 'plural' ),
+'gan-hans'     => array( 'plural' ),
+'gan-hant'     => array( 'plural' ),
 'gn'           => array( 'plural' ),
 'hak'          => array( 'plural' ),
 'hu'           => array( 'plural' ),
 'ja'           => array( 'plural' ), // Does not use plural
+'jv'           => array( 'plural' ),
 'ka'           => array( 'plural' ),
 'kk-arab'      => array( 'plural' ),
 'kk-cyrl'      => array( 'plural' ),
 'kk-latn'      => array( 'plural' ),
+'km'           => array( 'plural' ),
+'kn'           => array( 'plural' ),
 'ko'           => array( 'plural' ),
+'lzh'          => array( 'plural' ),
 'mn'           => array( 'plural' ),
 'ms'           => array( 'plural' ),
-'my'           => array( 'chars' ),  // Uses a lot zwnj
+'my'           => array( 'plural', 'chars' ),  // Uses a lot zwnj
 'sah'          => array( 'plural' ),
 'sq'           => array( 'plural' ),
 'tet'          => array( 'plural' ),
 'th'           => array( 'plural' ),
+'to'           => array( 'plural' ),
+'tr'           => array( 'plural' ),
+'vi'           => array( 'plural' ),
 'wuu'          => array( 'plural' ),
 'xmf'          => array( 'plural' ),
+'yo'           => array( 'plural' ),
 'yue'          => array( 'plural' ),
 'zh'           => array( 'plural' ),
 'zh-classical' => array( 'plural' ),