MediaWiki  1.29.2
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  ) {
132 
133  $components = $finder->getComponents();
134 
135  $updatedMessages = [];
136 
137  foreach ( $components as $key => $info ) {
138  $originFiles = $this->fetchFiles( $fetcherFactory, $info['orig'] );
139  $remoteFiles = $this->fetchFiles( $fetcherFactory, $this->expandRemotePath( $info, $repos ) );
140 
141  if ( $remoteFiles === [] ) {
142  // Small optimization: if nothing to compare with, skip
143  continue;
144  }
145 
146  $originMessages = $this->readMessages( $readerFactory, $originFiles );
147  $remoteMessages = $this->readMessages( $readerFactory, $remoteFiles );
148 
149  if ( !isset( $remoteMessages['en'] ) ) {
150  // Could not find remote messages
151  continue;
152  }
153 
154  // If remote translation in English is not present or differs, we do not want
155  // translations for other languages for those messages, as they are either not
156  // used in this version of code or can be incompatible.
157  $forbiddenKeys = $this->findChangedTranslations(
158  $originMessages['en'],
159  $remoteMessages['en']
160  );
161 
162  // We never accept updates for English strings
163  unset( $originMessages['en'], $remoteMessages['en'] );
164 
165  // message: string in all languages; translation: string in one language.
166  foreach ( $remoteMessages as $language => $remoteTranslations ) {
167  // Check for completely new languages
168  $originTranslations = [];
169  if ( isset( $originMessages[$language] ) ) {
170  $originTranslations = $originMessages[$language];
171  }
172 
173  $updatedTranslations = $this->findChangedTranslations(
174  $originTranslations,
175  $remoteTranslations,
176  $forbiddenKeys
177  );
178 
179  // Avoid empty arrays
180  if ( $updatedTranslations === [] ) {
181  continue;
182  }
183 
184  if ( !isset( $updatedMessages[$language] ) ) {
185  $updatedMessages[$language] = [];
186  }
187 
188  // In case of conflicts, which should not exist, this prefers the
189  // first translation seen.
190  $updatedMessages[$language] += $updatedTranslations;
191  }
192  }
193 
194  return $updatedMessages;
195  }
196 }
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
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
LocalisationUpdate
Definition: Fetcher.php:8
$value
$value
Definition: styleTest.css.php:45
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
$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 probably a stub 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:783
$path
$path
Definition: NoLocalSettings.php:26
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:65
LocalisationUpdate\Updater\execute
execute(Finder $finder, ReaderFactory $readerFactory, FetcherFactory $fetcherFactory, array $repos)
Definition: Updater.php:126
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
array
the array() calling protocol came about after MediaWiki 1.4rc1.