Merge "Setting up a way to have uploading by URL, but not on Special:Upload"
[lhc/web/wiklou.git] / includes / normal / UtfNormalTest.php
index 6360a7c..5872ec3 100644 (file)
@@ -1,29 +1,30 @@
 <?php
-# Copyright (C) 2004 Brion Vibber <brion@pobox.com>
-# http://www.mediawiki.org/
-# 
-# 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.,
-# 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
-# http://www.gnu.org/copyleft/gpl.html
-
 /**
  * Implements the conformance test at:
  * http://www.unicode.org/Public/UNIDATA/NormalizationTest.txt
- * @package MediaWiki
+ *
+ * Copyright © 2004 Brion Vibber <brion@pobox.com>
+ * http://www.mediawiki.org/
+ *
+ * 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 UtfNormal
  */
 
-/** */
 $verbose = true;
 #define( 'PRETTY_UTF8', true );
 
@@ -32,18 +33,24 @@ if( defined( 'PRETTY_UTF8' ) ) {
                return preg_replace( '/([\x00-\xff])/e',
                        'sprintf("%02X", ord("$1"))',
                        $string );
-       }       
+       }
 } else {
        /**
         * @ignore
+        * @return string
         */
        function pretty( $string ) {
                return trim( preg_replace( '/(.)/use',
                        'sprintf("%04X ", utf8ToCodepoint("$1"))',
                        $string ) );
-       }       
+       }
 }
 
+if( isset( $_SERVER['argv'] ) && in_array( '--icu', $_SERVER['argv'] ) ) {
+       dl( 'php_utfnormal.so' );
+}
+
+require_once 'UtfNormalDefines.php';
 require_once 'UtfNormalUtil.php';
 require_once 'UtfNormal.php';
 
@@ -69,6 +76,7 @@ $testedChars = array();
 while( false !== ( $line = fgets( $in ) ) ) {
        list( $data, $comment ) = explode( '#', $line );
        if( $data === '' ) continue;
+       $matches = array();
        if( preg_match( '/@Part([\d])/', $data, $matches ) ) {
                if( $matches[1] > 0 ) {
                        $ok = reportResults( $total, $success, $failure ) && $ok;
@@ -76,13 +84,13 @@ while( false !== ( $line = fgets( $in ) ) ) {
                print "Part {$matches[1]}: $comment";
                continue;
        }
-       
+
        $columns = array_map( "hexSequenceToUtf8", explode( ";", $data ) );
        array_unshift( $columns, '' );
-       
+
        $testedChars[$columns[1]] = true;
        $total++;
-       if( testNormals( $normalizer, $columns, $comment ) ) {
+       if( testNormals( $normalizer, $columns, $comment, $verbose ) ) {
                $success++;
        } else {
                $failure++;
@@ -106,13 +114,15 @@ while( false !== ($line = fgets( $in ) ) ) {
        $cols = explode( ';', $line );
        $char = codepointToUtf8( hexdec( $cols[0] ) );
        $desc = $cols[0] . ": " . $cols[1];
-       if( $char >= UTF8_SURROGATE_FIRST && $char <= UTF8_SURROGATE_LAST ) {
+       if( $char < "\x20" || $char >= UTF8_SURROGATE_FIRST && $char <= UTF8_SURROGATE_LAST ) {
+               # Can't check NULL with the ICU plugin, as null bytes fail in C land.
+               # Skip other control characters, as we strip them for XML safety.
                # Surrogates are illegal on their own or in UTF-8, ignore.
                continue;
        }
        if( empty( $testedChars[$char] ) ) {
                $total++;
-               if( testInvariant( $normalizer, $char, $desc ) ) {
+               if( testInvariant( $normalizer, $char, $desc, $verbose ) ) {
                        $success++;
                } else {
                        $failure++;
@@ -135,8 +145,8 @@ if( $ok ) {
 ## ------
 
 function reportResults( &$total, &$success, &$failure ) {
-       $percSucc = IntVal( $success * 100 / $total );
-       $percFail = IntVal( $failure * 100 / $total );
+       $percSucc = intval( $success * 100 / $total );
+       $percFail = intval( $failure * 100 / $total );
        print "\n";
        print "$success tests successful ($percSucc%)\n";
        print "$failure tests failed ($percFail%)\n\n";
@@ -147,16 +157,16 @@ function reportResults( &$total, &$success, &$failure ) {
        return $ok;
 }
 
-function testNormals( &$u, $c, $comment, $reportFailure = false ) {
+function testNormals( &$u, $c, $comment, $verbose, $reportFailure = false ) {
        $result = testNFC( $u, $c, $comment, $reportFailure );
        $result = testNFD( $u, $c, $comment, $reportFailure ) && $result;
        $result = testNFKC( $u, $c, $comment, $reportFailure ) && $result;
        $result = testNFKD( $u, $c, $comment, $reportFailure ) && $result;
-       
-       global $verbose;
+       $result = testCleanUp( $u, $c, $comment, $reportFailure ) && $result;
+
        if( $verbose && !$result && !$reportFailure ) {
                print $comment;
-               testNormals( $u, $c, $comment, true );
+               testNormals( $u, $c, $comment, $verbose, true );
        }
        return $result;
 }
@@ -183,6 +193,20 @@ function testNFC( &$u, $c, $comment, $verbose ) {
        return $result;
 }
 
+function testCleanUp( &$u, $c, $comment, $verbose ) {
+       $x = $c[1];
+       $result = verbosify( $c[2], $u->cleanUp( $x ), 1, 'cleanUp', $verbose );
+       $x = $c[2];
+       $result = verbosify( $c[2], $u->cleanUp( $x ), 2, 'cleanUp', $verbose ) && $result;
+       $x = $c[3];
+       $result = verbosify( $c[2], $u->cleanUp( $x ), 3, 'cleanUp', $verbose ) && $result;
+       $x = $c[4];
+       $result = verbosify( $c[4], $u->cleanUp( $x ), 4, 'cleanUp', $verbose ) && $result;
+       $x = $c[5];
+       $result = verbosify( $c[4], $u->cleanUp( $x ), 5, 'cleanUp', $verbose ) && $result;
+       return $result;
+}
+
 function testNFD( &$u, $c, $comment, $verbose ) {
        $result = verbosify( $c[3], $u->toNFD( $c[1] ), 1, 'NFD', $verbose );
        $result = verbosify( $c[3], $u->toNFD( $c[2] ), 2, 'NFD', $verbose ) && $result;
@@ -210,17 +234,16 @@ function testNFKD( &$u, $c, $comment, $verbose ) {
        return $result;
 }
 
-function testInvariant( &$u, $char, $desc, $reportFailure = false ) {
+function testInvariant( &$u, $char, $desc, $verbose, $reportFailure = false ) {
        $result = verbosify( $char, $u->toNFC( $char ), 1, 'NFC', $reportFailure );
        $result = verbosify( $char, $u->toNFD( $char ), 1, 'NFD', $reportFailure ) && $result;
        $result = verbosify( $char, $u->toNFKC( $char ), 1, 'NFKC', $reportFailure ) && $result;
        $result = verbosify( $char, $u->toNFKD( $char ), 1, 'NFKD', $reportFailure ) && $result;
-       global $verbose;
+       $result = verbosify( $char, $u->cleanUp( $char ), 1, 'cleanUp', $reportFailure ) && $result;
+
        if( $verbose && !$result && !$reportFailure ) {
                print $desc;
-               testInvariant( $u, $char, $desc, true );
+               testInvariant( $u, $char, $desc, $verbose, true );
        }
        return $result;
 }
-
-?>