Complex classes like File often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use File, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
68 | class File extends DataObject implements ShortcodeHandler, AssetContainer { |
||
69 | |||
70 | use ImageManipulation; |
||
71 | |||
72 | private static $default_sort = "\"Name\""; |
||
73 | |||
74 | private static $singular_name = "File"; |
||
75 | |||
76 | private static $plural_name = "Files"; |
||
77 | |||
78 | /** |
||
79 | * Permissions necessary to view files outside of the live stage (e.g. archive / draft stage). |
||
80 | * |
||
81 | * @config |
||
82 | * @var array |
||
83 | */ |
||
84 | private static $non_live_permissions = array('CMS_ACCESS_LeftAndMain', 'CMS_ACCESS_AssetAdmin', 'VIEW_DRAFT_CONTENT'); |
||
85 | |||
86 | private static $db = array( |
||
87 | "Name" =>"Varchar(255)", |
||
88 | "Title" =>"Varchar(255)", |
||
89 | "File" =>"DBFile", |
||
90 | // Only applies to files, doesn't inherit for folder |
||
91 | 'ShowInSearch' => 'Boolean(1)', |
||
92 | ); |
||
93 | |||
94 | private static $has_one = array( |
||
95 | "Parent" => "File", |
||
96 | "Owner" => "Member" |
||
97 | ); |
||
98 | |||
99 | private static $defaults = array( |
||
100 | "ShowInSearch" => 1, |
||
101 | ); |
||
102 | |||
103 | private static $extensions = array( |
||
104 | "Hierarchy", |
||
105 | "Versioned" |
||
106 | ); |
||
107 | |||
108 | private static $casting = array( |
||
109 | 'TreeTitle' => 'HTMLText' |
||
110 | ); |
||
111 | |||
112 | /** |
||
113 | * @config |
||
114 | * @var array List of allowed file extensions, enforced through {@link validate()}. |
||
115 | * |
||
116 | * Note: if you modify this, you should also change a configuration file in the assets directory. |
||
117 | * Otherwise, the files will be able to be uploaded but they won't be able to be served by the |
||
118 | * webserver. |
||
119 | * |
||
120 | * - If you are running Apache you will need to change assets/.htaccess |
||
121 | * - If you are running IIS you will need to change assets/web.config |
||
122 | * |
||
123 | * Instructions for the change you need to make are included in a comment in the config file. |
||
124 | */ |
||
125 | private static $allowed_extensions = array( |
||
126 | '', 'ace', 'arc', 'arj', 'asf', 'au', 'avi', 'bmp', 'bz2', 'cab', 'cda', 'css', 'csv', 'dmg', 'doc', |
||
127 | 'docx', 'dotx', 'dotm', 'flv', 'gif', 'gpx', 'gz', 'hqx', 'ico', 'jar', 'jpeg', 'jpg', 'js', 'kml', |
||
128 | 'm4a', 'm4v', 'mid', 'midi', 'mkv', 'mov', 'mp3', 'mp4', 'mpa', 'mpeg', 'mpg', 'ogg', 'ogv', 'pages', |
||
129 | 'pcx', 'pdf', 'png', 'pps', 'ppt', 'pptx', 'potx', 'potm', 'ra', 'ram', 'rm', 'rtf', 'sit', 'sitx', |
||
130 | 'tar', 'tgz', 'tif', 'tiff', 'txt', 'wav', 'webm', 'wma', 'wmv', 'xls', 'xlsx', 'xltx', 'xltm', 'zip', |
||
131 | 'zipx', |
||
132 | ); |
||
133 | |||
134 | /** |
||
135 | * @config |
||
136 | * @var array Category identifiers mapped to commonly used extensions. |
||
137 | */ |
||
138 | private static $app_categories = array( |
||
139 | 'archive' => array( |
||
140 | 'ace', 'arc', 'arj', 'bz', 'bz2', 'cab', 'dmg', 'gz', 'hqx', 'jar', 'rar', 'sit', 'sitx', 'tar', 'tgz', |
||
141 | 'zip', 'zipx', |
||
142 | ), |
||
143 | 'audio' => array( |
||
144 | 'aif', 'aifc', 'aiff', 'apl', 'au', 'avr', 'cda', 'm4a', 'mid', 'midi', 'mp3', 'ogg', 'ra', |
||
145 | 'ram', 'rm', 'snd', 'wav', 'wma', |
||
146 | ), |
||
147 | 'document' => array( |
||
148 | 'css', 'csv', 'doc', 'docx', 'dotm', 'dotx', 'htm', 'html', 'gpx', 'js', 'kml', 'pages', 'pdf', |
||
149 | 'potm', 'potx', 'pps', 'ppt', 'pptx', 'rtf', 'txt', 'xhtml', 'xls', 'xlsx', 'xltm', 'xltx', 'xml', |
||
150 | ), |
||
151 | 'image' => array( |
||
152 | 'alpha', 'als', 'bmp', 'cel', 'gif', 'ico', 'icon', 'jpeg', 'jpg', 'pcx', 'png', 'ps', 'tif', 'tiff', |
||
153 | ), |
||
154 | 'image/supported' => array( |
||
155 | 'gif', 'jpeg', 'jpg', 'png' |
||
156 | ), |
||
157 | 'flash' => array( |
||
158 | 'fla', 'swf' |
||
159 | ), |
||
160 | 'video' => array( |
||
161 | 'asf', 'avi', 'flv', 'ifo', 'm1v', 'm2v', 'm4v', 'mkv', 'mov', 'mp2', 'mp4', 'mpa', 'mpe', 'mpeg', |
||
162 | 'mpg', 'ogv', 'qt', 'vob', 'webm', 'wmv', |
||
163 | ), |
||
164 | ); |
||
165 | |||
166 | /** |
||
167 | * Map of file extensions to class type |
||
168 | * |
||
169 | * @config |
||
170 | * @var |
||
171 | */ |
||
172 | private static $class_for_file_extension = array( |
||
173 | '*' => 'File', |
||
174 | 'jpg' => 'Image', |
||
175 | 'jpeg' => 'Image', |
||
176 | 'png' => 'Image', |
||
177 | 'gif' => 'Image', |
||
178 | ); |
||
179 | |||
180 | /** |
||
181 | * @config |
||
182 | * @var If this is true, then restrictions set in {@link $allowed_max_file_size} and |
||
183 | * {@link $allowed_extensions} will be applied to users with admin privileges as |
||
184 | * well. |
||
185 | */ |
||
186 | private static $apply_restrictions_to_admin = true; |
||
187 | |||
188 | /** |
||
189 | * If enabled, legacy file dataobjects will be automatically imported into the APL |
||
190 | * |
||
191 | * @config |
||
192 | * @var bool |
||
193 | */ |
||
194 | private static $migrate_legacy_file = false; |
||
195 | |||
196 | /** |
||
197 | * @config |
||
198 | * @var boolean |
||
199 | */ |
||
200 | private static $update_filesystem = true; |
||
201 | |||
202 | public static function get_shortcodes() { |
||
205 | |||
206 | /** |
||
207 | * Replace"[file_link id=n]" shortcode with an anchor tag or link to the file. |
||
208 | * |
||
209 | * @param array $arguments Arguments passed to the parser |
||
210 | * @param string $content Raw shortcode |
||
211 | * @param ShortcodeParser $parser Parser |
||
212 | * @param string $shortcode Name of shortcode used to register this handler |
||
213 | * @param array $extra Extra arguments |
||
214 | * @return string Result of the handled shortcode |
||
215 | */ |
||
216 | public static function handle_shortcode($arguments, $content, $parser, $shortcode, $extra = array()) { |
||
245 | |||
246 | /** |
||
247 | * Find the record to use for a given shortcode. |
||
248 | * |
||
249 | * @param array $args Array of input shortcode arguments |
||
250 | * @param int $errorCode If the file is not found, or is inaccessible, this will be assigned to a HTTP error code. |
||
251 | * @return File|null The File DataObject, if it can be found. |
||
252 | */ |
||
253 | public static function find_shortcode_record($args, &$errorCode = null) { |
||
254 | // Validate shortcode |
||
255 | if(!isset($args['id']) || !is_numeric($args['id'])) { |
||
256 | return null; |
||
257 | } |
||
258 | |||
259 | // Check if the file is found |
||
260 | $file = File::get()->byID($args['id']); |
||
261 | if (!$file) { |
||
262 | $errorCode = 404; |
||
263 | return null; |
||
264 | } |
||
265 | |||
266 | // Check if the file is viewable |
||
267 | if(!$file->canView()) { |
||
268 | $errorCode = 403; |
||
269 | return null; |
||
270 | } |
||
271 | |||
272 | // Success |
||
273 | return $file; |
||
274 | } |
||
275 | |||
276 | /** |
||
277 | * Given a HTTP Error, find an appropriate substitute File or SiteTree data object instance. |
||
278 | * |
||
279 | * @param int $errorCode HTTP Error value |
||
280 | * @return File|SiteTree File or SiteTree object to use for the given error |
||
281 | */ |
||
282 | protected static function find_error_record($errorCode) { |
||
283 | $result = static::singleton()->invokeWithExtensions('getErrorRecordFor', $errorCode); |
||
284 | $result = array_filter($result); |
||
285 | if($result) { |
||
286 | return reset($result); |
||
287 | } |
||
288 | return null; |
||
289 | } |
||
290 | |||
291 | /** |
||
292 | * A file only exists if the file_exists() and is in the DB as a record |
||
293 | * |
||
294 | * Use $file->isInDB() to only check for a DB record |
||
295 | * Use $file->File->exists() to only check if the asset exists |
||
296 | * |
||
297 | * @return bool |
||
298 | */ |
||
299 | public function exists() { |
||
300 | return parent::exists() && $this->File->exists(); |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * Find a File object by the given filename. |
||
305 | * |
||
306 | * @param string $filename Filename to search for, including any custom parent directories. |
||
307 | * @return File |
||
308 | */ |
||
309 | public static function find($filename) { |
||
310 | // Split to folders and the actual filename, and traverse the structure. |
||
311 | $parts = explode("/", $filename); |
||
312 | $parentID = 0; |
||
313 | $item = null; |
||
314 | foreach($parts as $part) { |
||
315 | $item = File::get()->filter(array( |
||
316 | 'Name' => $part, |
||
317 | 'ParentID' => $parentID |
||
318 | ))->first(); |
||
319 | if(!$item) break; |
||
320 | $parentID = $item->ID; |
||
321 | } |
||
322 | |||
323 | return $item; |
||
324 | } |
||
325 | |||
326 | /** |
||
327 | * Just an alias function to keep a consistent API with SiteTree |
||
328 | * |
||
329 | * @return string The link to the file |
||
330 | */ |
||
331 | public function Link() { |
||
332 | return $this->getURL(); |
||
333 | } |
||
334 | |||
335 | /** |
||
336 | * @deprecated 4.0 |
||
337 | */ |
||
338 | public function RelativeLink() { |
||
339 | Deprecation::notice('4.0', 'Use getURL instead, as not all files will be relative to the site root.'); |
||
340 | return Director::makeRelative($this->getURL()); |
||
341 | } |
||
342 | |||
343 | /** |
||
344 | * Just an alias function to keep a consistent API with SiteTree |
||
345 | * |
||
346 | * @return string The absolute link to the file |
||
347 | */ |
||
348 | public function AbsoluteLink() { |
||
349 | return $this->getAbsoluteURL(); |
||
350 | } |
||
351 | |||
352 | /** |
||
353 | * @return string |
||
354 | */ |
||
355 | public function getTreeTitle() { |
||
356 | return Convert::raw2xml($this->Title); |
||
357 | } |
||
358 | |||
359 | /** |
||
360 | * @param Member $member |
||
361 | * @return bool |
||
362 | */ |
||
363 | public function canView($member = null) { |
||
364 | if(!$member) { |
||
365 | $member = Member::currentUser(); |
||
366 | } |
||
367 | |||
368 | $result = $this->extendedCan('canView', $member); |
||
369 | if($result !== null) { |
||
370 | return $result; |
||
371 | } |
||
372 | |||
373 | return true; |
||
374 | } |
||
375 | |||
376 | /** |
||
377 | * Check if this file can be modified |
||
378 | * |
||
379 | * @param Member $member |
||
380 | * @return boolean |
||
381 | */ |
||
382 | public function canEdit($member = null) { |
||
383 | if(!$member) { |
||
384 | $member = Member::currentUser(); |
||
385 | } |
||
386 | |||
387 | $result = $this->extendedCan('canEdit', $member); |
||
388 | if($result !== null) { |
||
389 | return $result; |
||
390 | } |
||
391 | |||
392 | return Permission::checkMember($member, array('CMS_ACCESS_AssetAdmin', 'CMS_ACCESS_LeftAndMain')); |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * Check if a file can be created |
||
397 | * |
||
398 | * @param Member $member |
||
399 | * @param array $context |
||
400 | * @return boolean |
||
401 | */ |
||
402 | public function canCreate($member = null, $context = array()) { |
||
403 | if(!$member) { |
||
404 | $member = Member::currentUser(); |
||
405 | } |
||
406 | |||
407 | $result = $this->extendedCan('canCreate', $member, $context); |
||
408 | if($result !== null) { |
||
409 | return $result; |
||
410 | } |
||
411 | |||
412 | return $this->canEdit($member); |
||
413 | } |
||
414 | |||
415 | /** |
||
416 | * Check if this file can be deleted |
||
417 | * |
||
418 | * @param Member $member |
||
419 | * @return boolean |
||
420 | */ |
||
421 | public function canDelete($member = null) { |
||
422 | if(!$member) { |
||
423 | $member = Member::currentUser(); |
||
424 | } |
||
425 | |||
426 | $result = $this->extendedCan('canDelete', $member); |
||
427 | if($result !== null) { |
||
428 | return $result; |
||
429 | } |
||
430 | |||
431 | return $this->canEdit($member); |
||
432 | } |
||
433 | |||
434 | /** |
||
435 | * Returns the fields to power the edit screen of files in the CMS. |
||
436 | * You can modify this FieldList by subclassing folder, or by creating a {@link DataExtension} |
||
437 | * and implemeting updateCMSFields(FieldList $fields) on that extension. |
||
438 | * |
||
439 | * @return FieldList |
||
440 | */ |
||
441 | public function getCMSFields() { |
||
442 | // Preview |
||
443 | $filePreview = CompositeField::create( |
||
444 | CompositeField::create(new LiteralField("ImageFull", $this->PreviewThumbnail())) |
||
445 | ->setName("FilePreviewImage") |
||
446 | ->addExtraClass('cms-file-info-preview'), |
||
447 | CompositeField::create( |
||
448 | CompositeField::create( |
||
449 | new ReadonlyField("FileType", _t('AssetTableField.TYPE','File type') . ':'), |
||
450 | new ReadonlyField("Size", _t('AssetTableField.SIZE','File size') . ':', $this->getSize()), |
||
451 | ReadonlyField::create( |
||
452 | 'ClickableURL', |
||
453 | _t('AssetTableField.URL','URL'), |
||
454 | sprintf('<a href="%s" target="_blank">%s</a>', $this->Link(), $this->Link()) |
||
455 | ) |
||
456 | ->setDontEscape(true), |
||
457 | new DateField_Disabled("Created", _t('AssetTableField.CREATED','First uploaded') . ':'), |
||
458 | new DateField_Disabled("LastEdited", _t('AssetTableField.LASTEDIT','Last changed') . ':') |
||
459 | ) |
||
460 | ) |
||
461 | ->setName("FilePreviewData") |
||
462 | ->addExtraClass('cms-file-info-data') |
||
463 | ) |
||
464 | ->setName("FilePreview") |
||
465 | ->addExtraClass('cms-file-info'); |
||
466 | |||
467 | //get a tree listing with only folder, no files |
||
468 | $fields = new FieldList( |
||
469 | new TabSet('Root', |
||
470 | new Tab('Main', |
||
471 | $filePreview, |
||
472 | new TextField("Title", _t('AssetTableField.TITLE','Title')), |
||
473 | new TextField("Name", _t('AssetTableField.FILENAME','Filename')), |
||
474 | DropdownField::create("OwnerID", _t('AssetTableField.OWNER','Owner'), Member::mapInCMSGroups()) |
||
475 | ->setHasEmptyDefault(true), |
||
476 | new TreeDropdownField("ParentID", _t('AssetTableField.FOLDER','Folder'), 'Folder') |
||
477 | ) |
||
478 | ) |
||
479 | ); |
||
480 | |||
481 | $this->extend('updateCMSFields', $fields); |
||
482 | return $fields; |
||
483 | } |
||
484 | |||
485 | /** |
||
486 | * Returns a category based on the file extension. |
||
487 | * This can be useful when grouping files by type, |
||
488 | * showing icons on filelinks, etc. |
||
489 | * Possible group values are:"audio","mov","zip","image". |
||
490 | * |
||
491 | * @param string $ext Extension to check |
||
492 | * @return string |
||
493 | */ |
||
494 | public static function get_app_category($ext) { |
||
495 | $ext = strtolower($ext); |
||
496 | foreach(Config::inst()->get('File', 'app_categories') as $category => $exts) { |
||
497 | if(in_array($ext, $exts)) return $category; |
||
498 | } |
||
499 | return false; |
||
500 | } |
||
501 | |||
502 | /** |
||
503 | * For a category or list of categories, get the list of file extensions |
||
504 | * |
||
505 | * @param array|string $categories List of categories, or single category |
||
506 | * @return array |
||
507 | */ |
||
508 | public static function get_category_extensions($categories) { |
||
509 | if(empty($categories)) { |
||
510 | return array(); |
||
511 | } |
||
512 | |||
513 | // Fix arguments into a single array |
||
514 | if(!is_array($categories)) { |
||
515 | $categories = array($categories); |
||
516 | } elseif(count($categories) === 1 && is_array(reset($categories))) { |
||
517 | $categories = reset($categories); |
||
518 | } |
||
519 | |||
520 | // Check configured categories |
||
521 | $appCategories = self::config()->app_categories; |
||
522 | |||
523 | // Merge all categories into list of extensions |
||
524 | $extensions = array(); |
||
525 | foreach(array_filter($categories) as $category) { |
||
526 | if(isset($appCategories[$category])) { |
||
527 | $extensions = array_merge($extensions, $appCategories[$category]); |
||
528 | } else { |
||
529 | throw new InvalidArgumentException("Unknown file category: $category"); |
||
530 | } |
||
531 | } |
||
532 | $extensions = array_unique($extensions); |
||
533 | sort($extensions); |
||
534 | return $extensions; |
||
535 | } |
||
536 | |||
537 | /** |
||
538 | * Returns a category based on the file extension. |
||
539 | * |
||
540 | * @return string |
||
541 | */ |
||
542 | public function appCategory() { |
||
543 | return self::get_app_category($this->getExtension()); |
||
544 | } |
||
545 | |||
546 | |||
547 | /** |
||
548 | * Should be called after the file was uploaded |
||
549 | */ |
||
550 | public function onAfterUpload() { |
||
551 | $this->extend('onAfterUpload'); |
||
552 | } |
||
553 | |||
554 | /** |
||
555 | * Make sure the file has a name |
||
556 | */ |
||
557 | protected function onBeforeWrite() { |
||
558 | // Set default owner |
||
559 | if(!$this->isInDB() && !$this->OwnerID) { |
||
560 | $this->OwnerID = Member::currentUserID(); |
||
561 | } |
||
562 | |||
563 | // Set default name |
||
564 | if(!$this->getField('Name')) { |
||
565 | $this->Name ="new-" . strtolower($this->class); |
||
566 | } |
||
567 | |||
568 | // Propegate changes to the AssetStore and update the DBFile field |
||
569 | $this->updateFilesystem(); |
||
570 | |||
571 | parent::onBeforeWrite(); |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * This will check if the parent record and/or name do not match the name on the underlying |
||
576 | * DBFile record, and if so, copy this file to the new location, and update the record to |
||
577 | * point to this new file. |
||
578 | * |
||
579 | * This method will update the File {@see DBFile} field value on success, so it must be called |
||
580 | * before writing to the database |
||
581 | * |
||
582 | * @return bool True if changed |
||
583 | */ |
||
584 | public function updateFilesystem() { |
||
585 | if(!$this->config()->update_filesystem) { |
||
586 | return false; |
||
587 | } |
||
588 | |||
589 | // Check the file exists |
||
590 | if(!$this->File->exists()) { |
||
591 | return false; |
||
592 | } |
||
593 | |||
594 | // Avoid moving files on live; Rely on this being done on stage prior to publish. |
||
595 | if(Versioned::get_stage() !== Versioned::DRAFT) { |
||
596 | return false; |
||
597 | } |
||
598 | |||
599 | // Check path updated record will point to |
||
600 | // If no changes necessary, skip |
||
601 | $pathBefore = $this->File->getFilename(); |
||
602 | $pathAfter = $this->generateFilename(); |
||
603 | if($pathAfter === $pathBefore) { |
||
604 | return false; |
||
605 | } |
||
606 | |||
607 | // Copy record to new location via stream |
||
608 | $stream = $this->File->getStream(); |
||
609 | $this->File->setFromStream($stream, $pathAfter); |
||
610 | return true; |
||
611 | } |
||
612 | |||
613 | /** |
||
614 | * Collate selected descendants of this page. |
||
615 | * $condition will be evaluated on each descendant, and if it is succeeds, that item will be added |
||
616 | * to the $collator array. |
||
617 | * |
||
618 | * @param string $condition The PHP condition to be evaluated. The page will be called $item |
||
619 | * @param array $collator An array, passed by reference, to collect all of the matching descendants. |
||
620 | * @return true|null |
||
621 | */ |
||
622 | public function collateDescendants($condition, &$collator) { |
||
623 | if($children = $this->Children()) { |
||
624 | foreach($children as $item) { |
||
625 | if(!$condition || eval("return $condition;")) $collator[] = $item; |
||
626 | $item->collateDescendants($condition, $collator); |
||
627 | } |
||
628 | return true; |
||
629 | } |
||
630 | } |
||
631 | |||
632 | /** |
||
633 | * Setter function for Name. Automatically sets a default title, |
||
634 | * and removes characters that might be invalid on the filesystem. |
||
635 | * Also adds a suffix to the name if the filename already exists |
||
636 | * on the filesystem, and is associated to a different {@link File} database record |
||
637 | * in the same folder. This means"myfile.jpg" might become"myfile-1.jpg". |
||
638 | * |
||
639 | * Does not change the filesystem itself, please use {@link write()} for this. |
||
640 | * |
||
641 | * @param string $name |
||
642 | * @return $this |
||
643 | */ |
||
644 | public function setName($name) { |
||
689 | |||
690 | /** |
||
691 | * Gets the URL of this file |
||
692 | * |
||
693 | * @return string |
||
694 | */ |
||
695 | public function getAbsoluteURL() { |
||
701 | |||
702 | /** |
||
703 | * Gets the URL of this file |
||
704 | * |
||
705 | * @uses Director::baseURL() |
||
706 | * @param bool $grant Ensures that the url for any protected assets is granted for the current user. |
||
707 | * @return string |
||
708 | */ |
||
709 | public function getURL($grant = true) { |
||
714 | |||
715 | /** |
||
716 | * Get URL, but without resampling. |
||
717 | * |
||
718 | * @param bool $grant Ensures that the url for any protected assets is granted for the current user. |
||
719 | * @return string |
||
720 | */ |
||
721 | public function getSourceURL($grant = true) { |
||
726 | |||
727 | /** |
||
728 | * @todo Coupling with cms module, remove this method. |
||
729 | * |
||
730 | * @return string |
||
731 | */ |
||
732 | public function DeleteLink() { |
||
735 | |||
736 | /** |
||
737 | * Get expected value of Filename tuple value. Will be used to trigger |
||
738 | * a file move on draft stage. |
||
739 | * |
||
740 | * @return string |
||
741 | */ |
||
742 | public function generateFilename() { |
||
743 | // Check if this file is nested within a folder |
||
744 | $parent = $this->Parent(); |
||
745 | if($parent && $parent->exists()) { |
||
746 | return $this->join_paths($parent->getFilename(), $this->Name); |
||
747 | } |
||
748 | return $this->Name; |
||
749 | } |
||
750 | |||
751 | /** |
||
752 | * Ensure that parent folders are published before this one is published |
||
753 | * |
||
754 | * @todo Solve this via triggered publishing / ownership in the future |
||
755 | */ |
||
756 | public function onBeforePublish() { |
||
763 | |||
764 | /** |
||
765 | * Update the ParentID and Name for the given filename. |
||
766 | * |
||
767 | * On save, the underlying DBFile record will move the underlying file to this location. |
||
768 | * Thus it will not update the underlying Filename value until this is done. |
||
769 | * |
||
770 | * @param string $filename |
||
771 | * @return $this |
||
772 | */ |
||
773 | public function setFilename($filename) { |
||
796 | |||
797 | /** |
||
798 | * Returns the file extension |
||
799 | * |
||
800 | * @return string |
||
801 | */ |
||
802 | public function getExtension() { |
||
805 | |||
806 | /** |
||
807 | * Gets the extension of a filepath or filename, |
||
808 | * by stripping away everything before the last"dot". |
||
809 | * Caution: Only returns the last extension in"double-barrelled" |
||
810 | * extensions (e.g."gz" for"tar.gz"). |
||
811 | * |
||
812 | * Examples: |
||
813 | * -"myfile" returns"" |
||
814 | * -"myfile.txt" returns"txt" |
||
815 | * -"myfile.tar.gz" returns"gz" |
||
816 | * |
||
817 | * @param string $filename |
||
818 | * @return string |
||
819 | */ |
||
820 | public static function get_file_extension($filename) { |
||
823 | |||
824 | /** |
||
825 | * Given an extension, determine the icon that should be used |
||
826 | * |
||
827 | * @param string $extension |
||
828 | * @return string Icon filename relative to base url |
||
829 | */ |
||
830 | public static function get_icon_for_extension($extension) { |
||
845 | |||
846 | /** |
||
847 | * Return the type of file for the given extension |
||
848 | * on the current file name. |
||
849 | * |
||
850 | * @return string |
||
851 | */ |
||
852 | public function getFileType() { |
||
855 | |||
856 | /** |
||
857 | * Get descriptive type of file based on filename |
||
858 | * |
||
859 | * @param string $filename |
||
860 | * @return string Description of file |
||
861 | */ |
||
862 | public static function get_file_type($filename) { |
||
891 | |||
892 | /** |
||
893 | * Returns the size of the file type in an appropriate format. |
||
894 | * |
||
895 | * @return string|false String value, or false if doesn't exist |
||
896 | */ |
||
897 | public function getSize() { |
||
904 | |||
905 | /** |
||
906 | * Formats a file size (eg: (int)42 becomes string '42 bytes') |
||
907 | * |
||
908 | * @todo unit tests |
||
909 | * |
||
910 | * @param int $size |
||
911 | * @return string |
||
912 | */ |
||
913 | public static function format_size($size) { |
||
931 | |||
932 | /** |
||
933 | * Convert a php.ini value (eg: 512M) to bytes |
||
934 | * |
||
935 | * @todo unit tests |
||
936 | * |
||
937 | * @param string $iniValue |
||
938 | * @return int |
||
939 | */ |
||
940 | public static function ini2bytes($iniValue) { |
||
951 | |||
952 | /** |
||
953 | * Return file size in bytes. |
||
954 | * |
||
955 | * @return int |
||
956 | */ |
||
957 | public function getAbsoluteSize(){ |
||
960 | |||
961 | public function validate() { |
||
967 | |||
968 | /** |
||
969 | * Maps a {@link File} subclass to a specific extension. |
||
970 | * By default, files with common image extensions will be created |
||
971 | * as {@link Image} instead of {@link File} when using |
||
972 | * {@link Folder::constructChild}, {@link Folder::addUploadToFolder}), |
||
973 | * and the {@link Upload} class (either directly or through {@link FileField}). |
||
974 | * For manually instanciated files please use this mapping getter. |
||
975 | * |
||
976 | * Caution: Changes to mapping doesn't apply to existing file records in the database. |
||
977 | * Also doesn't hook into {@link Object::getCustomClass()}. |
||
978 | * |
||
979 | * @param String File extension, without dot prefix. Use an asterisk ('*') |
||
980 | * to specify a generic fallback if no mapping is found for an extension. |
||
981 | * @return String Classname for a subclass of {@link File} |
||
982 | */ |
||
983 | public static function get_class_for_file_extension($ext) { |
||
987 | |||
988 | /** |
||
989 | * See {@link get_class_for_file_extension()}. |
||
990 | * |
||
991 | * @param String|array |
||
992 | * @param String |
||
993 | */ |
||
994 | public static function set_class_for_file_extension($exts, $class) { |
||
1005 | |||
1006 | public function getMetaData() { |
||
1011 | |||
1012 | public function getMimeType() { |
||
1017 | |||
1018 | public function getStream() { |
||
1023 | |||
1024 | public function getString() { |
||
1029 | |||
1030 | public function setFromLocalFile($path, $filename = null, $hash = null, $variant = null, $config = array()) { |
||
1039 | |||
1040 | public function setFromStream($stream, $filename, $hash = null, $variant = null, $config = array()) { |
||
1049 | |||
1050 | public function setFromString($data, $filename, $hash = null, $variant = null, $config = array()) { |
||
1059 | |||
1060 | public function getIsImage() { |
||
1063 | |||
1064 | public function getFilename() { |
||
1067 | |||
1068 | public function getHash() { |
||
1071 | |||
1072 | public function getVariant() { |
||
1075 | |||
1076 | /** |
||
1077 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
1078 | * |
||
1079 | * @return string |
||
1080 | */ |
||
1081 | public function forTemplate() { |
||
1084 | |||
1085 | /** |
||
1086 | * Return a html5 tag of the appropriate for this file (normally img or a) |
||
1087 | * |
||
1088 | * @return string |
||
1089 | */ |
||
1090 | public function getTag() { |
||
1097 | |||
1098 | public function requireDefaultRecords() { |
||
1111 | |||
1112 | /** |
||
1113 | * Joins one or more segments together to build a Filename identifier. |
||
1114 | * |
||
1115 | * Note that the result will not have a leading slash, and should not be used |
||
1116 | * with local file paths. |
||
1117 | * |
||
1118 | * @param string $part,... Parts |
||
1119 | * @return string |
||
1120 | */ |
||
1121 | public static function join_paths() { |
||
1137 | |||
1138 | public function deleteFile() { |
||
1141 | |||
1142 | public function getVisibility() { |
||
1145 | |||
1146 | public function publishFile() { |
||
1149 | |||
1150 | public function protectFile() { |
||
1153 | |||
1154 | public function grantFile() { |
||
1157 | |||
1158 | public function revokeFile() { |
||
1161 | |||
1162 | public function canViewFile() { |
||
1165 | } |
||
1166 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: