File: site2owl.php

package info (click to toggle)
owl-dms 0.90-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 17,292 kB
  • ctags: 10,919
  • sloc: php: 48,457; sql: 3,603; sh: 363; perl: 204; makefile: 73
file content (525 lines) | stat: -rw-r--r-- 16,742 bytes parent folder | download
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?php
/**
 * Name:          site2owl.php
 * 
 * Author:        Steve Bourgeois
 * 
 * Date:          2003-07-18
 * 
 * Copyright (c) 2003-2004 Bozz IT Consulting Inc.
 * Licensed under the GNU GPL. For full terms see the file COPYING.
 * 
 * Steps Taken to Migrate the sitescape database to Owl
 *
 * 1) If this is a SiteScape 6 Forum you need to upgrade it to 
 *    Sitescape 7, you can get your free demo from the SiteScape site.
 *    During the migration I selected to migrate to an MS SQL Server 7.0
 *
 * 2) I then purchased a tool called MSSQL-TO-MYSQL by Intelligent Converters
 *    www.convert-in.com to convert the MS SQL Server 7.0 to a mysql dump file
 *
 * 3) created the Mysql SiteScape datbase.
 * 
 * 4) Used this script to migrate the Sitescape Database to an empty Owl Database
 *
 *
 *
 */

// uncomment this to see plaintext output in your browser
// header("Content-Type: text/plain");

require("DB.php");


global $sForumPrefix, $sPrimaryGroup, $DEBUG, $sSiteScapeHiddenLocation, $sOwlDocumentLocation, $iAttachmentID;

$sForumPrefix = "tat_";
$sPrimaryGroup = "tat_user";
$DEBUG = false;
$sSiteScapeHiddenLocation = '/var/opt/tat';
$sOwlDocumentLocation = '/var/opt';
$iAttachmentID = 100;


// main()
fMigrateGroups();
fMigrateUsers();
fMigrateGroupMemberShip();
fMigrateData();


function fMigrateData()
{
   global $sForumPrefix, $sPrimaryGroup, $DEBUG;


   $iForumOffset = 0;


   print("<H1> Migrating DATA ....</H1>");
      //fMakeFolders("f_tat12a", $iForumOffset);
      //fMakeFolders("f_tat03", $iForumOffset);
      //fMakeFolders("f_tat00", $iForumOffset);
   //exit;
   $SiteScape = DB::connect("mysql://root:@localhost/sitescape",true);
   $query = "select forumName, propVal from ". $sForumPrefix ."_props where propId = 'title' and forumName LIKE 'f_%';";
   $result = $SiteScape->query($query);

   $Owl = DB::connect("mysql://root:@localhost/intranet");

   while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
   {
      print("Migrating '$row->forumName'...<br />");
      fMakeFolders($row->forumName, $iForumOffset);
      $iForumOffset = $iForumOffset + 1000;
   }

}




function fMoveDocToOwl($sForum, $iOwlParentId, $iSiteScapedocId, $filename, $iOwlDocId) 
{

   global $sForumPrefix, $sPrimaryGroup, $DEBUG, $sSiteScapeHiddenLocation, $sOwlDocumentLocation;

   $iFileExist = true;

   $sSourceFile = $sSiteScapeHiddenLocation . "/" . $sForum . "/" . $iSiteScapedocId . "/" . $filename;
   $sOriginalSourceFile = $sSiteScapeHiddenLocation . "/" . $sForum . "/" . $iSiteScapedocId . "/" . $filename;
   $sDestFile = $sOwlDocumentLocation . "/" . fGetDirPath($iOwlParentId);

   if(!file_exists($sSourceFile)) 
   {
      $sSourceFile = $sOwlDocumentLocation . "/tmp/" . $filename;
      if(!file_exists($sSourceFile))
      {
         print("<font color=red>--- NOT EXIST --- </font>Could Not Copy $sOriginalSourceFile ----->> $sDestFile<br />");
         $iFileExist = false;
      }
   }



   if($iFileExist)
   {
      $iFileSize = filesize ( $sSourceFile );
      $Owl = DB::connect("mysql://root:@localhost/intranet",true);
      $query = "Update files set size='$iFileSize' where id ='$iOwlDocId'";
      $result = $Owl->query($query);

      $sFullDestFilename = $sDestFile . "/" . $filename; 
      if(file_exists($sFullDestFilename))
      {
        $sDestFile = $sDestFile . "/" . $iOwlDocId . "_" . $filename;
      }
      $sCommand = "mv \"" . $sSourceFile . "\"  \"" . $sDestFile . "\"";
      //if ($iSiteScapedocId == "100035" )
      //{
         //print("ID: $iSiteScapedocId COMMAND: $sCommand <br />");
      //}
      system($sCommand);
   }
}



