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