Nikerabbit has uploaded a new change for review. https://gerrit.wikimedia.org/r/117385
Change subject: [WIP] LU rewrite ...................................................................... [WIP] LU rewrite * Start rewriting the "controller". This should go to separate class at some point. * Fetchers. Need HTTP fetcher and tests. This commit is likely to be split into smaller commits Change-Id: Ib683b02ec510e5cb7ea0acd0757067f584ed164d --- M LocalisationUpdate.php A fetcher/Fetcher.php M update.php 3 files changed, 143 insertions(+), 1 deletion(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/LocalisationUpdate refs/changes/85/117385/1 diff --git a/LocalisationUpdate.php b/LocalisationUpdate.php index eac97e9..b1f1d01 100644 --- a/LocalisationUpdate.php +++ b/LocalisationUpdate.php @@ -11,6 +11,13 @@ */ $wgLocalisationUpdateDirectory = false; +$wgLocalisationUpdateRepositories = array( + 'mediawiki' => + 'https://git.wikimedia.org/raw/mediawiki%2Fcore.git/HEAD/%PATH%', + 'extension' => + 'https://git.wikimedia.org/raw/mediawiki%2Fextensions%2F%NAME%.git/HEAD/%PATH%', +); + /** * These should point to either an HTTP-accessible file or local file system. * $1 is the name of the repo (for extensions) and $2 is the name of file in the repo. diff --git a/fetcher/Fetcher.php b/fetcher/Fetcher.php new file mode 100644 index 0000000..c1995b5 --- /dev/null +++ b/fetcher/Fetcher.php @@ -0,0 +1,34 @@ +<?php +/** + * @file + * @author Niklas Laxström + * @license GPL-2.0+ + */ + +/** + * ... + */ +interface LU_Fetcher { + public function fetchFile( $path ); + public function fetchDirector( $path ); +} + +class LU_FileSystemFetcher implements LU_Fetcher { + public function fetchFile( $path ) { + if ( !is_readable( $path ) ) { + return false; + } + + return file_get_contents( $path ); + } + + public function fetchDirectory( $path ) { + $data = array(); + foreach ( glob( "$path/*" ) as $file ) { + if ( is_readable( $file ) ) { + $data[$file] = file_get_contents( $file ); + } + } + return $data; + } +} diff --git a/update.php b/update.php index 04ea64c..72e740c 100644 --- a/update.php +++ b/update.php @@ -30,8 +30,109 @@ ini_set( "max_execution_time", 0 ); ini_set( 'memory_limit', -1 ); -LocalisationUpdate::updateMessages( $options ); +function isDirectory( $path ) { + $filename = basename( $path ); + return strpos( $filename, '.' ) === false; +} + +function expandRemotePath( $info, $repos ) { + $pattern = $repos[$info['type']]; + unset( $info['type'], $info['orig'] ); + $keys = array_map( 'strtoupper', array_keys( $info ) ); + $values = array_values( $info ); + return str_replace( $keys, $values, $pattern ); +} + +function readMessages( LU_ReaderFactory $readerFactory, array $files ) { + $messages = array(); + foreach ( $files as $filename => $contents ) { + $reader = $readerFactory->getReader( $filename ); + foreach ( $reader->parse( $contents ) as $language => $langMessages ) { + if ( !isset( $messages[$code] ) ) { + $messages[$code] = array(); + } + $messages[$code] = array_merge( $messages[$code], $langMessages ); + } + } + return $messages; +} + +function findChangedKeys( $origin, $remote ) { + $changed = array(); + foreach ( $origin as $key => $value ) { + if ( !isset( $remote[$key] ) || $remote[$key] !== $value ) { + $changed[] = $key; + } + } + return $changed; +} + + +global $wgExtensionMessagesFiles, $wgMessagesDirs, $IP; +global $wgLocalisationUpdateRepositories; + +$finder = new LU_Finder( $wgExtensionMessagesFiles, $wgMessagesDirs ); +$readerFactory = new LU_ReaderFactory(); +$fetcherFactory = new LU_FetcherFactory(); + + +$components = $finder->getComponents(); +// Special case the core for now +$components['mediawiki'] = array( + 'repo' => 'mediawiki', + 'orig' => 'languages/messages', + 'path' => "$IP/languages/messages", +); + +$updatedMessages = array(); + +foreach ( $components as $key => $info ) { + $originFetcher = new LU_FileSystemFetcher(); + $remoteFetcher = $fetcherFactory->getFetcher( $info ); + + $originPath = $info['orig']; + $remotePath = expandRemotePath( $info['orig'], $wgLocalisationUpdateRepositories ); + + // Assuming that both paths are directories at the same time. + if ( isDirectory( $originPath ) ) { + $originFiles = $originFetcher->fetchDirectory( $originPath ); + $remoteFiles = $remoteFetcher->fetchDirectory( $remotePath ); + } else { + $originFiles = array( $originPath => $originFetcher->fetchFile( $originPath ); + $remoteFiles = array( $remotePath => $originFetcher->fetchFile( $originPath ); + } + + $originMessages = readMessages( $readerFactory, $originFiles ); + $remoteMessages = readMessages( $readerFactory, $remoteFiles ); + + $forbiddenKeys = findChangedKeys( $originMessages['en'], $remoteMessages['en'] ); + $forbiddenKeys = array_flip( $forbiddenKeys ); + + unset( $originMessages['en'], $remoteMessages['en'] ) + + foreach ( $remoteMessages as $language => $messages ) { + foreach ( $messages as $key => $value ) { + if ( isset( $forbiddenKeys[$key] ) ) { + continue; + } + + // Allow new translations + if ( isset( $originMessages[$language][$key] ) + && $originMessages[$language][$key] === $value + ) { + continue; + } + + $updatedMessages[$language][$key] = $value; + } + } +} + +#LocalisationUpdate::updateMessages( $options ); $endtime = microtime( true ); $totaltime = ( $endtime - $starttime ); print "All done in " . $totaltime . " seconds\n"; + + + -- To view, visit https://gerrit.wikimedia.org/r/117385 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ib683b02ec510e5cb7ea0acd0757067f584ed164d Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/LocalisationUpdate Gerrit-Branch: json-rewrite Gerrit-Owner: Nikerabbit <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
