]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageHistory.php
Javascript fix.
[SourceForge/phpwiki.git] / lib / plugin / PageHistory.php
1 <?php // -*-php-*-
2 rcs_id('$Id: PageHistory.php,v 1.26 2003-02-27 21:15:14 dairiki Exp $');
3 /**
4  Copyright 1999, 2000, 2001, 2002 $ThePhpWikiProgrammingTeam
5
6  This file is part of PhpWiki.
7
8  PhpWiki is free software; you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  PhpWiki is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with PhpWiki; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23 /**
24  */
25 require_once('lib/plugin/RecentChanges.php');
26
27 class _PageHistory_PageRevisionIter
28 extends WikiDB_PageRevisionIterator
29 {
30     function _PageHistory_PageRevisionIter($rev_iter, $params) {
31
32         $this->_iter = $rev_iter;
33
34         extract($params);
35
36         if (isset($since))
37             $this->_since = $since;
38
39         $this->_include_major = empty($exclude_major_revisions);
40         if (! $this->_include_major)
41             $this->_include_minor = true;
42         else
43             $this->_include_minor = !empty($include_minor_revisions);
44
45         if (empty($include_all_revisions))
46             $this->_limit = 1;
47         else if (isset($limit))
48             $this->_limit = $limit;
49     }
50
51     function next() {
52         if (!$this->_iter)
53             return false;
54
55         if (isset($this->_limit)) {
56             if ($this->_limit <= 0) {
57                 $this->free();
58                 return false;
59             }
60             $this->_limit--;
61         }
62
63         while ( ($rev = $this->_iter->next()) ) {
64             if (isset($this->_since) && $rev->get('mtime') < $this->_since) {
65                 $this->free();
66                 return false;
67             }
68             if ($rev->get('is_minor_edit') ? $this->_include_minor : $this->_include_major)
69                 return $rev;
70         }
71         return false;
72     }
73
74
75     function free() {
76         if ($this->_iter)
77             $this->_iter->free();
78         $this->_iter = false;
79     }
80 }
81
82
83 class _PageHistory_HtmlFormatter
84 extends _RecentChanges_HtmlFormatter
85 {
86     function include_versions_in_URLs() {
87         return true;
88     }
89
90     function title() {
91         return array(fmt("PageHistory for %s",
92                          WikiLink($this->_args['page'])),
93                      "\n",
94                      $this->rss_icon());
95     }
96
97     function empty_message () {
98         return _("No revisions found");
99     }
100
101     function _javascript($script) {
102         return HTML::script(array('language' => 'JavaScript',
103                                   'type'     => 'text/javascript'),
104                             new RawXml("<!-- //\n$script\n// -->"));
105     }
106
107     function description() {
108         // Doesn't work (PHP bug?): $desc = parent::description() . "\n";
109         $button = HTML::input(array('type'  => 'submit',
110                                     'value' => _("compare revisions"),
111                                     'class' => 'wikiaction'));
112         return array(_RecentChanges_HtmlFormatter::description(), "\n",
113                      $this->_javascript(sprintf('document.write("%s");',
114                                                 _("Check any two boxes to compare revisions."))),
115                      HTML::noscript(fmt("Check any two boxes then %s.", $button)));
116     }
117
118
119     function format ($changes) {
120         $this->_itemcount = 0;
121
122         $pagename = $this->_args['page'];
123
124         $html[] = _RecentChanges_HtmlFormatter::format($changes);
125
126         $html[] = HTML::input(array('type'  => 'hidden',
127                                     'name'  => 'action',
128                                     'value' => 'diff'));
129         if (USE_PATH_INFO) {
130             $action = WikiURL($pagename);
131         }
132         else {
133             $action = SCRIPT_NAME;
134             $html[] = HTML::input(array('type'  => 'hidden',
135                                         'name'  => 'pagename',
136                                         'value' => $pagename));
137         }
138
139         return HTML(HTML::form(array('method' => 'get',
140                                      'action' => $action,
141                                      'name'   => 'diff-select'),
142                                $html),
143                     "\n",
144                     $this->_javascript('
145         var diffCkBoxes = document.forms["diff-select"].elements["versions[]"];
146
147         function diffCkBox_onclick() {
148             var nchecked = 0, box = diffCkBoxes;
149             for (i = 0; i < box.length; i++)
150                 if (box[i].checked) nchecked++;
151             if (nchecked == 2)
152                 this.form.submit();
153             else if (nchecked > 2) {
154                 for (i = 0; i < box.length; i++)
155                     if (box[i] != this) box[i].checked = 0;
156             }
157         }
158
159         for (i = 0; i < diffCkBoxes.length; i++)
160             diffCkBoxes[i].onclick = diffCkBox_onclick;'));
161     }
162
163     function diffLink ($rev) {
164         return HTML::input(array('type'  => 'checkbox',
165                                  'name'  => 'versions[]',
166                                  'value' => $rev->getVersion()));
167     }
168
169     function pageLink ($rev) {
170         $text = fmt("Version %d", $rev->getVersion());
171         return _RecentChanges_HtmlFormatter::pageLink($rev, $text);
172     }
173
174     function format_revision ($rev) {
175         $class = 'rc-' . $this->importance($rev);
176
177         $time = $this->time($rev);
178         if ($rev->get('is_minor_edit')) {
179             $minor_flag = HTML(" ",
180                                HTML::span(array('class' => 'pageinfo-minoredit'),
181                                           "(" . _("minor edit") . ")"));
182         }
183         else {
184             $time = HTML::strong(array('class' => 'pageinfo-majoredit'), $time);
185             $minor_flag = '';
186         }
187
188         return HTML::li(array('class' => $class),
189                         $this->diffLink($rev), ' ',
190                         $this->pageLink($rev), ' ',
191                         $time, ' ',
192                         $this->summaryAsHTML($rev),
193                         ' ... ',
194                         $this->authorLink($rev),
195                         $minor_flag);
196     }
197 }
198
199
200 class _PageHistory_RssFormatter
201 extends _RecentChanges_RssFormatter
202 {
203     function include_versions_in_URLs() {
204         return true;
205     }
206
207     function image_properties () {
208         return false;
209     }
210
211     function textinput_properties () {
212         return false;
213     }
214
215     function channel_properties () {
216         global $request;
217
218         $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
219
220         $title = sprintf(_("%s: %s"),
221                          WIKI_NAME,
222                          split_pagename($this->_args['page']));
223
224         return array('title'          => $title,
225                      'dc:description' => _("History of changes."),
226                      'link'           => $rc_url,
227                      'dc:date'        => Iso8601DateTime(time()));
228     }
229
230
231     function item_properties ($rev) {
232         if (!($title = $this->summary($rev)))
233             $title = sprintf(_("Version %d"), $rev->getVersion());
234
235         return array( 'title'           => $title,
236                       'link'            => $this->pageURL($rev),
237                       'dc:date'         => $this->time($rev),
238                       'dc:contributor'  => $rev->get('author'),
239                       'wiki:version'    => $rev->getVersion(),
240                       'wiki:importance' => $this->importance($rev),
241                       'wiki:status'     => $this->status($rev),
242                       'wiki:diff'       => $this->diffURL($rev),
243                       );
244     }
245 }
246
247 class WikiPlugin_PageHistory
248 extends WikiPlugin_RecentChanges
249 {
250     function getName () {
251         return _("PageHistory");
252     }
253
254     function getDescription () {
255         return sprintf(_("List PageHistory for %s"),'[pagename]');
256     }
257
258     function getVersion() {
259         return preg_replace("/[Revision: $]/", '',
260                             "\$Revision: 1.26 $");
261     }
262
263     function getDefaultArguments() {
264         return array('days'         => false,
265                      'show_minor'   => true,
266                      'show_major'   => true,
267                      'limit'        => false,
268                      'page'         => '[pagename]',
269                      'format'       => false);
270     }
271
272     function getDefaultFormArguments() {
273         $dflts = WikiPlugin_RecentChanges::getDefaultFormArguments();
274         $dflts['textinput'] = 'page';
275         return $dflts;
276     }
277
278     function getMostRecentParams ($args) {
279         $params = WikiPlugin_RecentChanges::getMostRecentParams($args);
280         $params['include_all_revisions'] = true;
281         return $params;
282     }
283
284     function getChanges ($dbi, $args) {
285         $page = $dbi->getPage($args['page']);
286         $iter = $page->getAllRevisions();
287         $params = $this->getMostRecentParams($args);
288         return new _PageHistory_PageRevisionIter($iter, $params);
289     }
290
291     function format ($changes, $args) {
292         global $Theme;
293         $format = $args['format'];
294
295         $fmt_class = $Theme->getFormatter('PageHistory', $format);
296         if (!$fmt_class) {
297             if ($format == 'rss')
298                 $fmt_class = '_PageHistory_RssFormatter';
299             else
300                 $fmt_class = '_PageHistory_HtmlFormatter';
301         }
302
303         $fmt = new $fmt_class($args);
304         return $fmt->format($changes);
305     }
306
307     function run ($dbi, $argstr, $request) {
308         $args = $this->getArgs($argstr, $request);
309         $pagename = $args['page'];
310         if (empty($pagename))
311             return $this->makeForm("", $request);
312
313         $page = $dbi->getPage($pagename);
314         $current = $page->getCurrentRevision();
315         if ($current->getVersion() < 1) {
316             return HTML(HTML::p(fmt("I'm sorry, there is no such page as %s.",
317                                     WikiLink($pagename, 'unknown'))),
318                         $this->makeForm("", $request));
319         }
320         // Hack alert: format() is a NORETURN for rss formatters.
321         return $this->format($this->getChanges($dbi, $args), $args);
322     }
323 };
324
325 // $Log: not supported by cvs2svn $
326 // Revision 1.25  2003/02/17 02:19:01  dairiki
327 // Fix so that PageHistory will work when the current revision
328 // of a page has been "deleted".
329 //
330 // Revision 1.24  2003/01/18 21:49:00  carstenklapp
331 // Code cleanup:
332 // Reformatting & tabs to spaces;
333 // Added copyleft, getVersion, getDescription, rcs_id.
334 //
335 // Revision 1.23  2003/01/04 23:27:39  carstenklapp
336 // New: Gracefully handle non-existant pages. Added copyleft;
337 // getVersion() for PluginManager.
338 //
339
340 // (c-file-style: "gnu")
341 // Local Variables:
342 // mode: php
343 // tab-width: 8
344 // c-basic-offset: 4
345 // c-hanging-comment-ender-p: nil
346 // indent-tabs-mode: nil
347 // End:
348 ?>