]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageHistory.php
Eliminate RC_SEPARATOR_{A,B} in favor of messy but general
[SourceForge/phpwiki.git] / lib / plugin / PageHistory.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PageHistory.php,v 1.6 2002-01-18 00:28:43 dairiki Exp $');
3 /**
4  */
5 require_once('lib/plugin/RecentChanges.php');
6
7 class _PageHistory_PageRevisionIter
8     extends WikiDB_PageRevisionIterator
9 {
10     function _PageHistory_PageRevisionIter($rev_iter, $params) {
11
12         $this->_iter = $rev_iter;
13
14         extract($params);
15
16         if (isset($since))
17             $this->_since = $since;
18
19         $this->_include_major = empty($exclude_major_revisions);
20         if (! $this->_include_major)
21             $this->_include_minor = true;
22         else
23             $this->_include_minor = !empty($include_minor_revisions);
24         
25         if (empty($include_all_revisions))
26             $this->_limit = 1;
27         else if (isset($limit))
28             $this->_limit = $limit;
29     }
30
31     function next() {
32         if (!$this->_iter)
33             return false;
34         
35         if (isset($this->_limit)) {
36             if ($this->_limit <= 0) {
37                 $this->free();
38                 return false;
39             }
40             $this->_limit--;
41         }
42         
43         while ( ($rev = $this->_iter->next()) ) {
44             if (isset($this->_since) && $rev->get('mtime') < $this->_since) {
45                 $this->free();
46                 return false;
47             }
48             if ($rev->get('is_minor_edit') ? $this->_include_minor : $this->_include_major)
49                 return $rev;
50         }
51         return false;
52     }
53
54     
55     function free() {
56         if ($this->_iter)
57             $this->_iter->free();
58         $this->_iter = false;
59     }
60 }
61
62
63 class _PageHistory_HtmlFormatter
64 extends _RecentChanges_HtmlFormatter
65 {
66     function include_versions_in_URLs() {
67         return true;
68     }
69
70     function title() {
71         return sprintf(_("PageHistory for %s"),
72                        LinkExistingWikiWord($this->_args['page']))
73             . "\n" . $this->rss_icon();
74     }
75
76     function _javascript($script) {
77         return Element('script', array('language' => 'JavaScript'),
78                        "<!-- //\n$script\n// -->");
79     }
80     
81     function description() {
82         // Doesn't work (PHP bug?): $desc = parent::description() . "\n";
83         $desc = _RecentChanges_HtmlFormatter::description() . "\n";
84
85         $desc .= $this->_javascript(sprintf('document.write("%s");',
86                                             _("Check any two boxes to compare revisions.")));
87         $button = Element('input', array('type' => 'submit',
88                                          'value' => _("compare revisions")));
89         $desc .= Element('noscript',
90                          sprintf(_("Check any two boxes then %s."), $button));
91
92         return $desc;
93     }
94     
95                          
96     function format ($changes) {
97         $this->_itemcount = 0;
98         
99         $pagename = $this->_args['page'];
100         
101         $html[] = _RecentChanges_HtmlFormatter::format($changes);
102
103         $html[] = Element('input', array('type' => 'hidden',
104                                          'name' => 'action',
105                                          'value' => 'diff'));
106         if (USE_PATH_INFO) {
107             $action = WikiURL($pagename);
108         }
109         else {
110             $action = SCRIPT_NAME;
111             $html[] = Element('input', array('type' => 'hidden',
112                                              'name' => 'pagename',
113                                              'value' => $pagename));
114         }
115
116         return Element('form', array('method' => 'get',
117                                      'action' => $action,
118                                      'name' => 'diff-select'),
119                        join("\n", $html))
120             . "\n"
121             . $this->_javascript('
122         var diffCkBoxes = document.forms["diff-select"].elements["versions[]"];
123
124         function diffCkBox_onclick() {
125           // If two checkboxes are checked, submit form
126           var nchecked = 0;
127           for (i = 0; i < diffCkBoxes.length; i++)
128             if (diffCkBoxes[i].checked && ++nchecked >= 2)
129               this.form.submit();
130         }
131
132         for (i = 0; i < diffCkBoxes.length; i++)
133           diffCkBoxes[i].onclick = diffCkBox_onclick;');
134     }
135     
136     function diffLink ($rev) {
137         return Element('input', array('type' => 'checkbox',
138                                       'name' => 'versions[]',
139                                       'value' => $rev->getVersion()));
140     }
141
142     function pageLink ($rev) {
143         return QElement('a', array('href' => $this->pageURL($rev), 'class' => 'wiki'),
144                         sprintf(_("Version %d"), $rev->getVersion()));
145     }
146 }
147
148
149 class _PageHistory_RssFormatter
150 extends _RecentChanges_RssFormatter
151 {
152     function include_versions_in_URLs() {
153         return true;
154     }
155
156     function image_properties () {
157         return false;
158     }
159
160     function textinput_properties () {
161         return false;
162     }
163     
164     function channel_properties () {
165         global $request;
166
167         $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
168
169         $title = sprintf("%s: %s",
170                          WIKI_NAME,
171                          split_pagename($this->_args['page']));
172         
173         return array('title' => $title,
174                      'dc:description' => _("History of changes."),
175                      'link' => $rc_url,
176                      'dc:date' => Iso8601DateTime(time()));
177     }
178     
179
180     function item_properties ($rev) {
181         if (!($title = $this->summary($rev)))
182             $title = sprintf(_("Version %d"), $rev->getVersion());
183         
184         return array( 'title'           => $title,
185                       'link'            => $this->pageURL($rev),
186                       'dc:date'         => $this->time($rev),
187                       'dc:contributor'  => $rev->get('author'),
188                       'wiki:version'    => $rev->getVersion(),
189                       'wiki:importance' => $this->importance($rev),
190                       'wiki:status'     => $this->status($rev),
191                       'wiki:diff'       => $this->diffURL($rev),
192                       );
193     }
194 }
195
196 class WikiPlugin_PageHistory
197 extends WikiPlugin_RecentChanges
198 {
199     function getName () {
200         return _("PageHistory");
201     }
202
203     function getDescription () {
204         return sprintf(_("List PageHistory for %s"),'[pagename]');
205     }
206     
207     function getDefaultArguments() {
208         return array('days'             => false,
209                      'show_minor'       => true,
210                      'show_major'       => true,
211                      'limit'            => false,
212                      'page'             => false,
213                      'format'           => false);
214     }
215
216     function getDefaultFormArguments() {
217         $dflts = WikiPlugin_RecentChanges::getDefaultFormArguments();
218         $dflts['textinput'] = 'page';
219         return $dflts;
220     }
221
222     function getMostRecentParams ($args) {
223         $params = WikiPlugin_RecentChanges::getMostRecentParams($args);
224         $params['include_all_revisions'] = true;
225         return $params;
226     }
227
228     function getChanges ($dbi, $args) {
229         $page = $dbi->getPage($args['page']);
230         $iter = $page->getAllRevisions();
231         $params = $this->getMostRecentParams($args);
232         return new _PageHistory_PageRevisionIter($iter, $params);
233     }
234
235     function format ($changes, $args) {
236         global $Theme;
237         $format = $args['format'];
238         
239         $fmt_class = $Theme->getFormatter('PageHistory', $format);
240         if (!$fmt_class) {
241             if ($format == 'rss')
242                 $fmt_class = '_PageHistory_RssFormatter';
243             else
244                 $fmt_class = '_PageHistory_HtmlFormatter';
245         }
246
247         $fmt = new $fmt_class($args);
248         return $fmt->format($changes);
249     }
250
251     function run ($dbi, $argstr, $request) {
252         $args = $this->getArgs($argstr, $request);
253         if (empty($args['page']))
254             return $this->makeForm("", $request);
255         // Hack alert: format() is a NORETURN for rss formatters.
256         return $this->format($this->getChanges($dbi, $args), $args);
257     }
258 };
259
260
261 // (c-file-style: "gnu")
262 // Local Variables:
263 // mode: php
264 // tab-width: 8
265 // c-basic-offset: 4
266 // c-hanging-comment-ender-p: nil
267 // indent-tabs-mode: nil
268 // End:   
269 ?>