Fix #7591 - scanning templates for multiple projects using Content manager
authorAlan <alan@roojs.com>
Fri, 24 Feb 2023 08:11:56 +0000 (16:11 +0800)
committerAlan <alan@roojs.com>
Fri, 24 Feb 2023 08:11:56 +0000 (16:11 +0800)
DataObjects/Cms_template.php
DataObjects/Cms_templatestr.php
OutputTranslations.php
UpdateBjsTemplates.php
UpdateTemplates.php
sql/cms_template.sql

index 2382eb8..ae2cb70 100644 (file)
@@ -7,9 +7,9 @@
  * , it then generates a matching set of strings
  * 
  */
-class_exists('DB_DataObject') ? '' : require_once 'DB/DataObject.php';
-
-class Pman_Cms_DataObjects_Cms_template  extends DB_DataObject 
+require_once 'Pman/Core/DataObjects/Core_template.php';
+class Pman_Cms_DataObjects_Cms_template  extends Pman_Core_DataObjects_Core_template 
 {
     ###START_AUTOCODE
     /* the code below is auto generated do not remove the above tag */
@@ -57,8 +57,12 @@ class Pman_Cms_DataObjects_Cms_template  extends DB_DataObject
         return $ret;
         
     }
-    
+     function factoryStr()
+    {
+        return DB_DataObject::factory('cms_templatestr');
+    }
     /*
+     * ?? is this still used - code is in updateTemplateBJS?
      * @param base (should be full path to template directory)
      * @param subdir = empty for top or subpath.
      */
@@ -154,226 +158,12 @@ class Pman_Cms_DataObjects_Cms_template  extends DB_DataObject
         }
     }
     
