MediaWiki  1.30.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  ) {
132  $components = $finder->getComponents();
133 
134  $updatedMessages = [];
135 
136  foreach ( $components as $key => $info ) {
137  $originFiles = $this->fetchFiles( $fetcherFactory, $info['orig'] );
138  $remoteFiles = $this->fetchFiles( $fetcherFactory, $this->expandRemotePath( $info, $repos ) );
139 
140  if ( $remoteFiles === [] ) {
141  // Small optimization: if nothing to compare with, skip
142  continue;
143  }
144 
145  $originMessages = $this->readMessages( $readerFactory, $originFiles );
146  $remoteMessages = $this->readMessages( $readerFactory, $remoteFiles );
147 
148  if ( !isset( $remoteMessages['en'] ) ) {
149  // Could not find remote messages
150  continue;
151  }
152 
153  // If remote translation in English is not present or differs, we do not want
154  // translations for other languages for those messages, as they are either not
155  // used in this version of code or can be incompatible.
156  $forbiddenKeys = $this->findChangedTranslations(
157  $originMessages['en'],
158  $remoteMessages['en']
159  );
160 
161  // We never accept updates for English strings
162  unset( $originMessages['en'], $remoteMessages['en'] );
163 
164  // message: string in all languages; translation: string in one language.
165  foreach ( $remoteMessages as $language => $remoteTranslations ) {
166  // Check for completely new languages
167  $originTranslations = [];
168  if ( isset( $originMessages[$language] ) ) {
169  $originTranslations = $originMessages[$language];
170  }
171 
172  $updatedTranslations = $this->findChangedTranslations(
173  $originTranslations,
174  $remoteTranslations,
175  $forbiddenKeys
176  );
177 
178  // Avoid empty arrays
179  if ( $updatedTranslations === [] ) {
180  continue;
181  }
182 
183  if ( !isset( $updatedMessages[$language] ) ) {
184  $updatedMessages[$language] = [];
185  }
186 
187  // In case of conflicts, which should not exist, this prefers the
188  // first translation seen.
189  $updatedMessages[$language] += $updatedTranslations;
190  }
191  }
192 
193  return $updatedMessages;
194  }
195 }
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:2141
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:781
$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.