Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatableBundle.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
7use Title;
8
22abstract class TranslatableBundle {
24 abstract public function getTitle(): Title;
25
30 abstract public function getMessageGroupId(): string;
31
37 abstract public function getTranslationPages(): array;
38
44 abstract public function getTranslationUnitPages( ?string $code = null ): array;
45
47 abstract public function isMoveable(): bool;
48
50 abstract public function isDeletable(): bool;
51
52 protected function getTranslationUnitPagesByTitle( Title $title, ?string $code = null ): array {
53 $dbw = wfGetDB( DB_PRIMARY );
54
55 $base = $title->getPrefixedDBkey();
56 // Including the / used as separator
57 $baseLength = strlen( $base ) + 1;
58
59 if ( $code === null ) {
60 $like = $dbw->buildLike( "$base/", $dbw->anyString() );
61 } else {
62 $like = $dbw->buildLike( "$base/", $dbw->anyString(), "/$code" );
63 }
64
65 $fields = [ 'page_namespace', 'page_title' ];
66 $conds = [
67 'page_namespace' => NS_TRANSLATIONS,
68 'page_title ' . $like
69 ];
70 $res = $dbw->select( 'page', $fields, $conds, __METHOD__ );
71
72 // Only include pages which belong to this translatable page.
73 // Problematic cases are when pages Foo and Foo/bar are both
74 // translatable. Then when querying for Foo, we also get units
75 // belonging to Foo/bar.
76 $units = [];
77 foreach ( $res as $row ) {
78 $title = Title::newFromRow( $row );
79
80 // Strip the language code and the name of the
81 // translatable to get plain translation unit id
82 $handle = new MessageHandle( $title );
83 $key = substr( $handle->getKey(), $baseLength );
84 if ( strpos( $key, '/' ) !== false ) {
85 // Probably belongs to translatable subpage
86 continue;
87 }
88
89 // We have a match :)
90 $units[] = $title;
91 }
92
93 return $units;
94 }
95}
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.