Skip to content

Commit

Permalink
Avoid loading extensions when building localization cache (#412)
Browse files Browse the repository at this point in the history
* Avoid loading extensions when building localization cache

* Refactor into a separate class

* Simplify by using ExtensionProcessor

---------

Co-authored-by: Thomas Arrow <tarrow@users.noreply.github.com>
  • Loading branch information
AndrewKostka and tarrow authored Jan 25, 2024
1 parent 99b1625 commit ded9dcd
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 0 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

Tags have the format: `<MediaWiki core version>-<PHP Version>-<date>-<build number>`

## 1.39-7.4-20240125-0
- Load conflicting extensions localisation at cache build time (T354953)

## 1.39-7.4-20240116-0
- On failure, propagate status code from Platform API to clients (T343744)

Expand Down
5 changes: 5 additions & 0 deletions dist-persist/wbstack/src/Settings/LocalSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
define( 'STDERR', fopen( 'php://stderr', 'w' ) );
}

require_once __DIR__ . '/Localization.php';

// Define some conditions to switch behaviour on
$wwDomainSaysLocal = preg_match("/(\w\.localhost)/", $_SERVER['SERVER_NAME']) === 1;
$wwDomainIsMaintenance = $wikiInfo->requestDomain === 'maintenance';
$wwIsPhpUnit = isset( $maintClass ) && $maintClass === 'PHPUnitMaintClass';
$wwIsLocalisationRebuild = basename( $_SERVER['SCRIPT_NAME'] ) === 'rebuildLocalisationCache.php';
$wwLocalization = new Localization( $wgExtensionMessagesFiles, $wgMessagesDirs, $wgBaseDirectory, $wwIsLocalisationRebuild );

