Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
29 / 29 |
|
100.00% |
3 / 3 |
CRAP | |
100.00% |
1 / 1 |
ArchiveNameHelper | |
100.00% |
29 / 29 |
|
100.00% |
3 / 3 |
13 | |
100.00% |
1 / 1 |
decideArchiveTitle | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
3 | |||
findLatestArchiveTitle | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
2 | |||
findLatestArchiveInfo | |
100.00% |
22 / 22 |
|
100.00% |
1 / 1 |
8 |
1 | <?php |
2 | |
3 | namespace Flow\Import; |
4 | |
5 | use Flow\Repository\TitleRepository; |
6 | use MediaWiki\MediaWikiServices; |
7 | use MediaWiki\Title\Title; |
8 | |
9 | class ArchiveNameHelper { |
10 | |
11 | /** |
12 | * Helper method decides on an archive title based on a set of printf formats. |
13 | * Each format should first have a %s for the base page name and a %d for the |
14 | * archive page number. Example: |
15 | * |
16 | * %s/Archive %d |
17 | * |
18 | * It will iterate through the formats looking for an existing format. If no |
19 | * formats are currently in use the first format will be returned with n=1. |
20 | * If a format is currently in used we will look for the first unused page |
21 | * >= to n=1 and <= to n=20. |
22 | * |
23 | * @param Title $source |
24 | * @param string[] $formats |
25 | * @param TitleRepository|null $titleRepo |
26 | * @return Title |
27 | * @throws ImportException |
28 | */ |
29 | public function decideArchiveTitle( Title $source, array $formats, ?TitleRepository $titleRepo = null ) { |
30 | $info = self::findLatestArchiveInfo( $source, $formats, $titleRepo ); |
31 | $format = $info ? $info['format'] : $formats[0]; |
32 | $counter = $info ? $info['counter'] + 1 : 1; |
33 | $text = $source->getPrefixedText(); |
34 | return Title::newFromText( sprintf( $format, $text, $counter ) ); |
35 | } |
36 | |
37 | /** |
38 | * @param Title $source |
39 | * @param string[] $formats |
40 | * @param TitleRepository|null $titleRepo |
41 | * @return bool|mixed |
42 | */ |
43 | public function findLatestArchiveTitle( Title $source, array $formats, ?TitleRepository $titleRepo = null ) { |
44 | $info = self::findLatestArchiveInfo( $source, $formats, $titleRepo ); |
45 | return $info ? $info['title'] : false; |
46 | } |
47 | |
48 | /** |
49 | * @param Title $source |
50 | * @param string[] $formats |
51 | * @param TitleRepository|null $titleRepo |
52 | * @return bool|array |
53 | */ |
54 | protected function findLatestArchiveInfo( Title $source, array $formats, ?TitleRepository $titleRepo = null ) { |
55 | $titleRepo ??= new TitleRepository(); |
56 | |
57 | $format = false; |
58 | $text = $source->getPrefixedText(); |
59 | $titleFactory = MediaWikiServices::getInstance()->getTitleFactory(); |
60 | foreach ( $formats as $potential ) { |
61 | $title = $titleFactory->newFromText( sprintf( $potential, $text, 1 ) ); |
62 | if ( $title && $titleRepo->exists( $title ) ) { |
63 | $format = $potential; |
64 | break; |
65 | } |
66 | } |
67 | if ( $format === false ) { |
68 | // no archive page matches any format |
69 | return false; |
70 | } |
71 | |
72 | $latestArchiveInfo = false; |
73 | for ( $n = 1; $n <= 20; ++$n ) { |
74 | $title = Title::newFromText( sprintf( $format, $text, $n ) ); |
75 | if ( !$title || !$titleRepo->exists( $title ) ) { |
76 | break; |
77 | } |
78 | $latestArchiveInfo = [ |
79 | 'title' => $title, |
80 | 'format' => $format, |
81 | 'counter' => $n |
82 | ]; |
83 | } |
84 | return $latestArchiveInfo; |
85 | } |
86 | |
87 | } |