00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00081 class Project
00082 {
00083
00084 var $projectid;
00085 var $name;
00086 var $target_dir;
00087 var $ftp_url;
00088 var $ftp_passive;
00089 var $cmd_after_publish;
00090 var $content_negotiation;
00091 var $cut_index;
00092
00093
00094 function Project( $projectid='' )
00095 {
00096 if ( intval($projectid) != 0 )
00097 $this->projectid = $projectid;
00098 }
00099
00100
00104 function available( $id )
00105 {
00106 $db = db_connection();
00107
00108 $sql = new Sql('SELECT 1 FROM {t_project} '.
00109 ' WHERE id={id}');
00110 $sql->setInt('id' ,$id );
00111
00112 return intval($db->getOne($sql->query)) == 1;
00113 }
00114
00115
00116
00117 function getAll()
00118 {
00119 return Project::getAllProjects();
00120 }
00121
00122
00123
00124 function getAllProjects()
00125 {
00126 $db = db_connection();
00127 $sql = new Sql( 'SELECT id,name FROM {t_project} '.
00128 ' ORDER BY name' );
00129
00130 return $db->getAssoc( $sql->query );
00131 }
00132
00133
00134
00135 function getAllProjectIds()
00136 {
00137 $db = db_connection();
00138 $sql = new Sql( 'SELECT id FROM {t_project} '.
00139 ' ORDER BY name' );
00140
00141 return $db->getCol( $sql->query );
00142 }
00143
00144
00145 function getLanguages()
00146 {
00147 $db = db_connection();
00148
00149 $sql = new Sql( 'SELECT id,name FROM {t_language}'.
00150 ' WHERE projectid={projectid} '.
00151 ' ORDER BY name' );
00152 $sql->setInt ('projectid',$this->projectid);
00153
00154 return $db->getAssoc( $sql->query );
00155 }
00156
00157
00158 function getLanguageIds()
00159 {
00160 return array_keys( $this->getLanguages() );
00161 }
00162
00163
00164 function getModels()
00165 {
00166 $db = db_connection();
00167
00168 $sql = new Sql( 'SELECT id,name FROM {t_projectmodel}'.
00169 ' WHERE projectid= {projectid} '.
00170 ' ORDER BY name' );
00171 $sql->setInt ('projectid',$this->projectid);
00172
00173 return $db->getAssoc( $sql->query );
00174 }
00175
00176
00177 function getModelIds()
00178 {
00179 return array_keys( $this->getModels() );
00180 }
00181
00182
00183 function getTemplateIds()
00184 {
00185 $db = db_connection();
00186
00187 $sql = new Sql( 'SELECT id FROM {t_template}'.
00188 ' WHERE projectid= {projectid} ' );
00189 $sql->setInt ('projectid',$this->projectid);
00190
00191 return $db->getCol( $sql->query );
00192 }
00193
00194
00195 function getTemplates()
00196 {
00197 $db = db_connection();
00198
00199 $sql = new Sql( 'SELECT id,name FROM {t_template}'.
00200 ' WHERE projectid= {projectid} ' );
00201 $sql->setInt ('projectid',$this->projectid);
00202
00203 return $db->getAssoc( $sql->query );
00204 }
00205
00206
00210 function getRootObjectId()
00211 {
00212 $db = db_connection();
00213
00214 $sql = new Sql('SELECT id FROM {t_object}'.
00215 ' WHERE parentid IS NULL'.
00216 ' AND projectid={projectid}' );
00217
00218 $sql->setInt('projectid',$this->projectid);
00219
00220 return( $db->getOne( $sql->query ) );
00221 }
00222
00223
00224
00225
00226 function load()
00227 {
00228 $db = db_connection();
00229
00230 $sql = new Sql( 'SELECT * FROM {t_project} '.
00231 ' WHERE id={projectid}' );
00232 $sql->setInt( 'projectid',$this->projectid );
00233
00234 $row = $db->getRow( $sql->query );
00235
00236 $this->name = $row['name' ];
00237 $this->target_dir = $row['target_dir' ];
00238 $this->ftp_url = $row['ftp_url' ];
00239 $this->ftp_passive = $row['ftp_passive' ];
00240 $this->cmd_after_publish = $row['cmd_after_publish' ];
00241 $this->content_negotiation = $row['content_negotiation'];
00242 $this->cut_index = $row['cut_index' ];
00243 }
00244
00245
00246
00247 function loadByName()
00248 {
00249 $db = db_connection();
00250
00251 $sql = new Sql( 'SELECT * FROM {t_project} '.
00252 ' WHERE name={projectname}' );
00253 $sql->setString( 'projectname',$this->name );
00254
00255 $row = $db->getRow( $sql->query );
00256
00257 $this->projectid = $row['id' ];
00258 $this->target_dir = $row['target_dir' ];
00259 $this->ftp_url = $row['ftp_url' ];
00260 $this->ftp_passive = $row['ftp_passive' ];
00261 $this->cmd_after_publish = $row['cmd_after_publish' ];
00262 $this->content_negotiation = $row['content_negotiation'];
00263 $this->cut_index = $row['cut_index' ];
00264 }
00265
00266
00267
00268 function save()
00269 {
00270 $db = db_connection();
00271
00272 $sql = new Sql( 'UPDATE {t_project}'.
00273 ' SET name = {name},'.
00274 ' target_dir = {target_dir},'.
00275 ' ftp_url = {ftp_url}, '.
00276 ' ftp_passive = {ftp_passive}, '.
00277 ' cut_index = {cut_index}, '.
00278 ' content_negotiation = {content_negotiation}, '.
00279 ' cmd_after_publish = {cmd_after_publish} '.
00280 'WHERE id= {projectid} ' );
00281
00282 $sql->setString('name' ,$this->name );
00283 $sql->setString('target_dir' ,$this->target_dir );
00284 $sql->setString('ftp_url' ,$this->ftp_url );
00285 $sql->setInt ('ftp_passive' ,$this->ftp_passive );
00286 $sql->setString('cmd_after_publish' ,$this->cmd_after_publish );
00287 $sql->setInt ('content_negotiation',$this->content_negotiation );
00288 $sql->setInt ('cut_index' ,$this->cut_index );
00289 $sql->setInt ('projectid' ,$this->projectid );
00290
00291 $db->query( $sql->query );
00292
00293 $rootFolder = new Folder( $this->getRootObjectId() );
00294 $rootFolder->load();
00295 $rootFolder->filename = $this->name;
00296 $rootFolder->save();
00297 }
00298
00299
00300
00301 function getProperties()
00302 {
00303 return Array( 'name' =>$this->name,
00304 'target_dir' =>$this->target_dir,
00305 'ftp_url' =>$this->ftp_url,
00306 'ftp_passive' =>$this->ftp_passive,
00307 'cmd_after_publish' =>$this->cmd_after_publish,
00308 'content_negotiation'=>$this->content_negotiation,
00309 'cut_index' =>$this->cut_index,
00310 'projectid' =>$this->projectid );
00311 }
00312
00313
00314
00315 function add()
00316 {
00317 $db = db_connection();
00318
00319 $sql = new Sql('SELECT MAX(id) FROM {t_project}');
00320 $this->projectid = intval($db->getOne($sql->query))+1;
00321
00322
00323
00324 $sql = new Sql( 'INSERT INTO {t_project} (id,name,target_dir,ftp_url,ftp_passive,cmd_after_publish,content_negotiation,cut_index) '.
00325 " VALUES( {projectid},{name},'','',0,'',0,0 ) " );
00326 $sql->setString('name' ,$this->name );
00327 $sql->setInt ('projectid',$this->projectid );
00328
00329 $db->query( $sql->query );
00330
00331
00332 $model = new Model();
00333 $model->projectid = $this->projectid;
00334 $model->name = 'html';
00335 $model->add();
00336
00337
00338 $language = new Language();
00339 $language->projectid = $this->projectid;
00340 $language->isoCode = 'en';
00341 $language->name = 'english';
00342 $language->add();
00343
00344
00345 $folder = new Folder();
00346 $folder->isRoot = true;
00347 $folder->projectid = $this->projectid;
00348 $folder->languageid = $language->languageid;
00349 $folder->filename = $this->name;
00350 $folder->name = $this->name;
00351 $folder->isRoot = true;
00352 $folder->add();
00353
00354
00355 $template = new Template();
00356 $template->projectid = $this->projectid;
00357 $template->name = '';
00358 $template->modelid = $model->modelid;
00359 $template->languageid = $language->languageid;
00360 $template->extension = 'html';
00361 $template->src = '<html><body><h1>Hello world</h1><hr><p>Hello, World.</p></body></html>';
00362 $template->add();
00363 $template->save();
00364
00365
00366 $page = new Page();
00367 $page->parentid = $folder->objectid;
00368 $page->projectid = $this->projectid;
00369 $page->languageid = $language->languageid;
00370 $page->templateid = $template->templateid;
00371 $page->filename = '';
00372 $page->name = 'OpenRat';
00373 $page->add();
00374 }
00375
00376
00377
00378 function delete()
00379 {
00380 $db = db_connection();
00381
00382
00383 $folder = new Folder( $this->getRootObjectId() );
00384 $folder->deleteAll();
00385
00386
00387 foreach( $this->getLanguageIds() as $languageid )
00388 {
00389 $language = new Language( $languageid );
00390 $language->delete();
00391 }
00392
00393
00394 foreach( $this->getTemplateIds() as $templateid )
00395 {
00396 $template = new Template( $templateid );
00397 $template->delete();
00398 }
00399
00400
00401 foreach( $this->getModelIds() as $modelid )
00402 {
00403 $model = new Model( $modelid );
00404 $model->delete();
00405 }
00406
00407
00408
00409 $sql = new Sql( 'DELETE FROM {t_project}'.
00410 ' WHERE id= {projectid} ' );
00411 $sql->setInt( 'projectid',$this->projectid );
00412 $db->query( $sql->query );
00413 }
00414
00415 function getDefaultLanguageId()
00416 {
00417 $db = Session::getDatabase();
00418
00419
00420
00421 $sql = new Sql( 'SELECT id FROM {t_language} '.
00422 ' WHERE projectid={projectid}'.
00423 ' ORDER BY is_default DESC' );
00424
00425 $sql->setInt('projectid',$this->projectid );
00426
00427 return $db->getOne( $sql->query );
00428 }
00429
00430
00431 function getDefaultModelId()
00432 {
00433 $db = Session::getDatabase();
00434
00435
00436
00437 $sql = new Sql( 'SELECT id FROM {t_model} '.
00438 ' WHERE projectid={projectid}'.
00439 ' ORDER BY is_default DESC' );
00440 $sql->setInt('projectid',$this->projectid );
00441
00442 return $db->getOne( $sql->query );
00443 }
00444
00445
00446 function checkLostFiles()
00447 {
00448 $db = &Session::getDatabase();
00449
00450 $sql = new Sql( <<<EOF
00451 SELECT thistab.id FROM {t_object} AS thistab
00452 LEFT JOIN {t_object} AS parenttab
00453 ON parenttab.id = thistab.parentid
00454 WHERE thistab.projectid={projectid} AND thistab.parentid IS NOT NULL AND parenttab.id IS NULL
00455 EOF
00456 );
00457 $sql->setInt('projectid',$this->projectid);
00458
00459 $lostAndFoundFolder = new Folder();
00460 $lostAndFoundFolder->projectid = $this->projectid;
00461 $lostAndFoundFolder->languageid = $this->getDefaultLanguageId();
00462 $lostAndFoundFolder->filename = "lostandfound";
00463 $lostAndFoundFolder->name = 'Lost+found';
00464 $lostAndFoundFolder->parentid = $this->getRootObjectId();
00465 $lostAndFoundFolder->add();
00466
00467 foreach( $db->getCol($sql->query) as $id )
00468 {
00469 echo 'Lost file! moving '.$id.' to lost+found.';
00470 $obj = new Object( $id );
00471 $obj->setParentId( $lostAndFoundFolder->objectid );
00472 }
00473 }
00474
00475
00484 function export( $dbid_destination )
00485 {
00486 global $conf;
00487 $zeit = date('Y-m-d\TH:i:sO');
00488
00489 $db_src = db_connection();
00490 $db_dest = new DB( $conf['database'][$dbid_destination] );
00491
00492
00493
00494
00495
00496 $mapping = array();
00497 $ids = array('project' => array('foreign_keys'=>array(),
00498 'primary_key' =>'id',
00499 'unique_idx' =>'name',
00500 'erase' =>array()
00501 ),
00502 'language' => array('foreign_keys'=>array('projectid'=>'project'),
00503 'primary_key' =>'id'
00504 ),
00505 'projectmodel' => array('foreign_keys'=>array('projectid'=>'project'),
00506 'primary_key' =>'id'
00507 ),
00508 'template' => array('foreign_keys'=>array('projectid'=>'project'),
00509 'primary_key' =>'id'
00510 ),
00511 'object' => array('foreign_keys'=>array('projectid' =>'project' ),
00512 'self_key' =>'parentid',
00513 'primary_key' =>'id',
00514 'erase' =>array('create_userid','lastchange_userid')
00515 ),
00516 'element' => array('foreign_keys'=>array('templateid' =>'template',
00517 'folderobjectid' =>'object',
00518 'default_objectid'=>'object' ),
00519 'primary_key' =>'id'
00520 ),
00521 'templatemodel'=> array('foreign_keys'=>array('projectmodelid'=>'projectmodel',
00522 'templateid' =>'template' ),
00523 'primary_key' =>'id',
00524 'replace' =>array('text'=>'element')
00525 ),
00526 'name' => array('foreign_keys'=>array('objectid' =>'object',
00527 'languageid'=>'language' ),
00528 'primary_key' =>'id'
00529 ),
00530 'page' => array('foreign_keys'=>array('objectid' =>'object',
00531 'templateid'=>'template' ),
00532 'primary_key' =>'id'
00533 ),
00534 'value' => array('foreign_keys'=>array('pageid' =>'page',
00535 'languageid'=>'language',
00536 'elementid'=>'element',
00537 'linkobjectid'=>'object' ),
00538 'erase' =>array('lastchange_userid'),
00539 'replace' =>array('text'=>'object'),
00540 'primary_key' =>'id'
00541 ),
00542 'link' => array('foreign_keys'=>array('objectid' =>'object',
00543 'link_objectid'=>'object' ),
00544 'primary_key' =>'id'
00545 ),
00546 'folder' => array('foreign_keys'=>array('objectid' =>'object' ),
00547 'primary_key' =>'id'
00548 ),
00549 'file' => array('foreign_keys'=>array('objectid' =>'object' ),
00550 'primary_key' =>'id',
00551 'binary' =>'value'
00552 ),
00553
00554 );
00555 foreach( $ids as $tabelle=>$data )
00556 {
00557
00558
00559 $mapping[$tabelle] = array();
00560 $idcolumn = $data['primary_key'];
00561
00562
00563 $sql = new Sql( 'SELECT MAX('.$idcolumn.') FROM {t_'.$tabelle.'}',$dbid_destination);
00564 $maxid = intval($db_dest->getOne($sql->query));
00565 $nextid = $maxid;
00566
00567
00568 if ( count($data['foreign_keys'])==0 )
00569 {
00570 $where = ' WHERE id='.$this->projectid;
00571 }
00572 else
00573 {
00574 foreach( $data['foreign_keys'] as $fkey_column=>$target_tabelle )
00575 {
00576 $where = ' WHERE '.$fkey_column.' IN ('.join(array_keys($mapping[$target_tabelle]),',').')';
00577 break;
00578 }
00579 }
00580 $sql = new Sql( 'SELECT '.$idcolumn.' FROM {t_'.$tabelle.'} '.$where);
00581
00582 foreach( $db_src->getCol($sql->query) as $srcid )
00583 {
00584 $mapping[$tabelle][$srcid] = ++$nextid;
00585
00586
00587
00588 $sql = new Sql( 'SELECT * FROM {t_'.$tabelle.'} WHERE id={id}');
00589 $sql->setInt('id',$srcid);
00590 $row = $db_src->getRow( $sql->query );
00591
00592
00593 $row[$idcolumn] = $mapping[$tabelle][$srcid];
00594
00595
00596 foreach( $data['foreign_keys'] as $fkey_column=>$target_tabelle)
00597 {
00598 if ( intval($row[$fkey_column]) != 0 )
00599 $row[$fkey_column] = $mapping[$target_tabelle][$row[$fkey_column]];
00600
00601
00602
00603 }
00604
00605 foreach( array_keys($row) as $key )
00606 {
00607 if ( isset($data['unique_idx']) && $key == $data['unique_idx'] )
00608 {
00609
00610 $sql = new Sql( 'SELECT 1 FROM {t_'.$tabelle.'} WHERE '.$key."='".$row[$key]."'",$dbid_destination);
00611
00612
00613 if ( intval($db_dest->getOne( $sql->query )) == 1 )
00614 $row[$key] = $row[$key].$zeit;
00615
00616 }
00617
00618 if ( isset($data['erase']) && in_array($key,$data['erase']) )
00619 $row[$key] = null;
00620
00621 if ( isset($data['self_key']) && $key == $data['self_key'] && intval($row[$key]) > 0 )
00622 $row[$key] = $row[$key]+$maxid;
00623 }
00624
00625 if ( isset($data['replace']) )
00626 {
00627 foreach( $data['replace'] as $repl_column=>$repl_tabelle)
00628 foreach( $mapping[$repl_tabelle] as $oldid=>$newid)
00629 {
00630 $row[$repl_column] = str_replace('{'.$oldid.'}','{'.$newid.'}' ,$row[$repl_column]);
00631 $row[$repl_column] = str_replace('"'.$oldid.'"','"'.$newid.'"' ,$row[$repl_column]);
00632 $row[$repl_column] = str_replace('->'.$oldid ,'->"'.$newid.'"',$row[$repl_column]);
00633 }
00634 }
00635
00636 if ( isset($data['binary']) )
00637 {
00638 if ( !$db_src->conf['base64'] && $db_dest->conf['base64'] )
00639 $row[$data['binary']] = base64_encode($row[$data['binary']]);
00640 elseif ( $db_src->conf['base64'] && !$db_dest->conf['base64'] )
00641 $row[$data['binary']] = base64_decode($row[$data['binary']]);
00642 }
00643
00644
00645
00646
00647 $sql = new Sql( 'INSERT INTO {t_'.$tabelle.'} ('.join(array_keys($row),',').') VALUES({'.join(array_keys($row),'},{').'})',$dbid_destination);
00648 foreach( $row as $key=>$value )
00649 {
00650 if ( isset($data['erase']) && in_array($key,$data['erase']) )
00651 $sql->setNull($key);
00652 else
00653 $sql->setVar($key,$value);
00654 }
00655
00656 $db_dest->query( $sql->query );
00657 }
00658
00659 if ( isset($data['self_key']) )
00660 {
00661 foreach( $mapping[$tabelle] as $oldid=>$newid )
00662 {
00663 $sql = new Sql( 'UPDATE {t_'.$tabelle.'} SET '.$data['self_key'].'='.$newid.' WHERE '.$data['self_key'].'='.($oldid+$maxid),$dbid_destination );
00664 $db_dest->query( $sql->query );
00665 }
00666 }
00667 }
00668 }
00669 }
00670
00671 ?>