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