Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
CargoZipFormat | |
0.00% |
0 / 43 |
|
0.00% |
0 / 3 |
272 | |
0.00% |
0 / 1 |
allowedParameters | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
2 | |||
getFiles | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
72 | |||
display | |
0.00% |
0 / 19 |
|
0.00% |
0 / 1 |
56 |
1 | <?php |
2 | /** |
3 | * @author Sanjay Thiyagarajan |
4 | * @ingroup Cargo |
5 | * |
6 | * Generates a zip file consisting all queried files |
7 | */ |
8 | |
9 | use MediaWiki\MediaWikiServices; |
10 | |
11 | class CargoZipFormat extends CargoDisplayFormat { |
12 | |
13 | public static function allowedParameters() { |
14 | return [ |
15 | 'filename' => [ 'type' => 'string' ], |
16 | 'link text' => [ 'type' => 'string' ] |
17 | ]; |
18 | } |
19 | |
20 | protected function getFiles( $valuesTable, $fieldDescriptions ) { |
21 | $fileField = null; |
22 | foreach ( $fieldDescriptions as $field => $fieldDesc ) { |
23 | if ( $fieldDesc->mType == 'File' || $fieldDesc->mType == 'Page' ) { |
24 | $fileField = $field; |
25 | break; |
26 | } |
27 | } |
28 | $fileNames = []; |
29 | foreach ( $valuesTable as $row ) { |
30 | if ( array_key_exists( $fileField, $row ) ) { |
31 | $fileNames[] = [ |
32 | 'title' => $row[$fileField] |
33 | ]; |
34 | } |
35 | } |
36 | $files = []; |
37 | foreach ( $fileNames as $f ) { |
38 | $title = Title::makeTitleSafe( NS_FILE, $f['title'] ); |
39 | if ( $title == null ) { |
40 | continue; |
41 | } |
42 | $files[] = [ |
43 | 'title' => $title |
44 | ]; |
45 | } |
46 | return $files; |
47 | } |
48 | |
49 | /** |
50 | * @param array $valuesTable Unused |
51 | * @param array $formattedValuesTable |
52 | * @param array $fieldDescriptions |
53 | * @param array $displayParams Unused |
54 | * @return string HTML |
55 | */ |
56 | public function display( $valuesTable, $formattedValuesTable, $fieldDescriptions, $displayParams ) { |
57 | $this->mOutput->addModules( [ 'ext.cargo.zip' ] ); |
58 | |
59 | $files = self::getFiles( $valuesTable, $fieldDescriptions ); |
60 | |
61 | if ( array_key_exists( 'filename', $displayParams ) && $displayParams['filename'] != '' ) { |
62 | $filename = $displayParams['filename']; |
63 | } else { |
64 | $filename = 'results.zip'; |
65 | } |
66 | |
67 | if ( array_key_exists( 'link text', $displayParams ) && $displayParams['link text'] != '' ) { |
68 | $linkText = $displayParams['link text']; |
69 | } else { |
70 | $linkText = wfMessage( 'cargo-downloadzip' ); |
71 | } |
72 | |
73 | $text = '<div class="downloadlink" data-fileurls="' . $filename . ' '; |
74 | |
75 | foreach ( $files as $file ) { |
76 | $filename = explode( ':', $file['title'] ); |
77 | $filename = array_pop( $filename ); |
78 | $localRepo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo(); |
79 | if ( $localRepo->findFile( $filename ) ) { |
80 | $url = $localRepo->findFile( $filename )->getFullUrl(); |
81 | $text .= $url . ' '; |
82 | } else { |
83 | throw new MWException( wfMessage( 'cargo-downloadzip-invalidformat' ) ); |
84 | } |
85 | } |
86 | |
87 | $text .= '">' . $linkText . '</div>'; |
88 | |
89 | return $text; |
90 | } |
91 | } |