final move of files
[web.mtrack] / MTrack / Wiki / Item.php
1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
3
4 // register..
5
6 require_once 'MTrack/SearchDB.php';
7 require_once 'MTrack/CommitChecker.php';
8 require_once 'MTrack/ACL.php';
9
10
11
12 class MTrack_Wiki_Item 
13 {
14   public $pagename = null;
15   public $filename = null;
16   public $version = null;
17   public $file = null;
18   static $wc = null;
19
20
21
22   static function commitNow() {
23     /* force any delayed push to invoke right now */
24     self::$wc = null;
25     putenv("MTRACK_WIKI_COMMIT=");
26   }
27
28   static function loadByPageName($name) {
29     $w = new MTrack_Wiki_Item($name);
30     if ($w->file) {
31       return $w;
32     }
33     return null;
34   }
35
36   static function getWC() {
37     if (self::$wc === null) {
38       self::getRepoAndRoot($repo);
39       self::$wc = $repo->getWorkingCopy();
40     }
41     return self::$wc;
42   }
43
44   static function getRepoAndRoot(&$repo) {
45     $repo = MTrackRepo::loadByName('default/wiki');
46     return $repo->getDefaultRoot();
47   }
48
49   static function index_item($object)
50   {
51     list($ignore, $ident) = explode(':', $object, 2);
52     $w = MTrack_Wiki_Item::loadByPageName($ident);
53
54     MTrackSearchDB::add("wiki:$w->pagename", array(
55       'wiki' => $w->content,
56       'who' => $w->who,
57       ), true);
58   }
59   static function _get_parent_for_acl($objectid) {
60     if (preg_match("/^(wiki:.*)\/([^\/]+)$/", $objectid, $M)) {
61       return $M[1];
62     }
63     if (preg_match("/^wiki:.*$/", $objectid, $M)) {
64       return 'Wiki';
65     }
66     return null;
67   }
68   function __get($name) {
69     if ($name == 'content') {
70       $this->content = stream_get_contents($this->file->cat());
71       return $this->content;
72     }
73   }
74   function __construct($name, $version = null) {
75     $this->pagename = $name;
76     $this->filename = self::getRepoAndRoot($repo) . $name;
77     $suf = MTrackConfig::get('core', 'wikifilenamesuffix');
78     if ($suf) {
79       $this->filename .= $suf;
80     }
81
82     if ($version !== null) {
83       $this->file = $repo->file($this->filename, 'rev', $version);
84     } else {
85       $this->file = $repo->file($this->filename);
86     }
87     if ($this->file && $repo->history($this->filename, 1)) {
88       $this->version = $this->file->rev;
89     } else {
90       $this->file = null;
91     }
92   }
93
94   function save(MTrackChangeset $changeset) {
95     $wc = self::getWC();
96     $lfilename = $this->pagename;
97     $suf = MTrackConfig::get('core', 'wikifilenamesuffix');
98     if ($suf) {
99       $lfilename .= $suf;
100     }
101
102     if (!strlen(trim($this->content))) {
103       if ($wc->file_exists($lfilename)) {
104         // removing
105         $wc->delFile($lfilename);
106       }
107     } else {
108       if (!$wc->file_exists($lfilename)) {
109         // handle dirs
110         $elements = explode('/', $lfilename);
111         $accum = array();
112         while (count($elements) > 1) {
113           $ent = array_shift($elements);
114           $accum[] = $ent;
115           $base = join(DIRECTORY_SEPARATOR, $accum);
116           if (!$wc->file_exists($base)) {
117             if (!mkdir($wc->getDir() . DIRECTORY_SEPARATOR . $base)) {
118               throw new Exception(
119                   "unable to mkdir(" . $wc->getDir() .
120                   DIRECTORY_SEPARATOR . "$base)");
121             }
122             $wc->addFile($base);
123           } else if (!is_dir($wc->getDir() . DIRECTORY_SEPARATOR . $base)) {
124             throw new Exception("$base is not a dir; cannot create $lfilename");
125           }
126         }
127         file_put_contents($wc->getDir() . DIRECTORY_SEPARATOR . $lfilename,
128             $this->content);
129         $wc->addFile($lfilename);
130       } else {
131         file_put_contents($wc->getDir() . DIRECTORY_SEPARATOR . $lfilename,
132             $this->content);
133       }
134     }
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);
139   }
140
141   function toHTML()
142   {
143       return MTrack_Wiki::format_to_html($this->content);
144   }
145 }
146