]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/savepage.php
Jeff's hacks II.
[SourceForge/phpwiki.git] / lib / savepage.php
1 <?php rcs_id('$Id: savepage.php,v 1.16 2001-09-18 19:16:23 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 .= "\n<LI>";
28     $html .= gettext ("Copy your changes to the clipboard or to another temporary place (e.g. text editor).");
29     $html .= "\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 .= "\n<LI>";
32     $html .= gettext ("Make changes to the file again. Paste your additions from the clipboard (or text editor).");
33     $html .= "\n<LI>";
34     $html .= gettext ("Press <b>Save</b> again.");
35     $html .= "</OL>\n<P>";
36     $html .= gettext ("Sorry for the inconvenience.");
37     $html .= "</P>";
38
39     echo GeneratePage('MESSAGE', $html,
40                       sprintf (gettext ("Problem while updating %s"), $pagename));
41     ExitWiki();
42 }
43
44 function PageIsLocked($pagename) {
45     $html = QElement('p',
46                      gettext("This page has been locked by the administrator and cannot be edited."));
47     $html .= QElement('p',
48                       gettext ("Sorry for the inconvenience."));
49
50     echo GeneratePage('MESSAGE', $html,
51                       sprintf (gettext ("Problem while editing %s"), $pagename));
52     ExitWiki ("");
53 }
54
55 function NoChangesMade($pagename) {
56     $html = QElement('p', gettext ("You have not made any changes."));
57     $html .= QElement('p', gettext ("New version not saved."));
58     echo GeneratePage('MESSAGE', $html,
59                       sprintf(gettext("Edit aborted: %s"), $pagename));
60     ExitWiki ("");
61 }
62
63 function BadFormVars($pagename) {
64     $html = QElement('p', gettext ("Bad form submission"));
65     $html .= QElement('p', gettext ("Required form variables are missing."));
66     echo GeneratePage('MESSAGE', $html,
67                       sprintf(gettext("Edit aborted: %s"), $pagename));
68     ExitWiki ("");
69 }
70     
71 function savePreview($dbi, $request) {
72     $pagename = $request->getArg('pagename');
73     $version = $request->getArg('version');
74
75     $page = $dbi->getPage($pagename);
76     $selected = $page->getRevision($version);
77
78     // FIXME: sanity checking about posted variables
79     // FIXME: check for simultaneous edits.
80     foreach (array('minor_edit', 'convert') as $key)
81         $formvars[$key] = $request->getArg($key) ? 'checked' : '';
82     foreach (array('content', 'editversion', 'summary', 'pagename', 'version') as $key)
83         @$formvars[$key] = htmlspecialchars($request->getArg($key));
84
85     $template = new WikiTemplate('EDITPAGE');
86     $template->setPageRevisionTokens($selected);
87     $template->replace('FORMVARS', $formvars);
88     $template->replace('PREVIEW_CONTENT', do_transform($request->getArg('content')));
89     echo $template->getExpansion();
90 }
91
92 function savePage ($dbi, $request) {
93     global $user;
94
95     // FIXME: fail if this check fails?
96     assert($request->get('REQUEST_METHOD') == 'POST');
97     
98     if ($request->getArg('preview'))
99         return savePreview($dbi, $request);
100     
101     $pagename = $request->getArg('pagename');
102     $version = $request->getArg('version');
103
104     $page = $dbi->getPage($pagename);
105     $current = $page->getCurrentRevision();
106
107     $content = $request->getArg('content');
108     $editversion = $request->getArg('editversion');
109     
110     if ( $content === false || $editversion === false )
111         BadFormVars($pagename); // noreturn
112
113     if ($page->get('locked') && !$user->is_admin())
114         PageIsLocked($args->pagename); // noreturn.
115
116     $meta['author'] = $user->id();
117     $meta['author_id'] = $user->authenticated_id();
118     $meta['is_minor_edit'] = (bool) $request->getArg('minor_edit');
119     $meta['summary'] = trim($request->getArg('summary'));
120
121     $content = preg_replace('/[ \t\r]+\n/', "\n", chop($content));
122     if ($request->getArg('convert'))
123         $content = CookSpaces($content);
124
125     if ($content == $current->getPackedContent()) {
126         NoChangesMade($pagename); // noreturn
127     }
128
129     ////////////////////////////////////////////////////////////////
130     //
131     // From here on, we're actually saving.
132     //
133     $newrevision = $page->createRevision($editversion + 1,
134                                          $content, $meta,
135                                          ExtractWikiPageLinks($content));
136     if (!is_object($newrevision)) {
137         // Save failed.
138         ConcurrentUpdates($pagename);
139     }
140
141     // Clean out archived versions of this page.
142     $cleaner = new ArchiveCleaner($GLOBALS['ExpireParams']);
143     $cleaner->cleanPageRevisions($page);
144     
145     $warnings = $dbi->GenericWarnings();
146     if (empty($warnings)) {
147         // Do redirect to browse page.
148         // In this case, the user will most likely not see the rest of
149         // the HTML we generate (below).
150         $request->redirect(WikiURL($pagename, false, 'absolute_url'));
151     }
152
153     $html = sprintf(gettext("Thank you for editing %s."),
154                     LinkExistingWikiWord($pagename));
155     $html .= "<br>\n";
156     $html .= gettext ("Your careful attention to detail is much appreciated.");
157     $html .= "\n";
158
159     if ($warnings) {
160         $html .= Element('p', "<b>Warning!</b> "
161                          . htmlspecialchars($warnings)
162                          . "<br>\n");
163     }
164
165     global $SignatureImg;
166     if (!empty($SignatureImg))
167         $html .= sprintf("<P><img src=\"%s\"></P>\n", DataURL($SignatureImg));
168     
169     $html .= "<hr noshade>\n";
170     $html .= do_transform($newrevision->getContent());
171     echo GeneratePage('BROWSE', $html, $pagename, $newrevision);
172 }
173
174     
175 // Local Variables:
176 // mode: php
177 // tab-width: 8
178 // c-basic-offset: 4
179 // c-hanging-comment-ender-p: nil
180 // indent-tabs-mode: nil
181 // End:   
182 ?>