* Fixed unclosed <p> tag
[lhc/web/wiklou.git] / includes / DateFormatter.php
index 6a8abf9..f5cad86 100755 (executable)
@@ -2,11 +2,11 @@
 /**
  * Contain things
  * @todo document
+ * @package MediaWiki
+ * @subpackage Parser
  */
 
-/**
- *
- */
+/** */
 define('DF_ALL', -1);
 define('DF_NONE', 0);
 define('DF_MDY', 1);
@@ -14,7 +14,6 @@ define('DF_DMY', 2);
 define('DF_YMD', 3);
 define('DF_ISO1', 4);
 define('DF_LASTPREF', 4);
-
 define('DF_ISO2', 5);
 define('DF_YDM', 6);
 define('DF_DM', 7);
@@ -23,6 +22,8 @@ define('DF_LAST', 8);
 
 /**
  * @todo preferences, OutputPage
+ * @package MediaWiki
+ * @subpackage Parser
  */
 class DateFormatter
 {
@@ -32,13 +33,18 @@ class DateFormatter
        var $regexes, $pDays, $pMonths, $pYears;
        var $rules, $xMonths;
        
-       function DateFormatter()
-       {
-               global $wgMonthNamesEn, $wgInputEncoding;
+       /**
+        * @todo document
+        */
+       function DateFormatter() {
+               global $wgContLang, $wgInputEncoding;
                
                $this->monthNames = $this->getMonthRegex();
                for ( $i=1; $i<=12; $i++ ) {
-                       $this->xMonths[strtolower( $wgMonthNamesEn[$i-1] )] = $i;
+                       $this->xMonths[strtolower( $wgContLang->getMonthName( $i ) )] = $i;
+               }
+               for ( $i=1; $i<=12; $i++ ) {
+                       $this->xMonths[strtolower( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
                }
                
                # Attempt at UTF-8 support, untested at the moment
@@ -95,8 +101,28 @@ class DateFormatter
                $this->rules[DF_NONE][DF_ISO2]  = DF_ISO1;
        }
        
-       function reformat( $preference, $text ) 
-       {
+       /**
+        * @static
+        */
+       function &getInstance() {
+               global $wgDBname, $wgMemc;
+               static $dateFormatter = false;
+               if ( !$dateFormatter ) {
+                       $dateFormatter = $wgMemc->get( "$wgDBname:dateformatter" );
+                       if ( !$dateFormatter ) {
+                               $dateFormatter = new DateFormatter;
+                               $wgMemc->set( "$wgDBname:dateformatter", $dateFormatter, 3600 );
+                       }
+               }
+               return $dateFormatter;
+       }       
+       
+       /**
+        * @param $preference
+        * @param $text
+        */
+       function reformat( $preference, $text ) {
+               if ($preference == 'ISO 8601') $preference = 4; # The ISO 8601 option used to be 4
                for ( $i=1; $i<=DF_LAST; $i++ ) {
                        $this->mSource = $i;
                        if ( @$this->rules[$preference][$i] ) {
@@ -117,9 +143,10 @@ class DateFormatter
                return $text;
        }
 
-       function replace( $matches )
-       {
-               global $wgMonthNamesEn;
+       /**
+        * @param $matches
+        */
+       function replace( $matches ) {
                # Extract information from $matches
                $bits = array();
                $key = $this->keys[$this->mSource];
@@ -139,14 +166,14 @@ class DateFormatter
                        $char = $format{$p};
                        switch ( $char ) {
                                case 'd': # ISO day of month
-                                       if ( is_null($bits['d']) ) {
+                                       if ( !isset($bits['d']) ) {
                                                $text .= sprintf( '%02d', $bits['j'] );
                                        } else {
                                                $text .= $bits['d'];
                                        }
                                        break;
                                case 'm': # ISO month
-                                       if ( is_null($bits['m']) ) {
+                                       if ( !isset($bits['m']) ) {
                                                $m = $this->makeIsoMonth( $bits['F'] );
                                                if ( !$m || $m == '00' ) {
                                                        $fail = true;
@@ -158,33 +185,34 @@ class DateFormatter
                                        }
                                        break;
                                case 'y': # ISO year
-                                       if ( is_null( $bits['y'] ) ) {
+                                       if ( !isset( $bits['y'] ) ) {
                                                $text .= $this->makeIsoYear( $bits['Y'] );
                                        } else {
                                                $text .= $bits['y'];
                                        }
                                        break;
                                case 'j': # ordinary day of month
-                                       if ( is_null($bits['j']) ) {
+                                       if ( !isset($bits['j']) ) {
                                                $text .= IntVal( $bits['d'] );
                                        } else {
                                                $text .= $bits['j'];
                                        }
                                        break;
                                case 'F': # long month
-                                       if ( is_null( $bits['F'] ) ) {
+                                       if ( !isset( $bits['F'] ) ) {
                                                $m = IntVal($bits['m']);
                                                if ( $m > 12 || $m < 1 ) {
                                                        $fail = true;
                                                } else {
-                                                       $text .= $wgMonthNamesEn[$m-1];
+                                                       global $wgContLang;
+                                                       $text .= $wgContLang->getMonthName( $m );
                                                }
                                        } else {
                                                $text .= ucfirst( $bits['F'] );
                                        }
                                        break;
                                case 'Y': # ordinary (optional BC) year
-                                       if ( is_null( $bits['Y'] ) ) {
+                                       if ( !isset( $bits['Y'] ) ) {
                                                $text .= $this->makeNormalYear( $bits['y'] );
                                        } else {
                                                $text .= $bits['Y'];
@@ -200,21 +228,35 @@ class DateFormatter
                return $text;
        }
        
-       function getMonthRegex()
-       {
-               global $wgMonthNamesEn;
-               return implode( '|', $wgMonthNamesEn );
+       /**
+        * @todo document
+        */
+       function getMonthRegex() {
+               global $wgContLang;
+               $names = array();
+               for( $i = 1; $i <= 12; $i++ ) {
+                       $names[] = $wgContLang->getMonthName( $i );
+                       $names[] = $wgContLang->getMonthAbbreviation( $i );
+               }
+               return implode( '|', $names );
        }
 
-       # Makes an ISO month, e.g. 02, from a month name
-       function makeIsoMonth( $monthName )
-       {
+       /**
+        * Makes an ISO month, e.g. 02, from a month name
+        * @param string $monthName Month name
+        * @return string ISO month name
+        */
+       function makeIsoMonth( $monthName ) {
                $n = $this->xMonths[strtolower( $monthName )];
                return sprintf( '%02d', $n );
        }
 
-       function makeIsoYear( $year )
-       {
+       /**
+        * @todo document
+        * @param string $year Year name
+        * @return string ISO year name
+        */
+       function makeIsoYear( $year ) {
                # Assumes the year is in a nice format, as enforced by the regex
                if ( substr( $year, -2 ) == 'BC' ) {
                        $num = IntVal(substr( $year, 0, -3 )) - 1;
@@ -227,10 +269,12 @@ class DateFormatter
                return $text;
        }
 
-       function makeNormalYear( $iso ) 
-       {
+       /**
+        * @todo document
+        */
+       function makeNormalYear( $iso ) {
                if ( $iso{0} == '-' ) {
-                       $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
+                       $text = (IntVal( substr( $iso, 1 ) ) + 1) . ' BC';
                } else {
                        $text = IntVal( $iso );
                }
@@ -238,9 +282,12 @@ class DateFormatter
        }
 }
 
-function wfMainDateReplace( $matches )
-{
-       global $wgDateFormatter;
-       return $wgDateFormatter->replace( $matches );
+/**
+ * @todo document
+ */
+function wfMainDateReplace( $matches ) {
+       $df =& DateFormatter::getInstance();
+       return $df->replace( $matches );
 }
+
 ?>