function fGetOwlGroupId($GroupName)
{
   global $sForumPrefix, $sPrimaryGroup, $DEBUG;

   $Owl = DB::connect("mysql://root:@localhost/intranet",true);
   $query = "SELECT id FROM groups where name='$GroupName'";
   $result = $Owl->query($query);
   $row = $result->fetchRow(DB_FETCHMODE_OBJECT);
 
   return $row->id;

}

function fGetOwlUserId($UserName)
{
   global $sForumPrefix, $sPrimaryGroup, $DEBUG;

   $Owl = DB::connect("mysql://root:@localhost/intranet",true);
   $query = "SELECT id FROM users where username='$UserName'";
   $result = $Owl->query($query);
   $row = $result->fetchRow(DB_FETCHMODE_OBJECT);
 
   return $row->id;

}


function fGetAbstract($Id, $sForum)
{
   global $sForumPrefix, $sPrimaryGroup, $DEBUG;

   $SiteScape = DB::connect("mysql://root:@localhost/sitescape",true);
   $query = "select kvpVal from ". $sForumPrefix .$sForum ."_k where docId = '$Id'";
   $result = $SiteScape->query($query);                                  
      
   $row = $result->fetchRow(DB_FETCHMODE_OBJECT);
   return $row->kvpVal;
}




function fInsertDocuments($iParentFolder, $sForum, $iForumOffset) 
{
   global $sForumPrefix, $sPrimaryGroup, $DEBUG, $iAttachmentID;

   

   $SiteScape = DB::connect("mysql://root:@localhost/sitescape",true);
   $query = "select attachmentFiles, uploadFileInfo, createdBy, modifiedOn, createdOn, docContent, subRoots, docId, parentId, documentType docLevel, title, parentFolder from ". $sForumPrefix .$sForum ."_d where subRoots is NULL and parentFolder = '$iParentFolder' and documentType<>'message'";
   $result = $SiteScape->query($query);

   $Owl = DB::connect("mysql://root:@localhost/intranet");

   while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
   {

      list($filename,$junk)= split ("\}", $row->uploadFileInfo, 2);
      $filename = ereg_replace('\}', "", $filename);
      $filename = ereg_replace('\{', "", $filename);

      if( strlen(trim($row->uploadFileInfo)) == strlen(trim($filename))) 
      {
           list($mime,$OLDfilename)= split (" ", $row->docContent, 2);
           $filename = ereg_replace('\}', "", $OLDfilename);
           $filename = ereg_replace('\{', "", $filename);
      }

      $description = fGetAbstract($row->docId, $sForum);

      if( $row->createdBy == '_none_')
      {
         $iOwlCreatorId = 1;
      }
      else
      {
         $iOwlCreatorId = fGetOwlUserId($row->createdBy);
      }


      $iOwlDocId = $row->docId + $iForumOffset;
      $iOwlParentId = $row->parentFolder + $iForumOffset;
      $iOwlGroupId = fGetOwlGroupId($sPrimaryGroup);
      $description = ereg_replace("'","\\'", $description);
      $title = ereg_replace("'","\\'", $row->title);
      $sOwlName  = trim($title);
      $sOwlName = ereg_replace("\/", "-", $sOwlName);

      
      $sInsertQuery = "INSERT INTO files set id='$iOwlDocId', name='$sOwlName', security='0', filename='$filename', parent='$iOwlParentId', created='$row->createdOn', description='$description', smodified='$row->modifiedOn', checked_out='0', major_revision='1', minor_revision='0', url='0', creatorid='$iOwlCreatorId', groupid='$iOwlGroupId'"; 

      $result3 = $Owl->query($sInsertQuery);
      //if ( $row->docId == "100035")
      //{
         //print("Q: $sInsertQuery <br />");
      //}

      fMoveDocToOwl($sForum, $iOwlParentId, $row->docId, $filename, $iOwlDocId);



      //**  IF this DOCUMENT HAS ATTACHMENTS

      if(strlen(trim($row->attachmentFiles)) <> 0)
      {
          
         $sTitle = $sOwlName . " -- Attachment --";

         $chars = preg_split( '//' , $row->attachmentFiles, -1, PREG_SPLIT_OFFSET_CAPTURE); 
         $bWordWithSpaces = false;
         $sFilename = "";

         foreach ($chars as $cChar)
         {
            //print $cChar; 
            if ($cChar == "{" || $cChar == " ")
            {
              if ($cChar == "{")
              {
                 $bWordWithSpaces = true;
              }
              else
              {
                 if(!$bWordWithSpaces) 
                 {
                    $sInsertQuery = "INSERT INTO files set id='$iAttachmentID', name='$sTitle', security='0', filename='$sFilename', parent='$iOwlParentId', created='$row->createdOn', description='$description', smodified='$row->modifiedOn', checked_out='0', major_revision='1', minor_revision='0', url='0', creatorid='$iOwlCreatorId', groupid='$iOwlGroupId'"; 
                    $result3 = $Owl->query($sInsertQuery);
                    fMoveDocToOwl($sForum, $iOwlParentId, $row->docId, $sFilename, $iAttachmentID);
                    $iAttachmentID++;
                    $sFilename = "";
                 }
                 else
                 {
                    $sFilename .= $cChar;
                 }
              }
            }
            else
            {
              if ($cChar == "}")
              {
                 $bWordWithSpaces = false;
              }
              else
              {
                    $sFilename .= $cChar;
              }
            }
         }
         $sInsertQuery = "INSERT INTO files set id='$iAttachmentID', name='$sTitle', security='0', filename='$sFilename', parent='$iOwlParentId', created='$row->createdOn', description='$description', smodified='$row->modifiedOn', checked_out='0', major_revision='1', minor_revision='0', url='0', creatorid='$iOwlCreatorId', groupid='$iOwlGroupId'"; 
         $result3 = $Owl->query($sInsertQuery);
         fMoveDocToOwl($sForum, $iOwlParentId, $row->docId, $sFilename, $iAttachmentID);
      }
      //**
      
      if ($DEBUG)
      {
         print("<br />DEBUG: InsertQuery -> $sInsertQuery<br />");
      }
     
     


   }

}


