]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/plugin/PageHistory.php
extra_empty_lines
[SourceForge/phpwiki.git] / lib / plugin / PageHistory.php
1 <?php // -*-php-*-
2 // $Id$
3 /**
4  * Copyright 1999, 2000, 2001, 2002, 2007 $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 along
19  * with PhpWiki; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21  */
22
23 require_once("lib/plugin/RecentChanges.php");
24
25 class _PageHistory_PageRevisionIter
26 extends WikiDB_PageRevisionIterator
27 {
28     function _PageHistory_PageRevisionIter($rev_iter, $params) {
29
30         $this->_iter = $rev_iter;
31
32         extract($params);
33
34         if (isset($since))
35             $this->_since = $since;
36
37         $this->_include_major = empty($exclude_major_revisions);
38         if (! $this->_include_major)
39             $this->_include_minor = true;
40         else
41             $this->_include_minor = !empty($include_minor_revisions);
42
43         if (empty($include_all_revisions))
44             $this->_limit = 1;
45         else if (isset($limit))
46             $this->_limit = $limit;
47     }
48
49     function next() {
50         if (!$this->_iter)
51             return false;
52
53         if (isset($this->_limit)) {
54             if ($this->_limit <= 0) {
55                 $this->free();
56                 return false;
57             }
58             $this->_limit--;
59         }
60
61         while ( ($rev = $this->_iter->next()) ) {
62             if (isset($this->_since) && $rev->get('mtime') < $this->_since) {
63                 $this->free();
64                 return false;
65             }
66             if ($rev->get('is_minor_edit') ? $this->_include_minor : $this->_include_major)
67                 return $rev;
68         }
69         return false;
70     }
71
72     function free() {
73         if ($this->_iter)
74             $this->_iter->free();
75         $this->_iter = false;
76     }
77 }
78
79 class _PageHistory_HtmlFormatter
80 extends _RecentChanges_HtmlFormatter
81 {
82     function include_versions_in_URLs() {
83         return true;
84     }
85
86     function headline() {
87         return HTML(fmt("PageHistory for %s",
88                          WikiLink($this->_args['page'])),
89                      "\n",
90                      $this->rss_icon(),
91                      $this->rss2_icon(),
92                      $this->atom_icon(),
93                      $this->rdf_icon());
94     }
95
96     function title() {
97         return "PageHistory:".$this->_args['page'];
98     }
99
100     function empty_message () {
101         return _("No revisions found");
102     }
103
104     function description() {
105         $button = HTML::input(array('type'  => 'submit',
106                                     'value' => _("compare revisions"),
107                                     'class' => 'wikiaction'));
108
109         $js_desc = $no_js_desc = _RecentChanges_HtmlFormatter::description();
110
111         $js_desc->pushContent("\n", _("Check any two boxes to compare revisions."));
112         $no_js_desc->pushContent("\n", fmt("Check any two boxes then %s.", $button));
113
114         return IfJavaScript($js_desc, $no_js_desc);
115     }
116
117     function format ($changes) {
118         $this->_itemcount = 0;
119
120         $pagename = $this->_args['page'];
121
122         $fmt = _RecentChanges_HtmlFormatter::format($changes);
123         $fmt->action = _("PageHistory");
124         $html[] = $fmt;
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                                      'id'     => 'diff-select'),
142                                $html),
143                     "\n",
144                     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         global $WikiTheme;
176         $class = 'rc-' . $this->importance($rev);
177
178         $time = $this->time($rev);
179         if ($rev->get('is_minor_edit')) {
180             $minor_flag = HTML(" ",
181                                HTML::span(array('class' => 'pageinfo-minoredit'),
182                                           "(" . _("minor edit") . ")"));
183         }
184         else {
185             $time = HTML::span(array('class' => 'pageinfo-majoredit'), $time);
186             $minor_flag = '';
187         }
188         $line = HTML::li(array('class' => $class));
189         if (isa($WikiTheme,'WikiTheme_MonoBook')) {
190             $line->pushContent(
191                                $this->diffLink($rev), ' ',
192                                $this->pageLink($rev), ' ',
193                                $time,' ',$this->date($rev), ' . . ',
194                                $this->authorLink($rev),' ',
195                                $this->authorContribs($rev),' ',
196                                $this->summaryAsHTML($rev),' ',
197                                $minor_flag);
198         } else {
199             $line->pushContent(
200                                $this->diffLink($rev), ' ',
201                                $this->pageLink($rev), ' ',
202                                $time, ' ',
203                                $this->summaryAsHTML($rev),
204                                ' ... ',
205                                $this->authorLink($rev),
206                                $minor_flag);
207         }
208         return $line;
209     }
210 }
211
212 class _PageHistory_RssFormatter
213 extends _RecentChanges_RssFormatter
214 {
215     function include_versions_in_URLs() {
216         return true;
217     }
218
219     function image_properties () {
220         return false;
221     }
222
223     function textinput_properties () {
224         return false;
225     }
226
227     function channel_properties () {
228         global $request;
229
230         $rc_url = WikiURL($request->getArg('pagename'), false, 'absurl');
231
232         $title = sprintf(_("%s: %s"),
233                          WIKI_NAME,
234                          SplitPagename($this->_args['page']));
235
236         return array('title'          => $title,
237                      'dc:description' => _("History of changes."),
238                      'link'           => $rc_url,
239                      'dc:date'        => Iso8601DateTime(time()));
240     }
241
242     function item_properties ($rev) {
243         if (!($title = $this->summary($rev)))
244             $title = sprintf(_("Version %d"), $rev->getVersion());
245
246         return array( 'title'           => $title,
247                       'link'            => $this->pageURL($rev),
248                       'dc:date'         => $this->time($rev),
249                       'dc:contributor'  => $rev->get('author'),
250                       'wiki:version'    => $rev->getVersion(),
251                       'wiki:importance' => $this->importance($rev),
252                       'wiki:status'     => $this->status($rev),
253                       'wiki:diff'       => $this->diffURL($rev),
254                       );
255     }
256 }
257
258 class WikiPlugin_PageHistory
259 extends WikiPlugin_RecentChanges
260 {
261     function getName () {
262         return _("PageHistory");
263     }
264
265     function getDescription () {
266         return sprintf(_("List PageHistory for %s"),'[pagename]');
267     }
268
269     function getDefaultArguments() {
270         return array('days'         => false,
271                      'show_minor'   => true,
272                      'show_major'   => true,
273                      'limit'        => false,
274                      'page'         => '[pagename]',
275                      'format'       => false);
276     }
277
278     function getDefaultFormArguments() {
279         $dflts = WikiPlugin_RecentChanges::getDefaultFormArguments();
280         $dflts['textinput'] = 'page';
281         return $dflts;
282     }
283
284     function getMostRecentParams ($args) {
285         $params = WikiPlugin_RecentChanges::getMostRecentParams($args);
286         $params['include_all_revisions'] = true;
287         return $params;
288     }
289
290     function getChanges ($dbi, $args) {
291         $page = $dbi->getPage($args['page']);
292         $iter = $page->getAllRevisions();
293         $params = $this->getMostRecentParams($args);
294         if (empty($args['days'])) unset($params['since']);
295         return new _PageHistory_PageRevisionIter($iter, $params);
296     }
297
298     function format ($changes, $args) {
299         global $WikiTheme;
300         $format = $args['format'];
301
302         $fmt_class = $WikiTheme->getFormatter('PageHistory', $format);
303         if (!$fmt_class) {
304             if ($format == 'rss')
305                 $fmt_class = '_PageHistory_RssFormatter';
306             else
307                 $fmt_class = '_PageHistory_HtmlFormatter';
308         }
309
310         $fmt = new $fmt_class($args);
311         $fmt->action = _("PageHistory");
312         return $fmt->format($changes);
313     }
314
315     function run($dbi, $argstr, &$request, $basepage) {
316         $args = $this->getArgs($argstr, $request);
317         $pagename = $args['page'];
318         if (empty($pagename))
319             return $this->makeForm("", $request);
320
321         $page = $dbi->getPage($pagename);
322         $current = $page->getCurrentRevision();
323         if ($current->getVersion() < 1) {
324             return HTML(HTML::p(fmt("I'm sorry, there is no such page as %s.",
325                                     WikiLink($pagename, 'unknown'))),
326                         $this->makeForm("", $request));
327         }
328         // Hack alert: format() is a NORETURN for rss formatters.
329         return $this->format($this->getChanges($dbi, $args), $args);
330     }
331 };
332
333 // Local Variables:
334 // mode: php
335 // tab-width: 8
336 // c-basic-offset: 4
337 // c-hanging-comment-ender-p: nil
338 // indent-tabs-mode: nil
339 // End:
340 ?>