Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 69 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
CargoFileData | |
0.00% |
0 / 69 |
|
0.00% |
0 / 2 |
812 | |
0.00% |
0 / 1 |
getTableSchema | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
132 | |||
storeValuesForFile | |
0.00% |
0 / 44 |
|
0.00% |
0 / 1 |
306 |
1 | <?php |
2 | |
3 | /** |
4 | * Static functions for dealing with the "_fileData" table. |
5 | * |
6 | * @author Yaron Koren |
7 | */ |
8 | class CargoFileData { |
9 | |
10 | /** |
11 | * Set the schema based on what has been entered in LocalSettings.php. |
12 | * |
13 | * @return CargoTableSchema |
14 | */ |
15 | public static function getTableSchema() { |
16 | global $wgCargoFileDataColumns; |
17 | |
18 | $fieldTypes = []; |
19 | |
20 | if ( in_array( 'mediaType', $wgCargoFileDataColumns ) ) { |
21 | $fieldTypes['_mediaType'] = [ 'type' => 'String' ]; |
22 | } |
23 | if ( in_array( 'path', $wgCargoFileDataColumns ) ) { |
24 | $fieldTypes['_path'] = [ 'type' => 'String', 'hidden' => true ]; |
25 | } |
26 | if ( in_array( 'lastUploadDate', $wgCargoFileDataColumns ) ) { |
27 | $fieldTypes['_lastUploadDate'] = [ 'type' => 'Datetime' ]; |
28 | } |
29 | if ( in_array( 'fullText', $wgCargoFileDataColumns ) ) { |
30 | $fieldTypes['_fullText'] = [ 'type' => 'Searchtext' ]; |
31 | } |
32 | if ( in_array( 'numPages', $wgCargoFileDataColumns ) ) { |
33 | $fieldTypes['_numPages'] = [ 'type' => 'Integer' ]; |
34 | } |
35 | |
36 | $tableSchema = new CargoTableSchema(); |
37 | foreach ( $fieldTypes as $field => $fieldVals ) { |
38 | $fieldDesc = new CargoFieldDescription(); |
39 | foreach ( $fieldVals as $fieldKey => $fieldVal ) { |
40 | if ( $fieldKey == 'type' ) { |
41 | $fieldDesc->mType = $fieldVal; |
42 | } elseif ( $fieldKey == 'list' ) { |
43 | // Not currently used. |
44 | $fieldDesc->mIsList = true; |
45 | $fieldDesc->setDelimiter( '|' ); |
46 | } elseif ( $fieldKey == 'hidden' ) { |
47 | $fieldDesc->mIsHidden = true; |
48 | } |
49 | } |
50 | $tableSchema->mFieldDescriptions[$field] = $fieldDesc; |
51 | } |
52 | |
53 | return $tableSchema; |
54 | } |
55 | |
56 | /** |
57 | * @param Title|null $title |
58 | * @param bool $createReplacement |
59 | */ |
60 | public static function storeValuesForFile( $title, $createReplacement ) { |
61 | global $wgCargoFileDataColumns, $wgLocalFileRepo; |
62 | |
63 | if ( $title == null ) { |
64 | return; |
65 | } |
66 | |
67 | // Exit if we're not in the File namespace. |
68 | if ( $title->getNamespace() != NS_FILE ) { |
69 | return; |
70 | } |
71 | |
72 | $fileDataTable = $createReplacement ? '_fileData__NEXT' : '_fileData'; |
73 | |
74 | // If there is no _fileData table, getTableSchemas() will |
75 | // throw an error. |
76 | try { |
77 | $tableSchemas = CargoUtils::getTableSchemas( [ $fileDataTable ] ); |
78 | } catch ( MWException $e ) { |
79 | return; |
80 | } |
81 | |
82 | $repo = new LocalRepo( $wgLocalFileRepo ); |
83 | $file = LocalFile::newFromTitle( $title, $repo ); |
84 | |
85 | $fileDataValues = []; |
86 | |
87 | if ( in_array( 'mediaType', $wgCargoFileDataColumns ) ) { |
88 | $fileDataValues['_mediaType'] = $file->getMimeType(); |
89 | } |
90 | |
91 | if ( in_array( 'path', $wgCargoFileDataColumns ) ) { |
92 | $fileDataValues['_path'] = $file->getLocalRefPath(); |
93 | } |
94 | |
95 | if ( in_array( 'lastUploadDate', $wgCargoFileDataColumns ) ) { |
96 | $fileDataValues['_lastUploadDate'] = $file->getTimestamp(); |
97 | } |
98 | |
99 | if ( in_array( 'fullText', $wgCargoFileDataColumns ) ) { |
100 | global $wgCargoPDFToText; |
101 | |
102 | if ( $wgCargoPDFToText == '' ) { |
103 | // Display an error message? |
104 | } elseif ( $file->getMimeType() != 'application/pdf' ) { |
105 | // We only handle PDF files. |
106 | } else { |
107 | // Copied in part from the PdfHandler extension. |
108 | $filePath = $file->getLocalRefPath(); |
109 | $cmd = wfEscapeShellArg( $wgCargoPDFToText ) . ' ' . wfEscapeShellArg( $filePath ) . ' - '; |
110 | $retval = ''; |
111 | $txt = wfShellExec( $cmd, $retval ); |
112 | if ( $retval == 0 ) { |
113 | $txt = str_replace( "\r\n", "\n", $txt ); |
114 | $txt = str_replace( "\f", "\n\n", $txt ); |
115 | $fileDataValues['_fullText'] = $txt; |
116 | } |
117 | } |
118 | } |
119 | |
120 | if ( in_array( 'numPages', $wgCargoFileDataColumns ) ) { |
121 | global $wgCargoPDFInfo; |
122 | if ( $wgCargoPDFInfo == '' ) { |
123 | // Display an error message? |
124 | } elseif ( $file->getMimeType() != 'application/pdf' ) { |
125 | // We only handle PDF files. |
126 | } else { |
127 | $filePath = $file->getLocalRefPath(); |
128 | $cmd = wfEscapeShellArg( $wgCargoPDFInfo ) . ' ' . wfEscapeShellArg( $filePath ); |
129 | $retval = ''; |
130 | $txt = wfShellExec( $cmd, $retval ); |
131 | if ( $retval == 0 ) { |
132 | $lines = explode( PHP_EOL, $txt ); |
133 | $matched = preg_grep( '/^Pages\:/', $lines ); |
134 | foreach ( $matched as $line ) { |
135 | $fileDataValues['_numPages'] = intval( trim( substr( $line, 7 ) ) ); |
136 | } |
137 | } |
138 | } |
139 | } |
140 | |
141 | CargoStore::storeAllData( $title, $fileDataTable, $fileDataValues, $tableSchemas[$fileDataTable] ); |
142 | } |
143 | |
144 | } |