function fid_to_name($parent)
{
      global $sForumPrefix, $sPrimaryGroup, $DEBUG;

   $Owl = DB::connect("mysql://root:@localhost/intranet",true);
   $query = "select name from folders where id = '$parent'";
   $result = $Owl->query($query);
   $row = $result->fetchRow(DB_FETCHMODE_OBJECT);

   return $row->name;
}



function fGetDirPath($parent)
{     
   $name = fid_to_name($parent);
   $navbar = "$name";
   $new = $parent;
   $Owl = DB::connect("mysql://root:@localhost/intranet",true);
   $query = "SELECT id FROM groups where name='$GroupName'";

   while ($new != "1")
   {
      $query = "select parent from folders where id = '$new'";
      $result = $Owl->query($query);
      $row = $result->fetchRow(DB_FETCHMODE_OBJECT);
      $newparentid = $row->parent;
      if ($newparentid == "") break;
      $name = fid_to_name($newparentid);
      $navbar = "$name/" . $navbar;
      $new = $newparentid;
   } 
   return $navbar;
}


function fMakeFolders($sForum, $iForumOffset)
{
   global $sForumPrefix, $sPrimaryGroup, $DEBUG, $sSiteScapeHiddenLocation, $sOwlDocumentLocation;

   $SiteScape = DB::connect("mysql://root:@localhost/sitescape",true);
   $query = "select subRoots, createdBy, docId, parentId, documentType docLevel, title, parentFolder from ". $sForumPrefix .$sForum ."_d where subRoots is not NULL";
   $result = $SiteScape->query($query);

   $Owl = DB::connect("mysql://root:@localhost/intranet");

   while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
   {
         $iOwlDocId = $row->docId + $iForumOffset;
         $iOwlParentId = $row->parentFolder + $iForumOffset;
         if( $row->createdBy == '_none_')
         {
            $iOwlCreatorId = 1;
         }
         else
         {
            $iOwlCreatorId = fGetOwlUserId($row->createdBy);
         }

         $description = fGetAbstract($row->docId, $sForum);
         $description = ereg_replace("'","\\'", $description);
         $iOwlGroupId = fGetOwlGroupId($sPrimaryGroup);
         $title = ereg_replace("'","\\'", $row->title);
         $sOwlName  = trim($title);
         $sOwlName = ereg_replace("\/", "-", $sOwlName);


         $sInsertQuery = "INSERT INTO folders set id='$iOwlDocId', name='$sOwlName', description='$description', security='51', creatorid='$iOwlCreatorId',groupid='$iOwlGroupId',  ";
         
         if ($row->parentFolder) 
         {
            //$sInsertQuery .= "parent='$row->parentFolder'";
            $sInsertQuery .= "parent='$iOwlParentId'";
            $result3 = $Owl->query($sInsertQuery);
         }
         else
         {
            $sInsertQuery .= "parent='1'";
            $result3 = $Owl->query($sInsertQuery);
         }
         
         $sOsCommand = "mkdir \"" . $sOwlDocumentLocation . "/" . fGetDirPath($iOwlDocId) . "\"";
         system($sOsCommand);
         
         fInsertDocuments($row->docId, $sForum, $iForumOffset);
         if ($DEBUG)
         {
            print("DEBUG: InsertQuery -> $sInsertQuery<br />");
         }
   }

}
function fMigrateGroupMemberShip()
{
    global $sForumPrefix, $sPrimaryGroup, $DEBUG;

   // tat__group_m (groupName, memberName)

   print("<H1> Migrating User Group Membership ....</H1>");


   $Owl = DB::connect("mysql://root:@localhost/intranet", true);
   $query = "SELECT id, username FROM users";
   $result = $Owl->query($query);

   $SiteScape = DB::connect("mysql://root:@localhost/sitescape");

   while($row = $result->fetchRow(DB_FETCHMODE_OBJECT))
   {
    
      $query = "SELECT groupName FROM " .$sForumPrefix ."_group_m where memberName='$row->username'";
      $result1 = $SiteScape->query($query);

      while($row1 = $result1->fetchRow(DB_FETCHMODE_OBJECT))
      {
          
         $iOwlGroupId = fGetOwlGroupId($row1->groupName);
         //$query2 = "SELECT id FROM groups where name='$row1->groupName'";
         //$result2 = $Owl->query($query2);
         //$row3 = $result2->fetchRow(DB_FETCHMODE_OBJECT);

         //$sInsertQuery = "INSERT INTO membergroup set userid='$row->id', groupid='$row3->id'";
         $sInsertQuery = "INSERT INTO membergroup set userid='$row->id', groupid='$iOwlGroupId'";
         $result3 = $Owl->query($sInsertQuery);
         if ($DEBUG)
         {
             print("<br />DEBUG: InsertQuery =  $sInsertQuery<br />");
         }
      }
   }

   // close database connection
   $SiteScape->disconnect();
   $Owl->disconnect();
}

