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
13 var $save = false, $preview = false;
14 var $minoredit = false, $watchthis = false;
15 var $textbox1 = "", $textbox2 = "", $summary = "";
16 var $edittime = "", $section = "";
19 function EditPage( $article ) {
20 $this->mArticle
=& $article;
22 $this->mTitle
=& $wgTitle;
25 # This is the function that gets called for "action=edit".
29 global $wgOut, $wgUser, $wgWhitelistEdit, $wgRequest;
30 // this is not an article
31 $wgOut->setArticleFlag(false);
33 $this->importFormData( $wgRequest );
35 if ( ! $this->mTitle
->userCanEdit() ) {
36 $wgOut->readOnlyPage( $this->mArticle
->getContent(), true );
39 if ( $wgUser->isBlocked() ) {
40 $this->blockedIPpage();
43 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
44 $this->userNotLoggedInPage();
48 if( $this->save ||
$this->preview
) {
49 $this->editForm( "preview" );
51 $wgOut->readOnlyPage( $this->mArticle
->getContent() );
55 if( !$wgRequest->wasPosted() ) $this->save
= false;
57 $this->editForm( "save" );
58 } else if ( $this->preview
) {
59 $this->editForm( "preview" );
60 } else { # First time through
61 $this->editForm( "initial" );
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" ) );
73 $this->edittime
= $request->getVal( 'wpEdittime' );
74 if( !preg_match( '/^\d{14}$/', $this->edittime
) ) $this->edittime
= "";
76 $this->save
= $request->getCheck( 'wpSave' );
77 $this->preview
= $request->getCheck( 'wpPreview' );
78 $this->minoredit
= $request->getCheck( 'wpMinoredit' );
79 $this->watchthis
= $request->getCheck( 'wpWatchthis' );
81 $this->oldid
= $request->getInt( 'oldid' );
83 # Section edit can come from either the form or a link
84 $this->section
= $request->getVal( 'wpSection', $request->getVal( 'section' ) );
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.
95 if( !$this->preview
) $this->save
= true;
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.
106 function editForm( $formtype )
108 global $wgOut, $wgUser;
109 global $wgLang, $wgParser, $wgTitle;
110 global $wgAllowAnonymousMinor;
112 $sk = $wgUser->getSkin();
115 if(!$this->mTitle
->getArticleID()) { # new article
116 $wgOut->addWikiText(wfmsg("newarticletext"));
119 $talknamespaces = array( NS_TALK
, NS_WP_TALK
, NS_IMAGE_TALK
, NS_MEDIAWIKI_TALK
);
120 if( in_array( $this->mTitle
->getNamespace(), $talknamespaces ) ) {
121 $wgOut->addWikiText(wfmsg("talkpagetext"));
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.
129 if ( "save" == $formtype ) {
130 if ( $wgUser->isBlocked() ) {
131 $this->blockedIPpage();
134 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
135 $this->userNotLoggedInPage();
138 if ( wfReadOnly() ) {
139 $wgOut->readOnlyPage();
142 # If article is new, insert it.
144 $aid = $this->mTitle
->getArticleID();
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() );
152 $this->mArticle
->insertNewArticle( $this->textbox1
, $this->summary
, $this->minoredit
, $this->watchthis
);
155 # Article exists. Check for edit conflict.
156 # Don't check for conflict when appending a comment - this should always work
158 $this->mArticle
->clear(); # Force reload of dates, etc.
159 if( ( $this->section
!= "new" ) &&
160 ( $this->mArticle
->getTimestamp() != $this->edittime
) ) {
163 $userid = $wgUser->getID();
165 # Suppress edit conflict with self
167 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
170 # switch from section editing to normal editing in edit conflict
171 # FIXME: This is confusing. In theory we should attempt to merge, finding
172 # the equivalent section if it's unchanged and avoid the conflict.
177 if ( ! $isConflict ) {
178 # All's well: update the article here
179 if($this->mArticle
->updateArticle( $this->textbox1
, $this->summary
, $this->minoredit
, $this->watchthis
, $this->section
))
185 # First time through: get contents, set time for conflict
188 if ( "initial" == $formtype ) {
189 $this->edittime
= $this->mArticle
->getTimestamp();
190 $this->textbox1
= $this->mArticle
->getContent(true);
193 $wgOut->setRobotpolicy( "noindex,nofollow" );
195 # Enabled article-related sidebar, toplinks, etc.
196 $wgOut->setArticleRelated( true );
199 $s = wfMsg( "editconflict", $this->mTitle
->getPrefixedText() );
200 $wgOut->setPageTitle( $s );
201 $wgOut->addHTML( wfMsg( "explainconflict" ) );
203 $this->textbox2
= $this->textbox1
;
204 $this->textbox1
= $this->mArticle
->getContent(true);
205 $this->edittime
= $this->mArticle
->getTimestamp();
207 $s = wfMsg( "editing", $this->mTitle
->getPrefixedText() );
209 if( $this->section
!= "" ) {
210 if( $this->section
== "new" ) {
211 $s.=wfMsg("commentedit");
213 $s.=wfMsg("sectionedit");
216 $wgOut->setPageTitle( $s );
217 if ( $this->oldid
) {
218 $this->mArticle
->setOldSubtitle();
219 $wgOut->addHTML( wfMsg( "editingold" ) );
224 $wgOut->addHTML( "<strong>" .
225 wfMsg( "readonlywarning" ) .
228 if( $this->mTitle
->isProtected() ) {
229 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
230 "</strong><br />\n" );
233 $kblength = (int)(strlen( $this->textbox1
) / 1024);
234 if( $kblength > 29 ) {
235 $wgOut->addHTML( "<strong>" .
236 wfMsg( "longpagewarning", $kblength )
240 $rows = $wgUser->getOption( "rows" );
241 $cols = $wgUser->getOption( "cols" );
243 $ew = $wgUser->getOption( "editwidth" );
244 if ( $ew ) $ew = " style=\"width:100%\"";
247 $q = "action=submit";
248 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
249 $action = $this->mTitle
->escapeLocalURL( $q );
251 $summary = wfMsg( "summary" );
252 $subject = wfMsg("subject");
253 $minor = wfMsg( "minoredit" );
254 $watchthis = wfMsg ("watchthis");
255 $save = wfMsg( "savearticle" );
256 $prev = wfMsg( "showpreview" );
258 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedURL(),
260 $edithelp = $sk->makeKnownLink( wfMsg( "edithelppage" ),
261 wfMsg( "edithelp" ) );
262 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
263 wfMsg( "copyrightpage" ) ) );
265 if($wgUser->getOption("showtoolbar")) {
266 // prepare toolbar for edit buttons
267 $toolbar=$sk->getEditToolbar();
270 // activate checkboxes if user wants them to be always active
271 if( !$this->preview
) {
272 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis
= true;
273 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit
= true;
275 // activate checkbox also if user is already watching the page,
276 // require wpWatchthis to be unset so that second condition is not
277 // checked unnecessarily
278 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
283 if ( 0 != $wgUser->getID() ||
$wgAllowAnonymousMinor ) {
285 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked":"")." id='wpMinoredit'>".
286 "<label for='wpMinoredit'>{$minor}</label>";
291 if ( 0 != $wgUser->getID() ) {
292 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked":"")." id='wpWatchthis'>".
293 "<label for='wpWatchthis'>{$watchthis}</label>";
296 $checkboxhtml = $minoredithtml . $watchhtml . "<br>";
298 if ( "preview" == $formtype) {
299 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
300 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large><p>\n";
302 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
305 $previewtext = wfUnescapeHTML( $this->textbox1
);
307 $parserOptions = ParserOptions
::newFromUser( $wgUser );
308 $parserOptions->setUseCategoryMagic( false );
309 $parserOptions->setEditSection( false );
310 $parserOptions->setEditSectionOnRightClick( false );
311 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $previewtext ) ."\n\n",
312 $wgTitle, $parserOptions );
313 $previewHTML = $parserOutput->mText
;
315 if($wgUser->getOption("previewontop")) {
316 $wgOut->addHTML($previewhead);
317 $wgOut->addHTML($previewHTML);
319 $wgOut->addHTML( "<br clear=\"all\" />\n" );
322 # if this is a comment, show a subject line at the top, which is also the edit summary.
323 # Otherwise, show a summary field at the bottom
324 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary
) ); # FIXME
325 if( $this->section
== "new" ) {
326 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60'><br>";
329 $commentsubject = "";
330 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60'><br>";
333 if( !$this->preview
) {
334 # Don't select the edit box on preview; this interferes with seeing what's going on.
335 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
339 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
340 enctype=\"application/x-www-form-urlencoded\">
342 <textarea tabindex='2' name=\"wpTextbox1\" rows='{$rows}'
343 cols='{$cols}'{$ew} wrap=\"virtual\">" .
344 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1
) ) .
349 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"s\">
350 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"p\">
351 <em>{$cancel}</em> | <em>{$edithelp}</em>
353 <input type=hidden value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\">
354 <input type=hidden value=\"{$this->edittime}\" name=\"wpEdittime\">\n" );
357 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
358 DifferenceEngine
::showDiff( $wpTextbox2, $wpTextbox1,
359 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
361 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
362 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
363 . htmlspecialchars( $wgLang->recodeForEdit( $wpTextbox2 ) ) .
367 $wgOut->addHTML( "</form>\n" );
368 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
369 $wgOut->addHTML($previewhead);
370 $wgOut->addHTML($previewHTML);
375 function blockedIPpage()
377 global $wgOut, $wgUser, $wgLang, $wgIP;
379 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
380 $wgOut->setRobotpolicy( "noindex,nofollow" );
381 $wgOut->setArticleRelated( false );
383 $id = $wgUser->blockedBy();
384 $reason = $wgUser->blockedFor();
387 $name = User
::whoIs( $id );
388 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
389 ":{$name}|{$name}]]";
391 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip ) );
392 $wgOut->returnToMain( false );
397 function userNotLoggedInPage()
399 global $wgOut, $wgUser, $wgLang;
401 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
402 $wgOut->setRobotpolicy( "noindex,nofollow" );
403 $wgOut->setArticleRelated( false );
405 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
406 $wgOut->returnToMain( false );