Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
CargoZipFormat
0.00% covered (danger)
0.00%
0 / 44
0.00% covered (danger)
0.00%
0 / 3
272
0.00% covered (danger)
0.00%
0 / 1
 allowedParameters
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getFiles
0.00% covered (danger)
0.00%
0 / 21
0.00% covered (danger)
0.00%
0 / 1
72
 display
0.00% covered (danger)
0.00%
0 / 19
0.00% covered (danger)
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
9use MediaWiki\MediaWikiServices;
10use MediaWiki\Title\Title;
11
12class 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 = '';
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            $fileName = htmlspecialchars_decode( $f['title'], ENT_QUOTES );
40            $title = Title::makeTitleSafe( NS_FILE, $fileName );
41            if ( $title == null ) {
42                continue;
43            }
44            $files[] = [
45                'title' => $title
46            ];
47        }
48        return $files;
49    }
50
51    /**
52     * @param array $valuesTable Unused
53     * @param array $formattedValuesTable
54     * @param array $fieldDescriptions
55     * @param array $displayParams Unused
56     * @return string HTML
57     */
58    public function display( $valuesTable, $formattedValuesTable, $fieldDescriptions, $displayParams ) {
59        $this->mOutput->addModules( [ 'ext.cargo.zip' ] );
60
61        $files = self::getFiles( $valuesTable, $fieldDescriptions );
62
63        if ( array_key_exists( 'filename', $displayParams ) && $displayParams['filename'] != '' ) {
64            $filename = $displayParams['filename'];
65        } else {
66            $filename = 'results.zip';
67        }
68
69        if ( array_key_exists( 'link text', $displayParams ) && $displayParams['link text'] != '' ) {
70            $linkText = $displayParams['link text'];
71        } else {
72            $linkText = wfMessage( 'cargo-downloadzip' );
73        }
74
75        $text = '<div class="downloadlink" data-fileurls="' . $filename . ' ';
76
77        foreach ( $files as $file ) {
78            $filename = explode( ':', $file['title'] );
79            $filename = array_pop( $filename );
80            $localRepo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
81            if ( $localRepo->findFile( $filename ) ) {
82                $url = $localRepo->findFile( $filename )->getFullUrl();
83                $text .= $url . ' ';
84            } else {
85                throw new MWException( wfMessage( 'cargo-downloadzip-invalidformat' ) );
86            }
87        }
88
89        $text .= '">' . $linkText . '</div>';
90
91        return $text;
92    }
93}