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