Put section-edit-summary-title back into head branch for now.
[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 if( Namespace::isTalk( $this->mTitle->getNamespace() ) ) {
120 $wgOut->addWikiText(wfmsg("talkpagetext"));
121 }
122
123 # Attempt submission here. This will check for edit conflicts,
124 # and redundantly check for locked database, blocked IPs, etc.
125 # that edit() already checked just in case someone tries to sneak
126 # in the back door with a hand-edited submission URL.
127
128 if ( "save" == $formtype ) {
129 if ( $wgUser->isBlocked() ) {
130 $this->blockedIPpage();
131 return;
132 }
133 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
134 $this->userNotLoggedInPage();
135 return;
136 }
137 if ( wfReadOnly() ) {
138 $wgOut->readOnlyPage();
139 return;
140 }
141 # If article is new, insert it.
142
143 $aid = $this->mTitle->getArticleID();
144 if ( 0 == $aid ) {
145 # Don't save a new article if it's blank.
146 if ( ( "" == $this->textbox1 ) ||
147 ( wfMsg( "newarticletext" ) == $this->textbox1 ) ) {
148 $wgOut->redirect( $this->mTitle->getFullURL() );
149 return;
150 }
151 $this->mArticle->insertNewArticle( $this->textbox1, $this->summary, $this->minoredit, $this->watchthis );
152 return;
153 }
154
155 # Article exists. Check for edit conflict.
156
157 $this->mArticle->clear(); # Force reload of dates, etc.
158
159 if( ( $this->section != "new" ) &&
160 ($this->mArticle->getTimestamp() != $this->edittime ) ) {
161 $isConflict = true;
162 }
163 $userid = $wgUser->getID();
164
165 $text = $this->mArticle->getTextOfLastEditWithSectionReplacedOrAdded(
166 $this->section, $this->textbox1, $this->summary);
167 # Suppress edit conflict with self
168
169 if ( ( 0 != $userid ) && ( $this->mArticle->getUser() == $userid ) ) {
170 $isConflict = false;
171 } else {
172 # switch from section editing to normal editing in edit conflict
173 # FIXME: This is confusing. In theory we should attempt to merge, finding
174 # the equivalent section if it's unchanged and avoid the conflict.
175 if($isConflict) {
176 if( $this->mergeChangesInto( $text ) ){
177 // Successful merge! Maybe we should tell the user the good news?
178 $isConflict = false;
179 } else {
180 $this->section = "";
181 $this->textbox1 = $text;
182 }
183 }
184 }
185 if ( ! $isConflict ) {
186 # All's well: update the article here
187 if($this->mArticle->updateArticle( $text, $this->summary, $this->minoredit, $this->watchthis ))
188 return;
189 else
190 $isConflict = true;
191 }
192 }
193 # First time through: get contents, set time for conflict
194 # checking, etc.
195
196 if ( "initial" == $formtype ) {
197 $this->edittime = $this->mArticle->getTimestamp();
198 $this->textbox1 = $this->mArticle->getContent(true);
199 $this->summary = "";
200 $this->proxyCheck();
201 }
202 $wgOut->setRobotpolicy( "noindex,nofollow" );
203
204 # Enabled article-related sidebar, toplinks, etc.
205 $wgOut->setArticleRelated( true );
206
207 if ( $isConflict ) {
208 $s = wfMsg( "editconflict", $this->mTitle->getPrefixedText() );
209 $wgOut->setPageTitle( $s );
210 $wgOut->addHTML( wfMsg( "explainconflict" ) );
211
212 $this->textbox2 = $this->textbox1;
213 $this->textbox1 = $this->mArticle->getContent(true);
214 $this->edittime = $this->mArticle->getTimestamp();
215 } else {
216 $s = wfMsg( "editing", $this->mTitle->getPrefixedText() );
217
218 if( $this->section != "" ) {
219 if( $this->section == "new" ) {
220 $s.=wfMsg("commentedit");
221 } else {
222 $s.=wfMsg("sectionedit");
223 }
224 if(!$this->preview) {
225 $sectitle=preg_match("/^=+(.*?)=+/mi",
226 $this->textbox1,
227 $matches);
228 if($matches[1]) { $this->summary = "(".trim($matches[1]).")"; }
229 }
230 }
231 $wgOut->setPageTitle( $s );
232 if ( $this->oldid ) {
233 $this->mArticle->setOldSubtitle();
234 $wgOut->addHTML( wfMsg( "editingold" ) );
235 }
236 }
237
238 if( wfReadOnly() ) {
239 $wgOut->addHTML( "<strong>" .
240 wfMsg( "readonlywarning" ) .
241 "</strong>" );
242 }
243 if( $this->mTitle->isProtected() ) {
244 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
245 "</strong><br />\n" );
246 }
247
248 $kblength = (int)(strlen( $this->textbox1 ) / 1024);
249 if( $kblength > 29 ) {
250 $wgOut->addHTML( "<strong>" .
251 wfMsg( "longpagewarning", $kblength )
252 . "</strong>" );
253 }
254
255 $rows = $wgUser->getOption( "rows" );
256 $cols = $wgUser->getOption( "cols" );
257
258 $ew = $wgUser->getOption( "editwidth" );
259 if ( $ew ) $ew = " style=\"width:100%\"";
260 else $ew = "" ;
261
262 $q = "action=submit";
263 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
264 $action = $this->mTitle->escapeLocalURL( $q );
265
266 $summary = wfMsg( "summary" );
267 $subject = wfMsg("subject");
268 $minor = wfMsg( "minoredit" );
269 $watchthis = wfMsg ("watchthis");
270 $save = wfMsg( "savearticle" );
271 $prev = wfMsg( "showpreview" );
272
273 $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedURL(),
274 wfMsg( "cancel" ) );
275 $edithelp = $sk->makeKnownLink( wfMsg( "edithelppage" ),
276 wfMsg( "edithelp" ) );
277 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
278 wfMsg( "copyrightpage" ) ) );
279
280 if($wgUser->getOption("showtoolbar")) {
281 // prepare toolbar for edit buttons
282 $toolbar=$sk->getEditToolbar();
283 }
284
285 // activate checkboxes if user wants them to be always active
286 if( !$this->preview ) {
287 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis = true;
288 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit = true;
289
290 // activate checkbox also if user is already watching the page,
291 // require wpWatchthis to be unset so that second condition is not
292 // checked unnecessarily
293 if( !$this->watchthis && $this->mTitle->userIsWatching() ) $this->watchthis = true;
294 }
295
296 $minoredithtml = "";
297
298 if ( 0 != $wgUser->getID() || $wgAllowAnonymousMinor ) {
299 $minoredithtml =
300 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?" checked":"")." id='wpMinoredit'>".
301 "<label for='wpMinoredit'>{$minor}</label>";
302 }
303
304 $watchhtml = "";
305
306 if ( 0 != $wgUser->getID() ) {
307 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?" checked":"")." id='wpWatchthis'>".
308 "<label for='wpWatchthis'>{$watchthis}</label>";
309 }
310
311 $checkboxhtml = $minoredithtml . $watchhtml . "<br>";
312
313 if ( "preview" == $formtype) {
314 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
315 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large><p>\n";
316 if ( $isConflict ) {
317 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
318 "</h2>\n";
319 }
320 $previewtext = wfUnescapeHTML( $this->textbox1 );
321
322 $parserOptions = ParserOptions::newFromUser( $wgUser );
323 $parserOptions->setUseCategoryMagic( false );
324 $parserOptions->setEditSection( false );
325 $parserOptions->setEditSectionOnRightClick( false );
326 $parserOutput = $wgParser->parse( $this->mArticle->preSaveTransform( $previewtext ) ."\n\n",
327 $wgTitle, $parserOptions );
328 $previewHTML = $parserOutput->mText;
329
330 if($wgUser->getOption("previewontop")) {
331 $wgOut->addHTML($previewhead);
332 $wgOut->addHTML($previewHTML);
333 }
334 $wgOut->addHTML( "<br clear=\"all\" />\n" );
335 }
336
337 # if this is a comment, show a subject line at the top, which is also the edit summary.
338 # Otherwise, show a summary field at the bottom
339 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary ) ); # FIXME
340 if( $this->section == "new" ) {
341 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60'><br>";
342 $editsummary = "";
343 } else {
344 $commentsubject = "";
345 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60'><br>";
346 }
347
348 if( !$this->preview ) {
349 # Don't select the edit box on preview; this interferes with seeing what's going on.
350 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
351 }
352 $wgOut->addHTML( "
353 {$toolbar}
354 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
355 enctype=\"application/x-www-form-urlencoded\">
356 {$commentsubject}
357 <textarea tabindex='2' name=\"wpTextbox1\" rows='{$rows}'
358 cols='{$cols}'{$ew} wrap=\"virtual\">" .
359 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1 ) ) .
360 "
361 </textarea>
362 <br>{$editsummary}
363 {$checkboxhtml}
364 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"s\">
365 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"p\">
366 <em>{$cancel}</em> | <em>{$edithelp}</em>
367 <br><br>{$copywarn}
368 <input type=hidden value=\"" . htmlspecialchars( $this->section ) . "\" name=\"wpSection\">
369 <input type=hidden value=\"{$this->edittime}\" name=\"wpEdittime\">\n" );
370
371 if ( $isConflict ) {
372 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
373 DifferenceEngine::showDiff( $this->textbox2, $this->textbox1,
374 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
375
376 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
377 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
378 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2 ) ) .
379 "
380 </textarea>" );
381 }
382 $wgOut->addHTML( "</form>\n" );
383 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
384 $wgOut->addHTML($previewhead);
385 $wgOut->addHTML($previewHTML);
386 }
387
388 }
389
390 function blockedIPpage()
391 {
392 global $wgOut, $wgUser, $wgLang, $wgIP;
393
394 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
395 $wgOut->setRobotpolicy( "noindex,nofollow" );
396 $wgOut->setArticleRelated( false );
397
398 $id = $wgUser->blockedBy();
399 $reason = $wgUser->blockedFor();
400 $ip = $wgIP;
401
402 $name = User::whoIs( $id );
403 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
404 ":{$name}|{$name}]]";
405
406 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip ) );
407 $wgOut->returnToMain( false );
408 }
409
410
411
412 function userNotLoggedInPage()
413 {
414 global $wgOut, $wgUser, $wgLang;
415
416 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
417 $wgOut->setRobotpolicy( "noindex,nofollow" );
418 $wgOut->setArticleRelated( false );
419
420 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
421 $wgOut->returnToMain( false );
422 }
423
424 # Forks processes to scan the originating IP for an open proxy server
425 # MemCached can be used to skip IPs that have already been scanned
426 function proxyCheck()
427 {
428 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
429 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
430
431 if ( !$wgBlockOpenProxies ) {
432 return;
433 }
434
435 # Get MemCached key
436 $skip = false;
437 if ( $wgUseMemCached ) {
438 $mcKey = "$wgDBname:proxy:ip:$wgIP";
439 $mcValue = $wgMemc->get( $mcKey );
440 if ( $mcValue ) {
441 $skip = true;
442 }
443 }
444
445 # Fork the processes
446 if ( !$skip ) {
447 $title = Title::makeTitle( NS_SPECIAL, "Blockme" );
448 $url = $title->getFullURL();
449 foreach ( $wgProxyPorts as $port ) {
450 $params = implode( " ", array(
451 escapeshellarg( $wgProxyScriptPath ),
452 escapeshellarg( $wgIP ),
453 escapeshellarg( $port ),
454 escapeshellarg( $url )
455 ));
456 exec( "php $params &>/dev/null &" );
457 }
458 # Set MemCached key
459 if ( $wgUseMemCached ) {
460 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
461 }
462 }
463 }
464
465 /* private */ function mergeChangesInto( &$text ){
466 $oldDate = $this->edittime;
467 $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
468 $this->mTitle->getArticleID() . " FOR UPDATE", DB_WRITE);
469 $obj = wfFetchObject($res);
470
471 $yourtext = $obj->cur_text;
472 $ns = $this->mTitle->getNamespace();
473 $title = wfStrencode( $this->mTitle->getDBkey() );
474 $res = wfQuery("SELECT old_text FROM old WHERE old_namespace = $ns AND ".
475 "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE);
476 $obj = wfFetchObject($res);
477 if(wfMerge($obj->old_text, $text, $yourtext, $result)){
478 $text = $result;
479 return true;
480 } else {
481 return false;
482 }
483 }
484 }
485
486 ?>