1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76
<?php
require_once APP . DS . 'vendors' . DS . 'shells'. DS . 'bedita_base.php';
class MediaShell extends BeditaBaseShell {
public function help() {
$this->hr();
$this->out('media script shell usage:');
$this->out('');
$this->out('./cake.sh media fixMissing -f <replacementBase>');
$this->out('');
}
public function fixMissing() {
if (!isset($this->params['f'])) {
$this->out("Missing -f parameter");
return;
}
$mediaRoot = Configure::read('mediaRoot');
$replacement = $this->params['f'];
$media = $this->missingMedia();
foreach ($media as $f) {
$pos = strrpos($f, '.');
$input = $replacement;
$output = $mediaRoot . $f;
$this->out('Saving file ' . $input . ' to ' . $output);
if ($pos > 0) {
$ext = substr($f, $pos+1);
if (file_exists($replacement . '.' . $ext)) {
$input = $replacement . '.' . $ext;
} else {
$input = $replacement;
}
}
if (!file_exists(dirname($output))) {
mkdir(dirname($output), 0777, true);
}
if (!file_exists($output)) {
symlink($input, $output);
}
}
$stillMissing = $this->missingMedia();
if (!empty($stillMissing)) {
$this->out('Media still missing in media root ' . $mediaRoot);
foreach ($stillMissing as $f) {
$this->out($f);
}
}
}
private function missingMedia() {
$missing = array();
$mediaRoot = Configure::read('mediaRoot');
$stream = ClassRegistry::init('Stream');
$streams = $stream->find('list', array(
'fields' => array('id', 'uri'),
'contain' => array()
));
foreach ($streams as $id => $uri) {
if((stripos($uri, "/") === 0) && !file_exists($mediaRoot.$uri)) {
$missing[] = $uri;
}
}
return $missing;
}
}
?>