Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatableBundle.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
7use MediaWiki\MediaWikiServices;
8use MediaWiki\Page\PageReference;
9use MediaWiki\Title\Title;
10
24abstract class TranslatableBundle {
26 abstract public function getTitle(): Title;
27
32 abstract public function getMessageGroupId(): string;
33
39 abstract public function getTranslationPages(): array;
40
46 abstract public function getTranslationUnitPages( ?string $code = null ): array;
47
49 abstract public function isMoveable(): bool;
50
52 abstract public function isDeletable(): bool;
53
54 protected function getTranslationUnitPagesByTitle( PageReference $title, ?string $code = null ): array {
55 $mwServices = MediaWikiServices::getInstance();
56
57 $dbw = $mwServices->getDBLoadBalancer()->getConnection( DB_PRIMARY );
58 $base = $mwServices->getTitleFormatter()->getPrefixedDBkey( $title );
59 // Including the / used as separator
60 $baseLength = strlen( $base ) + 1;
61
62 if ( $code === null ) {
63 $like = $dbw->buildLike( "$base/", $dbw->anyString() );
64 } else {
65 $like = $dbw->buildLike( "$base/", $dbw->anyString(), "/$code" );
66 }
67
68 $res = $dbw->newSelectQueryBuilder()
69 ->select( [ 'page_namespace', 'page_title' ] )
70 ->from( 'page' )
71 ->where( [
72 'page_namespace' => NS_TRANSLATIONS,
73 'page_title ' . $like
74 ] )
75 ->caller( __METHOD__ )
76 ->fetchResultSet();
77
78 // Only include pages which belong to this translatable page.
79 // Problematic cases are when pages Foo and Foo/bar are both
80 // translatable. Then when querying for Foo, we also get units
81 // belonging to Foo/bar.
82 $units = [];
83 foreach ( $res as $row ) {
84 $title = Title::newFromRow( $row );
85
86 // Strip the language code and the name of the
87 // translatable to get plain translation unit id
88 $handle = new MessageHandle( $title );
89 $key = substr( $handle->getKey(), $baseLength );
90 if ( str_contains( $key, '/' ) ) {
91 // Probably belongs to translatable subpage
92 continue;
93 }
94
95 // We have a match :)
96 $units[] = $title;
97 }
98
99 return $units;
100 }
101}
Translatable bundle represents a message group where its translatable content is defined on a wiki pa...
getTitle()
Return the title of the page where the translatable bundle is defined.
isDeletable()
Check if this is a deletable translatable bundle.
isMoveable()
Check if this translatable bundle is moveable.
getTranslationPages()
Return the available translation pages for the bundle.
getTranslationUnitPages(?string $code=null)
Return the available translation units for the bundle.
getMessageGroupId()
Return the message group id for the bundle Note that the message group id may refer to a message grou...
Class for pointing to messages, like Title class is for titles.