Browse Source

Restore wiki behavior with three tables

Léo Serre 7 months ago
parent
commit
50e8552059
2 changed files with 45 additions and 30 deletions
  1. 44
    29
      models/d.wiki.php
  2. 1
    1
      views/d.wiki.view.html

+ 44
- 29
models/d.wiki.php View File

@@ -14,7 +14,9 @@ require_once($config['third_folder']."Md/MarkdownExtra.inc.php");
14 14
 
15 15
 class WikiPage
16 16
 {
17
-	public $id = NULL;
17
+	public $content_id = NULL;
18
+	public $locale_id = NULL;
19
+	public $version_id = NULL;
18 20
 	public $permalink = NULL;
19 21
 	public $version = 0;
20 22
 	public $locale = NULL;
@@ -37,9 +39,9 @@ class WikiPage
37 39
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
38 40
 			or die ("Could not connect to server\n");
39 41
 
40
-		$query = "SELECT * FROM contents WHERE permalink=$1 AND type='wiki'";
42
+		$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='wiki'";
41 43
 		if($withArchive==0) {
42
-			$query .= " AND is_archive=FALSE AND is_public=TRUE";
44
+			$query .= " AND is_public=TRUE";
43 45
 		}
44 46
 		$query .= " ORDER BY update_date DESC LIMIT 1 OFFSET $2";
45 47
 
@@ -64,7 +66,9 @@ class WikiPage
64 66
 	** Populate the object using raw data from SQL
65 67
 	*****/
66 68
 	public function populate($row) {
67
-		$this->id = $row['id'];
69
+		$this->content_id = $row['content_id'];
70
+		$this->locale_id = $row['locale_id'];
71
+		$this->version_id = $row['version_id'];
68 72
 		$this->permalink = $row['permalink'];
69 73
 		$this->version = $row['version'];
70 74
 		$this->locale = $row['locale'];
@@ -86,46 +90,37 @@ class WikiPage
86 90
 		global $config;
87 91
 		global $user;
88 92
 
89
-		if($this->id == 0)
93
+		if($this->content_id == 0 || $this->locale_id == 0 || $this->version_id == 0)
90 94
 			die("Cannot update entry without giving ID");
91 95
 
92
-		$oldId = $this->id;
93
-		
94 96
 		$this->version++;
95 97
 
96 98
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
97 99
 			or die ("Could not connect to server\n");
98 100
 
99
-		$query = "UPDATE contents SET is_archive = TRUE WHERE permalink = $1 AND type='wiki'";
101
+		$query = "UPDATE content_versions SET is_archive = TRUE WHERE locale_id = $1";
100 102
 
101 103
 		pg_prepare($con, "prepare1", $query) 
102 104
 			or die ("Cannot prepare statement\n");
103
-		$result = pg_execute($con, "prepare1", array($this->permalink))
105
+		$result = pg_execute($con, "prepare1", array($this->locale_id))
104 106
 			or die ("Cannot execute statement\n");
105 107
 
106
-		$query = "INSERT INTO contents (permalink, version, locale, creation_date, update_date, author, is_public, is_archive, is_commentable, type, name, content) VALUES
107
-			($1, $2, $3, $4, $5, $6, TRUE, FALSE, FALSE, 'wiki', $7, $8) RETURNING id";
108
+		$query = "INSERT INTO content_versions (version, update_date, is_archive, name, content, locale_id) VALUES
109
+			($1, $2, FALSE, $3, $4, $5) RETURNING id";
108 110
 
109 111
 		pg_prepare($con, "prepare2", $query) 
110 112
 			or die ("Cannot prepare statement\n");
111
-		$result = pg_execute($con, "prepare2", array($this->permalink, $this->version, $this->locale, $this->creation_date, date('r'), $this->author, $this->name, $this->content))
113
+		$result = pg_execute($con, "prepare2", array($this->version, date('r'), $this->name, $this->content, $this->locale_id))
112 114
 			or die ("Cannot execute statement\n");
113 115
 
114
-		$this->id = pg_fetch_assoc($result)['id'];
115
-
116
-		$query = "INSERT INTO content_contributors (content, contributor) SELECT $1, contributor FROM content_contributors AS old WHERE old.content = $2";
117
-
118
-		pg_prepare($con, "prepare3", $query) 
119
-			or die ("Cannot prepare statement\n");
120
-		$result = pg_execute($con, "prepare3", array($this->id, $oldId))
121
-			or die ("Cannot execute statement\n");
116
+		$this->version_id = pg_fetch_assoc($result)['id'];
122 117
 
123 118
 		$query = "INSERT INTO content_contributors (content, contributor) VALUES
124 119
 			($1, $2) ON CONFLICT (content, contributor) DO NOTHING";
125 120
 
126
-		pg_prepare($con, "prepare4", $query) 
121
+		pg_prepare($con, "prepare3", $query) 
127 122
 			or die ("Cannot prepare statement\n");
128
-		$result = pg_execute($con, "prepare4", array($this->id, $user->id))
123
+		$result = pg_execute($con, "prepare3", array($this->locale_id, $user->id))
129 124
 			or die ("Cannot execute statement\n");
130 125
 
131 126
 		pg_close($con);
@@ -196,22 +191,42 @@ class WikiPage
196 191
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
197 192
 			or die ("Could not connect to server\n");
198 193
 
199
-		$query = "INSERT INTO contents (permalink, version, locale, creation_date, update_date, author, is_public, is_archive, is_commentable, type, name, content) VALUES
200
-			($1, '0', $2, $3, $4, $5, TRUE, FALSE, FALSE, 'wiki', $6, $7) RETURNING id";
194
+		$query = "INSERT INTO contents (permalink, creation_date, is_public, is_commentable, type) VALUES
195
+			($1, $2, TRUE, FALSE, 'wiki') RETURNING id";
201 196
 
202 197
 		pg_prepare($con, "prepare1", $query) 
203 198
 			or die ("Cannot prepare statement\n");
204
-		$result = pg_execute($con, "prepare1", array($this->permalink, $this->locale, date('r'), date('r'), $user->id, $this->name, $this->content))
199
+		$result = pg_execute($con, "prepare1", array($this->permalink, date('r')))
205 200
 			or die ("Cannot execute statement\n");
206 201
 
207
-		$this->id = pg_fetch_assoc($result)['id'];
202
+		$this->content_id = pg_fetch_assoc($result)['id'];
203
+
204
+		$query = "INSERT INTO content_locales (content_id, locale, author) VALUES
205
+			($1, $2, $3) RETURNING id";
206
+
207
+		pg_prepare($con, "prepare2", $query) 
208
+			or die ("Cannot prepare statement\n");
209
+		$result = pg_execute($con, "prepare2", array($this->content_id, $this->locale, $user->id))
210
+			or die ("Cannot execute statement\n");
211
+
212
+		$this->locale_id = pg_fetch_assoc($result)['id'];
213
+
214
+		$query = "INSERT INTO content_versions (version, update_date, is_archive, name, content, locale_id) VALUES
215
+			('0', $1, FALSE, $2, $3, $4) RETURNING id";
216
+
217
+		pg_prepare($con, "prepare3", $query) 
218
+			or die ("Cannot prepare statement\n");
219
+		$result = pg_execute($con, "prepare3", array(date('r'), $this->name, $this->content, $this->locale_id))
220
+			or die ("Cannot execute statement\n");
221
+
222
+		$this->version_id = pg_fetch_assoc($result)['id'];
208 223
 
209 224
 		$query = "INSERT INTO content_contributors (content, contributor) VALUES
210 225
 			($1, $2)";
211 226
 
212
-		pg_prepare($con, "prepare2", $query) 
227
+		pg_prepare($con, "prepare4", $query) 
213 228
 			or die ("Cannot prepare statement\n");
214
-		$result = pg_execute($con, "prepare2", array($this->id, $user->id))
229
+		$result = pg_execute($con, "prepare4", array($this->locale_id, $user->id))
215 230
 			or die ("Cannot execute statement\n");
216 231
 
217 232
 		pg_close($con);
@@ -252,7 +267,7 @@ class WikiPages
252 267
 		$con = pg_connect("host=".$config['SQL_host']." dbname=".$config['SQL_db']." user=".$config['SQL_user']." password=".$config['SQL_pass'])
253 268
 			or die ("Could not connect to server\n");
254 269
 
255
-		$query = "SELECT * FROM contents WHERE permalink=$1 AND type='wiki' ORDER BY update_date DESC";
270
+		$query = "SELECT content_versions.id AS version_id, * FROM contents INNER JOIN content_locales ON contents.id = content_locales.content_id INNER JOIN content_versions ON content_locales.id = content_versions.locale_id WHERE permalink=$1 AND type='wiki' ORDER BY update_date DESC";
256 271
 
257 272
 		pg_prepare($con, "prepare1", $query) 
258 273
 			or die ("Cannot prepare statement\n");

+ 1
- 1
views/d.wiki.view.html View File

@@ -15,7 +15,7 @@
15 15
 						<select id="wikihistory">
16 16
 							<? $i = 0;
17 17
 							foreach ($wikiHistory->objs as $row) { ?>
18
-								<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>
18
+								<option <?=$row->version_id==$wikiPage->version_id?'selected':''?> value="<?=$i?>"><?=$row->is_archive=="f"?'&bull; ':''?><? echo strftime('%d/%m/%Y %H:%M:%S',strtotime($row->update_date)) ?></option>
19 19
 							<? $i++;
20 20
 							} ?>
21 21
 						</select>

Loading…
Cancel
Save