Changeset 293
- Timestamp:
- 02/08/10 16:55:58 (11 years ago)
- Location:
- trunk/server/www/app
- Files:
-
- 3 added
- 11 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/server/www/app/config/bootstrap.php
r221 r293 33 33 * 34 34 */ 35 36 /** 37 * Set a path to the files directory 38 */ 39 define('FILES', APP . 'files' . DS); 35 40 36 41 /** -
trunk/server/www/app/config/sql/schema.php
r292 r293 1 1 <?php 2 2 /* SVN FILE: $Id$ */ 3 /* App schema generated on: 2010-02-0 3 13:02:23 : 1265200163*/3 /* App schema generated on: 2010-02-08 17:02:52 : 1265644912*/ 4 4 class AppSchema extends CakeSchema { 5 5 var $name = 'App'; … … 178 178 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1)) 179 179 ); 180 var $testsuites = array( 181 'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'primary'), 182 'name' => array('type' => 'string', 'null' => false, 'default' => NULL), 183 'source' => array('type' => 'text', 'null' => false, 'default' => NULL), 184 'root' => array('type' => 'text', 'null' => false, 'default' => NULL), 185 'gallery_id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'index'), 186 'created' => array('type' => 'datetime', 'null' => false, 'default' => NULL), 187 'modified' => array('type' => 'datetime', 'null' => false, 'default' => NULL), 188 'indexes' => array('PRIMARY' => array('column' => 'id', 'unique' => 1), 'gallery_id' => array('column' => 'gallery_id', 'unique' => 0)) 189 ); 180 190 var $users = array( 181 191 'id' => array('type' => 'string', 'null' => false, 'default' => NULL, 'length' => 36, 'key' => 'primary'), -
trunk/server/www/app/controllers/galleries_controller.php
r292 r293 73 73 74 74 /** 75 * Sort an array of Galleries threaded, like find('threaded') would do 76 */ 77 private function _sortThreaded($results) 78 { 79 $return = $idMap = array(); 80 $ids = Set::extract($results, '{n}.Gallery.id'); 81 82 foreach ($results as $result) { 83 $result['children'] = array(); 84 $id = $result['Gallery']['id']; 85 $parentId = $result['Gallery']['parent_id']; 86 if (isset($idMap[$id]['children'])) { 87 $idMap[$id] = array_merge($result, (array)$idMap[$id]); 88 } else { 89 $idMap[$id] = array_merge($result, array('children' => array())); 90 } 91 if (!$parentId || !in_array($parentId, $ids)) { 92 $return[] =& $idMap[$id]; 93 } else { 94 $idMap[$parentId]['children'][] =& $idMap[$id]; 95 } 96 } 97 if (count($return) > 1) { 98 $ids = array_unique(Set::extract('/Gallery/parent_id', $return)); 99 if (count($ids) > 1) { 100 $root = $return[0]['Gallery']['parent_id']; 101 foreach ($return as $key => $value) { 102 if ($value['Gallery']['parent_id'] != $root) { 103 unset($return[$key]); 104 } 105 } 106 } 107 } 108 return $return; 109 } 110 111 /** 75 112 * Show a list of all galleries 76 113 */ … … 81 118 82 119 foreach ($galleries as &$gallery) { 83 $num_documents = $this->Request->query('SELECT COUNT(*) as `count` 84 FROM `requests` AS `Request` 85 LEFT JOIN `galleries_requests` AS `GalleriesRequest` 86 ON `Request`.`id` = `GalleriesRequest`.`request_id` 87 LEFT JOIN `galleries` AS `Gallery` 88 ON `GalleriesRequest`.`gallery_id` = `Gallery`.`id` 89 WHERE 90 `Gallery`.`id` > ' . $gallery['Gallery']['lft'] . ' 91 AND `Gallery`.`id` < ' . $gallery['Gallery']['rght']); 92 $gallery['Gallery']['num_documents'] = sizeof($gallery['Request']) + $num_documents[0][0]['count']; 120 $gallery['Gallery']['num_documents'] = $this->Gallery->requestCount($gallery['Gallery']['id'], true); 93 121 } 94 122 … … 122 150 123 151 // Get the children of this gallery 124 $children = $this->Gallery->find('threaded', array( 152 // TODO: Do this without recursion below Request because it generates ridiculous amounts of 153 // duplicate queries that slow the system *a lot* 154 $children = $this->Gallery->find('all', array( 125 155 'conditions' => array( 126 'Gallery. id>' => $gallery['Gallery']['lft'],127 'Gallery. id<' => $gallery['Gallery']['rght'],156 'Gallery.lft >' => $gallery['Gallery']['lft'], 157 'Gallery.rght <' => $gallery['Gallery']['rght'], 128 158 ), 129 'contain' => array( 130 'Request', 131 'Request.Validator', 132 'Request.Job', 133 'Request.Job.Application', 134 'Request.Job.Platform', 135 'Request.Job.Result', 136 'Request.Job.Result.Validator', 137 ), 138 )); 159 'contain' => array('Request'), 160 )); 161 162 foreach ($children as &$child) { 163 foreach ($child['Request'] as &$request) { 164 $request['Validator'] = $this->Request->Validator->find('all', array( 165 'conditions' => array('Validator.parent_id' => $request['id']), 166 'recursive' => -1, 167 )); 168 169 $request['Job'] = $this->Request->Job->find('all', array( 170 'conditions' => array('Job.request_id' => $request['id']), 171 'contain' => array( 172 'Application', 173 'Platform', 174 'Result', 175 'Result.Validator' 176 ), 177 )); 178 } 179 } 180 181 $gallery['children'] = $this->_sortThreaded($children); 139 182 140 183 // Get the path to this gallery … … 144 187 // Check access 145 188 $access = $this->_checkAccess($slug); 146 147 $gallery['children'] =& $children;148 189 $this->set(compact('gallery', 'path', 'access')); 149 190 } -
trunk/server/www/app/models/behaviors/file.php
r287 r293 50 50 public function setup(&$model, $config = array()) 51 51 { 52 if (!is_array($config)) { 53 $config = array('directory' => $config); 54 } 55 56 $this->settings = array_merge($this->settings, $config); 57 58 if (!$this->settings['directory']) { 59 $this->settings['directory'] = 'files'; 60 } 61 62 $cwd = getcwd(); 63 chdir(APP); 64 $this->settings['directory'] = realpath($this->settings['directory']); 65 chdir($cwd); 66 67 if (!is_writeable($this->settings['directory'])) { 52 if (is_array($config)) { 53 $this->settings = array_merge($this->settings, $config); 54 } 55 56 if (!is_writeable(FILES)) { 68 57 $this->errors[] = __('File storage directory is not writeable.', true); 69 58 return; … … 83 72 public function setFileUpload(&$model, $file = array()) 84 73 { 74 $this->errors = array(); 75 85 76 if (!is_array($file) || sizeof($file) == 0) { 86 77 $this->errors[] = __('There was no uploaded file', true); … … 122 113 } 123 114 124 $destination = $this->settings['directory'] . DS . $model->data[$model->name][$this->settings['fields']['path']] . DS . $file['name'];115 $destination = FILES . $model->data[$model->name][$this->settings['fields']['path']] . DS . $file['name']; 125 116 if (!mkdir(dirname($destination), 0775, true)) { 126 117 $this->errors[] = __('Could not create the destination directory.', true); … … 151 142 public function setFileBuffer(&$model, &$buffer, $filename, $filters = false) 152 143 { 153 $destination = $this->settings['directory'] . DS . $model->data[$model->name][$this->settings['fields']['path']] . DS . $filename; 144 $this->errors = array(); 145 $destination = FILES . $model->data[$model->name][$this->settings['fields']['path']] . DS . $filename; 154 146 155 147 if (!file_exists(dirname($destination)) && !mkdir(dirname($destination), 0775, true)) { … … 199 191 200 192 /** 193 * Set the file records to any file 194 * 195 * @param object $model A reference to the model 196 * @param string $path full path to the file 197 * @param string $filename The filename that the file should get. Leave as false for the original filename 198 * @param boolean $move True if the file should be moved, false if it should be copied. 199 * @return boolean True on success or false on failure 200 */ 201 public function setFile(&$model, $path = false, $filename = false, $move = false) 202 { 203 $this->errors = array(); 204 205 if (!$path) { 206 $this->errors[] = __('There was no file', true); 207 return false; 208 } 209 210 if (!is_readable($path)) { 211 $this->errors[] = __('The file cannot be read.', true); 212 return false; 213 } 214 215 $mimestring = ''; 216 if (!$mimetype = $this->_getMimetype($model, $path, $mimestring)) { 217 $this->errors[] = sprintf(__('Invalid mimetype %s detected for file', true), $mimestring); 218 return false; 219 } 220 221 if (!$this->_checkMimetype($mimetype)) { 222 $this->errors[] = __('Illegal mimetype.', true); 223 return false; 224 } 225 226 if (!$filename) { 227 $filename = basename($path); 228 } 229 230 $destination = FILES . $model->field($this->settings['fields']['path']) . DS . $filename; 231 232 if (file_exists($destination)) { 233 if (!unlink($destination)) { 234 $this->errors[] = __('The destination path already exists and cannot be deleted.'); 235 return false; 236 } 237 } 238 239 if (!file_exists(dirname($destination))) { 240 if (!mkdir(dirname($destination), 0775, true)) { 241 $this->errors[] = __('Could not create the destination directory.', true); 242 return false; 243 } 244 } 245 246 if ($move) { 247 $result = rename($path, $destination); 248 } else { 249 $result = copy($path, $destination); 250 } 251 252 if (!$result) { 253 $this->errors = __('The file could not be copied or moved.'); 254 return false; 255 } 256 257 $model->set(array( 258 $this->settings['fields']['filename'] => $filename, 259 $this->settings['fields']['mimetype_id'] => $mimetype['Mimetype']['id'], 260 )); 261 262 return true; 263 } 264 265 /** 201 266 * Delete the currrent on-disk file, if any 202 267 * … … 235 300 236 301 if ($path = $this->getPath($model)) { 237 return file_get_contents($ this->settings['directory'] . DS . $path);302 return file_get_contents($path); 238 303 } 239 304 … … 262 327 } 263 328 264 if (!$path || !$filename || !is_file( $this->settings['directory'] . DS . $path . DS . $filename)) {329 if (!$path || !$filename || !is_file(FILES . $path . DS . $filename)) { 265 330 return false; // There is no file 266 331 } 267 332 268 return $this->settings['directory'] . DS . $path . DS . $filename;333 return FILES . $path . DS . $filename; 269 334 } 270 335 … … 284 349 if (strpos($mimestring, ';') !== false) { 285 350 $mimestring = substr($mimestring, 0, strpos($mimestring, ';')); 351 } 352 353 if ($mimestring == 'application/zip') { 354 // Guess from the extension 355 $ext = substr($path, strrpos($path, '.') + 1); 356 357 $mimetype = $model->Mimetype->find('first', array( 358 'conditions' => array( 359 'Mimetype.extension' => $ext, 360 ), 361 )); 362 363 return $mimetype; 286 364 } 287 365 … … 308 386 309 387 /** 310 * Abort the save if there were errors with the file records311 *312 * @param object $model A reference to the model313 * @return True on success, false to abort the save314 */315 public function beforeSave(&$model)316 {317 if (!isset($model->data[$model->name][$this->settings['fields']['filename']])) {318 return true; // No file is being saved319 }320 321 $path = $this->getPath($model);322 if ($path && !file_exists($path)) {323 $this->errors[] = __('The filename field is set but the file does not exist', true);324 }325 326 if (!empty($this->errors) && !$this->settings['saveOnError']) {327 return false;328 }329 330 return true;331 }332 333 /**334 388 * Delete the file when the record is deleted 335 389 * -
trunk/server/www/app/models/gallery.php
r292 r293 30 30 31 31 /** @var string Every application supports a certain ODF doctype */ 32 public $hasAndBelongsToMany = 'Request'; 32 public $hasAndBelongsToMany = array('Request' => array('unique' => false)); 33 34 /** Some galleries have a Testsuite */ 35 public $hasOne = 'Testsuite'; 33 36 34 37 /** @var array The model behaviors */ … … 90 93 91 94 /** 95 * Count the number of requests in this gallery 96 * @param int $id The Gallery ID 97 * @param bool $recursive True to count requests in subgalleries as well 98 */ 99 public function requestCount($id = null, $recursive = false) 100 { 101 if (!$id) { 102 $id = $this->id; 103 } 104 105 if (!$id) { 106 return 0; 107 } 108 109 $num_requests = $this->GalleriesRequest->find('count', array( 110 'conditions' => array('gallery_id' => $id), 111 )); 112 113 if ($recursive) { 114 $gallery = $this->read(null, $id); 115 116 if (!$gallery['Gallery']['lft'] && !$gallery['Gallery']['rght']) { 117 return $num_requests; 118 } 119 120 $num_documents = $this->Request->query('SELECT COUNT(*) as `count` 121 FROM `requests` AS `Request` 122 LEFT JOIN `galleries_requests` AS `GalleriesRequest` 123 ON `Request`.`id` = `GalleriesRequest`.`request_id` 124 LEFT JOIN `galleries` AS `Gallery` 125 ON `GalleriesRequest`.`gallery_id` = `Gallery`.`id` 126 WHERE 127 `Gallery`.`lft` > ' . $gallery['Gallery']['lft'] . ' 128 AND `Gallery`.`rght` < ' . $gallery['Gallery']['rght']); 129 $num_requests = $num_requests + $num_documents[0][0]['count']; 130 } 131 132 return $num_requests; 133 } 134 135 /** 92 136 * Convert the Markdown description to HTML before saving 93 137 * @return boolean True to continue saving -
trunk/server/www/app/models/job.php
r209 r293 67 67 } 68 68 } 69 70 /** 71 * Remove the result before removing the Job 72 */ 73 public function beforeDelete($cascade) 74 { 75 $result_id = $this->field('result_id'); 76 77 if ($result_id) { 78 $this->Result->delete($result_id); 79 } 80 81 return true; 82 } 69 83 } 70 84 -
trunk/server/www/app/models/request.php
r287 r293 47 47 48 48 /** @var array Requests can belong to multiple galleries */ 49 public $hasAndBelongsToMany = array('Gallery' );49 public $hasAndBelongsToMany = array('Gallery' => array('unique' => false)); 50 50 51 51 /** @var array Use the file behaviour to associate ODF files with Requests */ … … 53 53 'Containable', 54 54 'BeanStalk.Deferrable', 55 'File' => 'files',55 'File', 56 56 'Pipeline' => 'Preprocessor', 57 57 ); … … 210 210 211 211 /** 212 * Delete all jobs and results belonging to the request 213 */ 214 public function deleteJobs($id = null) 215 { 216 if (!$id) { 217 $id = $this->id; 218 } 219 220 if (!$id) { 221 return; 222 } 223 224 $request = $this->find('first', array( 225 'contain' => array('Job'), 226 'conditions' => array('Request.id' => $id), 227 )); 228 229 if (!$request || !isset($request['Job']) || empty($request['Job'])) { 230 return; 231 } 232 233 foreach ($request['Job'] as $job) { 234 $this->Job->delete($job['id']); // Results are deleted in Job::beforeDelete 235 } 236 } 237 238 /** 212 239 * Generate a zipfile containing the original request and all results 213 240 * … … 220 247 } 221 248 222 $root = $this->Behaviors->File->settings['directory'] . DS . $this->field('root');249 $root = FILES . $this->field('root'); 223 250 $filename = $this->field('filename'); 224 251 $filename = substr($filename, 0, strrpos($filename, '.')); … … 272 299 } 273 300 301 $this->contain(); 274 302 $this->read(); 303 275 304 $clamd = new Clamd($clamd_config); 276 305 $path = $this->getPath(); … … 318 347 public function validateFile() 319 348 { 349 $this->contain('Validator'); 320 350 $this->read(); 351 352 $this->errors = array(); 321 353 if (!is_array($this->data['Validator'])) { 322 354 $this->log('No validators found for Request: ' . $this->id, LOG_DEBUG); -
trunk/server/www/app/models/result.php
r287 r293 55 55 'Containable', 56 56 'BeanStalk.Deferrable', 57 'File' => 'files',57 'File', 58 58 'Pipeline' => 'Postprocessor', 59 59 ); -
trunk/server/www/app/vendors/shells/cron.php
r185 r293 39 39 public function expire() 40 40 { 41 App::import('Model', 'Request'); 42 $Request = new Request(); 41 $Request = ClassRegistry::init('Request'); 43 42 44 43 if (!$Request->expireAll()) { 45 44 $this->log('Cron: Could not expire old requests.'); 46 45 } 46 } 47 48 /** 49 * Update the test suites 50 */ 51 public function syncTestsuites() 52 { 53 $Testsuite = ClassRegistry::init('Testsuite'); 54 $Testsuite->synchronise(); 47 55 } 48 56 … … 58 66 $this->out('Commands:'); 59 67 $this->out("\n\texpire\n\t\tSet STATE_EXPIRED on all requests whose expiry time has passed.\n\t\tThis should be run every minute."); 68 $this->out("\n\tsyncTestsuites\n\t\tSynchronise all ODF testsuites.\n\t\tThis should be run every hour."); 60 69 $this->out("\n\thelp\n\t\tShow this help"); 61 70 $this->out(''); -
trunk/server/www/app/views/elements/gallery.ctp
r292 r293 9 9 <?php echo $this->element('gallery', array('gallery' => $child, 'indent' => $indent + 2)); ?> 10 10 <?php endforeach;?> 11 12 <?php if (isset($gallery['Request']) && is_array($gallery['Request'])):?> 11 13 <?php foreach ($gallery['Request'] as $request):?> 12 14 <tr class="request"> … … 49 51 <?php endforeach; ?> 50 52 <?php endforeach; ?> 53 <?php endif; ?> -
trunk/server/www/app/views/galleries/view.ctp
r292 r293 14 14 <div id="gallery-results" class="related"> 15 15 <h2><?php __('Documents');?></h2> 16 <?php if (!empty($gallery['Request'])):?>17 16 <table cellpadding = "0" cellspacing = "0"> 18 17 <tr> … … 25 24 <?php echo $this->element('gallery', array('gallery' => $gallery, 'indent' => 0)); ?> 26 25 </table> 27 <?php endif; ?>28 26 </div> 29 27
Note: See TracChangeset
for help on using the changeset viewer.