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