Fix #5661 - MTrack - daily email large and no branch
[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  
9
10
11 class MTrack_Wiki_Item 
12 {
13     
14     static $repo = false;
15     static $wc = null;
16     
17     public $pagename = null;
18     public $filename = null;
19     public $version = null;
20     public $file = null;
21     
22
23
24
25     static function commitNow()
26     {
27         //  force any delayed push to invoke right now 
28         self::$wc = null;
29         putenv("MTRACK_WIKI_COMMIT=");
30     }
31  
32     static function getWC()
33     {
34         if (self::$wc === null) {
35             self::getRepoAndRoot($repo);
36             self::$wc = $repo->getWorkingCopy();
37         }
38         return self::$wc;
39     }
40
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.
45         if (!self::$repo) {
46             throw new Exception("WIKI REPO IS NOT SET UP - MTrack_Wiki_Item::$repo must point to default/wiki");
47         }
48         $repo = self::$repo;
49         return self::$repo->getDefaultRoot();
50     }
51
52     static function index_item($object)
53     {
54         list($ignore, $ident) = explode(':', $object, 2);
55         $w = new MTrack_Wiki_Item($ident);
56
57         MTrackSearchDB::add("wiki:$w->pagename", array(
58           'wiki' => $w->content,
59           'who' => $w->who,
60           ), true);
61     }
62     
63     static function _get_parent_for_acl($objectid)
64     {
65         if (preg_match("/^(wiki:.*)\/([^\/]+)$/", $objectid, $M)) {
66           return $M[1];
67         }
68         if (preg_match("/^wiki:.*$/", $objectid, $M)) {
69           return 'Wiki';
70         }
71         return null;
72     }
73     
74     
75     function __construct($name, $version = null) {
76         $this->pagename = $name;
77         $this->filename = self::getRepoAndRoot($repo) . $name . '.wiki';
78        
79         //$suf = MTrackConfig::get('core', 'wikifilenamesuffix');
80         //var_dump($suf); exit;
81         //if ($suf) {
82         //  $this->filename .= $suf;
83         //}
84         
85         
86         if ($version !== null) {
87               $this->file = $repo->file($this->filename, 'rev', $version);
88         } else {
89               $this->file = $repo->file($this->filename);
90         }
91         if ($this->file && $repo->history($this->filename, 1)) {
92               $this->version = $this->file->rev;
93         } else {
94               $this->file = null;
95         } 
96     }
97     /*
98     function __get($name) //??? overload???/
99     {
100         throw new Exception("Overload is no going to work any more - it's just too confusing ");
101         
102         if ($name == 'content') {
103             $this->content = stream_get_contents($this->file->cat());
104             return $this->content;
105         }
106     }
107     
108     */
109     
110     
111     function save(MTrackChangeset $changeset)
112     {
113         $wc = self::getWC();
114         $lfilename = $this->pagename . '.wiki';;
115         //$suf = MTrackConfig::get('core', 'wikifilenamesuffix');
116         //if ($suf) {
117           //$lfilename .= $suf;
118         //}
119     
120         if (!strlen(trim($this->content))) {
121           if ($wc->file_exists($lfilename)) {
122             // removing
123             $wc->delFile($lfilename);
124           }
125         } else {
126           if (!$wc->file_exists($lfilename)) {
127             // handle dirs
128             $elements = explode('/', $lfilename);
129             $accum = array();
130             while (count($elements) > 1) {
131               $ent = array_shift($elements);
132               $accum[] = $ent;
133               $base = join(DIRECTORY_SEPARATOR, $accum);
134               if (!$wc->file_exists($base)) {
135                 if (!mkdir($wc->getDir() . DIRECTORY_SEPARATOR . $base)) {
136                   throw new Exception(
137                       "unable to mkdir(" . $wc->getDir() .
138                       DIRECTORY_SEPARATOR . "$base)");
139                 }
140                 $wc->addFile($base);
141               } else if (!is_dir($wc->getDir() . DIRECTORY_SEPARATOR . $base)) {
142                 throw new Exception("$base is not a dir; cannot create $lfilename");
143               }
144             }
145             file_put_contents($wc->getDir() . DIRECTORY_SEPARATOR . $lfilename,
146                 $this->content);
147             $wc->addFile($lfilename);
148           } else {
149             file_put_contents($wc->getDir() . DIRECTORY_SEPARATOR . $lfilename,
150                 $this->content);
151           }
152         }
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);
157     }
158
159     function toHTML()
160     {
161       return MTrack_Wiki::format_to_html($this->content);
162     }
163 }
164