Merged revisions 10677-11457 via svnmerge from
[wvapps.git] / gracefultavi / lib / page.php
blobd0bfefffe98a7abbeb2f3157c16946fc033fcd9c
1 <?php
3 // Abstractor to read and write wiki pages.
4 class WikiPage
6 var $page_id; // Primary Key ID of page.
7 var $name = ''; // Name of page.
8 var $dbname = ''; // Name used in DB queries.
9 var $text = ''; // Page's text in wiki markup form.
10 var $time = ''; // Page's modification time.
11 var $hostname = ''; // Hostname of last editor.
12 var $username = ''; // Username of last editor.
13 var $comment = ''; // Description of last edit.
14 var $version = -1; // Version number of page.
15 var $mutable = 1; // Whether page may be edited.
16 var $template = 0; // Whether page is a template.
17 var $exists = 0; // Whether page has a record in the db.
18 var $db; // Database object.
19 var $createtime; // Creation time of page.
20 var $updatetime; // Update time of page.
22 function WikiPage($db_, $name_ = '')
24 $this->db = $db_;
25 $this->name = $name_;
26 $this->dbname = str_replace('\\', '\\\\', $name_);
27 $this->dbname = str_replace('\'', '\\\'', $this->dbname);
30 // Checks whether the page exists. Returns true if the page exists in the
31 // database and has content. Otherwise, returns false. Not to confuse with
32 // the "exists" property of this class, which is true when the page exists
33 // in the database only, no matter if it has content or not.
34 function exists()
36 global $PgTbl, $page;
38 $qid = $this->db->query("SELECT id " .
39 "FROM $PgTbl " .
40 "WHERE title='$this->dbname' " .
41 "AND bodylength>1");
43 return !!($result = $this->db->result($qid));
46 // Read in a page's contents.
47 // Returns: contents of the page.
48 function read()
50 global $CoTbl, $PgTbl;
52 if ($this->version == -1) {
53 $qry_version = 'lastversion';
54 } else {
55 $qry_version = $this->version;
58 $qry = "SELECT id, time, author, body, attributes, version, " .
59 "username, comment, createtime, updatetime " .
60 "FROM $PgTbl, $CoTbl " .
61 "WHERE title='$this->dbname' " .
62 "AND id=page " .
63 "AND version=$qry_version";
64 $qid = $this->db->query($qry);
66 if (!($result = $this->db->result($qid))) {
67 return '';
70 $this->page_id = $result[0];
71 $this->time = $result[1];
72 $this->hostname = $result[2];
73 $this->exists = 1;
74 $this->version = $result[5];
75 $this->mutable = (($result[4] & MUTABLE_ATTR) == MUTABLE_ATTR ? 1 : 0);
76 $this->template = (($result[4] & TEMPLATE_ATTR) == TEMPLATE_ATTR ? 1 : 0);
77 $this->username = $result[6];
78 $this->text = $result[3];
79 $this->comment = $result[7];
80 $this->createtime = $result[8];
81 $this->updatetime = $result[9];
83 return $this->text;
86 // Write out a page's contents.
87 // Note: caller is responsible for performing locking.
88 // Note: it is assumed that the 'time' member actually contains the
89 // modification-time for the *previous* version. It is expected that
90 // the previous version will have been read into the same object.
91 // Yes, this is a tiny kludge. :-)
92 function write($minoredit = 0)
94 global $CoTbl, $PgTbl;
96 // Ensure a leading and trailing spaces free text but force a new line
97 // at the end.
98 $this->text = trim($this->text) . "\n";
100 $page_id = $this->page_id;
101 $body_length = strlen($this->text);
103 // minor edit is always disabled if body is empty
104 $insertMinorEdit = ($body_length <= 1) ? 0 : ($minoredit ? 1 : 0);
106 // template is disabled if body is empty
107 if ($body_length <= 1) { $this->template = 0; }
109 if ($insertMinorEdit && !trim($this->comment)) {
110 $this->comment = 'Minor edit';
113 // page table
114 $attributes = $this->mutable * MUTABLE_ATTR +
115 $this->template * TEMPLATE_ATTR;
116 if ($this->exists) {
117 // get roolback information
118 $qry = "SELECT lastversion, lastversion_major, bodylength, " .
119 "attributes, createtime, updatetime " .
120 "FROM $PgTbl " .
121 "WHERE id=$page_id";
122 $qid = $this->db->query($qry);
123 $rollback = $this->db->result($qid);
125 $qry = "UPDATE $PgTbl SET lastversion=$this->version, " .
126 "bodylength=$body_length, " .
127 "attributes=$attributes, " .
128 "createtime='$this->createtime'";
129 if ($insertMinorEdit) {
130 $qry .= ", updatetime='$this->updatetime'";
131 } else {
132 $qry .= ", updatetime=null" .
133 ", lastversion_major=$this->version";
135 $qry .= " WHERE id=$page_id";
136 $this->db->query($qry);
137 } else {
138 $metaphone = substr(metaphone($this->name), 0, 80);
139 $qry = "INSERT INTO $PgTbl (title, title_notbinary, " .
140 "lastversion, lastversion_major, metaphone, bodylength, " .
141 "attributes, createtime, updatetime) " .
142 "VALUES ('$this->dbname', '$this->dbname', " .
143 "$this->version, $this->version, '$metaphone', " .
144 "$body_length, $attributes, null, null)";
145 $this->db->query($qry);
146 $page_id = mysql_insert_id($this->db->handle);
147 if (!$page_id) { return false; }
150 // content table
151 $qry = "INSERT INTO $CoTbl (page, version, time, " .
152 "supercede, username, author, comment, " .
153 "body, minoredit) " .
154 "VALUES ($page_id, $this->version, NULL, NULL, " .
155 "'$this->username', '$this->hostname', " .
156 "'$this->comment', '$this->text', $insertMinorEdit)";
157 if (!($qid = mysql_query($qry, $this->db->handle))) {
158 // Roolback previous insert/update to restore data and preserve
159 // referential integrity.
160 if ($this->exists) {
161 $qry = "UPDATE $PgTbl SET lastversion = $rollback[0], " .
162 "lastversion_major = $rollback[1], " .
163 "bodylength = $rollback[2], " .
164 "attributes = $rollback[3], " .
165 "createtime = '$rollback[4]', " .
166 "updatetime = '$rollback[5]' " .
167 "WHERE id=$page_id";
168 } else {
169 $qry = "DELETE FROM $PgTbl " .
170 "WHERE id=$page_id";
172 $this->db->query($qry);
173 return false;
176 if ($this->version > 1) {
177 $this->db->query("UPDATE $CoTbl SET time='$this->time', " .
178 "supercede=NULL WHERE page=$page_id " .
179 "AND version=" . ($this->version - 1));
182 return true;
185 // Check if a user is subscribed to a page.
186 function isSubscribed($username)
188 global $SuTbl;
190 $query = "SELECT count(*) " .
191 "FROM $SuTbl " .
192 "WHERE page='$this->dbname' " .
193 "AND username='$username'";
195 $qid = $this->db->query($query);
197 return (($result = $this->db->result($qid)) && $result[0] > 0) ? 1 : 0;
201 // Toggle page subscription for a user
202 function toggleSubscribe($username)
204 global $SuTbl;
206 if ($username != '')
208 if ($this->isSubscribed($username))
209 $this->db->query("DELETE FROM $SuTbl " .
210 "WHERE page = '$this->dbname' " .
211 "AND username = '$username'");
212 else
213 $this->db->query("INSERT INTO $SuTbl (page, username) " .
214 "VALUES ('$this->dbname', '$username')");
217 return;
220 function getSubscribedUsers($skip_username = '')
222 global $SuTbl;
224 $query = "SELECT username " .
225 "FROM $SuTbl " .
226 "WHERE page='$this->dbname'";
227 if ($skip_username) {
228 $query .= " AND username<>'$skip_username'";
230 $qid = $this->db->query($query);
232 $usernames = array();
233 while ($result = $this->db->result($qid)) {
234 $usernames[] = $result[0];
237 return $usernames;