Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslateReplaceTitle
0.00% covered (danger)
0.00%
0 / 26
0.00% covered (danger)
0.00%
0 / 2
20
0.00% covered (danger)
0.00%
0 / 1
 getTitlesForMove
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
12
 getMatchingTitles
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageProcessing;
5
6use MediaWiki\Extension\Translate\MessageLoading\MessageHandle;
7use MediaWiki\Extension\Translate\Utilities\Utilities;
8use MediaWiki\MediaWikiServices;
9use MediaWiki\Title\Title;
10use MediaWiki\Title\TitleArrayFromResult;
11
12/**
13 * Helper class that contains utility methods to help with identifying and replace titles.
14 * @author Abijeet Patro
15 * @since 2019.10
16 * @license GPL-2.0-or-later
17 */
18class TranslateReplaceTitle {
19    /**
20     * Returns two lists: a set of message handles that would be moved/renamed by
21     * the current text replacement, and the set of message handles that would ordinarily
22     * be moved but are not movable, due to permissions or any other reason.
23     * @return Title[][]
24     */
25    public static function getTitlesForMove(
26        MessageHandle $sourceMessageHandle, string $replacement
27    ): array {
28        $titlesForMove = [];
29        $namespace = $sourceMessageHandle->getTitle()->getNamespace();
30
31        $titles = self::getMatchingTitles( $sourceMessageHandle );
32
33        foreach ( $titles as $title ) {
34            $handle = new MessageHandle( $title );
35            // This takes care of situations where we have two different titles
36            // foo and foo/bar, both will be matched and fetched but the slash
37            // does not represent a language separator
38            if ( $handle->getKey() !== $sourceMessageHandle->getKey() ) {
39                continue;
40            }
41            $targetTitle = Title::makeTitle(
42                $namespace,
43                Utilities::title( $replacement, $handle->getCode(), $namespace )
44            );
45            $titlesForMove[] = [ $title, $targetTitle ];
46        }
47
48        return $titlesForMove;
49    }
50
51    private static function getMatchingTitles( MessageHandle $handle ): TitleArrayFromResult {
52        $dbr = MediaWikiServices::getInstance()->getDBLoadBalancer()->getConnection( DB_PRIMARY );
53        $result = $dbr->newSelectQueryBuilder()
54            ->select( [ 'page_title', 'page_namespace', 'page_id' ] )
55            ->from( 'page' )
56            ->where( [
57                'page_namespace' => $handle->getTitle()->getNamespace(),
58                'page_title ' . $dbr->buildLike(
59                    $handle->getTitleForBase()->getDBkey(), '/', $dbr->anyString()
60                ),
61            ] )
62            ->caller( __METHOD__ )
63            ->fetchResultSet();
64
65        return new TitleArrayFromResult( $result );
66    }
67}