3 // Abstractor to read and write wiki pages.
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_ = '')
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.
38 $qid = $this->db
->query("SELECT id " .
40 "WHERE title='$this->dbname' " .
43 return !!($result = $this->db
->result($qid));
46 // Read in a page's contents.
47 // Returns: contents of the page.
50 global $CoTbl, $PgTbl;
52 if ($this->version
== -1) {
53 $qry_version = 'lastversion';
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' " .
63 "AND version=$qry_version";
64 $qid = $this->db
->query($qry);
66 if (!($result = $this->db
->result($qid))) {
70 $this->page_id
= $result[0];
71 $this->time
= $result[1];
72 $this->hostname
= $result[2];
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];
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
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';
114 $attributes = $this->mutable
* MUTABLE_ATTR +
115 $this->template
* TEMPLATE_ATTR
;
117 // get roolback information
118 $qry = "SELECT lastversion, lastversion_major, bodylength, " .
119 "attributes, createtime, updatetime " .
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'";
132 $qry .= ", updatetime=null" .
133 ", lastversion_major=$this->version";
135 $qry .= " WHERE id=$page_id";
136 $this->db
->query($qry);
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; }
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.
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]' " .
169 $qry = "DELETE FROM $PgTbl " .
172 $this->db
->query($qry);
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));
185 // Check if a user is subscribed to a page.
186 function isSubscribed($username)
190 $query = "SELECT count(*) " .
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)
208 if ($this->isSubscribed($username))
209 $this->db
->query("DELETE FROM $SuTbl " .
210 "WHERE page = '$this->dbname' " .
211 "AND username = '$username'");
213 $this->db
->query("INSERT INTO $SuTbl (page, username) " .
214 "VALUES ('$this->dbname', '$username')");
220 function getSubscribedUsers($skip_username = '')
224 $query = "SELECT username " .
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];