45 private const MAX_ITEMS_PER_QUERY = 2000;
50 private $definitions =
null;
58 protected $messages = [];
68 private $dbReviewData;
79 private $properties = [];
81 private $authors = [];
97 $collection = new self( $code );
98 $collection->definitions = $definitions;
99 $collection->resetForNewLanguage( $code );
104 public function getLanguage(): string {
116 $this->infile = $messages;
124 public function setTags(
string $type, array $keys ): void {
125 $this->tags[$type] = $keys;
132 public function keys(): array {
140 private function getTitles(): array {
141 return array_values( $this->keys );
149 return array_keys( $this->keys );
157 public function getTags(
string $type ): array {
158 return $this->tags[$type] ?? [];
168 $this->loadTranslations();
170 $authors = array_flip( $this->authors );
172 foreach ( $this->messages as $m ) {
175 $author = $m->getProperty(
'last-translator-text' );
177 if ( $author ===
null ) {
181 if ( !isset( $authors[$author] ) ) {
182 $authors[$author] = 1;
188 # arsort( $authors, SORT_NUMERIC );
190 $fuzzyBot = FuzzyBot::getName();
191 $filteredAuthors = [];
192 foreach ( $authors as $author => $edits ) {
193 if ( $author !== $fuzzyBot ) {
194 $filteredAuthors[] = $author;
198 return $filteredAuthors;
210 $authors = array_merge( $this->authors, $authors );
215 throw new MWException(
"Invalid mode $mode" );
218 $this->authors = array_unique( $authors );
231 $titleConds = $this->getTitleConds( $dbr );
233 $this->loadData( $this->keys, $titleConds );
234 $this->loadInfo( $this->keys, $titleConds );
235 $this->loadReviewInfo( $this->keys, $titleConds );
236 $this->initMessages();
245 $this->keys = $this->fixKeys();
248 $this->dbReviewData = [];
249 $this->messages =
null;
253 unset( $this->tags[
'fuzzy'] );
254 $this->reverseMap =
null;
264 public function slice( $offset, $limit ) {
265 $indexes = array_keys( $this->keys );
267 if ( $offset ===
'' ) {
272 if ( !ctype_digit( (
string)$offset ) ) {
273 $pos = array_search( $offset, array_keys( $this->keys ),
true );
275 $offset = $pos !==
false ? $pos : count( $this->keys );
279 $backwardsOffset = $forwardsOffset =
false;
289 $backwardsOffset = (string)( max( 0, $offset - $limit ) );
302 if ( isset( $indexes[$offset + $limit] ) ) {
303 $forwardsOffset = $indexes[$offset + $limit];
306 $this->keys = array_slice( $this->keys, $offset, $limit,
true );
308 return [ $backwardsOffset, $forwardsOffset, $offset ];
334 public function filter(
string $type,
bool $condition =
true, ?
int $value =
null ): void {
335 if ( !in_array( $type, self::getAvailableFilters(), true ) ) {
336 throw new MWException(
"Unknown filter $type" );
338 $this->applyFilter( $type, $condition, $value );
341 private static function getAvailableFilters(): array {
362 private function applyFilter(
string $filter,
bool $condition, ?
int $value ): void {
364 if ( $filter ===
'fuzzy' ) {
365 $keys = $this->filterFuzzy( $keys, $condition );
366 } elseif ( $filter ===
'hastranslation' ) {
367 $keys = $this->filterHastranslation( $keys, $condition );
368 } elseif ( $filter ===
'translated' ) {
369 $fuzzy = $this->filterFuzzy( $keys,
false );
370 $hastranslation = $this->filterHastranslation( $keys,
false );
372 $translated = $this->filterOnCondition( $hastranslation, $fuzzy );
373 $keys = $this->filterOnCondition( $keys, $translated, $condition );
374 } elseif ( $filter ===
'changed' ) {
375 $keys = $this->filterChanged( $keys, $condition );
376 } elseif ( $filter ===
'reviewer' ) {
377 $keys = $this->filterReviewer( $keys, $condition, $value );
378 } elseif ( $filter ===
'last-translator' ) {
379 $keys = $this->filterLastTranslator( $keys, $condition, $value );
382 if ( !isset( $this->tags[$filter] ) ) {
383 if ( $filter !==
'optional' && $filter !==
'ignored' ) {
384 throw new MWException(
"No tagged messages for custom filter $filter" );
386 $keys = $this->filterOnCondition( $keys, [], $condition );
388 $taggedKeys = array_flip( $this->tags[$filter] );
389 $keys = $this->filterOnCondition( $keys, $taggedKeys, $condition );
397 public function filterUntranslatedOptional(): void {
398 $optionalKeys = array_flip( $this->tags[
'optional'] ?? [] );
400 $optional = $this->filterOnCondition( $this->keys, $optionalKeys,
false );
403 $this->loadInfo( $this->keys );
404 $untranslatedOptional = $this->filterHastranslation( $optional,
true );
406 $this->keys = $this->filterOnCondition( $this->keys, $untranslatedOptional );
424 private function filterOnCondition( array $keys, array $condKeys,
bool $condition =
true ): array {
427 foreach ( array_keys( $condKeys ) as $key ) {
428 unset( $keys[$key] );
432 foreach ( array_keys( $keys ) as $key ) {
433 if ( !isset( $condKeys[$key] ) ) {
434 unset( $keys[$key] );
449 private function filterFuzzy( array $keys,
bool $condition ): array {
450 $this->loadInfo( $keys );
457 foreach ( $this->dbInfo as $row ) {
458 if ( $row->rt_type !==
null ) {
459 unset( $keys[$this->rowToKey( $row )] );
464 $keys = array_diff( $origKeys, $keys );
477 private function filterHastranslation( array $keys,
bool $condition ): array {
478 $this->loadInfo( $keys );
485 foreach ( $this->dbInfo as $row ) {
486 unset( $keys[$this->rowToKey( $row )] );
490 foreach ( array_keys( $this->infile ) as $inf ) {
491 unset( $keys[$inf] );
496 $keys = array_diff( $origKeys, $keys );
510 private function filterChanged( array $keys,
bool $condition ): array {
511 $this->loadData( $keys );
518 $revStore = MediaWikiServices::getInstance()->getRevisionStore();
520 foreach ( $this->dbData as $row ) {
521 $mkey = $this->rowToKey( $row );
522 if ( isset( $this->infile[$mkey] ) ) {
523 $infileRows[] = $row;
527 $revisions = $revStore->newRevisionsFromBatch( $infileRows, [
528 'slots' => [ SlotRecord::MAIN ],
531 foreach ( $infileRows as $row ) {
533 $rev = $revisions[$row->rev_id];
536 $content = $rev->getContent( SlotRecord::MAIN );
538 $mkey = $this->rowToKey( $row );
539 if ( $this->infile[$mkey] === $content->getText() ) {
541 unset( $keys[$mkey] );
549 $keys = $this->filterOnCondition( $origKeys, $keys );
563 private function filterReviewer( array $keys,
bool $condition, ?
int $userId ): array {
564 $this->loadReviewInfo( $keys );
569 foreach ( $this->dbReviewData as $row ) {
570 if ( $userId ===
null || (
int)$row->trr_user === $userId ) {
571 unset( $keys[$this->rowToKey( $row )] );
576 $keys = array_diff( $origKeys, $keys );
588 private function filterLastTranslator( array $keys,
bool $condition, ?
int $userId ): array {
589 $this->loadData( $keys );
592 $userId = $userId ?? 0;
593 foreach ( $this->dbData as $row ) {
594 if ( (
int)$row->rev_user === $userId ) {
595 unset( $keys[$this->rowToKey( $row )] );
600 $keys = array_diff( $origKeys, $keys );
610 private function fixKeys(): array {
613 $pages = $this->definitions->getPages();
614 foreach ( $pages as $key => $baseTitle ) {
615 $newkeys[$key] =
new TitleValue(
616 $baseTitle->getNamespace(),
617 $baseTitle->getDBkey() .
'/' . $this->code
629 private function loadInfo( array $keys, ?array $titleConds =
null ): void {
630 if ( $this->dbInfo !== [] ) {
634 if ( !count( $keys ) ) {
635 $this->dbInfo =
new EmptyIterator();
639 $dbr = Utilities::getSafeReadDB();
640 $tables = [
'page',
'revtag' ];
641 $fields = [
'page_namespace',
'page_title',
'rt_type' ];
642 $joins = [
'revtag' =>
645 [
'page_id=rt_page',
'page_latest=rt_revision',
'rt_type' => RevTagStore::FUZZY_TAG ]
649 $titleConds = $titleConds ?? $this->getTitleConds( $dbr );
650 $iterator =
new AppendIterator();
651 foreach ( $titleConds as $conds ) {
652 $iterator->append( $dbr->select( $tables, $fields, $conds, __METHOD__, [], $joins ) );
655 $this->dbInfo = $iterator;
660 $this->getReverseMap();
668 private function loadReviewInfo( array $keys, ?array $titleConds =
null ): void {
669 if ( $this->dbReviewData !== [] ) {
673 if ( !count( $keys ) ) {
674 $this->dbReviewData =
new EmptyIterator();
678 $dbr = Utilities::getSafeReadDB();
679 $tables = [
'page',
'translate_reviews' ];
680 $fields = [
'page_namespace',
'page_title',
'trr_user' ];
681 $joins = [
'translate_reviews' =>
684 [
'page_id=trr_page',
'page_latest=trr_revision' ]
688 $titleConds = $titleConds ?? $this->getTitleConds( $dbr );
689 $iterator =
new AppendIterator();
690 foreach ( $titleConds as $conds ) {
691 $iterator->append( $dbr->select( $tables, $fields, $conds, __METHOD__, [], $joins ) );
694 $this->dbReviewData = $iterator;
699 $this->getReverseMap();
707 private function loadData( array $keys, ?array $titleConds =
null ): void {
708 if ( $this->dbData !== [] ) {
712 if ( !count( $keys ) ) {
713 $this->dbData =
new EmptyIterator();
717 $dbr = Utilities::getSafeReadDB();
718 $revisionStore = MediaWikiServices::getInstance()->getRevisionStore();
719 $revQuery = $revisionStore->getQueryInfo( [
'page' ] );
720 $tables = $revQuery[
'tables'];
721 $fields = $revQuery[
'fields'];
722 $joins = $revQuery[
'joins'];
724 $titleConds = $titleConds ?? $this->getTitleConds( $dbr );
725 $iterator =
new AppendIterator();
726 foreach ( $titleConds as $conds ) {
727 $conds = [
'page_latest = rev_id', $conds ];
728 $iterator->append( $dbr->select( $tables, $fields, $conds, __METHOD__, [], $joins ) );
731 $this->dbData = $iterator;
736 $this->getReverseMap();
743 private function getTitleConds( IDatabase $db ): array {
744 $titles = $this->getTitles();
745 $chunks = array_chunk( $titles, self::MAX_ITEMS_PER_QUERY );
748 foreach ( $chunks as $titles ) {
751 foreach ( $titles as $title ) {
752 $namespace = $title->getNamespace();
753 $pagename = $title->getDBkey();
754 $byNamespace[$namespace][] = $pagename;
758 foreach ( $byNamespace as $namespaces => $pagenames ) {
760 'page_namespace' => $namespaces,
761 'page_title' => $pagenames,
764 $conds[] = $db->makeList( $cond, LIST_AND );
767 $results[] = $db->makeList( $conds, LIST_OR );
778 private function rowToKey( stdClass $row ): ?string {
779 $map = $this->getReverseMap();
780 if ( isset( $map[$row->page_namespace][$row->page_title] ) ) {
781 return $map[$row->page_namespace][$row->page_title];
783 wfWarn(
"Got unknown title from the database: {$row->page_namespace}:{$row->page_title}" );
790 private function getReverseMap(): array {
791 if ( isset( $this->reverseMap ) ) {
792 return $this->reverseMap;
797 foreach ( $this->keys as $mkey => $title ) {
798 $map[$title->getNamespace()][$title->getDBkey()] = $mkey;
801 $this->reverseMap = $map;
802 return $this->reverseMap;
810 if ( $this->messages !== null ) {
816 $revStore = MediaWikiServices::getInstance()->getRevisionStore();
817 $queryFlags = Utilities::shouldReadFromPrimary() ? $revStore::READ_LATEST : 0;
818 foreach ( array_keys( $this->keys ) as $mkey ) {
819 $messages[$mkey] =
new ThinMessage( $mkey, $definitions[$mkey] );
822 if ( $this->dbData !==
null ) {
823 $slotRows = $revStore->getContentBlobsForBatch(
824 $this->dbData, [ SlotRecord::MAIN ], $queryFlags
827 foreach ( $this->dbData as $row ) {
828 $mkey = $this->rowToKey( $row );
829 if ( !isset( $messages[$mkey] ) ) {
832 $messages[$mkey]->setRow( $row );
833 $messages[$mkey]->setProperty(
'revision', $row->page_latest );
835 if ( isset( $slotRows[$row->rev_id][SlotRecord::MAIN] ) ) {
836 $slot = $slotRows[$row->rev_id][SlotRecord::MAIN];
837 $messages[$mkey]->setTranslation( $slot->blob_data );
842 if ( $this->dbInfo !==
null ) {
844 foreach ( $this->dbInfo as $row ) {
845 if ( $row->rt_type !==
null ) {
846 $fuzzy[] = $this->rowToKey( $row );
850 $this->setTags(
'fuzzy', $fuzzy );
854 foreach ( $this->tags as $type => $keys ) {
855 foreach ( $keys as $mkey ) {
856 if ( isset( $messages[$mkey] ) ) {
857 $messages[$mkey]->addTag( $type );
863 foreach ( $this->properties as $type => $keys ) {
864 foreach ( $keys as $mkey => $value ) {
865 if ( isset( $messages[$mkey] ) ) {
866 $messages[$mkey]->setProperty( $type, $value );
872 foreach ( $this->infile as $mkey => $value ) {
873 if ( isset( $messages[$mkey] ) ) {
874 $messages[$mkey]->setInfile( $value );
878 foreach ( $this->dbReviewData as $row ) {
879 $mkey = $this->rowToKey( $row );
880 if ( !isset( $messages[$mkey] ) ) {
883 $messages[$mkey]->appendProperty(
'reviewers', $row->trr_user );
887 foreach ( $messages as $obj ) {
888 if ( $obj->hasTag(
'fuzzy' ) ) {
889 $obj->setProperty(
'status',
'fuzzy' );
890 } elseif ( is_array( $obj->getProperty(
'reviewers' ) ) ) {
891 $obj->setProperty(
'status',
'proofread' );
892 } elseif ( $obj->translation() !==
null ) {
893 $obj->setProperty(
'status',
'translated' );
895 $obj->setProperty(
'status',
'untranslated' );
899 $this->messages = $messages;
907 return isset( $this->keys[$offset] );
912 return $this->messages[$offset] ?? null;
920 $this->messages[$offset] = $value;
925 unset( $this->keys[$offset] );
935 public function __get(
string $name ): void {
936 throw new MWException( __METHOD__ .
": Trying to access unknown property $name" );
945 public function __set(
string $name, $value ): void {
946 throw new MWException( __METHOD__ .
": Trying to modify unknown property $name" );
955 reset( $this->keys );
958 #[\ReturnTypeWillChange]
959 public function current() {
960 if ( !count( $this->keys ) ) {
965 return $this->messages[key( $this->keys )];
968 public function key(): ?string {
969 return key( $this->keys );
972 public function next(): void {
976 public function valid(): bool {
977 return isset( $this->messages[key( $this->keys )] );
980 public function count(): int {
981 return count( $this->keys() );
return[ 'Translate:ConfigHelper'=> static function():ConfigHelper { return new ConfigHelper();}, 'Translate:CsvTranslationImporter'=> static function(MediaWikiServices $services):CsvTranslationImporter { return new CsvTranslationImporter( $services->getWikiPageFactory());}, 'Translate:EntitySearch'=> static function(MediaWikiServices $services):EntitySearch { return new EntitySearch($services->getMainWANObjectCache(), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), MessageGroups::singleton(), $services->getNamespaceInfo(), $services->get( 'Translate:MessageIndex'), $services->getTitleParser(), $services->getTitleFormatter());}, 'Translate:ExternalMessageSourceStateImporter'=> static function(MediaWikiServices $services):ExternalMessageSourceStateImporter { return new ExternalMessageSourceStateImporter($services->getMainConfig(), $services->get( 'Translate:GroupSynchronizationCache'), $services->getJobQueueGroup(), LoggerFactory::getInstance( 'Translate.GroupSynchronization'), $services->get( 'Translate:MessageIndex'));}, 'Translate:GroupSynchronizationCache'=> static function(MediaWikiServices $services):GroupSynchronizationCache { return new GroupSynchronizationCache( $services->get( 'Translate:PersistentCache'));}, 'Translate:MessageBundleStore'=> static function(MediaWikiServices $services):MessageBundleStore { return new MessageBundleStore(new RevTagStore(), $services->getJobQueueGroup(), $services->getLanguageNameUtils(), $services->get( 'Translate:MessageIndex'));}, 'Translate:MessageGroupReview'=> static function(MediaWikiServices $services):MessageGroupReview { return new MessageGroupReview($services->getDBLoadBalancer(), $services->getHookContainer());}, 'Translate:MessageGroupStatsTableFactory'=> static function(MediaWikiServices $services):MessageGroupStatsTableFactory { return new MessageGroupStatsTableFactory($services->get( 'Translate:ProgressStatsTableFactory'), $services->getDBLoadBalancer(), $services->getLinkRenderer(), $services->getMainConfig() ->get( 'TranslateWorkflowStates') !==false);}, 'Translate:MessageIndex'=> static function(MediaWikiServices $services):MessageIndex { $params=$services->getMainConfig() ->get( 'TranslateMessageIndex');if(is_string( $params)) { $params=(array) $params;} $class=array_shift( $params);return new $class( $params);}, 'Translate:MessagePrefixStats'=> static function(MediaWikiServices $services):MessagePrefixStats { return new MessagePrefixStats( $services->getTitleParser());}, 'Translate:ParsingPlaceholderFactory'=> static function():ParsingPlaceholderFactory { return new ParsingPlaceholderFactory();}, 'Translate:PersistentCache'=> static function(MediaWikiServices $services):PersistentCache { return new PersistentDatabaseCache($services->getDBLoadBalancer(), $services->getJsonCodec());}, 'Translate:ProgressStatsTableFactory'=> static function(MediaWikiServices $services):ProgressStatsTableFactory { return new ProgressStatsTableFactory($services->getLinkRenderer(), $services->get( 'Translate:ConfigHelper'));}, 'Translate:SubpageListBuilder'=> static function(MediaWikiServices $services):SubpageListBuilder { return new SubpageListBuilder($services->get( 'Translate:TranslatableBundleFactory'), $services->getLinkBatchFactory());}, 'Translate:TranslatableBundleFactory'=> static function(MediaWikiServices $services):TranslatableBundleFactory { return new TranslatableBundleFactory($services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:MessageBundleStore'));}, 'Translate:TranslatableBundleMover'=> static function(MediaWikiServices $services):TranslatableBundleMover { return new TranslatableBundleMover($services->getMovePageFactory(), $services->getJobQueueGroup(), $services->getLinkBatchFactory(), $services->get( 'Translate:TranslatableBundleFactory'), $services->get( 'Translate:SubpageListBuilder'), $services->getMainConfig() ->get( 'TranslatePageMoveLimit'));}, 'Translate:TranslatableBundleStatusStore'=> static function(MediaWikiServices $services):TranslatableBundleStatusStore { return new TranslatableBundleStatusStore($services->getDBLoadBalancer() ->getConnection(DB_PRIMARY), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), $services->getDBLoadBalancer() ->getMaintenanceConnectionRef(DB_PRIMARY));}, 'Translate:TranslatablePageParser'=> static function(MediaWikiServices $services):TranslatablePageParser { return new TranslatablePageParser($services->get( 'Translate:ParsingPlaceholderFactory'));}, 'Translate:TranslatablePageStore'=> static function(MediaWikiServices $services):TranslatablePageStore { return new TranslatablePageStore($services->get( 'Translate:MessageIndex'), $services->getJobQueueGroup(), new RevTagStore(), $services->getDBLoadBalancer(), $services->get( 'Translate:TranslatableBundleStatusStore'));}, 'Translate:TranslationStashReader'=> static function(MediaWikiServices $services):TranslationStashReader { $db=$services->getDBLoadBalancer() ->getConnectionRef(DB_REPLICA);return new TranslationStashStorage( $db);}, 'Translate:TranslationStatsDataProvider'=> static function(MediaWikiServices $services):TranslationStatsDataProvider { return new TranslationStatsDataProvider(new ServiceOptions(TranslationStatsDataProvider::CONSTRUCTOR_OPTIONS, $services->getMainConfig()), $services->getObjectFactory());}, 'Translate:TranslationUnitStoreFactory'=> static function(MediaWikiServices $services):TranslationUnitStoreFactory { return new TranslationUnitStoreFactory( $services->getDBLoadBalancer());}, 'Translate:TranslatorActivity'=> static function(MediaWikiServices $services):TranslatorActivity { $query=new TranslatorActivityQuery($services->getMainConfig(), $services->getDBLoadBalancer());return new TranslatorActivity($services->getMainObjectStash(), $query, $services->getJobQueueGroup());}, 'Translate:TtmServerFactory'=> static function(MediaWikiServices $services):TtmServerFactory { $config=$services->getMainConfig();$default=$config->get( 'TranslateTranslationDefaultService');if( $default===false) { $default=null;} return new TtmServerFactory( $config->get( 'TranslateTranslationServices'), $default);}]
@phpcs-require-sorted-array