no javascript: of course
[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 ), 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( true ) );
52 }
53 return;
54 }
55 if ( $this->save ) {
56 $this->editForm( "save" );
57 } else if ( $this->preview ) {
58 $this->editForm( "preview" );
59 } else { # First time through
60 $this->editForm( "initial" );
61 }
62 }
63
64 function importFormData( &$request ) {
65 # These fields need to be checked for encoding.
66 # Also remove trailing whitespace, but don't remove _initial_
67 # whitespace from the text boxes. This may be significant formatting.
68 $this->textbox1 = rtrim( $request->getText( "wpTextbox1" ) );
69 $this->textbox2 = rtrim( $request->getText( "wpTextbox2" ) );
70 $this->summary = trim( $request->getText( "wpSummary" ) );
71
72 $this->edittime = $request->getVal( 'wpEdittime' );
73 if( !preg_match( '/^\d{14}$/', $this->edittime ) ) $this->edittime = "";
74
75 $this->preview = $request->getCheck( 'wpPreview' );
76 $this->save = $request->wasPosted() && !$this->preview;
77 $this->minoredit = $request->getCheck( 'wpMinoredit' );
78 $this->watchthis = $request->getCheck( 'wpWatchthis' );
79
80 $this->oldid = $request->getInt( 'oldid' );
81
82 # Section edit can come from either the form or a link
83 $this->section = $request->getVal( 'wpSection', $request->getVal( 'section' ) );
84 }
85
86 # Since there is only one text field on the edit form,
87 # pressing <enter> will cause the form to be submitted, but
88 # the submit button value won't appear in the query, so we
89 # Fake it here before going back to edit(). This is kind of
90 # ugly, but it helps some old URLs to still work.
91
92 function submit()
93 {
94 if( !$this->preview ) $this->save = true;
95
96 $this->edit();
97 }
98
99 # The edit form is self-submitting, so that when things like
100 # preview and edit conflicts occur, we get the same form back
101 # with the extra stuff added. Only when the final submission
102 # is made and all is well do we actually save and redirect to
103 # the newly-edited page.
104
105 function editForm( $formtype )
106 {
107 global $wgOut, $wgUser;
108 global $wgLang, $wgParser, $wgTitle;
109 global $wgAllowAnonymousMinor;
110 global $wgWhitelistEdit;
111 global $wgSpamRegex;
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 # Check for spam
131 if ( $wgSpamRegex && preg_match( $wgSpamRegex, $wpTextbox1 ) ) {
132 sleep(10);
133 $wgOut->redirect( $this->mTitle->getFullURL() );
134 return;
135 }
136 if ( $wgUser->isBlocked() ) {
137 $this->blockedIPpage();
138 return;
139 }
140 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
141 $this->userNotLoggedInPage();
142 return;
143 }
144 if ( wfReadOnly() ) {
145 $wgOut->readOnlyPage();
146 return;
147 }
148
149 # If article is new, insert it.
150 $aid = $this->mTitle->getArticleID();
151 if ( 0 == $aid ) {
152 # Don't save a new article if it's blank.
153 if ( ( "" == $this->textbox1 ) ||
154 ( wfMsg( "newarticletext" ) == $this->textbox1 ) ) {
155 $wgOut->redirect( $this->mTitle->getFullURL() );
156 return;
157 }
158 $this->mArticle->insertNewArticle( $this->textbox1, $this->summary, $this->minoredit, $this->watchthis );
159 return;
160 }
161
162 # Article exists. Check for edit conflict.
163
164 $this->mArticle->clear(); # Force reload of dates, etc.
165
166 if( ( $this->section != "new" ) &&
167 ($this->mArticle->getTimestamp() != $this->edittime ) ) {
168 $isConflict = true;
169 }
170 $userid = $wgUser->getID();
171
172 $text = $this->mArticle->getTextOfLastEditWithSectionReplacedOrAdded(
173 $this->section, $this->textbox1, $this->summary);
174 # Suppress edit conflict with self
175
176 if ( ( 0 != $userid ) && ( $this->mArticle->getUser() == $userid ) ) {
177 $isConflict = false;
178 } else {
179 # switch from section editing to normal editing in edit conflict
180 if($isConflict) {
181 # Attempt merge
182 if( $this->mergeChangesInto( $text ) ){
183 // Successful merge! Maybe we should tell the user the good news?
184 $isConflict = false;
185 } else {
186 $this->section = "";
187 $this->textbox1 = $text;
188 }
189 }
190 }
191 if ( ! $isConflict ) {
192 # All's well: update the article here
193 if($this->mArticle->updateArticle( $text, $this->summary, $this->minoredit, $this->watchthis ))
194 return;
195 else
196 $isConflict = true;
197 }
198 }
199 # First time through: get contents, set time for conflict
200 # checking, etc.
201
202 if ( "initial" == $formtype ) {
203 $this->edittime = $this->mArticle->getTimestamp();
204 $this->textbox1 = $this->mArticle->getContent( true );
205 $this->summary = "";
206 $this->proxyCheck();
207 }
208 $wgOut->setRobotpolicy( "noindex,nofollow" );
209
210 # Enabled article-related sidebar, toplinks, etc.
211 $wgOut->setArticleRelated( true );
212
213 if ( $isConflict ) {
214 $s = wfMsg( "editconflict", $this->mTitle->getPrefixedText() );
215 $wgOut->setPageTitle( $s );
216 $wgOut->addHTML( wfMsg( "explainconflict" ) );
217
218 $this->textbox2 = $this->textbox1;
219 $this->textbox1 = $this->mArticle->getContent( true );
220 $this->edittime = $this->mArticle->getTimestamp();
221 } else {
222 $s = wfMsg( "editing", $this->mTitle->getPrefixedText() );
223
224 if( $this->section != "" ) {
225 if( $this->section == "new" ) {
226 $s.=wfMsg("commentedit");
227 } else {
228 $s.=wfMsg("sectionedit");
229 }
230 if(!$this->preview) {
231 $sectitle=preg_match("/^=+(.*?)=+/mi",
232 $this->textbox1,
233 $matches);
234 if( !empty( $matches[1] ) ) {
235 $this->summary = "/* ". trim($matches[1])." */ ";
236 }
237 }
238 }
239 $wgOut->setPageTitle( $s );
240 if ( $this->oldid ) {
241 $this->mArticle->setOldSubtitle();
242 $wgOut->addHTML( wfMsg( "editingold" ) );
243 }
244 }
245
246 if( wfReadOnly() ) {
247 $wgOut->addHTML( "<strong>" .
248 wfMsg( "readonlywarning" ) .
249 "</strong>" );
250 }
251 if( $this->mTitle->isProtected() ) {
252 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
253 "</strong><br />\n" );
254 }
255
256 $kblength = (int)(strlen( $this->textbox1 ) / 1024);
257 if( $kblength > 29 ) {
258 $wgOut->addHTML( "<strong>" .
259 wfMsg( "longpagewarning", $kblength )
260 . "</strong>" );
261 }
262
263 $rows = $wgUser->getOption( "rows" );
264 $cols = $wgUser->getOption( "cols" );
265
266 $ew = $wgUser->getOption( "editwidth" );
267 if ( $ew ) $ew = " style=\"width:100%\"";
268 else $ew = "" ;
269
270 $q = "action=submit";
271 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
272 $action = $this->mTitle->escapeLocalURL( $q );
273
274 $summary = wfMsg( "summary" );
275 $subject = wfMsg("subject");
276 $minor = wfMsg( "minoredit" );
277 $watchthis = wfMsg ("watchthis");
278 $save = wfMsg( "savearticle" );
279 $prev = wfMsg( "showpreview" );
280
281 $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedURL(),
282 wfMsg( "cancel" ) );
283 $edithelpurl = $sk->makeUrl( wfMsg( "edithelppage" ));
284 $edithelp = '<a onclick="window.open('.
285 "'$edithelpurl', 'helpwindow', 'width=610,height=400,left=10,top=10'".'); return false;" href="'.$edithelpurl.'">'.
286 wfMsg( "edithelp" ).'</a>';
287 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
288 wfMsg( "copyrightpage" ) ) );
289
290 if( $wgUser->getOption("showtoolbar") ) {
291 # prepare toolbar for edit buttons
292 $toolbar = $sk->getEditToolbar();
293 } else {
294 $toolbar = "";
295 }
296
297 // activate checkboxes if user wants them to be always active
298 if( !$this->preview ) {
299 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis = true;
300 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit = true;
301
302 // activate checkbox also if user is already watching the page,
303 // require wpWatchthis to be unset so that second condition is not
304 // checked unnecessarily
305 if( !$this->watchthis && $this->mTitle->userIsWatching() ) $this->watchthis = true;
306 }
307
308 $minoredithtml = "";
309
310 if ( 0 != $wgUser->getID() || $wgAllowAnonymousMinor ) {
311 $minoredithtml =
312 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?" checked='checked'":"").
313 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
314 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
315 }
316
317 $watchhtml = "";
318
319 if ( 0 != $wgUser->getID() ) {
320 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?" checked='checked'":"").
321 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
322 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
323 }
324
325 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
326
327 if ( "preview" == $formtype) {
328 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
329 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large></p>\n";
330 if ( $isConflict ) {
331 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
332 "</h2>\n";
333 }
334 $previewtext = wfUnescapeHTML( $this->textbox1 );
335
336 $parserOptions = ParserOptions::newFromUser( $wgUser );
337 $parserOptions->setUseCategoryMagic( false );
338 $parserOptions->setEditSection( false );
339 $parserOptions->setEditSectionOnRightClick( false );
340 $parserOutput = $wgParser->parse( $this->mArticle->preSaveTransform( $previewtext ) ."\n\n",
341 $wgTitle, $parserOptions );
342 $previewHTML = $parserOutput->mText;
343
344 if($wgUser->getOption("previewontop")) {
345 $wgOut->addHTML($previewhead);
346 $wgOut->addHTML($previewHTML);
347 }
348 $wgOut->addHTML( "<br clear=\"all\" />\n" );
349 }
350
351 # if this is a comment, show a subject line at the top, which is also the edit summary.
352 # Otherwise, show a summary field at the bottom
353 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary ) ); # FIXME
354 if( $this->section == "new" ) {
355 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
356 $editsummary = "";
357 } else {
358 $commentsubject = "";
359 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
360 }
361
362 if( !$this->preview ) {
363 # Don't select the edit box on preview; this interferes with seeing what's going on.
364 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
365 }
366 $wgOut->addHTML( "
367 {$toolbar}
368 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
369 enctype=\"application/x-www-form-urlencoded\">
370 {$commentsubject}
371 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
372 cols='{$cols}'{$ew}>" .
373 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1 ) ) .
374 "
375 </textarea>
376 <br />{$editsummary}
377 {$checkboxhtml}
378 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
379 " title=\"".wfMsg('tooltip-save')."\"/>
380 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
381 " title=\"".wfMsg('tooltip-preview')."\"/>
382 <em>{$cancel}</em> | <em>{$edithelp}</em>
383 <br /><br />{$copywarn}
384 <input type='hidden' value=\"" . htmlspecialchars( $this->section ) . "\" name=\"wpSection\" />
385 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
386
387 if ( $isConflict ) {
388 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
389 DifferenceEngine::showDiff( $this->textbox2, $this->textbox1,
390 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
391
392 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
393 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
394 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2 ) ) .
395 "
396 </textarea>" );
397 }
398 $wgOut->addHTML( "</form>\n" );
399 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
400 $wgOut->addHTML($previewhead);
401 $wgOut->addHTML($previewHTML);
402 }
403
404 }
405
406 function blockedIPpage()
407 {
408 global $wgOut, $wgUser, $wgLang, $wgIP;
409
410 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
411 $wgOut->setRobotpolicy( "noindex,nofollow" );
412 $wgOut->setArticleRelated( false );
413
414 $id = $wgUser->blockedBy();
415 $reason = $wgUser->blockedFor();
416 $ip = $wgIP;
417
418 $name = User::whoIs( $id );
419 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
420 ":{$name}|{$name}]]";
421
422 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip, $name ) );
423 $wgOut->returnToMain( false );
424 }
425
426
427
428 function userNotLoggedInPage()
429 {
430 global $wgOut, $wgUser, $wgLang;
431
432 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
433 $wgOut->setRobotpolicy( "noindex,nofollow" );
434 $wgOut->setArticleRelated( false );
435
436 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
437 $wgOut->returnToMain( false );
438 }
439
440 # Forks processes to scan the originating IP for an open proxy server
441 # MemCached can be used to skip IPs that have already been scanned
442 function proxyCheck()
443 {
444 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
445 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
446
447 if ( !$wgBlockOpenProxies ) {
448 return;
449 }
450
451 # Get MemCached key
452 $skip = false;
453 if ( $wgUseMemCached ) {
454 $mcKey = "$wgDBname:proxy:ip:$wgIP";
455 $mcValue = $wgMemc->get( $mcKey );
456 if ( $mcValue ) {
457 $skip = true;
458 }
459 }
460
461 # Fork the processes
462 if ( !$skip ) {
463 $title = Title::makeTitle( NS_SPECIAL, "Blockme" );
464 $iphash = md5( $wgIP . $wgProxyKey );
465 $url = $title->getFullURL( "ip=$iphash" );
466
467 foreach ( $wgProxyPorts as $port ) {
468 $params = implode( " ", array(
469 escapeshellarg( $wgProxyScriptPath ),
470 escapeshellarg( $wgIP ),
471 escapeshellarg( $port ),
472 escapeshellarg( $url )
473 ));
474 exec( "php $params &>/dev/null &" );
475 }
476 # Set MemCached key
477 if ( $wgUseMemCached ) {
478 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
479 }
480 }
481 }
482
483 /* private */ function mergeChangesInto( &$text ){
484 $oldDate = $this->edittime;
485 $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
486 $this->mTitle->getArticleID() . " FOR UPDATE", DB_WRITE);
487 $obj = wfFetchObject($res);
488
489 $yourtext = $obj->cur_text;
490 $ns = $this->mTitle->getNamespace();
491 $title = wfStrencode( $this->mTitle->getDBkey() );
492 $res = wfQuery("SELECT old_text FROM old WHERE old_namespace = $ns AND ".
493 "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE);
494 $obj = wfFetchObject($res);
495 if(wfMerge($obj->old_text, $text, $yourtext, $result)){
496 $text = $result;
497 return true;
498 } else {
499 return false;
500 }
501 }
502 }
503
504 ?>