$wwUseMailgunExtension = true; // default for wbstack
if (getenv('MW_MAILGUN_DISABLED') === 'yes') {
Expand Down Expand Up @@ -462,10 +465,12 @@ function onSkinTemplateNavigationUniversal( SkinTemplate $skin, array &$links )
# QuestyCaptcha
$wwUseQuestyCaptcha = $wikiInfo->getSetting('wwUseQuestyCaptcha');
if ($wwUseQuestyCaptcha) {
$wwLocalization->loadExtension( 'ConfirmEdit/ReCaptchaNoCaptcha' );
wfLoadExtensions([ 'ConfirmEdit', 'ConfirmEdit/QuestyCaptcha' ]);
$wgCaptchaClass = 'QuestyCaptcha';
$wgCaptchaQuestions = json_decode($wikiInfo->getSetting('wwCaptchaQuestions'), true);
} else {
$wwLocalization->loadExtension( 'ConfirmEdit/QuestyCaptcha' );
wfLoadExtensions([ 'ConfirmEdit', 'ConfirmEdit/ReCaptchaNoCaptcha' ]);
$wgCaptchaClass = 'ReCaptchaNoCaptcha';
$wgReCaptchaSendRemoteIP = true;
Expand Down
43 changes: 43 additions & 0 deletions dist-persist/wbstack/src/Settings/Localization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

class Localization {
private $extensionMessagesFiles;
private $messagesDirs;
private $baseDirectory;
private $isLocalisationRebuild;

public function __construct(
&$extensionMessagesFiles,
&$messagesDirs,
$baseDirectory,
$isLocalisationRebuild
) {
$this->extensionMessagesFiles = &$extensionMessagesFiles;
$this->messagesDirs = &$messagesDirs;
$this->baseDirectory = $baseDirectory;
$this->isLocalisationRebuild = $isLocalisationRebuild;
}

private function addLocalization( $path ) {
$processor = new ExtensionProcessor();
$processor->extractInfoFromFile( $path );
$info = $processor->getExtractedInfo();

if ( isset( $info[ 'globals' ][ 'wgExtensionMessagesFiles' ] ) ) {
$this->extensionMessagesFiles = array_merge(
$this->extensionMessagesFiles, $info[ 'globals' ][ 'wgExtensionMessagesFiles' ]
);
}
if ( isset( $info[ 'globals' ][ 'wgMessagesDirs' ] ) ) {
$this->messagesDirs = array_merge(
$this->messagesDirs, $info[ 'globals' ][ 'wgMessagesDirs' ]
);
}
}

public function loadExtension( $name ) {
if ( $this->isLocalisationRebuild ) {
self::addLocalization( "$this->baseDirectory/extensions/" . $name . "/extension.json" );
}
}
}
5 changes: 5 additions & 0 deletions dist/wbstack/src/Settings/LocalSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
define( 'STDERR', fopen( 'php://stderr', 'w' ) );
}

require_once __DIR__ . '/Localization.php';

// Define some conditions to switch behaviour on
$wwDomainSaysLocal = preg_match("/(\w\.localhost)/", $_SERVER['SERVER_NAME']) === 1;
$wwDomainIsMaintenance = $wikiInfo->requestDomain === 'maintenance';
$wwIsPhpUnit = isset( $maintClass ) && $maintClass === 'PHPUnitMaintClass';
$wwIsLocalisationRebuild = basename( $_SERVER['SCRIPT_NAME'] ) === 'rebuildLocalisationCache.php';
$wwLocalization = new Localization( $wgExtensionMessagesFiles, $wgMessagesDirs, $wgBaseDirectory, $wwIsLocalisationRebuild );

$wwUseMailgunExtension = true; // default for wbstack
if (getenv('MW_MAILGUN_DISABLED') === 'yes') {
Expand Down Expand Up @@ -462,10 +465,12 @@ function onSkinTemplateNavigationUniversal( SkinTemplate $skin, array &$links )
# QuestyCaptcha
$wwUseQuestyCaptcha = $wikiInfo->getSetting('wwUseQuestyCaptcha');
if ($wwUseQuestyCaptcha) {
$wwLocalization->loadExtension( 'ConfirmEdit/ReCaptchaNoCaptcha' );
wfLoadExtensions([ 'ConfirmEdit', 'ConfirmEdit/QuestyCaptcha' ]);
$wgCaptchaClass = 'QuestyCaptcha';
$wgCaptchaQuestions = json_decode($wikiInfo->getSetting('wwCaptchaQuestions'), true);
} else {
$wwLocalization->loadExtension( 'ConfirmEdit/QuestyCaptcha' );
wfLoadExtensions([ 'ConfirmEdit', 'ConfirmEdit/ReCaptchaNoCaptcha' ]);
$wgCaptchaClass = 'ReCaptchaNoCaptcha';
$wgReCaptchaSendRemoteIP = true;
Expand Down
43 changes: 43 additions & 0 deletions dist/wbstack/src/Settings/Localization.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

class Localization {
private $extensionMessagesFiles;
private $messagesDirs;
private $baseDirectory;
private $isLocalisationRebuild;

public function __construct(
&$extensionMessagesFiles,
&$messagesDirs,
$baseDirectory,
$isLocalisationRebuild
) {
$this->extensionMessagesFiles = &$extensionMessagesFiles;
$this->messagesDirs = &$messagesDirs;
$this->baseDirectory = $baseDirectory;
$this->isLocalisationRebuild = $isLocalisationRebuild;
}

private function addLocalization( $path ) {
$processor = new ExtensionProcessor();
$processor->extractInfoFromFile( $path );
$info = $processor->getExtractedInfo();

if ( isset( $info[ 'globals' ][ 'wgExtensionMessagesFiles' ] ) ) {
$this->extensionMessagesFiles = array_merge(
$this->extensionMessagesFiles, $info[ 'globals' ][ 'wgExtensionMessagesFiles' ]
);
}
if ( isset( $info[ 'globals' ][ 'wgMessagesDirs' ] ) ) {
$this->messagesDirs = array_merge(
$this->messagesDirs, $info[ 'globals' ][ 'wgMessagesDirs' ]
);
}
}

public function loadExtension( $name ) {
if ( $this->isLocalisationRebuild ) {
self::addLocalization( "$this->baseDirectory/extensions/" . $name . "/extension.json" );
}
}
}

0 comments on commit ded9dcd

Please sign in to comment.