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