function fMigrateGroups()
{
    global $sForumPrefix, $sPrimaryGroup, $DEBUG;

   // tat__group (modifiedBy, userName, createdOn, userType, title, createdBy, lcUserName)

   print("<H1> Migrating Groups ....</H1>");

   $SiteScape = DB::connect("mysql://root:@localhost/sitescape");
   // execute query
   $query = "SELECT userName FROM ".$sForumPrefix."_group";
   $result1 = $SiteScape->query($query);

   $Owl = DB::connect("mysql://root:@localhost/intranet");

   while($row = $result1->fetchRow(DB_FETCHMODE_OBJECT))
   {

      $sInsertQuery = "INSERT INTO groups set name = '$row->userName'";
      $result2 = $Owl->query($sInsertQuery);
   }

   // get and print number of rows in resultset
   echo "\n[" . $result1->numRows() . " Groups Imported]\n";

   // close database connection
   $SiteScape->disconnect();
   $Owl->disconnect();
}


function fMigrateUsers()
{
    global $sForumPrefix, $sPrimaryGroup, $DEBUG;

   // tat__user (displayDebugInfo, userType, firstName, createdBy, ntAccountInfo, lastLogin, lastName, lcUserName, userName, org, sortName, title, pass_word, nativeLanguage, modifiedBy, IRC_client, photograph, createdOn)

   $primaryGroup = DB::connect("mysql://root:@localhost/intranet");
   // execute query
   $query = "SELECT * FROM groups where name='$sPrimaryGroup'";
   $result0 = $primaryGroup->query($query);
   $row0 = $result0->fetchRow(DB_FETCHMODE_OBJECT);
   $iPrimaryGroupid = $row0->id;
   $primaryGroup->disconnect();


   print("<H1> Migrating Users ....</H1>");

   $SiteScape = DB::connect("mysql://root:@localhost/sitescape");
   // execute query
   $query = "SELECT * FROM ". $sForumPrefix ."_user where userName <> 'admin' and userName <> 'anonymous'";
   $result1 = $SiteScape->query($query);

   $Owl = DB::connect("mysql://root:@localhost/intranet");

   while($row = $result1->fetchRow(DB_FETCHMODE_OBJECT))
   {

      switch ($row->nativeLanguage)
      {
         case "en":
            $language = 'English';
            break;
         case "fr":
            $language = 'Francais';
            break;
         case "de" :
            $language = 'Deutsch';
            break;
         default:
            $language = 'English';
            break;
      }
 

      $sInsertQuery = "INSERT INTO users set groupid ='$iPrimaryGroupid', username = '$row->userName', name='" . $row->firstName  . " " . $row->lastName . "', password='5f4dcc3b5aa765d61d8327deb882cf99', quota_max='0', quota_current='0', email='" .  $row->email . "', notify='0', attachfile='0', disabled='0', language='" . $language . "', maxsessions='2', noprefaccess='0', lastlogin='" . $row->lastLogin . "', curlogin='" . $row->lastLogin . "', lastnews='0', newsadmin='0', comment_notify='0'";
      if ($DEBUG)
      {
          print("<br />DEBUG: Query =  $sInsertQuery<br />");
      }
      $result2 = $Owl->query($sInsertQuery);
   }

   // get and print number of rows in resultset
   echo "\n[" . $result1->numRows() . " Users Imported]\n";

   // close database connection
   $SiteScape->disconnect();
   $Owl->disconnect();
}

?>