2c04c1fdbdcd3eb49fe70ea5f6f86ab86c5c0432
[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, $wpTextbox1 ) ) {
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: update the article here
195 if($this->mArticle->updateArticle( $text, $this->summary, $this->minoredit, $this->watchthis ))
196 return;
197 else
198 $isConflict = true;
199 }
200 }
201 # First time through: get contents, set time for conflict
202 # checking, etc.
203
204 if ( "initial" == $formtype ) {
205 $this->edittime = $this->mArticle->getTimestamp();
206 $this->textbox1 = $this->mArticle->getContent( true );
207 $this->summary = "";
208 $this->proxyCheck();
209 }
210 $wgOut->setRobotpolicy( "noindex,nofollow" );
211
212 # Enabled article-related sidebar, toplinks, etc.
213 $wgOut->setArticleRelated( true );
214
215 if ( $isConflict ) {
216 $s = wfMsg( "editconflict", $this->mTitle->getPrefixedText() );
217 $wgOut->setPageTitle( $s );
218 $wgOut->addHTML( wfMsg( "explainconflict" ) );
219
220 $this->textbox2 = $this->textbox1;
221 $this->textbox1 = $this->mArticle->getContent( true );
222 $this->edittime = $this->mArticle->getTimestamp();
223 } else {
224 $s = wfMsg( "editing", $this->mTitle->getPrefixedText() );
225
226 if( $this->section != "" ) {
227 if( $this->section == "new" ) {
228 $s.=wfMsg("commentedit");
229 } else {
230 $s.=wfMsg("sectionedit");
231 }
232 if(!$this->preview) {
233 $sectitle=preg_match("/^=+(.*?)=+/mi",
234 $this->textbox1,
235 $matches);
236 if( !empty( $matches[1] ) ) {
237 $this->summary = "/* ". trim($matches[1])." */ ";
238 }
239 }
240 }
241 $wgOut->setPageTitle( $s );
242 if ( $this->oldid ) {
243 $this->mArticle->setOldSubtitle();
244 $wgOut->addHTML( wfMsg( "editingold" ) );
245 }
246 }
247
248 if( wfReadOnly() ) {
249 $wgOut->addHTML( "<strong>" .
250 wfMsg( "readonlywarning" ) .
251 "</strong>" );
252 } else if ( $isCssJsSubpage and "preview" != $formtype) {
253 $wgOut->addHTML( wfMsg( "usercssjsyoucanpreview" ));
254 }
255 if( $this->mTitle->isProtected() ) {
256 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
257 "</strong><br />\n" );
258 }
259
260 $kblength = (int)(strlen( $this->textbox1 ) / 1024);
261 if( $kblength > 29 ) {
262 $wgOut->addHTML( "<strong>" .
263 wfMsg( "longpagewarning", $kblength )
264 . "</strong>" );
265 }
266
267 $rows = $wgUser->getOption( "rows" );
268 $cols = $wgUser->getOption( "cols" );
269
270 $ew = $wgUser->getOption( "editwidth" );
271 if ( $ew ) $ew = " style=\"width:100%\"";
272 else $ew = "" ;
273
274 $q = "action=submit";
275 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
276 $action = $this->mTitle->escapeLocalURL( $q );
277
278 $summary = wfMsg( "summary" );
279 $subject = wfMsg("subject");
280 $minor = wfMsg( "minoredit" );
281 $watchthis = wfMsg ("watchthis");
282 $save = wfMsg( "savearticle" );
283 $prev = wfMsg( "showpreview" );
284
285 $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedURL(),
286 wfMsg( "cancel" ) );
287 $edithelpurl = $sk->makeUrl( wfMsg( "edithelppage" ));
288 $edithelp = '<a onclick="window.open('.
289 "'$edithelpurl', 'helpwindow', 'width=610,height=400,left=10,top=10'".'); return false;" href="'.$edithelpurl.'">'.
290 wfMsg( "edithelp" ).'</a>';
291 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
292 wfMsg( "copyrightpage" ) ) );
293
294 if( $wgUser->getOption("showtoolbar") and !$isCssJsSubpage ) {
295 # prepare toolbar for edit buttons
296 $toolbar = $sk->getEditToolbar();
297 } else {
298 $toolbar = "";
299 }
300
301 // activate checkboxes if user wants them to be always active
302 if( !$this->preview ) {
303 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis = true;
304 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit = true;
305
306 // activate checkbox also if user is already watching the page,
307 // require wpWatchthis to be unset so that second condition is not
308 // checked unnecessarily
309 if( !$this->watchthis && $this->mTitle->userIsWatching() ) $this->watchthis = true;
310 }
311
312 $minoredithtml = "";
313
314 if ( 0 != $wgUser->getID() || $wgAllowAnonymousMinor ) {
315 $minoredithtml =
316 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?" checked='checked'":"").
317 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
318 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
319 }
320
321 $watchhtml = "";
322
323 if ( 0 != $wgUser->getID() ) {
324 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?" checked='checked'":"").
325 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
326 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
327 }
328
329 $checkboxhtml = $minoredithtml . $watchhtml . "<br />";
330
331 if ( "preview" == $formtype) {
332 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
333 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large></p>\n";
334 if ( $isConflict ) {
335 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
336 "</h2>\n";
337 }
338 $previewtext = wfUnescapeHTML( $this->textbox1 );
339
340 $parserOptions = ParserOptions::newFromUser( $wgUser );
341 $parserOptions->setUseCategoryMagic( false );
342 $parserOptions->setEditSection( false );
343 $parserOptions->setEditSectionOnRightClick( false );
344 # don't parse user css/js, show message about preview
345 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
346 if ( $isCssJsSubpage ) {
347 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
348 $previewtext = wfMsg('usercsspreview');
349 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
350 $previewtext = wfMsg('userjspreview');
351 }
352 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
353 $wgOut->addHTML( $parserOutput->mText );
354 } else {
355 $parserOutput = $wgParser->parse( $this->mArticle->preSaveTransform( $previewtext ) ."\n\n",
356 $wgTitle, $parserOptions );
357 $previewHTML = $parserOutput->mText;
358
359 if($wgUser->getOption("previewontop")) {
360 $wgOut->addHTML($previewhead);
361 $wgOut->addHTML($previewHTML);
362 }
363 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
364 }
365 }
366
367 # if this is a comment, show a subject line at the top, which is also the edit summary.
368 # Otherwise, show a summary field at the bottom
369 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary ) ); # FIXME
370 if( $this->section == "new" ) {
371 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
372 $editsummary = "";
373 } else {
374 $commentsubject = "";
375 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
376 }
377
378 if( !$this->preview ) {
379 # Don't select the edit box on preview; this interferes with seeing what's going on.
380 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
381 }
382 $wgOut->addHTML( "
383 {$toolbar}
384 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
385 enctype=\"application/x-www-form-urlencoded\">
386 {$commentsubject}
387 <textarea tabindex='1' accesskey=\",\" name=\"wpTextbox1\" rows='{$rows}'
388 cols='{$cols}'{$ew}>" .
389 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1 ) ) .
390 "
391 </textarea>
392 <br />{$editsummary}
393 {$checkboxhtml}
394 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
395 " title=\"".wfMsg('tooltip-save')."\"/>
396 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
397 " title=\"".wfMsg('tooltip-preview')."\"/>
398 <em>{$cancel}</em> | <em>{$edithelp}</em>
399 <br /><br />{$copywarn}
400 <input type='hidden' value=\"" . htmlspecialchars( $this->section ) . "\" name=\"wpSection\" />
401 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
402
403 if ( $isConflict ) {
404 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
405 DifferenceEngine::showDiff( $this->textbox2, $this->textbox1,
406 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
407
408 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
409 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
410 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2 ) ) .
411 "
412 </textarea>" );
413 }
414 $wgOut->addHTML( "</form>\n" );
415 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
416 $wgOut->addHTML($previewhead);
417 $wgOut->addHTML($previewHTML);
418 }
419
420 }
421
422 function blockedIPpage()
423 {
424 global $wgOut, $wgUser, $wgLang, $wgIP;
425
426 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
427 $wgOut->setRobotpolicy( "noindex,nofollow" );
428 $wgOut->setArticleRelated( false );
429
430 $id = $wgUser->blockedBy();
431 $reason = $wgUser->blockedFor();
432 $ip = $wgIP;
433
434 $name = User::whoIs( $id );
435 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
436 ":{$name}|{$name}]]";
437
438 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip, $name ) );
439 $wgOut->returnToMain( false );
440 }
441
442
443
444 function userNotLoggedInPage()
445 {
446 global $wgOut, $wgUser, $wgLang;
447
448 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
449 $wgOut->setRobotpolicy( "noindex,nofollow" );
450 $wgOut->setArticleRelated( false );
451
452 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
453 $wgOut->returnToMain( false );
454 }
455
456 # Forks processes to scan the originating IP for an open proxy server
457 # MemCached can be used to skip IPs that have already been scanned
458 function proxyCheck()
459 {
460 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
461 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
462
463 if ( !$wgBlockOpenProxies ) {
464 return;
465 }
466
467 # Get MemCached key
468 $skip = false;
469 if ( $wgUseMemCached ) {
470 $mcKey = "$wgDBname:proxy:ip:$wgIP";
471 $mcValue = $wgMemc->get( $mcKey );
472 if ( $mcValue ) {
473 $skip = true;
474 }
475 }
476
477 # Fork the processes
478 if ( !$skip ) {
479 $title = Title::makeTitle( NS_SPECIAL, "Blockme" );
480 $iphash = md5( $wgIP . $wgProxyKey );
481 $url = $title->getFullURL( "ip=$iphash" );
482
483 foreach ( $wgProxyPorts as $port ) {
484 $params = implode( " ", array(
485 escapeshellarg( $wgProxyScriptPath ),
486 escapeshellarg( $wgIP ),
487 escapeshellarg( $port ),
488 escapeshellarg( $url )
489 ));
490 exec( "php $params &>/dev/null &" );
491 }
492 # Set MemCached key
493 if ( $wgUseMemCached ) {
494 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
495 }
496 }
497 }
498
499 /* private */ function mergeChangesInto( &$text ){
500 $oldDate = $this->edittime;
501 $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
502 $this->mTitle->getArticleID() . " FOR UPDATE", DB_WRITE);
503 $obj = wfFetchObject($res);
504
505 $yourtext = $obj->cur_text;
506 $ns = $this->mTitle->getNamespace();
507 $title = wfStrencode( $this->mTitle->getDBkey() );
508 $res = wfQuery("SELECT old_text FROM old WHERE old_namespace = $ns AND ".
509 "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE);
510 $obj = wfFetchObject($res);
511 if(wfMerge($obj->old_text, $text, $yourtext, $result)){
512 $text = $result;
513 return true;
514 } else {
515 return false;
516 }
517 }
518 }
519
520 ?>