summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
Diffstat (limited to 'MLEB/Translate/messagegroups/WikiPageMessageGroup.php')
-rw-r--r--MLEB/Translate/messagegroups/WikiPageMessageGroup.php87
1 files changed, 58 insertions, 29 deletions
diff --git a/MLEB/Translate/messagegroups/WikiPageMessageGroup.php b/MLEB/Translate/messagegroups/WikiPageMessageGroup.php
index 806c317b..747ab104 100644
--- a/MLEB/Translate/messagegroups/WikiPageMessageGroup.php
+++ b/MLEB/Translate/messagegroups/WikiPageMessageGroup.php
@@ -8,24 +8,31 @@
* @license GPL-2.0-or-later
*/
+use MediaWiki\MediaWikiServices;
+use MediaWiki\Revision\SlotRecord;
+
/**
* Wraps the translatable page sections into a message group.
* @ingroup PageTranslation MessageGroup
*/
-class WikiPageMessageGroup extends WikiMessageGroup implements IDBAccessObject {
+class WikiPageMessageGroup extends MessageGroupOld implements IDBAccessObject {
/**
* @var Title|string
*/
protected $title;
/**
+ * @var int
+ */
+ protected $namespace = NS_TRANSLATIONS;
+
+ /**
* @param string $id
- * @param Title|string $source
+ * @param Title|string $title
*/
- public function __construct( $id, $source ) {
+ public function __construct( $id, $title ) {
$this->id = $id;
- $this->title = $source;
- $this->namespace = NS_TRANSLATIONS;
+ $this->title = $title;
}
public function getSourceLanguage() {
@@ -50,22 +57,28 @@ class WikiPageMessageGroup extends WikiMessageGroup implements IDBAccessObject {
protected $definitions;
/**
- * @return array
+ * @return string[]
*/
public function getDefinitions() {
if ( is_array( $this->definitions ) ) {
return $this->definitions;
}
+ $title = $this->getTitle();
+
$dbr = TranslateUtils::getSafeReadDB();
- $tables = 'translate_sections';
+ $tables = [ 'page', 'translate_sections' ];
$vars = [ 'trs_key', 'trs_text' ];
- $conds = [ 'trs_page' => $this->getTitle()->getArticleID() ];
+ $conds = [
+ 'page_namespace' => $title->getNamespace(),
+ 'page_title' => $title->getDBkey(),
+ // The join condition
+ 'page_id = trs_page',
+ ];
$options = [ 'ORDER BY' => 'trs_order' ];
$res = $dbr->select( $tables, $vars, $conds, __METHOD__, $options );
$defs = [];
- $prefix = $this->getTitle()->getPrefixedDBkey() . '/';
foreach ( $res as $r ) {
$section = new TPSection();
@@ -73,16 +86,31 @@ class WikiPageMessageGroup extends WikiMessageGroup implements IDBAccessObject {
$defs[$r->trs_key] = $section->getTextWithVariables();
}
- $new_defs = [];
- foreach ( $defs as $k => $v ) {
- $k = str_replace( ' ', '_', $k );
- $new_defs[$prefix . $k] = $v;
- }
+ $groupKeys = $this->makeGroupKeys( array_keys( $defs ) );
+ $this->definitions = array_combine( $groupKeys, array_values( $defs ) );
- $this->definitions = $new_defs;
return $this->definitions;
}
+ public function makeGroupKeys( array $keys ): array {
+ $prefix = $this->getTitle()->getPrefixedDBkey() . '/';
+ foreach ( $keys as $index => $key ) {
+ $keys[$index] = $prefix . str_replace( ' ', '_', $key );
+ }
+ return $keys;
+ }
+
+ /**
+ * Overriding the getLabel method and deriving the label from the title.
+ * Mainly to reduce the amount of data stored in the cache.
+ *
+ * @param IContextSource|null $context
+ * @return string
+ */
+ public function getLabel( IContextSource $context = null ) {
+ return $this->getTitle()->getPrefixedText();
+ }
+
/**
* Clear caches to avoid stale data.
*
@@ -121,38 +149,39 @@ class WikiPageMessageGroup extends WikiMessageGroup implements IDBAccessObject {
$key = $title->getPrefixedDBkey();
}
- return isset( $stuff[$key] ) ? $stuff[$key] : null;
+ return $stuff[$key] ?? null;
}
$title = Title::makeTitleSafe( $this->getNamespace(), "$key/$code" );
if ( PageTranslationHooks::$renderingContext ) {
- $revFlags = Revision::READ_NORMAL; // bug T95753
+ $revFlags = IDBAccessObject::READ_NORMAL; // bug T95753
} else {
$revFlags = ( $flags & self::READ_LATEST ) == self::READ_LATEST
- ? Revision::READ_LATEST
- : Revision::READ_NORMAL;
+ ? IDBAccessObject::READ_LATEST
+ : IDBAccessObject::READ_NORMAL;
}
- $rev = Revision::newFromTitle( $title, false, $revFlags );
+ $rev = MediaWikiServices::getInstance()
+ ->getRevisionLookup()
+ ->getRevisionByTitle( $title, false, $revFlags );
if ( !$rev ) {
return null;
}
- return ContentHandler::getContentText( $rev->getContent() );
+ return ContentHandler::getContentText( $rev->getContent( SlotRecord::MAIN ) );
}
/**
- * @return MediaWikiMessageChecker
+ * @return MessageValidator
*/
- public function getChecker() {
- $checker = new MediaWikiMessageChecker( $this );
- $checker->setChecks( [
- [ $checker, 'pluralCheck' ],
- [ $checker, 'braceBalanceCheck' ],
- [ $checker, 'miscMWChecks' ]
+ public function getValidator() {
+ $validator = new MessageValidator( $this->getId() );
+ $validator->setValidators( [
+ [ 'id' => 'MediaWikiPlural' ],
+ [ 'id' => 'BraceBalance' ]
] );
- return $checker;
+ return $validator;
}
public function getInsertablesSuggester() {