-    
-       /**
-      *
-      * sync a single template
-      * This should only be called by the UpdateDatabase code.
-      * @param string $n      name of template
-      * @param boolean $force  force parsing.
-      * @param string $link ??? related to CMS page???
-        
-      * @return boolean|PEAR_Error errors if compile fails..
-      *
-      */
-     /*
-    function syncTemplate($n, $force = false, $link = '')
-    {
-        // read the template and extract the translatable strings.
-        ini_set('memory_limit', '512M');
-        
-        //var_dump($n);
-        $n= ltrim($n,'/');  // remove trailing slash..
-        
-        $fopts = HTML_FlexyFramework::get()->HTML_Template_Flexy;
-        $opts = HTML_FlexyFramework::get()->Pman_Cms;
-        //print_R($opts);
-        //$dir = $opts['templateDir'] . '/' . $node;
-        
-        $oo['templateDir'] = $opts['templateDir'] ;
-        $oo['compileDir'] = $fopts['compileDir'] . '_translation_files';
-        $oo['forceCompile'] = true; //?? only for force above???
-         
-        $prefix = explode('/', $link);
-        
-        $n_split  = explode('/', $n);
-        // this is project specific and should not be put in here..
-        // we need a better way to handle it...
-        // if first part of template dire matches an option in our settings...
-        $pf = false;
-        if (is_array($opts['templateDir']) &&  strlen($link) && isset($opts['templateDir'][$prefix[0]] )) {
-            //echo "renaming templatedir/file (2)\n";
-            $pf = $prefix[0];
-            $oo['templateDir'] = $opts['templateDir'][$pf];
-            
-            
-        }
-        
-        if (is_array($opts['templateDir']) &&  count($n_split) > 1 && isset($opts['templateDir'][$n_split[0]] )) {
-            //echo "renaming templatedir/file (2)\n";
-            $pf = $n_split[0];
-            $oo['templateDir'] = $opts['templateDir'][$pf];
-            $n = substr($n,  (strlen($pf)+1));
-            //var_dump($n);
-        }
-        // non-html templates - treat as such..
-        if (!preg_match('/\.html$/i', $n)) {
-            $oo['nonHTML'] = true;
-        }
-        ///print_r(array($oo, $n));
-        
-        
-        $flexy = new HTML_Template_Flexy( $oo );
-          
-        $r = $flexy->compile($n);
-       
-//        print_r( $flexy->compiler);
-      
-        //printf(" %0.3fs : $fname<BR>", $time);
-        if (is_a($r,'PEAR_Error')) {
-            return $r;
-        }
-        //print_R(number_format(memory_get_usage(),0, '.', ','))  ;
-        $words = file_exists($flexy->getTextStringsFile) ?
-                unserialize(file_get_contents($flexy->getTextStringsFile)) :
-                array();
-        
-        $contentStrings =  $flexy->compiler->contentStrings;
-        
-        print_R($contentStrings);
-        $ori_n = $n;
-        if($pf){// support prefix
-            
-            $n = $pf.'/'.$n;
-        }
-        
-        //check the template is static page or not 
-        $link_split = explode('/', $link);
-        $is_static = ($link_split[0] == 'Static') ? 1 : 0;
-        
-        $tmpl = DB_DataObject::Factory($this->tableName());
-        if (!$tmpl->get('template', $n)) {
-            $nn = $n;
-            if($is_static)
-            {
-                $nn = 'static/'.$n;
-            }
-            $tmpl->template = $nn;
-            $tmpl->lang = 'en'; /// ??? hard coded??
-            $tmpl->updated = date('Y-m-d H:i:s', filemtime($flexy->currentTemplate));
-            $tmpl->insert();
-        } else {
-            $xx =clone($tmpl);
-            // has it been cahnged...
-            if (!$force && filemtime($flexy->currentTemplate) == strtotime($tmpl->updated)) {
-                // nothing changed..
-                return $tmpl;
-            }
-            if (empty($tmpl->lang))  {
-                //echo "FIX LANG?";exit;
-                $tmpl->lang = 'en'; /// ??? hard coded??
-            }
-            
-            $tmpl->updated = date('Y-m-d H:i:s', filemtime($flexy->currentTemplate));
-            $tmpl->update($xx);
-        }
-        $cmsPage = false;
-        
-        
-        
-        if(!empty($link) && empty($is_static)){
-            
-            $page_id = DB_DataObject::factory('core_enum')->lookup('cms_page_type', 'page');
-            echo "$link \n";
-            $cmsPage = DB_DataObject::factory('cms_page');
-            $cmsPage->setFrom(array(
-                'page_link'  => $link,
-                'page_type_id'  => $page_id,
-                'translation_of_id' => 0,
-                'is_element' => 0,
-            ));
-            $parent = 0;
-            if(!$cmsPage->find(true)){
-                $cmsPage = DB_DataObject::factory('cms_page');
-            } else {
-                // existing parent..
-                $parent = $cmsPage->parent_id;
-            }
-            
-            if (!$parent && strpos($link,'/') !== false) {
-                $par = explode('/', $link);
-                array_pop($par);
-                $pname = implode('/', $par);
-                $cmsPageP = DB_DataObject::factory('cms_page');
-                if ($cmsPageP->get('page_link',$pname)) {
-                    $parent = $cmsPageP->id;
-                }
-            }
-            
-            //if not php was found then the page should be a static
-            $php = explode('/', $link);
-            array_shift($php);
-            
-            
-            $file = explode('/', $oo['templateDir']);
-            array_pop($file);
-            $file = implode('/', $file).'/'.implode('/', $php).'.php';
-            
-            $is_static = 1;
-        
-            // not sure how this is supposed to work..
-            // if a tempalte exists then static = 0 ??
-        
-            if(preg_match('/\.html$/', $ori_n) && file_exists($file)){
-                $is_static = 0;
-            }
-
-//            echo "$ori_n <<<< $is_static \n";
-            $run_opts  = HTML_FlexyFramework::get()->page->opts;
-            
-            $cmsPage->setFrom(array(
-                'parent_id' => $parent,
-                'page_link'  => $link,
-                'title' => basename($link),
-                'page_type_id'  => $page_id,
-                'template_id' => $tmpl->id,
-                'language' => 'en',
-                'translation_of_id' => 0,
-                'is_system_page' => 1,
-                'is_element' => 0,
-                'is_static' => $is_static,
-            ));
-            //print_r($contentStrings);
-            
-            if (!empty($run_opts['force-content-update']) || !$cmsPage->id) { // only do this for the initila load
-                foreach( array('title', 'body', 'extended') as $prop) {
-                    if (isset($contentStrings['{page.'. $prop.'}'])) {
-                        $cmsPage->{$prop} = $contentStrings['{page.'. $prop.'}'];
-                    }
-                    if (isset($contentStrings['{page.'. $prop.':h}'])) {
-                        $cmsPage->{$prop} = $contentStrings['{page.'. $prop.':h}'];
-                    }
-                    if (isset($contentStrings['{page.'. $prop.'ToDisplayHtml():h}'])) {
-                        $cmsPage->{$prop} = $contentStrings['{page.'. $prop.'ToDisplayHtml():h}'];
-                    }
-                    echo "cmrpage->{$prop} = ". $cmsPage->{$prop} ."\n";
-                }
-            }   
-            
-             
-            
-            if(!$cmsPage->id){
-                $cmsPage->insert();
-            } else {
-                $cmsPage->update();
-            }
-        }
-        
-        $x = DB_DataObject::Factory('cms_templatestr');
-        $x->syncTemplateWords($tmpl, $words);
-        
-        // if file_exists ( template/path/name.php << eg. a matching view..)
-        // then create a system page for this. 
-        
-        
-        
-        $x = DB_DataObject::Factory('cms_template_element');
-        $x->syncTemplateElement($tmpl, file_get_contents($flexy->currentTemplate), $flexy->compiler->contentStrings, $cmsPage);
-        
-        return $tmpl;
-    
-    }
-    */
+    /**
+     * ?? is this used?
+     * ?? code is in UpdateBJS?
+     *
+     */ 
+     
     function syncTemplatePage($pgdata)
     {
         $force = true;
index 0f87f1a..012bf83 100644 (file)
@@ -168,8 +168,10 @@ class Pman_Cms_DataObjects_Cms_templatestr extends DB_DataObject
                 
             default:
                 $x = DB_DataObject::factory($this->tableName());
+                $x->autoJoin();
                 $x->selectAdd();
-                $x->selectAdd('distinct(template_id) as template_id');
+                $x->selectAdd('distinct('.$this->tableName(). '.template_id) as template_id');
+                $x->whereAdd('join_template_id_id.is_deleted = 0');
                 $x->lang = $q['node'];
                 $ids = $x->fetchAll('template_id');
                 
index 7a48aea..25e0f2a 100644 (file)
@@ -154,6 +154,7 @@ class Pman_Cms_OutputTranslations extends Pman
     
     function is_cached()
     {
+        
         if (!empty($_GET['no_cache'])) {
             return false;
         }
@@ -178,6 +179,8 @@ class Pman_Cms_OutputTranslations extends Pman
         $cms_templatestr->selectAdd("MAX(updated) AS latest");
         $cms_templatestr->find(true);
         
+      
+        
         if(filemtime($this->cachePath) < strtotime($cms_templatestr->latest)){
             return false;
         }
index 6e0176c..3a8cd4e 100644 (file)
@@ -2,12 +2,16 @@
 
 /**
  *
+ * simlar to Pman/Admin/UpdateBjsTemplates...
+ *
+ * scan BJS files
+ * scans JS (for  ._("...") )
+ * scan PHP for 
  *
  */
 
-require_once 'Pman.php';
-class Pman_Cms_UpdateBjsTemplates extends Pman
+require_once 'Pman/Admin/UpdateBjsTemplates.php';
+class Pman_Cms_UpdateBjsTemplates extends Pman_Admin_UpdateBjsTemplates
 {
     
     static $cli_desc = "Update BJS Templates";
@@ -39,17 +43,17 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
     
     function updateData()
     {   
-        $this->scanTemplates();
+        $this->scanFiles();
         
         $this->scanTables();
-        
         $this->syncLanguage();
         
+        
         $this->jok('OK');
         
     }
     
-    function scanTemplates()
+    function scanFiles()
     {
         $ff = HTML_FlexyFramework::get();
         
@@ -57,10 +61,32 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
             $this->jerr("config[Pman_Cms] is not set");
         }
         
-        $base = $ff->Pman_Cms['project_name'];
+       
+        foreach(explode(',', $ff->Pman_Cms['project_name']) as $base) {
+            $this->scanFilesBase($base);
+        }
+    }
+    
+    /*
+     * this does all the scan work.
+     * it's pretty simple (does not do subdirectories...)
+     *
+     * only supports BJS / PHP / JS currently.
+     * 
+     */
+    
+    
+    function scanFilesBase($base)
+    {
+        
+        $ff = HTML_FlexyFramework::get();
+
         $dir = $ff->Pman_Cms['site_dir'][$base];
+        //var_dump($dir);
         $dh = opendir($dir);
         
+        $tp = DB_DAtaObject::Factory('cms_template');
+        
         $ret = array();
         
         if(!$dh){
@@ -70,7 +96,41 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
          
         while (($fn = readdir($dh)) !== false) {
             
-            if(empty($fn) || $fn[0] == '.' || !preg_match('/\.bjs$/', $fn)){
+            if(empty($fn) || $fn[0] == '.') {
+                continue;
+            }
+            $fp = $dir . '/'. $fn;
+            //var_dump($fp);
+            if (is_dir($fp)) {
+                continue;
+            }
+            if (is_link($fp)) {
+                continue;
+            }
+            if (preg_match('/\.js$/', $fn) && !file_exists($dir.'/'. preg_replace('/\.js$/', '.bjs', $fn))) {
+                $temp = $tp->syncJsWords(array(
+                    'base' => $base, 
+                    'template_dir' =>  $dir,
+                    'template' => $fn
+                ));
+                if ($temp) {
+                    $ids[] = $temp->id;
+                }
+                continue;
+            }
+            if (preg_match('/\.php$/', $fn)) {
+                $temp = $tp->syncPhpGetText(array(
+                    'base' =>$base, 
+                    'template_dir' =>  $dir,
+                    'template' => $fn
+                ));
+                if ($temp) {
+                    $ids[] = $temp->id;
+                }
+                continue;
+            }
+            
+            if (!preg_match('/\.bjs$/', $fn)){
                 continue;
             }
             // var_dump($fn);exit;
@@ -78,6 +138,9 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
                 echo "Processing {$fn} \n";
             }
             
+            
+            
+            
             $template = DB_DataObject::factory('cms_template');
             $template->setFrom(array(
                 'template' => $fn,
@@ -90,10 +153,18 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
             if($template->find(true)){
                 $o = clone ($template);
             }
+            $template->filetype = 'bjs';
             
             $template->updated = $template->sqlValue("NOW()");
             
-            (empty($o)) ? $template->insert() : $template->update($o);
+            if (empty($o)) {
+                $ids[] = $template->insert() ;
+            } else {
+                $template->update($o);
+                $ids[] = $o->id;
+            }
+            
+            
             
             $data = json_decode(file_get_contents($base . '/' . $fn), true);
             
@@ -102,61 +173,21 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
             $x = DB_DataObject::Factory('cms_templatestr');
             $x->syncTemplateWords($template, false);
         }
-        // pman templates are done via core templates
-       // $this->scanPmanTemplates();
         
-    }
-    
-    function scanPmanTemplates()
-    {
-        foreach ($this->modules() as $m){
-            $view_name = "Pman.$m";
-            
-            $dh = opendir("Pman/$m");
         
-            $ret = array();
-
-            if(!$dh){
-                continue;
-            }
-            
-            while (($fn = readdir($dh)) !== false) {
-            
-                if(empty($fn) || $fn[0] == '.' || !preg_match('/\.bjs$/', $fn)){
-                    continue;
-                }
-
-                if($this->cli){
-                    echo "Processing {$fn} \n";
-                }
-
-                $template = DB_DataObject::factory('cms_template');
-                $template->setFrom(array(
-                    'template' => $fn,
-                    'lang' => 'en',
-                    'view_name' => $view_name
-                ));
-
-                $o = false;
-
-                if($template->find(true)){
-                    $o = clone ($template);
-                }
-
-                $template->updated = $template->sqlValue("NOW()");
-
-                (empty($o)) ? $template->insert() : $template->update($o);
-
-                $data = json_decode(file_get_contents('Pman' . '/' . $m . '/' . $fn), true);
-
-                $template->words = empty($data['strings']) ? array() : $data['strings'];
-
-                $x = DB_DataObject::Factory('cms_templatestr');
-                $x->syncTemplateWords($template, false);
-            }
+        $del = DB_DataObject::factory('cms_template');
+        $del->whereAddIn('!id', $ids, 'int');
+        $del->view_name = $base;
+        $del->whereAddIn('filetype' , array( 'php', 'bjs' , 'js' ), 'string');
+        $delids = $del->fetchAll('id');
+        if ($delids) {
+            DB_DataObject::factory('core_template')->query(
+                'update cms_template set is_deleted =  1 where id in('. implode(',', $delids). ')'
+            );
         }
         
     }
+     
     
     function scanTables()
     {
@@ -186,17 +217,16 @@ class Pman_Cms_UpdateBjsTemplates extends Pman
     {
         $ff = HTML_FlexyFramework::get();
         
-        if (empty($ff->Pman_Cms)) {
-            $this->jerr("config[Pman_Cms] is not set");
+        if (empty($ff->Pman_Cms) || empty($ff->Pman_Cms['languages']) || !is_array($ff->Pman_Cms['languages']) ) {
+            $this->jerr("config[Pman_Cms][languages] is not set");
         }
         
-        $opt_lang = empty($ff->Pman_Core_I18n) ? array( 'l' => array()) : $ff->Pman_Core_I18n;
         
         if($this->cli){
             echo "Sync the Languages template.....\n";
         }
         
-        foreach($opt_lang['l'] as $l) {
+        foreach($ff->Pman_Cms['languages'] as $l) {
             if($this->cli){
                 echo "Sync $l Language.....\n";
             }
index c76ca09..43fcf5a 100644 (file)
@@ -2,7 +2,7 @@
 
 /**
  *
+  is this used????
  *
  */
 
index 9183fa7..743261e 100644 (file)
@@ -9,3 +9,6 @@ CREATE TABLE  cms_template (
 );
 
 ALTER TABLE cms_template ADD COLUMN view_name varchar(32) NOT NULL default '';
+ALTER TABLE cms_template ADD COLUMN filetype varchar(32) NOT NULL default '';
+ALTER TABLE cms_template ADD COLUMN is_deleted INT(2) NOT NULL default 0;
+alter table cms_template add index lookupa(view_name, template, lang, filetype, is_deleted);