MediaWiki  1.33.0
Updater.php
Go to the documentation of this file.
1 <?php
8 namespace LocalisationUpdate;
9 
13 class Updater {
21  public function isDirectory( $path ) {
22  $filename = basename( $path );
23  return strpos( $filename, '*' ) !== false;
24  }
25 
35  public function expandRemotePath( $info, $repos ) {
36  $pattern = $repos[$info['repo']];
37  unset( $info['repo'], $info['orig'] );
38 
39  // This assumes all other keys are used as variables
40  // in the pattern. For example name -> %NAME%.
41  $keys = [];
42  foreach ( array_keys( $info ) as $key ) {
43  $keys[] = '%' . strtoupper( $key ) . '%';
44  }
45 
46  $values = array_values( $info );
47  return str_replace( $keys, $values, $pattern );
48  }
49 
57  public function readMessages( ReaderFactory $readerFactory, array $files ) {
58  $messages = [];
59 
60  foreach ( $files as $filename => $contents ) {
61  $reader = $readerFactory->getReader( $filename );
62  try {
63  $parsed = $reader->parse( $contents );
64  } catch ( \Exception $e ) {
65  trigger_error( __METHOD__ . ": Unable to parse messages from $filename", E_USER_WARNING );
66  continue;
67  }
68 
69  foreach ( $parsed as $code => $langMessages ) {
70  if ( !isset( $messages[$code] ) ) {
71  $messages[$code] = [];
72  }
73  $messages[$code] = array_merge( $messages[$code], $langMessages );
74  }
75 
76  $c = array_sum( array_map( 'count', $parsed ) );
77  // Useful for debugging, maybe create interface to pass this to the script?
78  # echo "$filename with " . get_class( $reader ) . " and $c\n";
79  }
80 
81  return $messages;
82  }
83 
92  public function findChangedTranslations( $origin, $remote, $blacklist = [] ) {
93  $changed = [];
94  foreach ( $remote as $key => $value ) {
95  if ( isset( $blacklist[$key] ) ) {
96  continue;
97  }
98 
99  if ( !isset( $origin[$key] ) || $value !== $origin[$key] ) {
100  $changed[$key] = $value;
101  }
102  }
103  return $changed;
104  }
105 
113  public function fetchFiles( FetcherFactory $factory, $path ) {
114  $fetcher = $factory->getFetcher( $path );
115 
116  if ( $this->isDirectory( $path ) ) {
117  $files = $fetcher->fetchDirectory( $path );
118  } else {
119  $files = [ $path => $fetcher->fetchFile( $path ) ];
120  }
121 
122  // Remove files which were not found
123  return array_filter( $files );
124  }
125 
126  public function execute(
127  Finder $finder,
128  ReaderFactory $readerFactory,
129  FetcherFactory $fetcherFactory,
130  array $repos,
131  $logger
132  ) {
133  $components = $finder->getComponents();
134 
135  $updatedMessages = [];
136 
137  foreach ( $components as $key => $info ) {
138  $logger->logInfo( "Updating component $key" );
139 
140  $originFiles = $this->fetchFiles( $fetcherFactory, $info['orig'] );
141  $remotePath = $this->expandRemotePath( $info, $repos );
142  try {
143  $remoteFiles = $this->fetchFiles( $fetcherFactory, $remotePath );
144  } catch ( \Exception $e ) {
145  $logger->logError( __METHOD__ . ": Unable to fetch messages from $remotePath" );
146  continue;
147  }
148 
149  if ( $remoteFiles === [] ) {
150  // Small optimization: if nothing to compare with, skip
151  continue;
152  }
153 
154  $originMessages = $this->readMessages( $readerFactory, $originFiles );
155  $remoteMessages = $this->readMessages( $readerFactory, $remoteFiles );
156 
157  if ( !isset( $remoteMessages['en'] ) ) {
158  // Could not find remote messages
159  continue;
160  }
161 
162  // If remote translation in English is not present or differs, we do not want
163  // translations for other languages for those messages, as they are either not
164  // used in this version of code or can be incompatible.
165  $forbiddenKeys = $this->findChangedTranslations(
166  $originMessages['en'],
167  $remoteMessages['en']
168  );
169 
170  // We never accept updates for English strings
171  unset( $originMessages['en'], $remoteMessages['en'] );
172 
173  // message: string in all languages; translation: string in one language.
174  foreach ( $remoteMessages as $language => $remoteTranslations ) {
175  // Check for completely new languages
176  $originTranslations = [];
177  if ( isset( $originMessages[$language] ) ) {
178  $originTranslations = $originMessages[$language];
179  }
180 
181  $updatedTranslations = $this->findChangedTranslations(
182  $originTranslations,
183  $remoteTranslations,
184  $forbiddenKeys
185  );
186 
187  // Avoid empty arrays
188  if ( $updatedTranslations === [] ) {
189  continue;
190  }
191 
192  if ( !isset( $updatedMessages[$language] ) ) {
193  $updatedMessages[$language] = [];
194  }
195 
196  // In case of conflicts, which should not exist, this prefers the
197  // first translation seen.
198  $updatedMessages[$language] += $updatedTranslations;
199  }
200  }
201 
202  return $updatedMessages;
203  }
204 }
LocalisationUpdate\Updater\findChangedTranslations
findChangedTranslations( $origin, $remote, $blacklist=[])
Find new and changed translations in $remote and returns them.
Definition: Updater.php:92
$messages
$messages
Definition: LogTests.i18n.php:8
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
LocalisationUpdate\Updater\isDirectory
isDirectory( $path)
Whether the path is a pattern and thus we need to use appropriate code for fetching directories.
Definition: Updater.php:21
LocalisationUpdate\Updater\expandRemotePath
expandRemotePath( $info, $repos)
Expands repository relative path to full url with the given repository patterns.
Definition: Updater.php:35
LocalisationUpdate\Updater
Executes the localisation update.
Definition: Updater.php:13
$code
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping see BaseTemplate::getToolbox and BaseTemplate::makeListItem for details on the format of individual items inside of this array or by returning and letting standard HTTP rendering take place modifiable or by returning false and taking over the output modifiable & $code
Definition: hooks.txt:780
array
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2162
LocalisationUpdate
Definition: Fetcher.php:8
$value
$value
Definition: styleTest.css.php:49
LocalisationUpdate\ReaderFactory
Constructs readers for files based on the names.
Definition: ReaderFactory.php:13
LocalisationUpdate\Updater\fetchFiles
fetchFiles(FetcherFactory $factory, $path)
Fetches files from given Url pattern.
Definition: Updater.php:113
$path
$path
Definition: NoLocalSettings.php:25
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$keys
$keys
Definition: testCompression.php:67
LocalisationUpdate\Updater\readMessages
readMessages(ReaderFactory $readerFactory, array $files)
Parses translations from given list of files.
Definition: Updater.php:57
LocalisationUpdate\ReaderFactory\getReader
getReader( $filename)
Constructs a suitable reader for a given path.
Definition: ReaderFactory.php:20
LocalisationUpdate\Updater\execute
execute(Finder $finder, ReaderFactory $readerFactory, FetcherFactory $fetcherFactory, array $repos, $logger)
Definition: Updater.php:126