]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageHistory.php
Two new convenience functions in lib/Theme.php: WikiLink() and Button().
[SourceForge/phpwiki.git] / lib / plugin / PageHistory.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PageHistory.php,v 1.17 2002-01-30 23:41:54 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 array(fmt("PageHistory for %s",
72                          WikiLink($this->_args['page'])),
73                      "\n",
74                      $this->rss_icon());
75     }
76
77     function _javascript($script) {
78         return HTML::script(array('language' => 'JavaScript',
79                                   'type'     => 'text/javascript'),
80                             new RawXml("<!-- //\n$script\n// -->"));
81     }
82
83     function description() {
84         // Doesn't work (PHP bug?): $desc = parent::description() . "\n";
85         $button = HTML::input(array('type'  => 'submit',
86                                     'value' => _("compare revisions")));
87         return array(_RecentChanges_HtmlFormatter::description(), "\n",
88                      $this->_javascript(sprintf('document.write("%s");',
89                                                 _("Check any two boxes to compare revisions."))),
90                      HTML::noscript(fmt("Check any two boxes then %s.", $button)));
91     }
92
93
94     function format ($changes) {
95         $this->_itemcount = 0;
96
97         $pagename = $this->_args['page'];
98
99         $html[] = _RecentChanges_HtmlFormatter::format($changes);
100
101         $html[] = HTML::input(array('type'  => 'hidden',
102                                     'name'  => 'action',
103                                     'value' => 'diff'));
104         if (USE_PATH_INFO) {
105             $action = WikiURL($pagename);
106         }
107         else {
108             $action = SCRIPT_NAME;
109             $html[] = HTML::input(array('type'  => 'hidden',
110                                         'name'  => 'pagename',
111                                         'value' => $pagename));
112         }
113
114         return HTML(HTML::form(array('method' => 'get',
115                                      'action' => $action,
116                                      'name'   => 'diff-select'),
117                                $html),
118                     "\n",
119                     $this->_javascript('
120         var diffCkBoxes = document.forms["diff-select"].elements["versions[]"];
121
122         function diffCkBox_onclick() {
123           // If two checkboxes are checked, submit form
124           var nchecked = 0;
125           for (i = 0; i < diffCkBoxes.length; i++)
126             if (diffCkBoxes[i].checked && ++nchecked >= 2)
127               this.form.submit();
128         }
129
130         for (i = 0; i < diffCkBoxes.length; i++)
131           diffCkBoxes[i].onclick = diffCkBox_onclick;'));
132     }
133     
134     function diffLink ($rev) {
135         return HTML::input(array('type'  => 'checkbox',
136                                  'name'  => 'versions[]',
137                                  'value' => $rev->getVersion()));
138     }
139
140     function pageLink ($rev) {
141         return HTML::a(array('href'  => $this->pageURL($rev),
142                              'class' => 'wiki'),
143                        fmt("Version %d", $rev->getVersion()));
144     }
145
146     function format_revision ($rev) {
147         $class = 'rc-' . $this->importance($rev);
148
149         $time = $this->time($rev);
150         if ($rev->get('is_minor_edit')) {
151             $minor_flag = HTML::small(HTML::em(" (" . _("minor edit") . ")"));
152         }
153         else {
154             $time = HTML::strong($time);
155             $minor_flag = '';
156         }
157         
158         return HTML::li(array('class' => $class),
159                         $this->diffLink($rev), ' ',
160                         $this->pageLink($rev), ' ',
161                         $time, ' ',
162                         $this->summaryAsHTML($rev),
163                         ' ... ',
164                         $this->authorLink($rev),
165                         $minor_flag);
166     }
167 }
168
169
170 class _PageHistory_RssFormatter
171 extends _RecentChanges_RssFormatter
172 {
173     function include_versions_in_URLs() {
174         return true;
175     }
176
177     function image_properties () {
178         return false;
179     }
180
181     function textinput_properties () {
182         return false;
183     }
184
185     function channel_properties () {
186         global $request;
187
188         $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
189
190         $title = sprintf("%s: %s",
191                          WIKI_NAME,
192                          split_pagename($this->_args['page']));
193
194         return array('title'          => $title,
195                      'dc:description' => _("History of changes."),
196                      'link'           => $rc_url,
197                      'dc:date'        => Iso8601DateTime(time()));
198     }
199
200
201     function item_properties ($rev) {
202         if (!($title = $this->summary($rev)))
203             $title = sprintf(_("Version %d"), $rev->getVersion());
204  
205         return array( 'title'           => $title,
206                       'link'            => $this->pageURL($rev),
207                       'dc:date'         => $this->time($rev),
208                       'dc:contributor'  => $rev->get('author'),
209                       'wiki:version'    => $rev->getVersion(),
210                       'wiki:importance' => $this->importance($rev),
211                       'wiki:status'     => $this->status($rev),
212                       'wiki:diff'       => $this->diffURL($rev),
213                       );
214     }
215 }
216
217 class WikiPlugin_PageHistory
218 extends WikiPlugin_RecentChanges
219 {
220     function getName () {
221         return _("PageHistory");
222     }
223
224     function getDescription () {
225         return sprintf(_("List PageHistory for %s"),'[pagename]');
226     }
227     
228     function getDefaultArguments() {
229         return array('days'             => false,
230                      'show_minor'       => true,
231                      'show_major'       => true,
232                      'limit'            => false,
233                      'page'             => '[pagename]',
234                      'format'           => false);
235     }
236
237     function getDefaultFormArguments() {
238         $dflts = WikiPlugin_RecentChanges::getDefaultFormArguments();
239         $dflts['textinput'] = 'page';
240         return $dflts;
241     }
242
243     function getMostRecentParams ($args) {
244         $params = WikiPlugin_RecentChanges::getMostRecentParams($args);
245         $params['include_all_revisions'] = true;
246         return $params;
247     }
248
249     function getChanges ($dbi, $args) {
250         $page = $dbi->getPage($args['page']);
251         $iter = $page->getAllRevisions();
252         $params = $this->getMostRecentParams($args);
253         return new _PageHistory_PageRevisionIter($iter, $params);
254     }
255
256     function format ($changes, $args) {
257         global $Theme;
258         $format = $args['format'];
259
260         $fmt_class = $Theme->getFormatter('PageHistory', $format);
261         if (!$fmt_class) {
262             if ($format == 'rss')
263                 $fmt_class = '_PageHistory_RssFormatter';
264             else
265                 $fmt_class = '_PageHistory_HtmlFormatter';
266         }
267
268         $fmt = new $fmt_class($args);
269         return $fmt->format($changes);
270     }
271
272     function run ($dbi, $argstr, $request) {
273         $args = $this->getArgs($argstr, $request);
274         if (empty($args['page']))
275             return $this->makeForm("", $request);
276         // Hack alert: format() is a NORETURN for rss formatters.
277         return $this->format($this->getChanges($dbi, $args), $args);
278     }
279 };
280
281
282 // (c-file-style: "gnu")
283 // Local Variables:
284 // mode: php
285 // tab-width: 8
286 // c-basic-offset: 4
287 // c-hanging-comment-ender-p: nil
288 // indent-tabs-mode: nil
289 // End:   
290 ?>