TTMServer.php

Go to the documentation of this file.
00001 <?php
00018 class TTMServer {
00019     protected $config;
00020 
00021     protected function __construct( $config ) {
00022         $this->config = $config;
00023     }
00024 
00025     public static function factory( $config ) {
00026         if ( isset( $config['class'] ) ) {
00027             $class = $config['class'];
00028 
00029             return new $class( $config );
00030         } elseif ( isset( $config['type'] ) ) {
00031             $type = $config['type'];
00032             switch ( $type ) {
00033                 case 'ttmserver':
00034                     return new DatabaseTTMServer( $config );
00035                 case 'remote-ttmserver':
00036                     return new RemoteTTMServer( $config );
00037                 default:
00038                     return null;
00039             }
00040         }
00041 
00042         throw new MWEXception( "TTMServer with no type" );
00043     }
00044 
00051     public static function primary() {
00052         global $wgTranslateTranslationServices;
00053         if ( isset( $wgTranslateTranslationServices['TTMServer'] ) ) {
00054             $obj = self::factory( $wgTranslateTranslationServices['TTMServer'] );
00055             if ( $obj instanceof WritableTTMServer ) {
00056                 return $obj;
00057             }
00058         }
00059 
00060         return new FakeTTMServer();
00061     }
00062 
00063     public static function sortSuggestions( array $suggestions ) {
00064         usort( $suggestions, array( __CLASS__, 'qualitySort' ) );
00065 
00066         return $suggestions;
00067     }
00068 
00069     protected static function qualitySort( $a, $b ) {
00070         list( $c, $d ) = array( $a['quality'], $b['quality'] );
00071         if ( $c === $d ) {
00072             return 0;
00073         }
00074 
00075         // Descending sort
00076         return ( $c > $d ) ? -1 : 1;
00077     }
00078 
00090     public static function levenshtein( $str1, $str2, $length1, $length2 ) {
00091         if ( $length1 == 0 ) {
00092             return $length2;
00093         }
00094         if ( $length2 == 0 ) {
00095             return $length1;
00096         }
00097         if ( $str1 === $str2 ) {
00098             return 0;
00099         }
00100 
00101         $bytelength1 = strlen( $str1 );
00102         $bytelength2 = strlen( $str2 );
00103         if ( $bytelength1 === $length1 && $bytelength1 <= 255
00104             && $bytelength2 === $length2 && $bytelength2 <= 255
00105         ) {
00106             return levenshtein( $str1, $str2 );
00107         }
00108 
00109         $prevRow = range( 0, $length2 );
00110         for ( $i = 0; $i < $length1; $i++ ) {
00111             $currentRow = array();
00112             $currentRow[0] = $i + 1;
00113             $c1 = mb_substr( $str1, $i, 1 );
00114             for ( $j = 0; $j < $length2; $j++ ) {
00115                 $c2 = mb_substr( $str2, $j, 1 );
00116                 $insertions = $prevRow[$j + 1] + 1;
00117                 $deletions = $currentRow[$j] + 1;
00118                 $substitutions = $prevRow[$j] + ( ( $c1 != $c2 ) ? 1 : 0 );
00119                 $currentRow[] = min( $insertions, $deletions, $substitutions );
00120             }
00121             $prevRow = $currentRow;
00122         }
00123 
00124         return $prevRow[$length2];
00125     }
00126 
00130     public static function onDelete( $wikipage ) {
00131         $handle = new MessageHandle( $wikipage->getTitle() );
00132         TTMServer::primary()->update( $handle, null );
00133 
00134         return true;
00135     }
00136 
00138     public static function onChange( MessageHandle $handle, $text, $fuzzy ) {
00139         if ( $fuzzy ) {
00140             $text = null;
00141         }
00142         TTMServer::primary()->update( $handle, $text );
00143     }
00144 
00145     public static function onGroupChange( MessageHandle $handle, $old, $new ) {
00146         if ( $old === array() ) {
00147             // Don't bother for newly added messages
00148             return true;
00149         }
00150 
00151         $job = TTMServerMessageUpdateJob::newJob( $handle );
00152         $job->insert();
00153 
00154         return true;
00155     }
00156 }
Generated on Tue Oct 29 00:00:25 2013 for MediaWiki Translate Extension by  doxygen 1.6.3