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