Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslateReplaceTitle.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageProcessing;
5
7use Title;
8use TitleArray;
10
24 public static function getTitlesForMove(
25 MessageHandle $sourceMessageHandle, string $replacement
26 ): array {
27 $titlesForMove = [];
28 $namespace = $sourceMessageHandle->getTitle()->getNamespace();
29
30 $titles = self::getMatchingTitles( $sourceMessageHandle );
31
32 foreach ( $titles as $title ) {
33 $handle = new MessageHandle( $title );
34 // This takes care of situations where we have two different titles
35 // foo and foo/bar, both will be matched and fetched but the slash
36 // does not represent a language separator
37 if ( $handle->getKey() !== $sourceMessageHandle->getKey() ) {
38 continue;
39 }
40 $targetTitle = Title::makeTitle(
41 $namespace,
42 TranslateUtils::title( $replacement, $handle->getCode(), $namespace )
43 );
44 $titlesForMove[] = [ $title, $targetTitle ];
45 }
46
47 return $titlesForMove;
48 }
49
50 private static function getMatchingTitles( MessageHandle $handle ): TitleArray {
51 $dbr = wfGetDB( DB_PRIMARY );
52
53 $tables = [ 'page' ];
54 $vars = [ 'page_title', 'page_namespace', 'page_id' ];
55
56 $comparisonCond = 'page_title ' . $dbr->buildLike(
57 $handle->getTitleForBase()->getDBkey(), '/', $dbr->anyString()
58 );
59
60 $conds = [
61 $comparisonCond,
62 'page_namespace' => $handle->getTitle()->getNamespace(),
63 ];
64
65 $result = $dbr->select( $tables, $vars, $conds, __METHOD__ );
66
67 return TitleArray::newFromResult( $result );
68 }
69}
Helper class that cotains utility methods to help with identifying and replace titles.
static getTitlesForMove(MessageHandle $sourceMessageHandle, string $replacement)
Returns two lists: a set of message handles that would be moved/renamed by the current text replaceme...
Class for pointing to messages, like Title class is for titles.
getTitle()
Get the original title.
getTitleForBase()
Get the title for the page base.
getKey()
Returns the identified or guessed message key.
Essentially random collection of helper functions, similar to GlobalFunctions.php.