summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'MLEB/Translate/TranslateUtils.php')
-rw-r--r--MLEB/Translate/TranslateUtils.php123
1 files changed, 91 insertions, 32 deletions
diff --git a/MLEB/Translate/TranslateUtils.php b/MLEB/Translate/TranslateUtils.php
index 3cb7ee5e..67b7ec3a 100644
--- a/MLEB/Translate/TranslateUtils.php
+++ b/MLEB/Translate/TranslateUtils.php
@@ -8,6 +8,7 @@
*/
use MediaWiki\MediaWikiServices;
+use MediaWiki\Permissions\Authority;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
@@ -133,7 +134,8 @@ class TranslateUtils {
return null;
}
- $wiki = ContentHandler::getContentText( $revision->getContent( SlotRecord::MAIN ) );
+ $content = $revision->getContent( SlotRecord::MAIN );
+ $wiki = ( $content instanceof TextContent ) ? $content->getText() : null;
if ( !$wiki ) {
return null;
@@ -199,7 +201,7 @@ class TranslateUtils {
}
unset( $row );
- usort( $rows, function ( $a, $b ) {
+ usort( $rows, static function ( $a, $b ) {
$x = strcmp( $a->lang, $b->lang );
if ( !$x ) {
// descending order
@@ -366,7 +368,7 @@ class TranslateUtils {
*/
public static function assetPath( $path ) {
global $wgExtensionAssetsPath;
-
+ // @phan-suppress-next-line PhanPossiblyUndeclaredVariable
return "$wgExtensionAssetsPath/Translate/$path";
}
@@ -435,47 +437,39 @@ class TranslateUtils {
}
/**
- * Parses list of language codes to an array.
- * @param string $codes Comma separated list of language codes. "*" for all.
- * @return string[] Language codes.
- */
- public static function parseLanguageCodes( $codes ) {
- $langs = array_map( 'trim', explode( ',', $codes ) );
- if ( $langs[0] === '*' ) {
- $languages = Language::fetchLanguageNames();
- ksort( $languages );
- $langs = array_keys( $languages );
- }
-
- return $langs;
- }
-
- /**
* Get a DB handle suitable for read and read-for-write cases
*
- * @return \Wikimedia\Rdbms\IDatabase Master for HTTP POST, CLI, DB already changed;
+ * @return \Wikimedia\Rdbms\IDatabase Primary for HTTP POST, CLI, DB already changed;
* replica otherwise
*/
public static function getSafeReadDB() {
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
- $index = self::shouldReadFromMaster() ? DB_MASTER : DB_REPLICA;
+ $index = self::shouldReadFromPrimary() ? DB_PRIMARY : DB_REPLICA;
return $lb->getConnectionRef( $index );
}
/**
- * Check whether master should be used for reads to avoid reading stale data.
+ * Check whether primary should be used for reads to avoid reading stale data.
*
* @return bool
*/
- public static function shouldReadFromMaster() {
+ public static function shouldReadFromPrimary() {
$lb = MediaWikiServices::getInstance()->getDBLoadBalancer();
// Parsing APIs need POST for payloads but are read-only, so avoid spamming
- // the master then. No good way to check this at the moment...
+ // the primary then. No good way to check this at the moment...
if ( PageTranslationHooks::$renderingContext ) {
return false;
}
+ if ( method_exists( $lb, 'hasOrMadeRecentPrimaryChanges' ) ) {
+ // MW 1.37+
+ return PHP_SAPI === 'cli' ||
+ RequestContext::getMain()->getRequest()->wasPosted() ||
+ $lb->hasOrMadeRecentPrimaryChanges();
+ }
+
+ // MW >=1.36
return PHP_SAPI === 'cli' ||
RequestContext::getMain()->getRequest()->wasPosted() ||
$lb->hasOrMadeRecentMasterChanges();
@@ -494,7 +488,7 @@ class TranslateUtils {
$title = MediaWikiServices::getInstance()
->getSpecialPageFactory()->getPage( 'Translate' )->getPageTitle();
- return $title->getLocalURL( [
+ return $title->getFullURL( [
'showMessage' => $handle->getInternalKey(),
'group' => $handle->getGroup()->getId(),
'language' => $handle->getCode(),
@@ -557,15 +551,80 @@ class TranslateUtils {
}
/**
- * Add support for <= 1.34. Wrapper method to fetch the the MW version
- * @return string
+ * Wrapper around WikiPage::doEditContent() or WikiPage::doUserEditContent(),
+ * depending on the version. The Translate extenion supports MediaWiki 1.35+
+ * but ::doUserEditContent() is only available in 1.36. Once only 1.36+ is
+ * required this should be replaced with using ::doUserEditContent() directly
+ *
+ * @see WikiPage::doEditContent()
+ * @see WikiPage::doUserEditContent()
+ *
+ * The defaults for the parameters here match the defaults for both of the
+ * WikiPage methods, and the parameter order here matches that of
+ * ::doUserEditContent(). This function requires a Authority even though
+ * ::doEditContent() does not, because that falls back to the global $wgUser.
+ *
+ * @param WikiPage $wikiPage
+ * @param Content $content
+ * @param Authority $user for versions before 1.36 when Authority didn't exist, use a
+ * User object
+ * @param string|CommentStoreComment $summary
+ * @param int $flags
+ * @param bool|int $originalRevId
+ * @param array|null $tags
+ * @param int $undidRevId
+ * @return Status
*/
- public static function getMWVersion(): string {
- if ( defined( 'MW_VERSION' ) ) {
- return MW_VERSION;
+ public static function doPageEdit(
+ WikiPage $wikiPage,
+ Content $content,
+ $user,
+ $summary,
+ $flags = 0,
+ $originalRevId = false,
+ $tags = [],
+ $undidRevId = 0
+ ) {
+ if ( method_exists( $wikiPage, 'doUserEditContent' ) ) {
+ // MW 1.36+
+ return $wikiPage->doUserEditContent(
+ $content,
+ $user,
+ $summary,
+ $flags,
+ $originalRevId,
+ $tags,
+ $undidRevId
+ );
}
- global $wgVersion;
- return $wgVersion;
+ // MW 1.35
+ // Note: doEditContent() has an extra parameter, $serialFormat, but that has been
+ // ignored since 1.32.
+ return $wikiPage->doEditContent(
+ $content,
+ $summary,
+ $flags,
+ $originalRevId,
+ $user,
+ null, // $serialFormat
+ $tags,
+ $undidRevId
+ );
}
+
+ /**
+ * Checks whether a language code is supported for translation at the wiki level.
+ * Note that it is possible that message groups define other language codes which
+ * are not supported by the wiki, in which case this function would return false
+ * for those.
+ *
+ * @param string $code
+ * @return bool
+ */
+ public static function isSupportedLanguageCode( string $code ): bool {
+ $all = self::getLanguageNames( null );
+ return isset( $all[ $code ] );
+ }
+
}