Source for file CMS3_Wiki.php

Documentation is available at CMS3_Wiki.php

  1. <?php
  2. //              (F)
  3. // CMS3 - A Three Content Management System.
  4. // Copyright (C) 2007  Jop... (Jonas F. Jensen).
  5. // 
  6. // This program is free software; you can redistribute it and/or
  7. // modify it under the terms of the GNU General Public License
  8. // as published by the Free Software Foundation; either version 2
  9. // of the License, or (at your option) any later version.
  10. // 
  11. // This program is distributed in the hope that it will be useful,
  12. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. // GNU General Public License for more details.
  15. //
  16. // You should have received a copy of the GNU General Public License
  17. // along with this program; if not, write to the Free Software
  18. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
  19.  
  20. /**
  21. *This file defines CMS3_Wiki
  22. *
  23. @package    Wiki
  24. @author    Jonas F. Jensen <jopsen@gmail.com>
  25. @copyright    2007 Jonas F. Jensen.
  26. @license    http://www.gnu.org/licenses/gpl.txt
  27. */
  28.  
  29. /**
  30. * Very simple plugin that allows creation of an image gallery
  31. */
  32. {
  33.  
  34.         //Implementation of IProvidesContent
  35.         /**
  36.      * Print the page of a given identifier.
  37.      *
  38.      * @param string identifier Identifier of the requested page.
  39.  
  40.      * @param IOutputHandler OutputHandler OutputHandler used the parse the output. You may disable this OutputHandler with
  41.     *  OutputHandler->Disable() and print the content manually.
  42.  
  43.      * @return 
  44.      * @access public
  45.      */
  46.     public function &GetPage$identifier,  &$OutputHandler ){
  47.         //Initialize body variable
  48.         $body "";
  49.  
  50.         //Get a url
  51.         $Url $this->Owner->GetCMS3URL($this->Owner->GetNamespace($this->GetPluginID()) "/";
  52.  
  53.         //Use the first string before the first slash
  54.         $cmd explode("/",$identifier,2);
  55.         switch($cmd[0])
  56.         {
  57.             case "Edit":
  58.                 $identifier substr($identifier,5)//Cut of the Edit/
  59.                 //Include TinyMCE editor
  60.                 $head ="<script language='javascript' type='text/javascript' src='" $this->Owner->GetCMS3URL("bin/Wiki/tiny_mce/tiny_mce.js'></script>";
  61.                 $head .= "<script language='javascript' type='text/javascript'>tinyMCE.init({mode : 'textareas', theme : 'simple' });</script>";
  62.                 $OutputHandler->ExtentHeader($head);
  63.             
  64.                 //Select the latest revision of the page requested
  65.                 $Query "SELECT Content From CMS3_Wiki WHERE Identifier = '" $identifier "' ORDER BY Revision DESC LIMIT 1";
  66.                 //Get database handle
  67.                 $dbh $this->Owner->GetDatabaseHandle();
  68.                 //Execute query
  69.                 if(!$Result mysql_query($Query$dbh)){
  70.                     $this->Owner->LogMySQL($this->GetPluginID()"Error while trying to get wikipage$identifier for editing",  $dbh);
  71.                     //Redirect to root
  72.                     header("Location: " $Url);
  73.                 }else{
  74.                     $body .= "<form action='" $Url "Save/" $identifier "' method='post'>";
  75.                     if(mysql_num_rows($Result!= 0){
  76.                         $body .= "<h1>Edit: " ($identifier == "" "Wiki" $identifier."</h1>";
  77.                         //Parse the content to body variable
  78.                         $Row mysql_fetch_row($Result);
  79.                         $body .= "<textarea style='width: 98%; min-height: 300px;' name='Content'>" $Row[0"</textarea>";
  80.                     }else{
  81.                         //Create new page
  82.                         $body .= "<h1>Create: " $identifier ."</h1>";
  83.                         $body .= "<textarea style='width: 98%; min-height: 300px;' name='Content'>Create a new page...<br /></textarea>";
  84.                     }
  85.                     $body .= "<br/ ><input type='submit' value='Save' /></form>";
  86.                 }
  87.             break;
  88.  
  89.             case "History":
  90.                 $identifier substr($identifier,8)//Cut of the History/
  91.                 //List old revision of this identifier
  92.  
  93.                 //Select a list of revisions of the page requested
  94.                 $Query "SELECT Revision, Created From CMS3_Wiki WHERE Identifier = '" $identifier "' ORDER BY Revision DESC";
  95.                 //Get database handle
  96.                 $dbh $this->Owner->GetDatabaseHandle();
  97.                 //Execute query
  98.                 if(!$Result mysql_query($Query$dbh)){
  99.                     $this->Owner->LogMySQL($this->GetPluginID()"Error while trying to get revision$identifier",  $dbh);
  100.                     //Redirect to root
  101.                     header("Location: " $Url);
  102.                 }else{
  103.                     if(mysql_num_rows($Result!= 0){
  104.                         $body .= "<h1>History of " ($identifier == "" "Wiki" $identifier"</h1>";
  105.                         //Parse the content to body variable
  106.                         while($Row mysql_fetch_array($ResultMYSQL_ASSOC)){
  107.                             $body .= "<a href='" $Url .  "Revision/" $Row["Revision""'>Revision: " $Row["Revision"" - " date(DATE_RFC822$Row["Created"]"</a><br>";
  108.                         }
  109.                     }else{
  110.                         //Redirect to editor
  111.                         header("Location: " $Url "Edit/" $identifier);
  112.                     }
  113.                 }
  114.  
  115.             break;
  116.  
  117.             case "Revision":
  118.                 $identifier substr($identifier,9)//Cut of the Revision/
  119.                 //Select the revision requested
  120.                 $Query "SELECT Content, Created, Identifier From CMS3_Wiki WHERE Revision = '" $identifier "' LIMIT 1";
  121.                 //Get database handle
  122.                 $dbh $this->Owner->GetDatabaseHandle();
  123.                 //Execute query
  124.                 if(!$Result mysql_query($Query$dbh)){
  125.                     $this->Owner->LogMySQL($this->GetPluginID()"Error while trying to get revision$identifier",  $dbh);
  126.                     //Redirect to root
  127.                     header("Location: " $Url);
  128.                 }else{
  129.                     if(mysql_num_rows($Result!= 0){
  130.                         //Parse the content to body variable
  131.                         $Row mysql_fetch_row($Result);
  132.                         $body .= "<h1>" ($Row[2== "" "Wiki" $Row[2]."</h1><i>Revision: " $identifier ", created " $Row[1"</i><p>" $Row[0"</p>";
  133.                         $identifier $Row[2];
  134.                     }else{
  135.                         //Redirect to root
  136.                         header("Location: " $Url);
  137.                     }
  138.                 }
  139.             break;
  140.  
  141.             case "Save":
  142.                 //Make the cut of the Save/ part, make it parseable for default
  143.                 $identifier substr($identifier,5);
  144.                 //Always insert into
  145.                 $Query "INSERT INTO CMS3_Wiki (Identifier, Content) VALUES ('" $identifier "', '" $_POST["Content""')";
  146.                 $dbh $this->Owner->GetDatabaseHandle();
  147.                 //Execute query
  148.                 if(!$Result mysql_query($Query$dbh)){
  149.                     $this->Owner->LogMySQL($this->GetPluginID()"Error while trying to get wikipage$identifier",  $dbh);
  150.                 }
  151.             default//just display the page the identifier is identifing :)
  152.                 //Select the latest revision of the page requested
  153.                 $Query "SELECT Content, Created, Revision From CMS3_Wiki WHERE Identifier = '" $identifier "' ORDER BY Revision DESC LIMIT 1";
  154.                 //Get database handle
  155.                 $dbh $this->Owner->GetDatabaseHandle();
  156.                 //Execute query
  157.                 if(!$Result mysql_query($Query$dbh)){
  158.                     $this->Owner->LogMySQL($this->GetPluginID()"Error while trying to get wikipage$identifier",  $dbh);
  159.                     //Redirect to root
  160.                     header("Location: " $Url);
  161.                 }else{
  162.                     if(mysql_num_rows($Result!= 0){
  163.                         //Parse the content to body variable
  164.                         $Row mysql_fetch_row($Result);
  165.                         $body .= "<h1>" ($identifier == "" "Wiki" $identifier."</h1><i>Revision: " $Row[2", created " $Row[1"</i><p>" $Row[0"</p>";
  166.                     }else{
  167.                         //Redirect to editor
  168.                         header("Location: " $Url "Edit/" $identifier);
  169.                     }
  170.                 }
  171.             break;
  172.         }
  173.  
  174.         //Add a little toolbar at the top of the wiki page
  175.         $body "<div style=\"float: right;\"><a href=\"" $Url $identifier "\">Article</a> | <a href=\"" $Url "Edit/" $identifier "\">Edit</a> | <a href=\"" $Url "History/" $identifier "\">History</a></div><br />" $body;
  176.  
  177.         $OutputHandler->SetTitle($identifier);
  178.         $OutputHandler->SetBody($body);
  179.  
  180.         $OutputHandler->Parse();
  181.         return $OutputHandler;
  182.     }
  183.  
  184.         //Implementation of IPlugin
  185.  
  186.     
  187.     /**
  188.      * Gets the pluginID of the plugin.
  189.      *
  190.      * @return string 
  191.      * @access public
  192.      */
  193.     public function GetPluginID){
  194.         return "Wiki";
  195.     }
  196.  
  197.  
  198.     //Implementation of ILinkable
  199.  
  200.     
  201.     /**
  202.      * Gets a list of links that the plugins exposes
  203.      * 
  204.      * Each entry in the array is an array with 3 fields, one for the display name, one for the URL of the page, and one for an array of sub entries. Each sub entry consits of 3 fields, just like the toplevel entries.
  205.      * 
  206.      * @access public
  207.      * @return array 
  208.      */
  209.     public function GetLinks){
  210.         //Get out namespace
  211.         if($Namespace $this->Owner->GetNamespace($this->GetPluginID())){
  212.             //Get a URL
  213.             $URL $this->Owner->GetCMS3URL($Namespace "/";
  214.  
  215.             //Create an plugin entry
  216.             $Links array($Namespace,$URLarray());
  217.  
  218.             //Generate content
  219.             $Query "SELECT DISTINCT Identifier FROM CMS3_Wiki ORDER by Identifier";
  220.             $Result mysql_query($Query$this->Owner->GetDatabaseHandle());
  221.  
  222.             if(mysql_num_rows($Result!= 0){
  223.                 while($Row mysql_fetch_array($ResultMYSQL_ASSOC))
  224.                 {
  225.                     //Default page is representated as plugin entry
  226.                     if($Row["Identifier"!= "")
  227.                         $Links[2][=array($Row["Identifier"]$URL $Row["Identifier"null)
  228.                 }
  229.             }
  230.  
  231.             //Add toplevel array and return it
  232.             return array($Links);
  233.         }else{
  234.             return array(array($this->GetPluginID()nullnull));
  235.         }
  236.     }
  237.  
  238.         //IConfigure implementation
  239.  
  240.     
  241.     /**
  242.     * Gets an absolute address of an icon for this plugin
  243.     */
  244.     public function GetControlCenterIcon(){
  245.         return $this->Owner->GetCMS3URL("bin/" $this->GetPluginID("/icon.png";
  246.     }
  247.  
  248.     /**
  249.     * Gets menu entry string
  250.     */
  251.     public function GetControlCenterMenuEntry(){
  252.         //TODO: i18n
  253.         return "Configure " $this->GetPluginID();
  254.     }
  255.  
  256.     /**
  257.      * Gets a dojo based ajax guide for configuration of the plugin.
  258.      *
  259.      * @param IEmbedmentOutputHandler OutputHandler
  260.      * @return IEmbedmentOutputHandler 
  261.      * @access public
  262.      */
  263.     public function &Configure($Identifier null&$EmbedmentOutputHandler ){
  264.         //Load templates from files
  265.         $Head file_get_contents($this->Owner->GetCMS3Path("data/" $this->GetPluginID("/ConfigHead.htm");
  266.         $Body file_get_contents($this->Owner->GetCMS3Path("data/" $this->GetPluginID("/ConfigBody.htm");
  267.  
  268.         $Body str_replace("CMS3URL",$this->Owner->GetCMS3URL(,$Body);
  269.         $Body str_replace("NAMESPACE",$this->Owner->GetNamespace($this->GetPluginID()) ,$Body);
  270.  
  271.         //Parse the output
  272.         $EmbedmentOutputHandler->ExtentHeader($Head);
  273.         $EmbedmentOutputHandler->SetBody($Body);
  274.         return $EmbedmentOutputHandler;
  275.     }
  276.  
  277.         //ISystemCallback implementation
  278.  
  279.     
  280.     /**
  281.      * Handles a callback from ajax application.
  282.      *
  283.      * @param string data Data attached to this callback
  284.      * @return 
  285.      * @access public
  286.      */
  287.     public function SystemCallback$data ){
  288.         if($data == "SaveNamespace"){
  289.             //Find old namespace
  290.             $OldNS $this->Owner->GetNamespace($this->GetPluginID());
  291.  
  292.             //Does old namespace exist?
  293.             if(!$OldNS){
  294.                 //If does not exist
  295.  
  296.                 //TODO: remove illegal chars...
  297.                 //Try to register new namespace
  298.                 if($this->Owner->RegisterNamespace($_POST["Namespace"]$this->GetPluginID())){
  299.                     print "TRUE";
  300.                 }else{
  301.                         //TODO i18n
  302.                     print "Registration failed, make sure you have chosen a unique namespace.";
  303.                 }
  304.             }else{
  305.                 //If does exist
  306.                 
  307.                 //Unregister current namespace (should not be able to fail)
  308.                 if($this->Owner->UnregisterNamespace($OldNS)) //NOTE: a plugin can't have two namespaces due to database layout.
  309.                 {
  310.                     //TODO: remove illegal chars...
  311.                     //Try to register new namespace
  312.                     if($this->Owner->RegisterNamespace($_POST["Namespace"]$this->GetPluginID())){
  313.                         print "TRUE";
  314.                     }else{
  315.                         //Try to reregister old namespace
  316.                         if($this->Owner->RegisterNamespace($OldNS$this->GetPluginID())){
  317.                                 //TODO i18n
  318.                             print "Registration failed, make sure you have chosen a unique namespace. You still have the old namespace.";
  319.                         }else{
  320.                                 //TODO i18n
  321.                             print "Registration failed, make sure you have chosen a unique namespace. You don't have any namespace now.";
  322.                             //Not owning any namespace
  323.                         }
  324.                     }
  325.                 }else{
  326.                     //Could not unregister current namespace
  327.                         //TODO: i18n
  328.                     $msg "Unregistration of current namespace failed."
  329.                     print $msg;
  330.                     $this->Owner->Log($this->GetPluginID()$msg);
  331.                 }
  332.             }
  333.         }else{
  334.             //If identifier doesn't identify the request just give a soft 404..
  335.             print "Error: 404, data not found.";
  336.         }
  337.     }
  338.  
  339.         //Implementation of IInstall
  340.  
  341.     
  342.     /**
  343.      * Runs a script to complet installation
  344.      *
  345.      * This creates database tables, and default data.
  346.      *
  347.      * @return bool True if installations script went okay, otherwise return false.
  348.      * @access public
  349.      */
  350.     public function Install()
  351.     {
  352.         //Get database handle
  353.         $dbh $this->Owner->GetDatabaseHandle();
  354.  
  355.         //Create a table
  356.         if(!mysql_query("CREATE TABLE IF NOT EXISTS `CMS3_Wiki` (   `Revision` int(11) NOT NULL auto_increment,   `Identifier` varchar(255) NOT NULL,   `Content` text NOT NULL,   `Created` timestamp NOT NULL default CURRENT_TIMESTAMP,   PRIMARY KEY  (`Revision`),   KEY `Identifier` (`Identifier`) ) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1 AUTO_INCREMENT=5"$dbh )) 
  357.         {
  358.             //Log potentiol errors
  359.             $this->Owner->LogMySQL($this->GetPluginID()"Error during creating of tables "$dbh);
  360.             return false;
  361.         }else{
  362.             return true;
  363.         }
  364.     }
  365.  
  366.         //Implementation of IRemove
  367.         /**
  368.      * Removes files and databases created after fysical installation
  369.      *
  370.      * @return bool true/false depending on success
  371.      * @access public
  372.      */
  373.     public function Remove)
  374.     {
  375.         //Unregister namespace
  376.         $this->Owner->UnregisterNamespace($this->Owner->GetNamespace($this->GetPluginID()));
  377.  
  378.         //Drop tables
  379.         $sql 'DROP TABLE CMS3_Wiki';
  380.         $dbh $this->Owner->GetDatabaseHandle();
  381.         if(mysql_query($sql,  $dbh)) 
  382.         {
  383.             return true;
  384.         }else{    
  385.             $this->Owner->LogMySQL($this->GetPluginID()"Error during deletion of database table " ,$dbh);
  386.             return false;
  387.         }
  388.     }
  389.  
  390. }
  391.  
  392.  
  393. ?>

Documentation generated on Mon, 30 Apr 2007 01:59:11 +0200 by phpDocumentor 1.3.1