Browse Source

First implementation of wiki

leosw 8 months ago
parent
commit
2ff2d03b9c

+ 19 - 20
controllers/d.wiki.php

@@ -6,18 +6,22 @@ $head['css'] = "d.index.css;d.wiki.css";
 
 $wikiPage = new Kabano\WikiPage();
 // Page doesn't exists
-if(isset($controller->splitted_url[1]) && !$wikiPage->checkUrl($controller->splitted_url[1],$user->rankIsHigher('premium')) && $controller->splitted_url[1]!="") {
+if(isset($controller->splitted_url[1]) && !$wikiPage->checkPermalink($controller->splitted_url[1],$user->rankIsHigher('premium')) && $controller->splitted_url[1]!="") {
 	if($user->rankIsHigher('moderator')) {
+		$wikiPage->permalink = $controller->splitted_url[1];
 		// Create new page
 		if(isset($_POST['submit'])) {
 			$wikiPage->content = $_POST['content'];
 			$wikiPage->locale = $_POST['locale'];
-			$wikiPage->title = $_POST['title'];
+			$wikiPage->name = $_POST['name'];
 			$wikiPage->insert();
 
-			header('Location: '.$config['rel_root_folder']."wiki/".$wikiPage->url);
+			header('Location: '.$config['rel_root_folder']."wiki/".$wikiPage->permalink);
 		}
 		else {
+			$locales = new Kabano\Locales();
+			$locales->getAll();
+
 			$head['title'] = "Nouvelle page";
 			include ($config['views_folder']."d.wiki.edit.html");
 		}
@@ -27,45 +31,40 @@ if(isset($controller->splitted_url[1]) && !$wikiPage->checkUrl($controller->spli
 	}
 }
 // Page exists
-else if(isset($controller->splitted_url[1]) && $wikiPage->checkUrl($controller->splitted_url[1],$user->rankIsHigher('premium'))) {
-	if (isset($controller->splitted_url[2]) && $controller->splitted_url[2]=="edit" && $user->rankIsHigher('administrator')) {
+else if(isset($controller->splitted_url[1]) && $wikiPage->checkPermalink($controller->splitted_url[1],$user->rankIsHigher('premium'))) {
+	$wikiPage->permalink = $controller->splitted_url[1];
+	if (isset($controller->splitted_url[2]) && $controller->splitted_url[2]=="edit" && $user->rankIsHigher('moderator')) {
 		// Edit page
 		if(isset($_POST['submit'])) {
 			$wikiPage->content = $_POST['content'];
 			$wikiPage->locale = $_POST['locale'];
-			$wikiPage->title = $_POST['title'];
+			$wikiPage->name = $_POST['name'];
 			$wikiPage->update();
 
-			header('Location: '.$config['rel_root_folder']."wiki/".$wikiPage->url);
+			header('Location: '.$config['rel_root_folder']."wiki/".$wikiPage->permalink);
 		}
 		else {
-			$wikiPage->populate();
-			$head['title'] = $wikiPage->title;
+			$locales = new Kabano\Locales();
+			$locales->getAll();
+			
+			$head['title'] = $wikiPage->name;
 			include ($config['views_folder']."d.wiki.edit.html");
 		}
 	} else if (isset($controller->splitted_url[2]) && $controller->splitted_url[2]=="delete" && $user->rankIsHigher('moderator')) {
 		// Delete page
 		$wikiPage->delete();
-		header('Location: '.$config['rel_root_folder']."wiki/".$wikiPage->url);
+		header('Location: '.$config['rel_root_folder']."wiki/".$wikiPage->permalink);
 	} else {
 		// Display page
 		if($user->rankIsHigher('premium')) {
 			$wikiHistory = new Kabano\WikiPages();
 			$wikiHistory->getHistory($controller->splitted_url[1]);
-
-			$i = 0;
-			foreach ($wikiHistory->ids as $row) {
-				$wikiHistory_list[$i] = new Kabano\WikiPage();
-				$wikiHistory_list[$i]->id = $row;
-				$wikiHistory_list[$i]->populate();
-				$i++;
-			}
 		}
 		if (isset($controller->splitted_url[2]) && is_numeric($controller->splitted_url[2]))
-			$wikiPage->checkUrl($controller->splitted_url[1], $user->rankIsHigher('premium'), $controller->splitted_url[2]);
+			$wikiPage->checkPermalink($controller->splitted_url[1], $user->rankIsHigher('premium'), $controller->splitted_url[2]);
 
 		$wikiPage->md2html();
-		$head['title'] = $wikiPage->title;
+		$head['title'] = $wikiPage->name;
 		include ($config['views_folder']."d.wiki.view.html");
 	}
 }

+ 1 - 8
models/d.locales.php

@@ -12,7 +12,7 @@ namespace Kabano;
 
 class Locale
 {
-    private $name = 0;
+    public $name = 0;
     public $display_name = NULL;
     public $flag_name = NULL;
 
@@ -52,13 +52,6 @@ class Locale
 	    $this->display_name = $row['display_name'];
 	    $this->flag_name = $row['flag_name'];
 	}
-
-	/*****
-	** Simple return only functions
-	*****/
-	public function get_name() {
-	    return $this->name;
-	}
 }
 
 /**********************************************************

+ 1 - 1
models/d.users.php

@@ -218,7 +218,7 @@ class User
 			or die ("Could not connect to server\n");
 
 		$query = "INSERT INTO users (name, version, email, password, website, is_avatar_present, is_archive, rank, locale, timezone, visit_date, register_date) VALUES
-			($1, '0', $2, $3, $4, 'f', 'f', 'registered', $5, $6, $7, $8)";
+			($1, '0', $2, $3, $4, FALSE, FALSE, 'registered', $5, $6, $7, $8)";
 
 		pg_prepare($con, "prepare1", $query) 
 			or die ("Cannot prepare statement\n");

+ 46 - 48
models/d.wiki.php

@@ -14,30 +14,30 @@ require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
 
 class WikiPage
 {
-	private $id = 0;
-	private $permalink = 0;
-	private $version = 0;
-	private $locale = NULL;
-	private $creation_date = NULL;
-	private $update_date = NULL;
-	private $author = NULL;
-	private $is_public = NULL;
-	private $is_archive = NULL;
-	private $is_commentable = NULL;
-	private $type = "wiki";
+	public $id = 0;
+	public $permalink = 0;
+	public $version = 0;
+	public $locale = NULL;
+	public $creation_date = NULL;
+	public $update_date = NULL;
+	public $author = NULL;
+	public $is_public = NULL;
+	public $is_archive = NULL;
+	public $is_commentable = NULL;
+	public $type = "wiki";
 	public $name = NULL;
 	public $content = NULL;
 
 	/*****
-	** Checks if a page at this URL exists and return the populated element
+	** Checks if a page at this ermalink exists and return the populated element
 	*****/
-	public function checkUrl($url, $withArchive=0, $elementNb=0) {
+	public function checkPermalink($permalink, $withArchive=0, $elementNb=0) {
 		global $config;
 		
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
 			or die ("Could not connect to server\n");
 
-		$query = "SELECT * FROM contents WHERE permalink=$1";
+		$query = "SELECT * FROM contents WHERE permalink=$1 AND type='wiki'";
 		if($withArchive==0) {
 			$query .= " AND is_archive=FALSE AND is_public=TRUE";
 		}
@@ -45,7 +45,7 @@ class WikiPage
 
 		pg_prepare($con, "prepare1", $query) 
 			or die ("Cannot prepare statement\n");
-		$result = pg_execute($con, "prepare1", array($url, $elementNb))
+		$result = pg_execute($con, "prepare1", array($permalink, $elementNb))
 			or die ("Cannot execute statement\n");
 
 		pg_close($con);
@@ -63,7 +63,8 @@ class WikiPage
 	/*****
 	** Populate the object using raw data from SQL
 	*****/
-	private function populate($row) {
+	public function populate($row) {
+		$this->id = $row['id'];
 		$this->permalink = $row['permalink'];
 		$this->version = $row['version'];
 		$this->locale = $row['locale'];
@@ -79,49 +80,37 @@ class WikiPage
 	}
 
 	/*****
-	** Return archive status
-	*****/
-	public function is_archive() {
-		return $this->is_archive;
-	}
-
-	/*****
-	** Return archive status
-	*****/
-	public function update_date() {
-		return $this->update_date;
-	}
-
-	/*****
 	** Edit a page by archiving the current one and inserting a new one ID
 	*****/
 	public function update() {
 		global $config;
-		global $user;
+		//global $user;
 		
+		$this->version++;
+
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
 			or die ("Could not connect to server\n");
 
-		$query = "UPDATE wiki SET archive = TRUE WHERE url = $1";
+		$query = "UPDATE contents SET is_archive = TRUE WHERE permalink = $1 AND type='wiki'";
 
 		pg_prepare($con, "prepare1", $query) 
 			or die ("Cannot prepare statement\n");
-		$result = pg_execute($con, "prepare1", array($this->url))
+		$result = pg_execute($con, "prepare1", array($this->permalink))
 			or die ("Cannot execute statement\n");
 
 
-		$query = "INSERT INTO wiki (url, title, content, lastedit, archive, locale) VALUES
-			($1, $2, $3, $4, FALSE, $5)";
+		$query = "INSERT INTO contents (permalink, version, locale, creation_date, update_date, author, is_public, is_archive, is_commentable, type, name, content) VALUES
+			($1, $2, $3, $4, $5, $6, TRUE, FALSE, FALSE, 'wiki', $7, $8)";
 
 		pg_prepare($con, "prepare2", $query) 
 			or die ("Cannot prepare statement\n");
-		$result = pg_execute($con, "prepare2", array($this->url, $this->title, $this->content, date('r'), $this->locale))
+		$result = pg_execute($con, "prepare2", array($this->permalink, $this->version, $this->locale, $this->creation_date, date('r'), $this->author, $this->name, $this->content))
 			or die ("Cannot execute statement\n");
 
 		pg_close($con);
 
 		error_log(
-			date('r')." \t".$user->name." (".$user->id.") \tUPDATE \tEdit wiki page '".$this->url."'\r\n",
+			date('r')." \t".$user->name." (".$user->id.") \tUPDATE \tEdit wiki page '".$this->permalink."'\r\n",
 			3,
 			$config['logs_folder'].'wiki.log');
 	}
@@ -136,17 +125,17 @@ class WikiPage
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
 			or die ("Could not connect to server\n");
 
-		$query = "UPDATE wiki SET archive = TRUE WHERE url = $1";
+		$query = "UPDATE contents SET is_archive = TRUE WHERE permalink = $1 AND type='wiki'";
 
 		pg_prepare($con, "prepare1", $query) 
 			or die ("Cannot prepare statement\n");
-		$result = pg_execute($con, "prepare1", array($this->url))
+		$result = pg_execute($con, "prepare1", array($this->permalink))
 			or die ("Cannot execute statement\n");
 
 		pg_close($con);
 
 		error_log(
-			date('r')." \t".$user->name." (".$user->id.") \tDELETE \tArchive wiki page '".$this->url."'\r\n",
+			date('r')." \t".$user->name." (".$user->id.") \tDELETE \tArchive wiki page '".$this->permalink."'\r\n",
 			3,
 			$config['logs_folder'].'wiki.log');
 	}
@@ -161,18 +150,18 @@ class WikiPage
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
 			or die ("Could not connect to server\n");
 
-		$query = "INSERT INTO wiki (url, title, content, lastedit, archive, locale) VALUES
-			($1, $2, $3, $4, FALSE, $5)";
+		$query = "INSERT INTO contents (permalink, version, locale, creation_date, update_date, author, is_public, is_archive, is_commentable, type, name, content) VALUES
+			($1, '0', $2, $3, $4, $5, TRUE, FALSE, FALSE, 'wiki', $6, $7)";
 
-		pg_prepare($con, "prepare2", $query) 
+		pg_prepare($con, "prepare1", $query) 
 			or die ("Cannot prepare statement\n");
-		$result = pg_execute($con, "prepare2", array($this->url, $this->title, $this->content, date('r'), $this->locale))
+		$result = pg_execute($con, "prepare1", array($this->permalink, $this->locale, date('r'), date('r'), $user->id, $this->name, $this->content))
 			or die ("Cannot execute statement\n");
 
 		pg_close($con);
 
 		error_log(
-			date('r')." \t".$user->name." (".$user->id.") \tINSERT \tCreate new wiki page '".$this->url."'\r\n",
+			date('r')." \t".$user->name." (".$user->id.") \tINSERT \tCreate new wiki page '".$this->permalink."'\r\n",
 			3,
 			$config['logs_folder'].'wiki.log');
 	}
@@ -185,9 +174,17 @@ class WikiPage
 	}
 }
 
+/**********************************************************
+***********************************************************
+**  
+**  This class is to manage a wiki page object
+**  
+***********************************************************
+**********************************************************/
+
 class WikiPages
 {
-	public $ids = array();
+	public $objs = array();
 	public $number = NULL;
 
 	/*****
@@ -199,7 +196,7 @@ class WikiPages
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
 			or die ("Could not connect to server\n");
 
-		$query = "SELECT id FROM wiki WHERE url=$1 ORDER BY lastedit DESC";
+		$query = "SELECT * FROM contents WHERE permalink=$1 AND type='wiki' ORDER BY update_date DESC";
 
 		pg_prepare($con, "prepare1", $query) 
 			or die ("Cannot prepare statement\n");
@@ -212,7 +209,8 @@ class WikiPages
 
 		for($i = 0; $i < $this->number; $i++) {
 			$row = pg_fetch_assoc($result, $i);
-			$this->ids[$i] = $row['id'];
+			$this->objs[$i] = new WikiPage;
+			$this->objs[$i]->populate($row);
 		}
 	}
 }

+ 1 - 1
views/d.user.profile.edit.html

@@ -44,7 +44,7 @@
 								<li><label for="locale">Langue :</label>
 									<select name="locale" id="locale">
 										<? foreach($locales->objs as $locale) { ?>
-											<option <?=$userProfile->get_locale()==$locale->display_name?'selected':''?> value="<?=$locale->get_name()?>"><?=$locale->display_name?></option>
+											<option <?=$userProfile->get_locale()==$locale->display_name?'selected':''?> value="<?=$locale->name?>"><?=$locale->display_name?></option>
 										<? } ?>
 									</select></li>
 								<? if($user->rankIsHigher("administrator")) { ?>

+ 4 - 4
views/d.wiki.edit.html

@@ -8,14 +8,14 @@
 		<? include('blocks/d.nav.html'); ?>
 
 		<section>
-			<form class="form" action="<?=$config['rel_root_folder']?>wiki/<?=$wikiPage->url?>/edit" method="post">
+			<form class="form" action="<?=$config['rel_root_folder']?>wiki/<?=$wikiPage->permalink?>/edit" method="post">
 				<h1>
 					<select name="locale" id="locale">
-						<? foreach($config['locales'] as $locale) { ?>
-							<option <?=$wikiPage->locale==$locale[0]?'selected':''?> value="<?=$locale[0]?>"><?=$locale[5]?></option>
+						<? foreach($locales->objs as $locale) { ?>
+							<option <?=$wikiPage->locale==$locale->name?'selected':''?> value="<?=$locale->name?>"><?=$locale->display_name?></option>
 						<? } ?>
 					</select>
-					<input type="text" value="<?=$wikiPage->title?>" name="title" id="title" placeholder="Titre">
+					<input type="text" value="<?=$wikiPage->name?>" name="name" id="name" placeholder="Titre">
 				</h1>
 
 				<textarea rows="30" name="content" id="content" placeholder="Contenu de la page"><?=$wikiPage->content?></textarea>

+ 10 - 10
views/d.wiki.view.html

@@ -7,27 +7,27 @@
 
 		<? include('blocks/d.nav.html'); ?>
 
-		<section id="wiki_page" <?=!$wikiPage->is_archive()?'class="archive"':''?>>
+		<section id="wiki_page" <?=$wikiPage->is_archive=="t"?'class="archive"':''?>>
 			<h1><?=$wikiPage->name?>.</h1>
 				<? if($user->rankIsHigher('premium')) { ?>
 					<span class="subtitle">
-					<? if(isset($wikiHistory_list)) { ?>
+					<? if(isset($wikiHistory)) { ?>
 						<select id="wikihistory">
 							<? $i = 0;
-							foreach ($wikiHistory_list as $row) { ?>
-								<option <?=$row->id==$wikiPage->id?'selected':''?> value="<?=$i?>"><?=$row->is_archive=="f"?'&bull; ':''?><? echo strftime('%d/%m/%Y %H:%M:%S',strtotime($row->lastedit)) ?></option>
+							foreach ($wikiHistory->objs as $row) { ?>
+								<option <?=$row->id==$wikiPage->id?'selected':''?> value="<?=$i?>"><?=$row->is_archive=="f"?'&bull; ':''?><? echo strftime('%d/%m/%Y %H:%M:%S',strtotime($row->update_date)) ?></option>
 							<? $i++;
 							} ?>
 						</select>
 					<? } 
-					if ($user->rankIsHigher('moderator') && isset($wikiHistory_list)) { ?>
+					if ($user->rankIsHigher('moderator') && isset($wikiHistory)) { ?>
 						 &mdash; 
 					<? }
 					if ($user->rankIsHigher('moderator')) { ?>
-						<a href="<?=$config['rel_root_folder']?>wiki/<?=$wikiPage->url?>/edit"><i class="fa fa-pencil"></i> Éditer la page</a>
-						<? if (!$wikiPage->is_archive()) { ?>
+						<a href="<?=$config['rel_root_folder']?>wiki/<?=$wikiPage->permalink?>/edit"><i class="fas fa-pencil-alt"></i> Éditer la page</a>
+						<? if ($wikiPage->is_archive=="f") { ?>
 							 &mdash; 
-							<a href="<?=$config['rel_root_folder']?>wiki/<?=$wikiPage->url?>/delete"><i class="fa fa-trash"></i> Effacer la page</a>
+							<a href="<?=$config['rel_root_folder']?>wiki/<?=$wikiPage->permalink?>/delete"><i class="fas fa-trash"></i> Effacer la page</a>
 						<? } ?>
 					<? } ?>
 					</span>
@@ -37,7 +37,7 @@
 				<?=$wikiPage->content_html?>
 			</div>
 
-			<p id="wikiTimestamp">Page mise à jour le <? echo strftime('%e %B %G, %kh%Mm%Ss',strtotime($wikiPage->update_date())) ?> <small><abbr title="Temps Universel Coordonné">UTC</abbr></small></p>
+			<p id="wikiTimestamp">Page mise à jour le <? echo strftime('%e %B %G, %kh%Mm%Ss',strtotime($wikiPage->update_date)) ?> <small><abbr title="Temps Universel Coordonné">UTC</abbr></small></p>
 
 			<div style="clear: both;"> </div>
 		</section>
@@ -45,7 +45,7 @@
 <? if($user->rankIsHigher('premium')) { ?>
 		<script type="text/javascript">
 $( "#wikihistory" ).change(function() {
-  window.location.href = "<?=$config['rel_root_folder']?>wiki/<?=$wikiPage->url?>/"+$( this ).val();
+  window.location.href = "<?=$config['rel_root_folder']?>wiki/<?=$wikiPage->permalink?>/"+$( this ).val();
 });
 		</script>
 <? } ?>