98 $threads = $this->
getOption(
'threads', 1 );
99 if ( $threads < 1 || $threads != intval( $threads ) ) {
100 $this->
output(
"Invalid thread count specified; running single-threaded.\n" );
104 $this->
output(
"Threaded rebuild is not supported on Windows; running single-threaded.\n" );
107 if ( $threads > 1 && ( !extension_loaded(
'sockets' ) || !function_exists(
'pcntl_fork' ) ) ) {
108 $this->
output(
"Threaded rebuild requires ext-pcntl and ext-sockets; running single-threaded.\n" );
112 $conf = $this->
getConfig()->get( MainConfigNames::LocalisationCacheConf );
114 $conf[
'manualRecache'] =
false;
115 $conf[
'forceRecache'] = $force || !empty( $conf[
'forceRecache'] );
117 $conf[
'storeDirectory'] = $this->
getOption(
'outdir' );
120 if ( $this->
hasOption(
'store-class' ) ) {
121 $conf[
'storeClass'] = $this->
getOption(
'store-class' );
125 $services = MediaWikiServices::getInstance();
128 LocalisationCache::CONSTRUCTOR_OPTIONS,
130 $services->getMainConfig()
132 LocalisationCache::getStoreFromConf( $conf, $this->getConfig()->get( MainConfigNames::CacheDirectory ) ),
133 LoggerFactory::getInstance(
'localisation' ),
134 $this->hasOption(
'skip-message-purge' ) ? [] :
135 [
static function () use ( $services ) {
136 MessageBlobStore::clearGlobalCacheEntry( $services->getMainWANObjectCache() );
138 $services->getLanguageNameUtils(),
139 $services->getHookContainer()
142 $allCodes = array_keys( $services
143 ->getLanguageNameUtils()
144 ->getLanguageNames( LanguageNameUtils::AUTONYMS, LanguageNameUtils::SUPPORTED ) );
146 # Validate requested languages
147 $codes = array_intersect( $allCodes,
148 explode(
',', $this->
getOption(
'lang' ) ) );
149 # Bailed out if nothing is left
150 if ( count( $codes ) == 0 ) {
151 $this->
fatalError(
'None of the languages specified exists.' );
154 # By default get all languages
160 $total = count( $codes );
163 if ( $threads <= 1 ) {
165 $numRebuilt += $this->doRebuild( $codes, $lc, $force );
168 $chunks = array_chunk( $codes, ceil( count( $codes ) / $threads ) );
172 foreach ( $chunks as $codes ) {
176 if ( !socket_create_pair( AF_UNIX, SOCK_STREAM, 0, $socketpair ) ) {
177 $this->
fatalError(
'socket_create_pair failed' );
184 } elseif ( $pid === 0 ) {
187 mt_srand( getmypid() );
189 $numRebuilt = $this->doRebuild( $codes, $lc, $force );
191 $msg = strval( $numRebuilt ) .
"\n";
192 socket_write( $socketpair[1], $msg, strlen( $msg ) );
197 $sockets[$pid] = $socketpair[0];
202 foreach ( $sockets as $pid => $socket ) {
204 pcntl_waitpid( $pid, $status );
206 if ( pcntl_wifexited( $status ) ) {
207 $code = pcntl_wexitstatus( $status );
209 $this->
output(
"Pid $pid exited with status $code !!\n" );
212 $res = socket_read( $socket, 512, PHP_NORMAL_READ );
213 if (
$res ===
false ) {
214 $this->
output(
"socket_read failed in parent\n" );
216 $numRebuilt += intval(
$res );
221 $parentStatus |= $code;
222 } elseif ( pcntl_wifsignaled( $status ) ) {
223 $signum = pcntl_wtermsig( $status );
224 $this->
output(
"Pid $pid terminated by signal $signum !!\n" );
230 $this->
output(
"$numRebuilt languages rebuilt out of $total\n" );
231 if ( $numRebuilt === 0 ) {
232 $this->
output(
"Use --force to rebuild the caches which are still fresh.\n" );
234 if ( $parentStatus ) {
235 $this->
fatalError(
'Failed.', $parentStatus );