http://www.mediawiki.org/wiki/Special:Code/MediaWiki/56301

Revision: 56301
Author:   siebrand
Date:     2009-09-14 13:43:07 +0000 (Mon, 14 Sep 2009)

Log Message:
-----------
* don't fuzzy English in web importer and Special:Translate/manage
* fix bug in MessageWebImporter.php: $dbr -> $dbw
* added FIXMEs for duplication
* coding style updates

Modified Paths:
--------------
    trunk/extensions/Translate/SpecialManageGroups.php
    trunk/extensions/Translate/utils/MessageWebImporter.php

Modified: trunk/extensions/Translate/SpecialManageGroups.php
===================================================================
--- trunk/extensions/Translate/SpecialManageGroups.php  2009-09-14 13:39:25 UTC 
(rev 56300)
+++ trunk/extensions/Translate/SpecialManageGroups.php  2009-09-14 13:43:07 UTC 
(rev 56301)
@@ -1,9 +1,7 @@
 <?php
 
 class SpecialManageGroups {
-
        protected $skin, $user, $out;
-
        protected $processingTime = 10; // Seconds
 
        public function __construct() {
@@ -18,38 +16,33 @@
 
                $this->out->setPageTitle( htmlspecialchars( wfMsg( 
'translate-managegroups' ) ) );
 
-
                $group = $wgRequest->getText( 'group' );
                $group = MessageGroups::getGroup( $group );
 
                if ( $group ) {
-
                        if ( $wgRequest->getBool( 'rebuildall', false ) &&
-                               $wgRequest->wasPosted() && 
+                               $wgRequest->wasPosted() &&
                                $this->user->isAllowed( 'translate-manage' ) &&
                                $this->user->matchEditToken( 
$wgRequest->getVal( 'token' ) ) ) {
 
                                $cache = new MessageGroupCache( $group );
                                $languages = explode( ',', $wgRequest->getText( 
'codes' ) );
                                foreach ( $languages as $code ) {
-                                       $messages = $group->load($code);
-                                       if ( count($messages) ) $cache->create( 
$messages, $code );
+                                       $messages = $group->load( $code );
+                                       if ( count( $messages ) ) 
$cache->create( $messages, $code );
                                }
                        }
 
-
                        $cache = new MessageGroupCache( $group );
                        $code = $wgRequest->getText( 'language', 'en' );
                        $this->importForm( $cache, $group, $code );
                } else {
-
                        global $wgLang, $wgOut;
                        $groups = MessageGroups::singleton()->getGroups();
                        $wgOut->wrapWikiMsg( '==$1==', 
'translate-manage-listgroups' );
                        foreach ( $groups as $group ) {
                                if ( !$group instanceof FileBasedMessageGroup ) 
continue;
 
-
                                $link = $this->skin->link( $this->getTitle(), 
$group->getLabel(), array(), array( 'group' => $group->getId() ) );
                                $wgOut->addHtml( $link );
 
@@ -67,14 +60,16 @@
                        }
 
                        global $wgOut;
+
                        $wgOut->wrapWikiMsg( '==$1==', 
'translate-manage-listgroups-old' );
                        $wgOut->addHTML( '<ul>' );
+
                        foreach ( $groups as $group ) {
                                if ( $group instanceof FileBasedMessageGroup ) 
continue;
                                $wgOut->addHtml( Xml::element( 'li', null, 
$group->getLabel() ) );
                        }
+
                        $wgOut->addHTML( '</ul>' );
-
                }
        }
 
@@ -104,7 +99,7 @@
 
                global $wgRequest;
                if ( $wgRequest->wasPosted() &&
-                       $wgRequest->getBool( 'process', false ) && 
+                       $wgRequest->getBool( 'process', false ) &&
                        $this->user->isAllowed( 'translate-manage' ) &&
                        $this->user->matchEditToken( $wgRequest->getVal( 
'token' ) ) ) {
                        $process = true;
@@ -130,17 +125,15 @@
                $collection = $group->initCollection( $code );
                $collection->loadTranslations();
 
-
                $diff = new DifferenceEngine;
                $diff->showDiffStyle();
                $diff->setReducedLineNumbers();
 
                $changed = array();
                foreach ( $messages as $key => $value ) {
-               
+                       $fuzzy = $old = false;
 
-                       $fuzzy = $old = false;
-                       if ( isset($collection[$key]) ) {
+                       if ( isset( $collection[$key] ) ) {
                                $old = $collection[$key]->translation();
                                $fuzzy = TranslateEditAddons::hasFuzzyString( 
$old ) ||
                                                TranslateEditAddons::isFuzzy( 
self::makeTitle( $group, $key, $code ) );
@@ -148,32 +141,45 @@
                        }
 
                        // No changes at all, ignore
-                       if ( $old === $value ) continue;
+                       if ( $old === $value ) {
+                               continue;
+                       }
 
                        if ( $old === false ) {
-                               $name = wfMsgHtml( 
'translate-manage-import-new', 
-                                       '<code style="font-weight:normal;">' . 
htmlspecialchars($key) . '</code>'
+                               $name = wfMsgHtml( 
'translate-manage-import-new',
+                                       '<code style="font-weight:normal;">' . 
htmlspecialchars( $key ) . '</code>'
                                );
+
                                $text = 
TranslateUtils::convertWhiteSpaceToHTML( $value );
+
                                $changed[] = $this->makeSectionElement( $name, 
'new', $text );
                        } else {
-                               if ( $fuzzy ) $old = TRANSLATE_FUZZY . $old;
+                               if ( $fuzzy ) {
+                                       $old = TRANSLATE_FUZZY . $old;
+                               }
+
                                $diff->setText( $old, $value );
                                $text = $diff->getDiff( '', '' );
                                $type = 'changed';
 
                                if ( $process ) {
-                                       if ( !count($changed) ) $changed[] = 
'<ul>';
+                                       if ( !count( $changed ) ) {
+                                               $changed[] = '<ul>';
+                                       }
 
                                        global $wgRequest, $wgLang;
+
                                        $action = $wgRequest->getVal( 
"action-$type-$key" );
+
                                        if ( $action === null ) {
                                                $message = wfMsgExt( 
'translate-manage-inconsistent', 'parseinline', wfEscapeWikiText( 
"action-$type-$key" ) );
                                                $changed[] = 
"<li>$message</li></ul>";
                                                $process = false;
                                        } else {
                                                // Check processing time
-                                               if ( !isset($this->time) ) 
$this->time = wfTimestamp();
+                                               if ( !isset( $this->time ) ) {
+                                                       $this->time = 
wfTimestamp();
+                                               }
 
                                                $message = $this->doAction( 
$action, $group, $key, $code, $value );
 
@@ -200,13 +206,14 @@
 
                                $act = array();
                                $defaction = $fuzzy ? 'conflict' : 'import';
+
                                foreach ( $actions as $action ) {
                                        $label = wfMsg( 
"translate-manage-action-$action" );
                                        $act[] = Xml::radioLabel( $label, 
"action-$type-$key", $action, "action-$key-$action", $action === $defaction );
                                }
 
                                $name = wfMsg( 'translate-manage-import-diff',
-                                       '<code style="font-weight:normal;">' . 
htmlspecialchars($key) . '</code>',
+                                       '<code style="font-weight:normal;">' . 
htmlspecialchars( $key ) . '</code>',
                                        implode( ' ', $act )
                                );
 
@@ -214,26 +221,31 @@
                        }
                }
 
-
                if ( !$process ) {
                        $collection->filter( 'hastranslation', false );
-                       $keys = array_keys($collection->keys());
+                       $keys = array_keys( $collection->keys() );
 
-                       $diff = array_diff( $keys, array_keys($messages) );
+                       $diff = array_diff( $keys, array_keys( $messages ) );
 
                        foreach ( $diff as $s ) {
                                $name = wfMsgHtml( 
'translate-manage-import-deleted',
-                                       '<code style="font-weight:normal;">' . 
htmlspecialchars($s) . '</code>'
+                                       '<code style="font-weight:normal;">' . 
htmlspecialchars( $s ) . '</code>'
                                );
+
                                $text = 
TranslateUtils::convertWhiteSpaceToHTML(  $collection[$s]->translation() );
+
                                $changed[] = $this->makeSectionElement( $name, 
'deleted', $text );
                        }
                }
 
-               if ( $process || (!count($changed) && $code !== 'en') ) {
-                       if ( !count($changed) ) $this->out->addWikiMsg( 
'translate-manage-nochanges-other' );
+               if ( $process || ( !count( $changed ) && $code !== 'en' ) ) {
+                       if ( !count( $changed ) ) {
+                               $this->out->addWikiMsg( 
'translate-manage-nochanges-other' );
+                       }
 
-                       if ( !count($changed) || strpos( 
$changed[count($changed)-1], '<li>' ) !== 0 ) $changed[] = '<ul>';
+                       if ( !count( $changed ) || strpos( $changed[count( 
$changed ) - 1], '<li>' ) !== 0 ) {
+                               $changed[] = '<ul>';
+                       }
 
                        $cache->create( $messages, $code );
                        $message = wfMsgExt( 'translate-manage-import-rebuild', 
'parseinline' );
@@ -242,10 +254,8 @@
                        $changed[] = "<li>$message</li></ul>";
                        $this->out->addHTML( implode( "\n", $changed ) );
                } else {
-
                        // END
-
-                       if ( count($changed) ) {
+                       if ( count( $changed ) ) {
                                if ( $code === 'en' ) {
                                        $this->out->addWikiMsg( 
'translate-manage-intro-en' );
                                } else {
@@ -266,7 +276,6 @@
                if ( $code === 'en' ) {
                        $this->doModLangs( $cache, $group );
                } else {
-
                        $this->out->addHTML( '<p>' . $this->skin->link(
                                $this->getTitle(),
                                wfMsgHtml( 'translate-manage-return-to-group' ),
@@ -274,20 +283,27 @@
                                array( 'group' => $group->getId() )
                        ) . '</p>' );
                }
-
        }
 
        public function doModLangs( $cache, $group ) {
                global $wgLang;
-               $languages = array_keys( Language::getLanguageNames(false) );
+
+               $languages = array_keys( Language::getLanguageNames( false ) );
                $modified = $codes = array();
+
                foreach ( $languages as $code ) {
-                       if ( $code === 'en' ) continue;
+                       if ( $code === 'en' ) {
+                               continue;
+                       }
+
                        $filename = $group->getSourceFilePath( $code );
                        $mtime = file_exists( $filename ) ? filemtime( 
$filename ) : false;
-                       $cachetime = $cache->exists($code) ? 
$cache->getTimestamp($code) : false;
-                       if ( $mtime === false && $cachetime === false ) 
continue;
+                       $cachetime = $cache->exists( $code ) ? 
$cache->getTimestamp( $code ) : false;
 
+                       if ( $mtime === false && $cachetime === false ) {
+                               continue;
+                       }
+
                        $link = $this->skin->link(
                                $this->getTitle(),
                                htmlspecialchars( 
TranslateUtils::getLanguageName( $code, false, $wgLang->getCode() ) . " 
($code)" ),
@@ -304,9 +320,9 @@
                        $codes[] = $code;
                }
 
-               if ( count($modified) ) {
+               if ( count( $modified ) ) {
                        $this->out->addWikiMsg( 'translate-manage-modlangs',
-                               $wgLang->formatNum( count($modified) )
+                               $wgLang->formatNum( count( $modified ) )
                        );
 
                        $this->out->addHTML(
@@ -319,6 +335,7 @@
                        );
 
                        global $wgRequest;
+
                        if ( $wgRequest->wasPosted() &&
                                $this->user->isAllowed( 'translate-manage' ) &&
                                $this->user->matchEditToken( 
$wgRequest->getVal( 'token' ) ) ) {
@@ -337,7 +354,6 @@
                                Xml::submitButton( wfMsg( 
'translate-manage-import-rebuild-all' ) ) .
                                Xml::closeElement( 'form' )
                        );
-
                }
        }
 
@@ -351,12 +367,14 @@
                        }
 
                        $title = self::makeTitle( $group, $key, $code );
+
                        return $this->doImport( $title, $message, $comment );
                } elseif ( $action === 'ignore' ) {
                        return array( 'translate-manage-import-ignore', $key );
-               } elseif ( $action === 'fuzzy' ) {
+               } elseif ( $action === 'fuzzy' && $code != 'en' ) {
                        $title = Title::makeTitleSafe( $group->getNamespace(), 
$key );
                        $comment = wfMsgForContentNoTrans( 
'translate-manage-fuzzy-summary' );
+
                        return $this->doFuzzy( $title, $message, $comment );
                } else {
                        throw new MWException( "Unhandled action $action" );
@@ -366,7 +384,7 @@
        protected function checkProcessTime() {
                return wfTimestamp() - $this->time >= $this->processingTime;
        }
-
+       
        protected function doImport( $title, $message, $comment, $user = null ) 
{
                $flags = EDIT_FORCE_BOT;
                $article = new Article( $title );
@@ -403,6 +421,7 @@
 
                $changed = array();
                $fuzzybot = self::getFuzzyBot();
+
                foreach ( $rows as $row ) {
                        $ttitle = Title::makeTitle( $row->page_namespace, 
$row->page_title );
 
@@ -413,10 +432,9 @@
 
                        $changed[] = $this->doImport( $ttitle, $text, 
"[{$wgUser->getName()}] " . $comment, $fuzzybot );
                        if ( $this->checkProcessTime() ) break;
-
                }
 
-               if ( count($changed) === $rows->numRows() ) {
+               if ( count( $changed ) === $rows->numRows() ) {
                        $comment = "[{$wgUser->getName()}] " . 
wfMsgForContentNoTrans( 'translate-manage-import-summary' );
                        $title = Title::makeTitleSafe( $title->getNamespace(), 
$title->getPrefixedDbKey() . '/en' );
                        $changed[] = $this->doImport( $title, $message, 
$comment, $fuzzybot );
@@ -435,15 +453,21 @@
 
        protected static function getFuzzyBot() {
                global $wgTranslateFuzzyBotName;
+
                $user = User::newFromName( $wgTranslateFuzzyBotName );
-               if ( !$user->isLoggedIn() ) $user->addToDatabase();
+
+               if ( !$user->isLoggedIn() ) {
+                       $user->addToDatabase();
+               }
+
                return $user;
        }
 
        protected static function makeTitle( $group, $key, $code ) {
                $ns = $group->getNamespace();
                $titlekey = "$key/$code";
-               return Title::makeTitleSafe( $ns, $titlekey ); 
+
+               return Title::makeTitleSafe( $ns, $titlekey );
        }
 
        protected function setSubtitle( $group, $code ) {
@@ -471,5 +495,4 @@
 
                $this->out->setSubtitle( implode( ' > ', $links ) );
        }
-
-}
\ No newline at end of file
+}

Modified: trunk/extensions/Translate/utils/MessageWebImporter.php
===================================================================
--- trunk/extensions/Translate/utils/MessageWebImporter.php     2009-09-14 
13:39:25 UTC (rev 56300)
+++ trunk/extensions/Translate/utils/MessageWebImporter.php     2009-09-14 
13:43:07 UTC (rev 56301)
@@ -65,7 +65,7 @@
                        'action' => $this->getAction(),
                        'class'  => 'mw-translate-manage'
                );
-               
+
                return
                        Xml::openElement( 'form', $formParams ) .
                        Xml::hidden( 'title', 
$this->getTitle()->getPrefixedText() ) .
@@ -81,7 +81,7 @@
        protected function allowProcess() {
                global $wgRequest;
                if ( $wgRequest->wasPosted() &&
-                       $wgRequest->getBool( 'process', false ) && 
+                       $wgRequest->getBool( 'process', false ) &&
                        $this->getUser()->matchEditToken( $wgRequest->getVal( 
'token' ) ) ) {
 
                        return true;
@@ -101,8 +101,8 @@
                if ( $action ) return $action;
                return $fuzzy ? 'conflict' : 'import';
        }
-       
 
+
        public function execute( $messages ) {
                global $wgOut;
                $this->out = $wgOut;
@@ -128,18 +128,18 @@
                $changed = array();
                foreach ( $messages as $key => $value ) {
                        $fuzzy = $old = false;
-                       if ( isset($collection[$key]) ) {
+                       if ( isset( $collection[$key] ) ) {
                                $old = $collection[$key]->translation();
                                $fuzzy = TranslateEditAddons::hasFuzzyString( 
$old ) ||
                                                TranslateEditAddons::isFuzzy( 
self::makeTitle( $group, $key, $code ) );
                        }
 
                        // No changes at all, ignore
-                       if ( strval($old) === strval($value) ) continue;
+                       if ( strval( $old ) === strval( $value ) ) continue;
 
                        if ( $old === false ) {
-                               $name = wfMsgHtml( 
'translate-manage-import-new', 
-                                       '<code style="font-weight:normal;">' . 
htmlspecialchars($key) . '</code>'
+                               $name = wfMsgHtml( 
'translate-manage-import-new',
+                                       '<code style="font-weight:normal;">' . 
htmlspecialchars( $key ) . '</code>'
                                );
                                $text = 
TranslateUtils::convertWhiteSpaceToHTML( $value );
                                $changed[] = $this->makeSectionElement( $name, 
'new', $text );
@@ -152,7 +152,7 @@
                                $action = $wgRequest->getVal( 
"action-$type-$key" );
 
                                if ( $process ) {
-                                       if ( !count($changed) ) $changed[] = 
'<ul>';
+                                       if ( !count( $changed ) ) $changed[] = 
'<ul>';
 
                                        global $wgLang;
                                        if ( $action === null ) {
@@ -161,7 +161,7 @@
                                                $process = false;
                                        } else {
                                                // Check processing time
-                                               if ( !isset($this->time) ) 
$this->time = wfTimestamp();
+                                               if ( !isset( $this->time ) ) 
$this->time = wfTimestamp();
 
                                                $message = $this->doAction( 
$action, $group, $key, $code, $value );
 
@@ -193,7 +193,7 @@
                                }
 
                                $name = wfMsg( 'translate-manage-import-diff',
-                                       '<code style="font-weight:normal;">' . 
htmlspecialchars($key) . '</code>',
+                                       '<code style="font-weight:normal;">' . 
htmlspecialchars( $key ) . '</code>',
                                        implode( ' ', $act )
                                );
 
@@ -204,23 +204,23 @@
 
                if ( !$process ) {
                        $collection->filter( 'hastranslation', false );
-                       $keys = array_keys($collection->keys());
+                       $keys = array_keys( $collection->keys() );
 
-                       $diff = array_diff( $keys, array_keys($messages) );
+                       $diff = array_diff( $keys, array_keys( $messages ) );
 
                        foreach ( $diff as $s ) {
                                $name = wfMsgHtml( 
'translate-manage-import-deleted',
-                                       '<code style="font-weight:normal;">' . 
htmlspecialchars($s) . '</code>'
+                                       '<code style="font-weight:normal;">' . 
htmlspecialchars( $s ) . '</code>'
                                );
                                $text = 
TranslateUtils::convertWhiteSpaceToHTML(  $collection[$s]->translation() );
                                $changed[] = $this->makeSectionElement( $name, 
'deleted', $text );
                        }
                }
 
-               if ( $process || (!count($changed) && $code !== 'en') ) {
-                       if ( !count($changed) ) $this->out->addWikiMsg( 
'translate-manage-nochanges-other' );
+               if ( $process || ( !count( $changed ) && $code !== 'en' ) ) {
+                       if ( !count( $changed ) ) $this->out->addWikiMsg( 
'translate-manage-nochanges-other' );
 
-                       if ( !count($changed) || strpos( 
$changed[count($changed)-1], '<li>' ) !== 0 ) $changed[] = '<ul>';
+                       if ( !count( $changed ) || strpos( $changed[count( 
$changed ) - 1], '<li>' ) !== 0 ) $changed[] = '<ul>';
 
                        $message = wfMsgExt( 'translate-manage-import-done', 
'parseinline' );
                        $changed[] = "<li>$message</li></ul>";
@@ -229,7 +229,7 @@
 
                        // END
 
-                       if ( count($changed) ) {
+                       if ( count( $changed ) ) {
                                if ( $code === 'en' ) {
                                        $this->out->addWikiMsg( 
'translate-manage-intro-en' );
                                } else {
@@ -249,9 +249,9 @@
                return $alldone;
        }
 
+       // FIXME: lot of duplication with SpecialManageGroups::doAction()
        protected function doAction( $action, $group, $key, $code, $message, 
$comment = '' ) {
                if ( $action === 'import' || $action === 'conflict' ) {
-
                        if ( $action === 'import' ) {
                                $comment = wfMsgForContentNoTrans( 
'translate-manage-import-summary' );
                        } else {
@@ -260,10 +260,11 @@
                        }
 
                        $title = self::makeTitle( $group, $key, $code );
+
                        return $this->doImport( $title, $message, $comment );
                } elseif ( $action === 'ignore' ) {
                        return array( 'translate-manage-import-ignore', $key );
-               } elseif ( $action === 'fuzzy' ) {
+               } elseif ( $action === 'fuzzy' && $code != 'en' ) {
                        return $this->doFuzzy( $title, $message, $comment );
                } else {
                        throw new MWException( "Unhandled action $action" );
@@ -274,9 +275,10 @@
                return wfTimestamp() - $this->time >= $this->processingTime;
        }
 
+       // FIXME: lot of duplication with SpecialManageGroups::doImport()
        protected function doImport( $title, $message, $comment, $user = null ) 
{
                $flags = EDIT_FORCE_BOT;
-               $article = new Article( $title );                
+               $article = new Article( $title );
                $status = $article->doEdit( $message, $comment, $flags );
                $success = $status->isOK();
 
@@ -289,17 +291,18 @@
                }
        }
 
+       // FIXME: lot of duplication with SpecialManageGroups::doFuzzy()
        protected function doFuzzy( $title, $message, $comment ) {
                $dbw = wfGetDB( DB_MASTER );
                $titleText = $title->getDBKey();
                $condArray = array(
-                       'page_namespace'    => $title->getNamespace(),
+                       'page_namespace' => $title->getNamespace(),
                        'page_latest=rev_id',
                        'rev_text_id=old_id',
                        "page_title LIKE '{$dbw->escapeLike( $titleText )}/%%'"
                );
 
-               $rows = $dbr->select(
+               $rows = $dbw->select(
                        array( 'page', 'revision', 'text' ),
                        array( 'page_title', 'page_namespace', 'old_text', 
'old_flags' ),
                        $conds,
@@ -308,6 +311,7 @@
 
                $changed = array();
                $fuzzybot = self::getFuzzyBot();
+
                foreach ( $rows as $row ) {
                        $ttitle = Title::makeTitle( $row->page_namespace, 
$row->page_title );
 
@@ -322,7 +326,7 @@
 
                }
 
-               if ( count($changed) === count($rows) ) {
+               if ( count( $changed ) === count( $rows ) ) {
                        $comment = wfMsgForContentNoTrans( 
'translate-manage-import-summary' );
                        $changed[] = $this->doImport( $title, $message, 
$comment );
                }
@@ -338,17 +342,25 @@
                );
        }
 
+       // FIXME: duplicate of SpecialManageGroups::getFuzzyBot()
        protected static function getFuzzyBot() {
                global $wgTranslateFuzzyBotName;
+
                $user = User::newFromName( $wgTranslateFuzzyBotName );
-               if ( !$user->isLoggedIn() ) $user->addToDatabase();
+
+               if ( !$user->isLoggedIn() ) {
+                       $user->addToDatabase();
+               }
+
                return $user;
        }
 
+       // FIXME: duplicate of SpecialManageGroups::makeTitle()
        protected static function makeTitle( $group, $key, $code ) {
                $ns = $group->getNamespace();
                $titlekey = "$key/$code";
-               return Title::makeTitleSafe( $ns, $titlekey ); 
+
+               return Title::makeTitleSafe( $ns, $titlekey );
        }
 
        protected function makeSectionElement( $legend, $type, $content ) {
@@ -361,4 +373,4 @@
                        Xml::tags( 'div', $contentParams, $content )
                );
        }
-}
\ No newline at end of file
+}



_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to