]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/savepage.php
Refactor/cleanup of login code continues.
[SourceForge/phpwiki.git] / lib / savepage.php
1 <?php rcs_id('$Id: savepage.php,v 1.32 2002-01-23 05:10:22 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 // converts spaces to tabs
14 function CookSpaces($pagearray) {
15     return preg_replace("/ {3,8}/", "\t", $pagearray);
16 }
17
18 // FIXME: some links so that it's easy to get back to someplace useful
19 // from these error pages.
20
21 function ConcurrentUpdates(&$request) {
22    /*
23      xgettext only knows about c/c++ line-continuation strings
24      it does not know about php's dot operator.
25      We want to translate this entire paragraph as one string, of course.
26    */
27     $step = array(_("Use your browser's <b>Back</b> button to go back to the edit page."),
28                   _("Copy your changes to the clipboard or to another temporary place (e.g. text editor)."),
29                   _("<b>Reload</b> the page. You should now see the most current version of the page. Your changes are no longer there."),
30                   _("Make changes to the file again. Paste your additions from the clipboard (or text editor)."),
31                   _("Press <b>Save</b> again."));
32
33     
34         
35     $html[] = HTML::p(_("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."));
36
37     $html[] = HTML::p(_("In order to recover from this situation follow these steps:"));
38
39     $steps = HTML::ol();
40     foreach ($steps as $step)
41         $steps->pushContent(HTML::li($step));
42     $html[] = $steps;
43
44     $html[] = HTML::p(_("Sorry for the inconvenience."));
45
46     echo GeneratePage('MESSAGE', $html,
47                       sprintf(_("Problem while updating %s"), $request->getArg('pagename')));
48     $request->finish();
49 }
50
51 function PageIsLocked (&$request) {
52     $html[] = HTML::p(_("This page has been locked by the administrator so your changes could not be saved."));
53     $html[] = HTML::p(_("Use your browser's <b>Back</b> button to go back to the edit page."),
54                       ' ',
55                       _("Copy your changes to the clipboard. You can try editing a different page or save your text in a text editor."));
56     $html[] = HTML::p(_("Sorry for the inconvenience."));
57     
58     echo GeneratePage('MESSAGE', $html,
59                       sprintf (_("Problem while editing %s"), $request->getArg('pagename')));
60     $request->finish();
61 }
62
63 function BadFormVars (&$request) {
64     $html[] = HTML::p(_("Bad form submission"));
65     $html[] = HTML::p(_("Required form variables are missing."));
66     echo GeneratePage('MESSAGE', $html,
67                       sprintf(_("Edit aborted: %s"), $request->getArg('pagename')));
68     $request->finish();
69 }
70
71 function savePage (&$request) {
72     global $Theme;
73     
74     if (! $request->isPost())
75         BadFormVars($request);
76     
77     if ($request->getArg('preview') || !$request->getArg('save')) {
78         include_once('lib/editpage.php');
79         return editPage($request, 'do_preview');
80     }
81     
82     $pagename = $request->getArg('pagename');
83     $version = $request->getArg('version');
84     
85     $page = $request->getPage();
86     $current = $page->getCurrentRevision();
87     
88     $content = $request->getArg('content');
89     $editversion = $request->getArg('editversion');
90     
91     if ( $content === false || $editversion === false )
92         BadFormVars($request); // noreturn
93
94     $user = $request->getUser();
95     if ($page->get('locked') && !$user->isAdmin())
96         PageIsLocked($request); // noreturn.
97
98     $meta['author'] = $user->getId();
99     $meta['author_id'] = $user->getAuthenticatedId();
100     $meta['is_minor_edit'] = (bool) $request->getArg('minor_edit');
101     $meta['summary'] = trim($request->getArg('summary'));
102
103     $content = preg_replace('/[ \t\r]+\n/', "\n", chop($content));
104     if ($request->getArg('convert'))
105         $content = CookSpaces($content);
106
107     if ($content == $current->getPackedContent()) {
108         // Save failed. No changes made.
109         include_once('lib/display.php');
110         // force browse of current version:
111         $request->setArg('version', false);
112         return displayPage($request, 'nochanges');
113     }
114
115     ////////////////////////////////////////////////////////////////
116     //
117     // From here on, we're actually saving.
118     //
119     $newrevision = $page->createRevision($editversion + 1,
120                                          $content, $meta,
121                                          ExtractWikiPageLinks($content));
122         
123         
124     if (!is_object($newrevision)) {
125         // Save failed.  (Concurrent updates).
126         // FIXME: this should return one to the editing form,
127         //        (with an explanatory message, and options to
128         //        view diffs & merge...)
129         ConcurrentUpdates($request);
130     }
131     // New contents successfully saved...
132
133     // Clean out archived versions of this page.
134     $cleaner = new ArchiveCleaner($GLOBALS['ExpireParams']);
135     $cleaner->cleanPageRevisions($page);
136
137     $dbi = $request->getDbh();
138     $warnings = $dbi->GenericWarnings();
139
140     if (empty($warnings) && ! $Theme->getImageURL('signature')) {
141         // Do redirect to browse page if no signature has
142         // been defined.  In this case, the user will most
143         // likely not see the rest of the HTML we generate
144         // (below).
145         $request->redirect(WikiURL($pagename, false,
146                                    'absolute_url'));
147     }
148
149     // Force browse of current page version.
150     $request->setArg('version', false);
151     
152     $wrapper = new WikiTemplate('top');
153     $wrapper->qreplace('TITLE', sprintf(_("Saved: %s"), $pagename));
154     $wrapper->replace('HEADER', fmt("Saved: %s",
155                                     $Theme->linkExistingWikiWord($pagename)));
156     $wrapper->setPageRevisionTokens($newrevision);
157
158     $template = new WikiTemplate('savepage');
159     if (!empty($warnings))
160         $template->qreplace('WARNINGS', $warnings);
161     $template->replace('CONTENT', do_transform($newrevision->getContent()));
162
163     $wrapper->printExpansion($template);
164 }
165
166 // Local Variables:
167 // mode: php
168 // tab-width: 8
169 // c-basic-offset: 4
170 // c-hanging-comment-ender-p: nil
171 // indent-tabs-mode: nil
172 // End:   
173 ?>