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