]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/savepage.php
XHTML fixes. At this point, PhpWiki's output is almost valid XHTML.
[SourceForge/phpwiki.git] / lib / savepage.php
1 <?php rcs_id('$Id: savepage.php,v 1.17 2001-11-14 21:05:38 dairiki Exp $');
2 require_once('lib/Template.php');
3 require_once('lib/transform.php');
4 require_once('lib/ArchiveCleaner.php');
5
6 /*
7    All page saving events take place here.
8    All page info is also taken care of here.
9    This is klugey. But it works. There's probably a slicker way of
10    coding it.
11 */
12
13 // FIXME: some links so that it's easy to get back to someplace useful from these
14 // error pages.
15
16 function ConcurrentUpdates($pagename) {
17    /* xgettext only knows about c/c++ line-continuation strings
18      is does not know about php's dot operator.
19      We want to translate this entire paragraph as one string, of course.
20    */
21     $html = "<p>";
22     $html .= gettext ("PhpWiki is unable to save your changes, because another user edited and saved the page while you were editing the page too. If saving proceeded now changes from the previous author would be lost.");
23     $html .= "</p>\n<p>";
24     $html .= gettext ("In order to recover from this situation follow these steps:");
25     $html .= "\n<ol><li>";
26     $html .= gettext ("Use your browser's <b>Back</b> button to go back to the edit page.");
27     $html .= "</li>\n<li>";
28     $html .= gettext ("Copy your changes to the clipboard or to another temporary place (e.g. text editor).");
29     $html .= "</li>\n<li>";
30     $html .= gettext ("<b>Reload</b> the page. You should now see the most current version of the page. Your changes are no longer there.");
31     $html .= "</li>\n<li>";
32     $html .= gettext ("Make changes to the file again. Paste your additions from the clipboard (or text editor).");
33     $html .= "</li>\n<li>";
34     $html .= gettext ("Press <b>Save</b> again.");
35     $html .= "</li></ol></p>\n";
36     $html .= QElement('p', gettext ("Sorry for the inconvenience."));
37
38     echo GeneratePage('MESSAGE', $html,
39                       sprintf (gettext ("Problem while updating %s"), $pagename));
40     ExitWiki();
41 }
42
43 function PageIsLocked($pagename) {
44     $html = QElement('p',
45                      gettext("This page has been locked by the administrator and cannot be edited."));
46     $html .= QElement('p',
47                       gettext ("Sorry for the inconvenience."));
48
49     echo GeneratePage('MESSAGE', $html,
50                       sprintf (gettext ("Problem while editing %s"), $pagename));
51     ExitWiki ("");
52 }
53
54 function NoChangesMade($pagename) {
55     $html = QElement('p', gettext ("You have not made any changes."));
56     $html .= QElement('p', gettext ("New version not saved."));
57     echo GeneratePage('MESSAGE', $html,
58                       sprintf(gettext("Edit aborted: %s"), $pagename));
59     ExitWiki ("");
60 }
61
62 function BadFormVars($pagename) {
63     $html = QElement('p', gettext ("Bad form submission"));
64     $html .= QElement('p', gettext ("Required form variables are missing."));
65     echo GeneratePage('MESSAGE', $html,
66                       sprintf(gettext("Edit aborted: %s"), $pagename));
67     ExitWiki ("");
68 }
69     
70 function savePreview($dbi, $request) {
71     $pagename = $request->getArg('pagename');
72     $version = $request->getArg('version');
73
74     $page = $dbi->getPage($pagename);
75     $selected = $page->getRevision($version);
76
77     // FIXME: sanity checking about posted variables
78     // FIXME: check for simultaneous edits.
79     foreach (array('minor_edit', 'convert') as $key)
80         $formvars[$key] = $request->getArg($key) ? 'checked' : '';
81     foreach (array('content', 'editversion', 'summary', 'pagename', 'version') as $key)
82         @$formvars[$key] = htmlspecialchars($request->getArg($key));
83
84     $template = new WikiTemplate('EDITPAGE');
85     $template->setPageRevisionTokens($selected);
86     $template->replace('FORMVARS', $formvars);
87     $template->replace('PREVIEW_CONTENT', do_transform($request->getArg('content')));
88     echo $template->getExpansion();
89 }
90
91 function savePage ($dbi, $request) {
92     global $user;
93
94     // FIXME: fail if this check fails?
95     assert($request->get('REQUEST_METHOD') == 'POST');
96     
97     if ($request->getArg('preview'))
98         return savePreview($dbi, $request);
99     
100     $pagename = $request->getArg('pagename');
101     $version = $request->getArg('version');
102
103     $page = $dbi->getPage($pagename);
104     $current = $page->getCurrentRevision();
105
106     $content = $request->getArg('content');
107     $editversion = $request->getArg('editversion');
108     
109     if ( $content === false || $editversion === false )
110         BadFormVars($pagename); // noreturn
111
112     if ($page->get('locked') && !$user->is_admin())
113         PageIsLocked($args->pagename); // noreturn.
114
115     $meta['author'] = $user->id();
116     $meta['author_id'] = $user->authenticated_id();
117     $meta['is_minor_edit'] = (bool) $request->getArg('minor_edit');
118     $meta['summary'] = trim($request->getArg('summary'));
119
120     $content = preg_replace('/[ \t\r]+\n/', "\n", chop($content));
121     if ($request->getArg('convert'))
122         $content = CookSpaces($content);
123
124     if ($content == $current->getPackedContent()) {
125         NoChangesMade($pagename); // noreturn
126     }
127
128     ////////////////////////////////////////////////////////////////
129     //
130     // From here on, we're actually saving.
131     //
132     $newrevision = $page->createRevision($editversion + 1,
133                                          $content, $meta,
134                                          ExtractWikiPageLinks($content));
135     if (!is_object($newrevision)) {
136         // Save failed.
137         ConcurrentUpdates($pagename);
138     }
139
140     // Clean out archived versions of this page.
141     $cleaner = new ArchiveCleaner($GLOBALS['ExpireParams']);
142     $cleaner->cleanPageRevisions($page);
143     
144     $warnings = $dbi->GenericWarnings();
145     if (empty($warnings)) {
146         // Do redirect to browse page.
147         // In this case, the user will most likely not see the rest of
148         // the HTML we generate (below).
149         $request->redirect(WikiURL($pagename, false, 'absolute_url'));
150     }
151
152     $html = sprintf(gettext("Thank you for editing %s."),
153                     LinkExistingWikiWord($pagename));
154     $html .= "<br>\n";
155     $html .= gettext ("Your careful attention to detail is much appreciated.");
156     $html .= "\n";
157
158     if ($warnings) {
159         $html .= Element('p', "<b>Warning!</b> "
160                          . htmlspecialchars($warnings)
161                          . "<br>\n");
162     }
163
164     global $SignatureImg;
165     if (!empty($SignatureImg))
166         $html .= sprintf("<P><img src=\"%s\"></P>\n", DataURL($SignatureImg));
167     
168     $html .= "<hr noshade>\n";
169     $html .= do_transform($newrevision->getContent());
170     echo GeneratePage('BROWSE', $html, $pagename, $newrevision);
171 }
172
173     
174 // Local Variables:
175 // mode: php
176 // tab-width: 8
177 // c-basic-offset: 4
178 // c-hanging-comment-ender-p: nil
179 // indent-tabs-mode: nil
180 // End:   
181 ?>