PageRenderTime 37ms CodeModel.GetById 6ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/wiki/models/talk.mod.php

http://awarenet.googlecode.com/
PHP | 347 lines | 200 code | 64 blank | 83 comment | 27 complexity | 6e5c4621a02f370be3d6284016cf1800 MD5 | raw file
Possible License(s): GPL-3.0
  1. <?
  2. require_once($kapenta->installPath . 'modules/wiki/inc/wikicode.class.php');
  3. //--------------------------------------------------------------------------------------------------
  4. //* object representing wiki talk pages
  5. //--------------------------------------------------------------------------------------------------
  6. //+ These are very similar to wiki articles
  7. class Wiki_Talk {
  8. //----------------------------------------------------------------------------------------------
  9. // member variables (as retieved from database)
  10. //----------------------------------------------------------------------------------------------
  11. var $data; //_ currently loaded database record [array]
  12. var $dbSchema; //_ database table definition [array]
  13. var $loaded; //_ set to true when an object has been loaded [bool]
  14. var $UID; //_ UID [string]
  15. var $title; //_ title [string]
  16. var $content; //_ text [string]
  17. var $nav; //_ text [string]
  18. var $locked; //_ varchar(30) [string]
  19. var $namespace; //_ varchar(30) [string]
  20. var $createdOn; //_ datetime [string]
  21. var $createdBy; //_ ref:Users_User [string]
  22. var $editedOn; //_ datetime [string]
  23. var $editedBy; //_ ref:Users_User [string]
  24. var $alias; //_ alias [string]
  25. var $wikicode;
  26. var $defaultIndexPage = 'modules/wiki/index.wiki.php'; // php enclosed wikicode [string]
  27. var $talk; //
  28. var $expanded = false; // [bool]
  29. //----------------------------------------------------------------------------------------------
  30. //. constructor
  31. //----------------------------------------------------------------------------------------------
  32. //opt: raUID - UID or alias of a Article object [string]
  33. function Wiki_Talk($raUID = '') {
  34. global $db;
  35. $this->dbSchema = $this->getDbSchema(); // initialise table schema
  36. $this->wikicode = new WikiCode();
  37. if ('' != $raUID) { $this->load($raUID); } // try load an object from the database
  38. if (false == $this->loaded) { // check if we did
  39. $this->data = $db->makeBlank($this->dbSchema); // make new object
  40. $this->loadArray($this->data); // initialize
  41. $this->title = 'New Article ' . $this->UID; // set default title
  42. $this->locked = 'user'; // TODO - make this a setting
  43. }
  44. }
  45. //----------------------------------------------------------------------------------------------
  46. //. load an object from the database given its UID or an alias
  47. //----------------------------------------------------------------------------------------------
  48. //arg: raUID - UID or alias of a Article object [string]
  49. //returns: true on success, false on failure [bool]
  50. function load($raUID = '') {
  51. global $db;
  52. $objary = $db->loadAlias($raUID, $this->dbSchema);
  53. if (false != $objary) { $this->loadArray($objary); return true; }
  54. return false;
  55. }
  56. //----------------------------------------------------------------------------------------------
  57. //. load Article object serialized as an associative array
  58. //----------------------------------------------------------------------------------------------
  59. //arg: ary - associative array of members and values [array]
  60. //returns: true on success, false on failure [bool]
  61. function loadArray($ary) {
  62. global $db;
  63. if (false == $db->validate($ary, $this->dbSchema)) { return false; }
  64. $this->UID = $ary['UID'];
  65. $this->title = $ary['title'];
  66. $this->content = $ary['content'];
  67. $this->nav = $ary['nav'];
  68. $this->locked = $ary['locked'];
  69. $this->namespace = $ary['namespace'];
  70. $this->createdOn = $ary['createdOn'];
  71. $this->createdBy = $ary['createdBy'];
  72. $this->editedOn = $ary['editedOn'];
  73. $this->editedBy = $ary['editedBy'];
  74. $this->alias = $ary['alias'];
  75. $this->loaded = true;
  76. $this->wikicode->source = $this->content;
  77. return true;
  78. }
  79. //----------------------------------------------------------------------------------------------
  80. //. save the current object to database
  81. //----------------------------------------------------------------------------------------------
  82. //returns: null string on success, html report of errors on failure [string]
  83. //: $db->save(...) will raise an object_updated event if successful
  84. function save() {
  85. global $db, $aliases;
  86. $report = $this->verify();
  87. if ('' != $report) { return $report; }
  88. $this->alias = $aliases->create('wiki', 'Wiki_Talk', $this->UID, $this->title);
  89. $check = $db->save($this->toArray(), $this->dbSchema);
  90. if (false == $check) { return "Database error.<br/>\n"; }
  91. return '';
  92. }
  93. //----------------------------------------------------------------------------------------------
  94. //. check that object is correct before allowing it to be stored in database
  95. //----------------------------------------------------------------------------------------------
  96. //returns: null string if object passes, warning message if not [string]
  97. function verify() {
  98. $report = '';
  99. if ('' == $this->UID) { $report .= "No UID.<br/>\n"; }
  100. return $report;
  101. }
  102. //----------------------------------------------------------------------------------------------
  103. //. database table definition and content versioning
  104. //----------------------------------------------------------------------------------------------
  105. //returns: information for constructing SQL queries [array]
  106. function getDbSchema() {
  107. $dbSchema = array();
  108. $dbSchema['module'] = 'wiki';
  109. $dbSchema['model'] = 'Wiki_Talk';
  110. $dbSchema['archive'] = 'yes';
  111. //table columns
  112. $dbSchema['fields'] = array(
  113. 'UID' => 'VARCHAR(33)',
  114. 'articleUID' => 'VARCHAR(255)',
  115. 'content' => 'TEXT',
  116. 'nav' => 'TEXT',
  117. 'locked' => 'VARCHAR(30)',
  118. 'namespace' => 'VARCHAR(33)',
  119. 'createdOn' => 'DATETIME',
  120. 'createdBy' => 'VARCHAR(33)',
  121. 'editedOn' => 'DATETIME',
  122. 'editedBy' => 'VARCHAR(33)',
  123. 'alias' => 'VARCHAR(255)' );
  124. //these fields will be indexed
  125. $dbSchema['indices'] = array(
  126. 'UID' => '10',
  127. 'createdOn' => '',
  128. 'createdBy' => '10',
  129. 'editedOn' => '',
  130. 'editedBy' => '10',
  131. 'alias' => '10' );
  132. //revision history kept by wiki module, not default revision system
  133. $dbSchema['nodiff'] = array(
  134. 'UID',
  135. 'title',
  136. 'content',
  137. 'nav',
  138. 'locked',
  139. 'namespace',
  140. 'createdOn',
  141. 'createdBy',
  142. 'editedOn',
  143. 'editedBy',
  144. 'alias'
  145. );
  146. return $dbSchema;
  147. }
  148. //----------------------------------------------------------------------------------------------
  149. //. serialize this object to an array
  150. //----------------------------------------------------------------------------------------------
  151. //returns: associative array of all members which define this instance [array]
  152. function toArray() {
  153. $serialize = array(
  154. 'UID' => $this->UID,
  155. 'title' => $this->title,
  156. 'content' => $this->content,
  157. 'nav' => $this->nav,
  158. 'locked' => $this->locked,
  159. 'namespace' => $this->namespace,
  160. 'createdOn' => $this->createdOn,
  161. 'createdBy' => $this->createdBy,
  162. 'editedOn' => $this->editedOn,
  163. 'editedBy' => $this->editedBy,
  164. 'alias' => $this->alias
  165. );
  166. return $serialize;
  167. }
  168. //----------------------------------------------------------------------------------------------
  169. //. make an extended array of all data a view will need
  170. //----------------------------------------------------------------------------------------------
  171. //returns: extended array of member variables and metadata [array]
  172. function extArray() {
  173. global $kapenta;
  174. global $user;
  175. global $theme;
  176. $ary = $this->toArray(); //% return value [dict]
  177. $ary['editUrl'] = ''; $ary['editLink'] = ''; // edit
  178. $ary['viewUrl'] = ''; $ary['viewLink'] = ''; // view
  179. $ary['newUrl'] = ''; $ary['newLink'] = ''; // new article form
  180. $ary['nomUrl'] = ''; $ary['nomLink'] = ''; // nominate for deletion
  181. $ary['delUrl'] = ''; $ary['delLink'] = ''; // delete
  182. //------------------------------------------------------------------------------------------
  183. // links
  184. //------------------------------------------------------------------------------------------
  185. if ($user->authHas('wiki', 'Wiki_Talk', 'show', $this->UID)) {
  186. $ary['viewUrl'] = '%%serverPath%%wiki/' . $ary['alias'];
  187. $ary['viewLink'] = "<a href='%%serverPath%%wiki/" . $ary['alias'] . "'>"
  188. . "[read on &gt;&gt;]</a>";
  189. }
  190. if ($user->authHas('wiki', 'Wiki_Talk', 'edit', $this->UID)) {
  191. $ary['editUrl'] = '%%serverPath%%wiki/edit/' . $this->alias;
  192. $ary['editLink'] = "<a href='" . $ary['editUrl'] . "'>[edit]</a>";
  193. $ary['nomUrl'] = '%%serverPath%%wiki/nominatedelete/' . $this->alias;
  194. $ary['nomLink'] = "<a href='" . $ary['nomUrl'] . "'>[nominate for deletion]</a>";
  195. }
  196. if ($user->authHas('wiki', 'Wiki_Talk', 'delete', $this->UID)) {
  197. $ary['delUrl'] = '%%serverPath%%wiki/confirmdelete/uid_' . $this->UID;
  198. $ary['delLink'] = "<a href='" . $ary['delUrl'] . "'>[delete]</a>";
  199. }
  200. if ($user->authHas('wiki', 'Wiki_Talk', 'new', $this->UID)) {
  201. $ary['newUrl'] = "%%serverPath%%wiki/new/";
  202. $ary['newLink'] = "<a href='" . $ary['newUrl'] . "'>[create new article]</a>";
  203. }
  204. //------------------------------------------------------------------------------------------
  205. // sanitize content for editing
  206. //------------------------------------------------------------------------------------------
  207. $ary['contentSafe'] = str_replace('[[:', '[[%%delme%%:', $ary['content']);
  208. $ary['navSafe'] = str_replace('[[:', '[[%%delme%%:', $ary['nav']);
  209. //------------------------------------------------------------------------------------------
  210. // strandardise date format to previous website
  211. //------------------------------------------------------------------------------------------
  212. $ary['createdOnLong'] = $kapenta->longDate($ary['createdOn']);
  213. $ary['editedOnLong'] = $kapenta->longDate($ary['editedOn']);
  214. $ary['titleUpper'] = strtoupper($ary['title']);
  215. //------------------------------------------------------------------------------------------
  216. // if the wikicode has been expanded, compile contents block, document, languages, etc
  217. //------------------------------------------------------------------------------------------
  218. $ary['contents'] = '';
  219. $ary['contentHtml'] = '';
  220. $ary['talkContents'] = '';
  221. $ary['talkHtml'] = '';
  222. $ary['infobox'] = '';
  223. $ary['interlingua'] = ''; // TODO
  224. $ary['seealso'] = ''; // TODO
  225. $ary['references'] = ''; // TODO
  226. if (count($this->expanded) == true) {
  227. $ary['contents'] = $this->wikicode->contents;
  228. $ary['contentHtml'] = $theme->expandBlocks($this->wikicode->html, '');
  229. $ary['infobox'] = $this->wikicode->infobox;
  230. //$ary['talkContents'] = $this->talk->contents; // TODO - stabilize
  231. //$ary['talkHtml'] = $this->talk->html;
  232. }
  233. //------------------------------------------------------------------------------------------
  234. // avoid collision with page title
  235. //------------------------------------------------------------------------------------------
  236. $ary['articleTitle'] = $ary['title'];
  237. //------------------------------------------------------------------------------------------
  238. // summary
  239. //------------------------------------------------------------------------------------------
  240. //$ary['summary'] = strip_tags($ary['content']);
  241. //$ary['summary'] = substr($ary['summary'], 0, 1000) . '...';
  242. return $ary;
  243. }
  244. //----------------------------------------------------------------------------------------------
  245. //. expand wiki text and talk pages
  246. //----------------------------------------------------------------------------------------------
  247. function expandWikiCode() {
  248. $this->wikicode = new WikiCode();
  249. //$this->content->source = $this->content;
  250. $this->wikicode->source = $this->content;
  251. $this->wikicode->navsource = $this->nav;
  252. $this->wikicode->expandWikiCode();
  253. //$this->talk = new WikiCode();
  254. //$this->talk->source = $this->talk;
  255. //$this->talk->expandWikiCode();
  256. $this->expanded = true;
  257. }
  258. //----------------------------------------------------------------------------------------------
  259. //. set this article to be the default page
  260. //----------------------------------------------------------------------------------------------
  261. //returns: true on success, false on failure [bool]
  262. function mkDefault() {
  263. global $kapenta, $user;
  264. $this->title = 'Index';
  265. $raw = $kapenta->fileGetContents($this->defaultIndexPage, false, true);
  266. if (false == $raw) { $raw = "(editable by admins only)"; }
  267. $this->content = $raw;
  268. $this->locked = 'admin';
  269. return true;
  270. }
  271. //----------------------------------------------------------------------------------------------
  272. //. delete current object from the database
  273. //----------------------------------------------------------------------------------------------
  274. //: $db->delete(...) will raise an object_deleted event on success [bool]
  275. //returns: true on success, false on failure [bool]
  276. function delete() {
  277. global $db;
  278. if (false == $this->loaded) { return false; } // nothing to do
  279. if (false == $db->delete($this->UID, $this->dbSchema)) { return false; }
  280. return true;
  281. }
  282. }
  283. ?>