MCMS
[ class tree: MCMS ] [ index: MCMS ] [ all elements ]

Source for file Compiler.php

Documentation is available at Compiler.php

  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | MCMS: a PHP Content Management System for creating accessible sites. |
  5. // | Copyright (C) 2005  Silvio Moioli                                    |
  6. // |                                                                      |
  7. // | This program is free software; you can redistribute it and/or modify |
  8. // | it under the terms of the GNU General Public License as published by |
  9. // | the Free Software Foundation; either version 2 of the License, or    |
  10. // | (at your option) any later version.                                  |
  11. // |                                                                      |
  12. // | This program is distributed in the hope that it will be useful,      |
  13. // | but WITHOUT ANY WARRANTY; without even the implied warranty of       |
  14. // | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the        |
  15. // | GNU General Public License for more details.                         |
  16. // |                                                                      |
  17. // | You should have received a copy of the GNU General Public License    |
  18. // | along with this program; if not, write to the Free Software          |
  19. // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 |
  20. // +----------------------------------------------------------------------+
  21. // | Authors: Silvio Moioli <silvio at moioli dot net> www.moioli.net     |
  22. // +----------------------------------------------------------------------+
  23. //
  24. //$Id:
  25.  
  26. /** Carica la classe base di MCMS per l'interfacciamento al Database */
  27. require_once $_SERVER["DOCUMENT_ROOT"]."/admin/lib/model/Engine.php";
  28. /** Carica la classe dei siti di MCMS */
  29. require_once $_SERVER["DOCUMENT_ROOT"]."/admin/lib/model/Site.php";
  30. /** Carica il gestore dei template (Smarty) */
  31. require_once $_SERVER["DOCUMENT_ROOT"]."/admin/include/smarty/libs/Smarty.class.php";
  32. /** Carica la libreria per semplificare l'aggiunta di tag XML */
  33. require_once $_SERVER["DOCUMENT_ROOT"]."/admin/lib/view/Tagger.php";
  34. /** Carica le pagine della vista User */
  35. require_once $_SERVER["DOCUMENT_ROOT"]."/admin/lib/view/alluser.inc";
  36.  
  37. /**
  38.  * Compilatore di interfacce XHTML. Crea/aggiorna i file
  39.  * XHTML di cui è composto un sito.
  40.  * 
  41.  * @package  MCMS
  42.  * @version  2
  43.  * @author   Silvio Moioli <silvio at moioli dot net>
  44.  *
  45.  */
  46. class Compiler extends PEAR
  47. {
  48.     /** @var Smarty l'oggetto per la gestione dei template */
  49.     var $smarty = null;
  50.     /** @var string il nome del template da usare per questo sito */
  51.     var $templateFileName = null;
  52.     /** @var string il nome della directory dove creare i file XHTML */
  53.     var $compilingDir = null;
  54.     /** @var il path dell'utility Tidy */
  55.     var $tidy = null;
  56.     /** @var string il nome dell'eseguibile di Tidy per Windows */
  57.     var $tidyWindows = "tidy.exe";
  58.     /** @var string il nome dell'eseguibile di Tidy per Linux */
  59.     var $tidyLinux = "tidy";
  60.     /** @var bool Tidy è supportato? */
  61.     var $tidyEnabled = false;
  62.     /** @var string il path del log degli errori di Tidy */
  63.     var $tidyLog = null;
  64.     
  65.     /**
  66.      * Costruttore di default.
  67.      *
  68.      * Costruisce un compilatore di interfacce XHTML per il sito specificato.
  69.      */
  70.     function Compiler()
  71.     {
  72.         $this->smarty = new Smarty();
  73.         $this->compilingDir = $_SERVER["DOCUMENT_ROOT"]."/";
  74.         $this->templateFileName = $_SERVER["DOCUMENT_ROOT"]."/admin/include/smarty/".
  75.         "templates/userTemplate.tpl";
  76.         $tidyDir $_SERVER["DOCUMENT_ROOT"]."/admin/include/tidy/";
  77.         $this->tidyLog = $tidyDir."error.log";
  78.         
  79.         if (strtoupper(substr(PHP_OS03)) == "WIN"){
  80.             $this->tidyEnabled = true;
  81.             $tidy $tidyDir.$this->tidyWindows;
  82.         }
  83.         if (strtoupper(substr(PHP_OS03)) == "LIN"){
  84.             $this->tidyEnabled = true;
  85.             $this->tidy = $tidyDir.$this->tidyLinux;
  86.         }
  87.     }
  88.     
  89.    /**
  90.     * Visualizza direttamente la pagina passata.
  91.     *
  92.     * @param Compilable $page la pagina da visualizzare
  93.     * @see Compilable
  94.     */
  95.     function display($page)
  96.     {
  97.         //Assegno le variabili a Smarty
  98.         $this->smarty->assign('title',$page->getTitle());
  99.         $this->smarty->assign('menuEntries',$page->getMenuEntries());
  100.         $this->smarty->assign('contents',$page->getContents());
  101.         $this->smarty->assign('keywords',$page->getKeywords());
  102.         $this->smarty->assign('description',$page->getDescription());
  103.         
  104.         //Visualizzo la pagina
  105.         $this->smarty->display($page->getTemplate());
  106.     }
  107.  
  108.    /**
  109.     * Salva la pagina su file.
  110.     *
  111.     * @param Savable $page la pagina da salvare
  112.     * @see Compilable
  113.     */
  114.     function save($page)
  115.     {
  116.         //Inizio del caching
  117.         ob_start();
  118.         $this->smarty = $this->smarty;
  119.         
  120.         $contents $page->getContents();
  121.         $menuEntries $page->getMenuEntries();
  122.         $title $page->getTitle();
  123.         $description $page->getDescription();
  124.         $keywords $page->getKeywords();
  125.         
  126.         //Applico i componenti opzionali, se possibile.
  127.         if (is_a($page,"UserPage")) {
  128.             $components $page->getComponents();
  129.             foreach ($components as $i{
  130.                 $i->applyComponent($contents$menuEntries$title$description,
  131.                     $page->getSite());
  132.             }
  133.         }
  134.         
  135.         //Assegno le variabili a Smarty
  136.         $this->smarty->assign('title',$title);
  137.         $this->smarty->assign('menuEntries',$menuEntries);
  138.         $this->smarty->assign('contents',$contents);
  139.         $this->smarty->assign('keywords',$keywords);
  140.         $this->smarty->assign('description',$description);
  141.         
  142.         //Compilo la pagina
  143.         $this->smarty->display($this->templateFileName);
  144.     
  145.         //Ritorno come stringa e ripulisco
  146.         $result ob_get_contents();
  147.         ob_end_clean();
  148.         
  149.         $this->fileSave($page->getFileName()$resulttrue);
  150.     }
  151.     
  152.     /**
  153.      * Salva su file i contenuti XHTML specificati, se indicato tramite Tidy.
  154.      *
  155.      * @param string $fileName il nome del file su cui salvare (deve essere
  156.      *  scrivibile)
  157.      * @param string il contenuto XHTML del file
  158.      * @param bool $useTidy true se utilizzare Tidy (se possibile)
  159.      */
  160.     function fileSave($fileName$contents$useTidy=true)
  161.     {
  162.         //Se è abilitato, usa Tidy
  163.         if ($this->tidyEnabled && $useTidy == true){
  164. /*            $fp = fopen($this->tidyLog, 'a');
  165.             fwrite($fp, "Processing: $fileName\n");
  166.             fclose($fp);*/
  167.             $result $this->runExternal($this->tidy." -m --force-output true ".
  168.                 "--indent true --indent-spaces 4 --wrap 160 ".
  169.                 "--char-encoding latin1 --output-xhtml true",$contents,$code);
  170.         }
  171.         $fullName $this->compilingDir."/".$fileName;
  172.         $dir dirname($fullName);
  173.         if (is_dir($dir== false){
  174.             mkdir($dir);
  175.         }
  176.         $fp fopen($fullName'w');
  177.         fwrite($fp$result);
  178.         fclose($fp);
  179.     }
  180.     
  181.    /**
  182.     * Salva su file tutti gli elementi un sito.
  183.     *
  184.     * @param Site $site il sito da salvare
  185.     */
  186.     function saveAll($site)
  187.     {
  188.         $this->save(new UserHomepage($site));
  189.         $this->save(new UserNews($site));
  190.         $articles $site->getArticles();
  191.         foreach ($articles as $i{
  192.             $this->save(new UserArticle($site$i));
  193.         }
  194.         $categories $site->getCategories();
  195.         foreach ($categories as $i{
  196.             $this->save(new UserCategory($site$i));
  197.         }
  198.         
  199.         $this->saveComponents($site);
  200.     }
  201.     
  202.     /**
  203.      * Permette a tutti i plugin attivati sul sito specificato
  204.      * di salvare i loro file aggiuntivi.
  205.      * 
  206.      * @param Site $site il sito di cui salvare i file dei plugin
  207.      * @see Component
  208.      */
  209.     function saveComponents($site)
  210.     {
  211.         $components $site->getComponents();
  212.         foreach ($components as $i{
  213.             $i->saveFiles($site$this);
  214.         }
  215.     }
  216.        
  217.    /**
  218.     * Cancella il file corrispondente a una pagina di un sito (se esiste).
  219.     *
  220.     * @param Savable $savable l'oggetto Savable da eliminare
  221.     */
  222.     function remove($savable){
  223.         $fname $this->compilingDir.$savable->getFileName();
  224.         if (file_exists($fname)){
  225.             unlink($fname);
  226.         }
  227.     }
  228.    
  229.    /**
  230.     * Elimina tutti gli elementi di questo sito (cancellando i file relativi).
  231.     */
  232.     function removeAll($site)
  233.     {
  234.         $this->remove(new UserHomepage($site));
  235.         $this->remove(new UserNews($site));
  236.         $this->remove(new UserNews($site));
  237.         $articles $site->getArticles();
  238.         foreach ($articles as $i{
  239.             $this->remove(new UserArticle($site$i));
  240.         }
  241.         $categories $site->getCategories();
  242.         foreach ($categories as $i{
  243.             $this->remove(new UserCategory($site$i));
  244.         }
  245.         //TODO: removeComponents...
  246.     }
  247.     
  248.     /**
  249.      * Filtra la stringa specificata in stdin attraverso un programma
  250.      * esterno.
  251.      *
  252.      * @param string command il comando da eseguire
  253.      * @param string stdin la stringa da filtrare
  254.      * @return string la stringa filtrata
  255.      */
  256.     function runExternal($command,$stdin{
  257.         $descriptorSpec array(=> array("pipe""r"),
  258.                                 => array('pipe''w'),
  259.                                 => array('pipe''w'));
  260.         $process proc_open($command$descriptorSpec$pipes);
  261.         $txOff 0$txLen strlen($stdin);
  262.         $stdout ''$stdoutDone FALSE;
  263.         $stderr ''$stderrDone FALSE;
  264.         stream_set_blocking($pipes[0]0)// Make stdin/stdout/stderr non-blocking
  265.         stream_set_blocking($pipes[1]0);
  266.         stream_set_blocking($pipes[2]0);
  267.         if ($txLen == 0fclose($pipes[0]);
  268.         while (TRUE{
  269.             $rx array()// The program's stdout/stderr
  270.             if (!$stdoutDone$rx[$pipes[1];
  271.             if (!$stderrDone$rx[$pipes[2];
  272.             $tx array()// The program's stdin
  273.             if ($txOff $txLen$tx[$pipes[0];
  274.                 stream_select($rx$tx$ex NULLNULLNULL)// Block til r/w possible
  275.                 if (!empty($tx)) {
  276.                     $txRet fwrite($pipes[0]substr($stdin$txOff8192));
  277.                     if ($txRet !== FALSE$txOff += $txRet;
  278.                     if ($txOff >= $txLenfclose($pipes[0]);
  279.                 }
  280.                 foreach ($rx as $r{
  281.                     if ($r == $pipes[1]{
  282.                         $stdout .= fread($pipes[1]8192);
  283.                         if (feof($pipes[1])) {
  284.                             fclose($pipes[1]);
  285.                             $stdoutDone TRUE;
  286.                         }
  287.                     }
  288.                     else if ($r == $pipes[2]{
  289.                         $stderr .= fread($pipes[2]8192);
  290.                         if (feof($pipes[2])) {
  291.                             fclose($pipes[2]);
  292.                             $stderrDone TRUE;
  293.                         }
  294.                     }
  295.                 }
  296.                 if (!is_resource($process)) break;
  297.                 if ($txOff >= $txLen && $stdoutDone && $stderrDonebreak;
  298.         }
  299.         $returnValue proc_close($process);
  300.         return $stdout;
  301.     }
  302. }
  303.  
  304. ?>

Documentation generated on Wed, 26 Jul 2006 21:44:52 +0200 by phpDocumentor 1.3.0RC6