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';
11 class MTrack_Wiki_Item
17 public $pagename = null;
18 public $filename = null;
19 public $version = null;
25 static function commitNow()
27 // force any delayed push to invoke right now
29 putenv("MTRACK_WIKI_COMMIT=");
32 static function getWC()
34 if (self::$wc === null) {
35 self::getRepoAndRoot($repo);
36 self::$wc = $repo->getWorkingCopy();
41 static function getRepoAndRoot(&$repo) {
42 // $repo = MTrack_Repo::factory(array('default/wiki');
43 // <<< difficult... Frontend can set up the REPO object,
44 // but we should not be able to call frontend.
46 throw new Exception("WIKI REPO IS NOT SET UP - MTrack_Wiki_Item::$repo must point to default/wiki");
49 return self::$repo->getDefaultRoot();
52 static function index_item($object)
54 list($ignore, $ident) = explode(':', $object, 2);
55 $w = new MTrack_Wiki_Item($ident);
57 MTrackSearchDB::add("wiki:$w->pagename", array(
58 'wiki' => $w->content,
63 static function _get_parent_for_acl($objectid)
65 if (preg_match("/^(wiki:.*)\/([^\/]+)$/", $objectid, $M)) {
68 if (preg_match("/^wiki:.*$/", $objectid, $M)) {
75 function __construct($name, $version = null) {
76 $this->pagename = $name;
77 $this->filename = self::getRepoAndRoot($repo) . $name . '.wiki';
79 //$suf = MTrackConfig::get('core', 'wikifilenamesuffix');
80 //var_dump($suf); exit;
82 // $this->filename .= $suf;
86 if ($version !== null) {
87 $this->file = $repo->file($this->filename, 'rev', $version);
89 $this->file = $repo->file($this->filename);
91 if ($this->file && $repo->history($this->filename, 1)) {
92 $this->version = $this->file->rev;
98 function __get($name) //??? overload???/
100 throw new Exception("Overload is no going to work any more - it's just too confusing ");
102 if ($name == 'content') {
103 $this->content = stream_get_contents($this->file->cat());
104 return $this->content;
111 function save(MTrackChangeset $changeset)
114 $lfilename = $this->pagename . '.wiki';;
115 //$suf = MTrackConfig::get('core', 'wikifilenamesuffix');
117 //$lfilename .= $suf;
120 if (!strlen(trim($this->content))) {
121 if ($wc->file_exists($lfilename)) {
123 $wc->delFile($lfilename);
126 if (!$wc->file_exists($lfilename)) {
128 $elements = explode('/', $lfilename);
130 while (count($elements) > 1) {
131 $ent = array_shift($elements);
133 $base = join(DIRECTORY_SEPARATOR, $accum);
134 if (!$wc->file_exists($base)) {
135 if (!mkdir($wc->getDir() . DIRECTORY_SEPARATOR . $base)) {
137 "unable to mkdir(" . $wc->getDir() .
138 DIRECTORY_SEPARATOR . "$base)");
141 } else if (!is_dir($wc->getDir() . DIRECTORY_SEPARATOR . $base)) {
142 throw new Exception("$base is not a dir; cannot create $lfilename");
145 file_put_contents($wc->getDir() . DIRECTORY_SEPARATOR . $lfilename,
147 $wc->addFile($lfilename);
149 file_put_contents($wc->getDir() . DIRECTORY_SEPARATOR . $lfilename,
153 /* use an env var to signal to the commit hook that it does not
154 * need to make a changeset for this commit */
155 putenv("MTRACK_WIKI_COMMIT=1");
156 $wc->commit($changeset);
161 return MTrack_Wiki::format_to_html($this->content);