added $wgEnableEditToolbar to disable JS toolbar
[lhc/web/wiklou.git] / includes / EditPage.php
1 <?
2
3 # Splitting edit page/HTML interface from Article...
4 # The actual database and text munging is still in Article,
5 # but it should get easier to call those from alternate
6 # interfaces.
7
8 class EditPage {
9 var $mArticle;
10 var $mTitle;
11
12 function EditPage( $article ) {
13 $this->mArticle =& $article;
14 global $wgTitle;
15 $this->mTitle =& $wgTitle;
16 }
17
18 # This is the function that gets called for "action=edit".
19
20 function edit()
21 {
22 global $wgOut, $wgUser, $wgWhitelistEdit;
23 global $wpTextbox1, $wpSummary, $wpSave, $wpPreview;
24 global $wpMinoredit, $wpEdittime, $wpTextbox2;
25
26 $fields = array( "wpTextbox1", "wpSummary", "wpTextbox2" );
27 wfCleanFormFields( $fields );
28
29 if ( ! $this->mTitle->userCanEdit() ) {
30 $wgOut->readOnlyPage( $this->mArticle->getContent(), true );
31 return;
32 }
33 if ( $wgUser->isBlocked() ) {
34 $this->blockedIPpage();
35 return;
36 }
37 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
38 $this->userNotLoggedInPage();
39 return;
40 }
41 if ( wfReadOnly() ) {
42 if( isset( $wpSave ) or isset( $wpPreview ) ) {
43 $this->editForm( "preview" );
44 } else {
45 $wgOut->readOnlyPage( $this->mArticle->getContent() );
46 }
47 return;
48 }
49 if ( $_SERVER['REQUEST_METHOD'] != "POST" ) unset( $wpSave );
50 if ( isset( $wpSave ) ) {
51 $this->editForm( "save" );
52 } else if ( isset( $wpPreview ) ) {
53 $this->editForm( "preview" );
54 } else { # First time through
55 $this->editForm( "initial" );
56 }
57 }
58
59 # Since there is only one text field on the edit form,
60 # pressing <enter> will cause the form to be submitted, but
61 # the submit button value won't appear in the query, so we
62 # Fake it here before going back to edit(). This is kind of
63 # ugly, but it helps some old URLs to still work.
64
65 function submit()
66 {
67 global $wpSave, $wpPreview;
68 if ( ! isset( $wpPreview ) ) { $wpSave = 1; }
69
70 $this->edit();
71 }
72
73 # The edit form is self-submitting, so that when things like
74 # preview and edit conflicts occur, we get the same form back
75 # with the extra stuff added. Only when the final submission
76 # is made and all is well do we actually save and redirect to
77 # the newly-edited page.
78
79 function editForm( $formtype )
80 {
81 global $wgOut, $wgUser;
82 global $wpTextbox1, $wpSummary, $wpWatchthis;
83 global $wpSave, $wpPreview;
84 global $wpMinoredit, $wpEdittime, $wpTextbox2, $wpSection;
85 global $oldid, $redirect, $section;
86 global $wgLang, $wgEnableEditToolbar;
87
88 if(isset($wpSection)) { $section=$wpSection; } else { $wpSection=$section; }
89
90 $sk = $wgUser->getSkin();
91 $isConflict = false;
92 $wpTextbox1 = rtrim ( $wpTextbox1 ) ; # To avoid text getting longer on each preview
93
94 if(!$this->mTitle->getArticleID()) { # new article
95
96 $wgOut->addWikiText(wfmsg("newarticletext"));
97
98 }
99
100 # Attempt submission here. This will check for edit conflicts,
101 # and redundantly check for locked database, blocked IPs, etc.
102 # that edit() already checked just in case someone tries to sneak
103 # in the back door with a hand-edited submission URL.
104
105 if ( "save" == $formtype ) {
106 if ( $wgUser->isBlocked() ) {
107 $this->blockedIPpage();
108 return;
109 }
110 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
111 $this->userNotLoggedInPage();
112 return;
113 }
114 if ( wfReadOnly() ) {
115 $wgOut->readOnlyPage();
116 return;
117 }
118 # If article is new, insert it.
119
120 $aid = $this->mTitle->getArticleID();
121 if ( 0 == $aid ) {
122 # we need to strip Windoze linebreaks because some browsers
123 # append them and the string comparison fails
124 if ( ( "" == $wpTextbox1 ) ||
125 ( wfMsg( "newarticletext" ) == rtrim( preg_replace("/\r/","",$wpTextbox1) ) ) ) {
126 $wgOut->redirect( wfLocalUrl(
127 $this->mTitle->getPrefixedURL() ) );
128 return;
129 }
130 $this->mArticle->insertNewArticle( $wpTextbox1, $wpSummary, $wpMinoredit, $wpWatchthis );
131 return;
132 }
133 # Article exists. Check for edit conflict.
134 # Don't check for conflict when appending a comment - this should always work
135
136 $this->mArticle->clear(); # Force reload of dates, etc.
137 if ( $section!="new" && ( $this->mArticle->getTimestamp() != $wpEdittime ) ) {
138 $isConflict = true;
139 }
140 $u = $wgUser->getID();
141
142 # Supress edit conflict with self
143
144 if ( ( 0 != $u ) && ( $this->mArticle->getUser() == $u ) ) {
145 $isConflict = false;
146 } else {
147 # switch from section editing to normal editing in edit conflict
148 if($isConflict) {
149 $section="";$wpSection="";
150 }
151
152 }
153 if ( ! $isConflict ) {
154 # All's well: update the article here
155 if($this->mArticle->updateArticle( $wpTextbox1, $wpSummary, $wpMinoredit, $wpWatchthis, $wpSection ))
156 return;
157 else
158 $isConflict = true;
159 }
160 }
161 # First time through: get contents, set time for conflict
162 # checking, etc.
163
164 if ( "initial" == $formtype ) {
165 $wpEdittime = $this->mArticle->getTimestamp();
166 $wpTextbox1 = $this->mArticle->getContent(true);
167 $wpSummary = "";
168 }
169 $wgOut->setRobotpolicy( "noindex,nofollow" );
170
171 # Enabled article-related sidebar, toplinks, etc.
172 $wgOut->setArticleRelated( true );
173
174 if ( $isConflict ) {
175 $s = wfMsg( "editconflict", $this->mTitle->getPrefixedText() );
176 $wgOut->setPageTitle( $s );
177 $wgOut->addHTML( wfMsg( "explainconflict" ) );
178
179 $wpTextbox2 = $wpTextbox1;
180 $wpTextbox1 = $this->mArticle->getContent(true);
181 $wpEdittime = $this->mArticle->getTimestamp();
182 } else {
183 $s = wfMsg( "editing", $this->mTitle->getPrefixedText() );
184
185 if($section!="") {
186 if($section=="new") {
187 $s.=wfMsg("commentedit");
188 } else {
189 $s.=wfMsg("sectionedit");
190 }
191 }
192 $wgOut->setPageTitle( $s );
193 if ( $oldid ) {
194 $this->mArticle->setOldSubtitle();
195 $wgOut->addHTML( wfMsg( "editingold" ) );
196 }
197 }
198
199 if( wfReadOnly() ) {
200 $wgOut->addHTML( "<strong>" .
201 wfMsg( "readonlywarning" ) .
202 "</strong>" );
203 }
204 if( $this->mTitle->isProtected() ) {
205 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
206 "</strong><br />\n" );
207 }
208
209 $kblength = (int)(strlen( $wpTextbox1 ) / 1024);
210 if( $kblength > 29 ) {
211 $wgOut->addHTML( "<strong>" .
212 wfMsg( "longpagewarning", $kblength )
213 . "</strong>" );
214 }
215
216 $rows = $wgUser->getOption( "rows" );
217 $cols = $wgUser->getOption( "cols" );
218
219 $ew = $wgUser->getOption( "editwidth" );
220 if ( $ew ) $ew = " style=\"width:100%\"";
221 else $ew = "" ;
222
223 $q = "action=submit";
224 if ( "no" == $redirect ) { $q .= "&redirect=no"; }
225 $action = wfEscapeHTML( wfLocalUrl( $this->mTitle->getPrefixedURL(), $q ) );
226
227 $summary = wfMsg( "summary" );
228 $subject = wfMsg("subject");
229 $minor = wfMsg( "minoredit" );
230 $watchthis = wfMsg ("watchthis");
231 $save = wfMsg( "savearticle" );
232 $prev = wfMsg( "showpreview" );
233
234 $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedURL(),
235 wfMsg( "cancel" ) );
236 $edithelp = $sk->makeKnownLink( wfMsg( "edithelppage" ),
237 wfMsg( "edithelp" ) );
238 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
239 wfMsg( "copyrightpage" ) ) );
240
241 $wpTextbox1 = wfEscapeHTML( $wpTextbox1 );
242 $wpTextbox2 = wfEscapeHTML( $wpTextbox2 );
243 $wpSummary = wfEscapeHTML( $wpSummary );
244
245
246 if($wgUser->getOption("showtoolbar") && $wgEnableEditToolbar) {
247 // prepare toolbar for edit buttons
248 $toolbar=$sk->getEditToolbar();
249 }
250
251 // activate checkboxes if user wants them to be always active
252 if (!$wpPreview && $wgUser->getOption("watchdefault")) $wpWatchthis=1;
253 if (!$wpPreview && $wgUser->getOption("minordefault")) $wpMinoredit=1;
254
255 // activate checkbox also if user is already watching the page,
256 // require wpWatchthis to be unset so that second condition is not
257 // checked unnecessarily
258 if (!$wpWatchthis && !$wpPreview && $this->mTitle->userIsWatching()) $wpWatchthis=1;
259
260 if ( 0 != $wgUser->getID() ) {
261 $checkboxhtml=
262 "<input tabindex=3 type=checkbox value=1 name='wpMinoredit'".($wpMinoredit?" checked":"")." id='wpMinoredit'>".
263 "<label for='wpMinoredit'>{$minor}</label>".
264 "<input tabindex=4 type=checkbox name='wpWatchthis'".($wpWatchthis?" checked":"")." id='wpWatchthis'>".
265 "<label for='wpWatchthis'>{$watchthis}</label><br>";
266
267 } else {
268 $checkboxhtml="";
269 }
270
271
272 if ( "preview" == $formtype) {
273
274 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
275 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large><P>\n";
276 if ( $isConflict ) {
277 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
278 "</h2>\n";
279 }
280 $previewtext = wfUnescapeHTML( $wpTextbox1 );
281
282 if($wgUser->getOption("previewontop")) {
283 $wgOut->addHTML($previewhead);
284 $wgOut->addWikiText( $this->mArticle->preSaveTransform( $previewtext ) ."\n\n");
285 }
286 $wgOut->addHTML( "<br clear=\"all\" />\n" );
287 }
288
289 # if this is a comment, show a subject line at the top, which is also the edit summary.
290 # Otherwise, show a summary field at the bottom
291 if($section=="new") {
292
293 $commentsubject="{$subject}: <input tabindex=1 type=text value=\"{$wpSummary}\" name=\"wpSummary\" maxlength=200 size=60><br>";
294 } else {
295
296 $editsummary="{$summary}: <input tabindex=3 type=text value=\"{$wpSummary}\" name=\"wpSummary\" maxlength=200 size=60><br>";
297 }
298
299 if( $_GET["action"] == "edit" ) {
300 # Don't select the edit box on preview; this interferes with seeing what's going on.
301 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
302 }
303 $wgOut->addHTML( "
304 {$toolbar}
305 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
306 enctype=\"application/x-www-form-urlencoded\">
307 {$commentsubject}
308 <textarea tabindex=2 name=\"wpTextbox1\" rows={$rows}
309 cols={$cols}{$ew} wrap=\"virtual\">" .
310 $wgLang->recodeForEdit( $wpTextbox1 ) .
311 "
312 </textarea>
313 <br>{$editsummary}
314 {$checkboxhtml}
315 <input tabindex=5 type=submit value=\"{$save}\" name=\"wpSave\" accesskey=\"s\">
316 <input tabindex=6 type=submit value=\"{$prev}\" name=\"wpPreview\" accesskey=\"p\">
317 <em>{$cancel}</em> | <em>{$edithelp}</em>
318 <br><br>{$copywarn}
319 <input type=hidden value=\"{$section}\" name=\"wpSection\">
320 <input type=hidden value=\"{$wpEdittime}\" name=\"wpEdittime\">\n" );
321
322 if ( $isConflict ) {
323 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
324 DifferenceEngine::showDiff( $wpTextbox2, $wpTextbox1,
325 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
326
327 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
328 <textarea tabindex=6 name=\"wpTextbox2\" rows={$rows} cols={$cols} wrap=virtual>"
329 . $wgLang->recodeForEdit( $wpTextbox2 ) .
330 "
331 </textarea>" );
332 }
333 $wgOut->addHTML( "</form>\n" );
334 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
335 $wgOut->addHTML($previewhead);
336 $wgOut->addWikiText( $this->mArticle->preSaveTransform( $previewtext ) );
337 }
338
339 }
340
341 function blockedIPpage()
342 {
343 global $wgOut, $wgUser, $wgLang;
344
345 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
346 $wgOut->setRobotpolicy( "noindex,nofollow" );
347 $wgOut->setArticleRelated( false );
348
349 $id = $wgUser->blockedBy();
350 $reason = $wgUser->blockedFor();
351 $ip = getenv( "REMOTE_ADDR" );
352
353 $name = User::whoIs( $id );
354 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
355 ":{$name}|{$name}]]";
356
357 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip ) );
358 $wgOut->returnToMain( false );
359 }
360
361
362
363 function userNotLoggedInPage()
364 {
365 global $wgOut, $wgUser, $wgLang;
366
367 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
368 $wgOut->setRobotpolicy( "noindex,nofollow" );
369 $wgOut->setArticleRelated( false );
370
371 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
372 $wgOut->returnToMain( false );
373 }
374
375
376 }
377
378 ?>