Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatableBundle.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use MediaWiki\MediaWikiServices;
7use MediaWiki\Page\PageReference;
9use 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 $dbw = wfGetDB( DB_PRIMARY );
56
57 $base = MediaWikiServices::getInstance()->getTitleFormatter()->getPrefixedDBkey( $title );
58 // Including the / used as separator
59 $baseLength = strlen( $base ) + 1;
60
61 if ( $code === null ) {
62 $like = $dbw->buildLike( "$base/", $dbw->anyString() );
63 } else {
64 $like = $dbw->buildLike( "$base/", $dbw->anyString(), "/$code" );
65 }
66
67 $res = $dbw->newSelectQueryBuilder()
68 ->select( [ 'page_namespace', 'page_title' ] )
69 ->from( 'page' )
70 ->where( [
71 'page_namespace' => NS_TRANSLATIONS,
72 'page_title ' . $like
73 ] )
74 ->caller( __METHOD__ )
75 ->fetchResultSet();
76
77 // Only include pages which belong to this translatable page.
78 // Problematic cases are when pages Foo and Foo/bar are both
79 // translatable. Then when querying for Foo, we also get units
80 // belonging to Foo/bar.
81 $units = [];
82 foreach ( $res as $row ) {
83 $title = Title::newFromRow( $row );
84
85 // Strip the language code and the name of the
86 // translatable to get plain translation unit id
87 $handle = new MessageHandle( $title );
88 $key = substr( $handle->getKey(), $baseLength );
89 if ( str_contains( $key, '/' ) ) {
90 // Probably belongs to translatable subpage
91 continue;
92 }
93
94 // We have a match :)
95 $units[] = $title;
96 }
97
98 return $units;
99 }
100}
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.