1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
6 require_once 'MTrack/SearchDB.php';
7 require_once 'MTrack/CommitChecker.php';
8 require_once 'MTrack/ACL.php';
12 class MTrack_Wiki_Item
14 public $pagename = null;
15 public $filename = null;
16 public $version = null;
22 static function commitNow() {
23 /* force any delayed push to invoke right now */
25 putenv("MTRACK_WIKI_COMMIT=");
28 static function loadByPageName($name) {
29 $w = new MTrack_Wiki_Item($name);
36 static function getWC() {
37 if (self::$wc === null) {
38 self::getRepoAndRoot($repo);
39 self::$wc = $repo->getWorkingCopy();
44 static function getRepoAndRoot(&$repo) {
45 $repo = MTrackRepo::loadByName('default/wiki');
46 return $repo->getDefaultRoot();
49 static function index_item($object)
51 list($ignore, $ident) = explode(':', $object, 2);
52 $w = MTrack_Wiki_Item::loadByPageName($ident);
54 MTrackSearchDB::add("wiki:$w->pagename", array(
55 'wiki' => $w->content,
59 static function _get_parent_for_acl($objectid) {
60 if (preg_match("/^(wiki:.*)\/([^\/]+)$/", $objectid, $M)) {
63 if (preg_match("/^wiki:.*$/", $objectid, $M)) {
68 function __get($name) {
69 if ($name == 'content') {
70 $this->content = stream_get_contents($this->file->cat());
71 return $this->content;
74 function __construct($name, $version = null) {
75 $this->pagename = $name;
76 $this->filename = self::getRepoAndRoot($repo) . $name;
77 $suf = MTrackConfig::get('core', 'wikifilenamesuffix');
79 $this->filename .= $suf;
82 if ($version !== null) {
83 $this->file = $repo->file($this->filename, 'rev', $version);
85 $this->file = $repo->file($this->filename);
87 if ($this->file && $repo->history($this->filename, 1)) {
88 $this->version = $this->file->rev;
94 function save(MTrackChangeset $changeset) {
96 $lfilename = $this->pagename;
97 $suf = MTrackConfig::get('core', 'wikifilenamesuffix');
102 if (!strlen(trim($this->content))) {
103 if ($wc->file_exists($lfilename)) {
105 $wc->delFile($lfilename);
108 if (!$wc->file_exists($lfilename)) {
110 $elements = explode('/', $lfilename);
112 while (count($elements) > 1) {
113 $ent = array_shift($elements);
115 $base = join(DIRECTORY_SEPARATOR, $accum);
116 if (!$wc->file_exists($base)) {
117 if (!mkdir($wc->getDir() . DIRECTORY_SEPARATOR . $base)) {
119 "unable to mkdir(" . $wc->getDir() .
120 DIRECTORY_SEPARATOR . "$base)");
123 } else if (!is_dir($wc->getDir() . DIRECTORY_SEPARATOR . $base)) {
124 throw new Exception("$base is not a dir; cannot create $lfilename");
127 file_put_contents($wc->getDir() . DIRECTORY_SEPARATOR . $lfilename,
129 $wc->addFile($lfilename);
131 file_put_contents($wc->getDir() . DIRECTORY_SEPARATOR . $lfilename,
135 /* use an env var to signal to the commit hook that it does not
136 * need to make a changeset for this commit */
137 putenv("MTRACK_WIKI_COMMIT=1");
138 $wc->commit($changeset);
143 return MTrack_Wiki::format_to_html($this->content);