Added per page support for inhibiting editsection links via the __NOEDITSECTION__...
[mediawiki.git] / includes / EditPage.php
blobf85a42420bbef30f1f8c8841e6b07ca639d8312c
1 <?
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.
8 class EditPage {
9 var $mArticle;
10 var $mTitle;
12 function EditPage( $article ) {
13 $this->mArticle =& $article;
14 global $wgTitle;
15 $this->mTitle =& $wgTitle;
18 # This is the function that gets called for "action=edit".
20 function edit()
22 global $wgOut, $wgUser, $wgWhitelistEdit;
23 global $wpTextbox1, $wpSummary, $wpSave, $wpPreview;
24 global $wpMinoredit, $wpEdittime, $wpTextbox2;
26 $fields = array( "wpTextbox1", "wpSummary", "wpTextbox2" );
27 wfCleanFormFields( $fields );
29 if ( ! $this->mTitle->userCanEdit() ) {
30 $this->mArticle->view();
31 return;
33 if ( $wgUser->isBlocked() ) {
34 $this->blockedIPpage();
35 return;
37 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
38 $this->userNotLoggedInPage();
39 return;
41 if ( wfReadOnly() ) {
42 if( isset( $wpSave ) or isset( $wpPreview ) ) {
43 $this->editForm( "preview" );
44 } else {
45 $wgOut->readOnlyPage( $this->mArticle->getContent() );
47 return;
49 if ( $_SERVER['REQUEST_METHOD'] != "POST" ) unset( $wpSave );
50 if ( isset( $wpSave ) ) {
51 $this->editForm( "save" );
52 } else if ( isset( $wpPreview ) ) {
53 $this->editForm( "preview" );
54 } else { # First time through
55 $this->editForm( "initial" );
59 # Since there is only one text field on the edit form,
60 # pressing <enter> will cause the form to be submitted, but
61 # the submit button value won't appear in the query, so we
62 # Fake it here before going back to edit(). This is kind of
63 # ugly, but it helps some old URLs to still work.
65 function submit()
67 global $wpSave, $wpPreview;
68 if ( ! isset( $wpPreview ) ) { $wpSave = 1; }
70 $this->edit();
73 # The edit form is self-submitting, so that when things like
74 # preview and edit conflicts occur, we get the same form back
75 # with the extra stuff added. Only when the final submission
76 # is made and all is well do we actually save and redirect to
77 # the newly-edited page.
79 function editForm( $formtype )
81 global $wgOut, $wgUser;
82 global $wpTextbox1, $wpSummary, $wpWatchthis;
83 global $wpSave, $wpPreview;
84 global $wpMinoredit, $wpEdittime, $wpTextbox2, $wpSection;
85 global $oldid, $redirect, $section;
86 global $wgLang;
88 if(isset($wpSection)) { $section=$wpSection; } else { $wpSection=$section; }
90 $sk = $wgUser->getSkin();
91 $isConflict = false;
92 $wpTextbox1 = rtrim ( $wpTextbox1 ) ; # To avoid text getting longer on each preview
94 if(!$this->mTitle->getArticleID()) { # new article
96 $wgOut->addWikiText(wfmsg("newarticletext"));
100 # Attempt submission here. This will check for edit conflicts,
101 # and redundantly check for locked database, blocked IPs, etc.
102 # that edit() already checked just in case someone tries to sneak
103 # in the back door with a hand-edited submission URL.
105 if ( "save" == $formtype ) {
106 if ( $wgUser->isBlocked() ) {
107 $this->blockedIPpage();
108 return;
110 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
111 $this->userNotLoggedInPage();
112 return;
114 if ( wfReadOnly() ) {
115 $wgOut->readOnlyPage();
116 return;
118 # If article is new, insert it.
120 $aid = $this->mTitle->getArticleID();
121 if ( 0 == $aid ) {
122 # we need to strip Windoze linebreaks because some browsers
123 # append them and the string comparison fails
124 if ( ( "" == $wpTextbox1 ) ||
125 ( wfMsg( "newarticletext" ) == rtrim( preg_replace("/\r/","",$wpTextbox1) ) ) ) {
126 $wgOut->redirect( wfLocalUrl(
127 $this->mTitle->getPrefixedURL() ) );
128 return;
130 $this->mCountAdjustment = $this->mArticle->isCountable( $wpTextbox1 );
131 $this->mArticle->insertNewArticle( $wpTextbox1, $wpSummary, $wpMinoredit, $wpWatchthis );
132 return;
134 # Article exists. Check for edit conflict.
135 # Don't check for conflict when appending a comment - this should always work
137 $this->mArticle->clear(); # Force reload of dates, etc.
138 if ( $section!="new" && ( $this->mArticle->getTimestamp() != $wpEdittime ) ) {
139 $isConflict = true;
141 $u = $wgUser->getID();
143 # Supress edit conflict with self
145 if ( ( 0 != $u ) && ( $this->mArticle->getUser() == $u ) ) {
146 $isConflict = false;
147 } else {
148 # switch from section editing to normal editing in edit conflict
149 if($isConflict) {
150 $section="";$wpSection="";
154 if ( ! $isConflict ) {
155 # All's well: update the article here
156 if($this->mArticle->updateArticle( $wpTextbox1, $wpSummary, $wpMinoredit, $wpWatchthis, $wpSection ))
157 return;
158 else
159 $isConflict = true;
162 # First time through: get contents, set time for conflict
163 # checking, etc.
165 if ( "initial" == $formtype ) {
166 $wpEdittime = $this->mArticle->getTimestamp();
167 $wpTextbox1 = $this->mArticle->getContent(true);
168 $wpSummary = "";
170 $wgOut->setRobotpolicy( "noindex,nofollow" );
171 $wgOut->setArticleFlag( false );
173 if ( $isConflict ) {
174 $s = str_replace( "$1", $this->mTitle->getPrefixedText(),
175 wfMsg( "editconflict" ) );
176 $wgOut->setPageTitle( $s );
177 $wgOut->addHTML( wfMsg( "explainconflict" ) );
179 $wpTextbox2 = $wpTextbox1;
180 $wpTextbox1 = $this->mArticle->getContent(true);
181 $wpEdittime = $this->mArticle->getTimestamp();
182 } else {
183 $s = str_replace( "$1", $this->mTitle->getPrefixedText(),
184 wfMsg( "editing" ) );
186 if($section!="") {
187 if($section=="new") {
188 $s.=wfMsg("commentedit");
189 } else {
190 $s.=wfMsg("sectionedit");
193 $wgOut->setPageTitle( $s );
194 if ( $oldid ) {
195 $this->mArticle->setOldSubtitle();
196 $wgOut->addHTML( wfMsg( "editingold" ) );
200 if( wfReadOnly() ) {
201 $wgOut->addHTML( "<strong>" .
202 wfMsg( "readonlywarning" ) .
203 "</strong>" );
205 if( $this->mTitle->isProtected() ) {
206 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
207 "</strong><br />\n" );
210 $kblength = (int)(strlen( $wpTextbox1 ) / 1024);
211 if( $kblength > 29 ) {
212 $wgOut->addHTML( "<strong>" .
213 str_replace( '$1', $kblength , wfMsg( "longpagewarning" ) )
214 . "</strong>" );
217 $rows = $wgUser->getOption( "rows" );
218 $cols = $wgUser->getOption( "cols" );
220 $ew = $wgUser->getOption( "editwidth" );
221 if ( $ew ) $ew = " style=\"width:100%\"";
222 else $ew = "" ;
224 $q = "action=submit";
225 if ( "no" == $redirect ) { $q .= "&redirect=no"; }
226 $action = wfEscapeHTML( wfLocalUrl( $this->mTitle->getPrefixedURL(), $q ) );
228 $summary = wfMsg( "summary" );
229 $subject = wfMsg("subject");
230 $minor = wfMsg( "minoredit" );
231 $watchthis = wfMsg ("watchthis");
232 $save = wfMsg( "savearticle" );
233 $prev = wfMsg( "showpreview" );
235 $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedURL(),
236 wfMsg( "cancel" ) );
237 $edithelp = $sk->makeKnownLink( wfMsg( "edithelppage" ),
238 wfMsg( "edithelp" ) );
239 $copywarn = str_replace( "$1", $sk->makeKnownLink(
240 wfMsg( "copyrightpage" ) ), wfMsg( "copyrightwarning" ) );
242 $wpTextbox1 = wfEscapeHTML( $wpTextbox1 );
243 $wpTextbox2 = wfEscapeHTML( $wpTextbox2 );
244 $wpSummary = wfEscapeHTML( $wpSummary );
246 // activate checkboxes if user wants them to be always active
247 if (!$wpPreview && $wgUser->getOption("watchdefault")) $wpWatchthis=1;
248 if (!$wpPreview && $wgUser->getOption("minordefault")) $wpMinoredit=1;
250 // activate checkbox also if user is already watching the page,
251 // require wpWatchthis to be unset so that second condition is not
252 // checked unnecessarily
253 if (!$wpWatchthis && !$wpPreview && $this->mTitle->userIsWatching()) $wpWatchthis=1;
255 if ( 0 != $wgUser->getID() ) {
256 $checkboxhtml=
257 "<input tabindex=3 type=checkbox value=1 name='wpMinoredit'".($wpMinoredit?" checked":"")." id='wpMinoredit'>".
258 "<label for='wpMinoredit'>{$minor}</label>".
259 "<input tabindex=4 type=checkbox name='wpWatchthis'".($wpWatchthis?" checked":"")." id='wpWatchthis'>".
260 "<label for='wpWatchthis'>{$watchthis}</label><br>";
262 } else {
263 $checkboxhtml="";
267 if ( "preview" == $formtype) {
269 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
270 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large><P>\n";
271 if ( $isConflict ) {
272 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
273 "</h2>\n";
275 $previewtext = wfUnescapeHTML( $wpTextbox1 );
277 if($wgUser->getOption("previewontop")) {
278 $wgOut->addHTML($previewhead);
279 $wgOut->addWikiText( $this->mArticle->preSaveTransform( $previewtext ) ."\n\n");
281 $wgOut->addHTML( "<br clear=\"all\" />\n" );
284 # if this is a comment, show a subject line at the top, which is also the edit summary.
285 # Otherwise, show a summary field at the bottom
286 if($section=="new") {
288 $commentsubject="{$subject}: <input tabindex=1 type=text value=\"{$wpSummary}\" name=\"wpSummary\" maxlength=200 size=60><br>";
289 } else {
291 $editsummary="{$summary}: <input tabindex=3 type=text value=\"{$wpSummary}\" name=\"wpSummary\" maxlength=200 size=60><br>";
294 $wgOut->addHTML( "
295 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
296 enctype=\"application/x-www-form-urlencoded\">
297 {$commentsubject}
298 <textarea tabindex=2 name=\"wpTextbox1\" rows={$rows}
299 cols={$cols}{$ew} wrap=\"virtual\">" .
300 $wgLang->recodeForEdit( $wpTextbox1 ) .
302 </textarea>
303 <br>{$editsummary}
304 {$checkboxhtml}
305 <input tabindex=5 type=submit value=\"{$save}\" name=\"wpSave\">
306 <input tabindex=6 type=submit value=\"{$prev}\" name=\"wpPreview\">
307 <em>{$cancel}</em> | <em>{$edithelp}</em>
308 <br><br>{$copywarn}
309 <input type=hidden value=\"{$section}\" name=\"wpSection\">
310 <input type=hidden value=\"{$wpEdittime}\" name=\"wpEdittime\">\n" );
312 if ( $isConflict ) {
313 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
314 DifferenceEngine::showDiff( $wpTextbox2, $wpTextbox1,
315 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
317 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
318 <textarea tabindex=6 name=\"wpTextbox2\" rows={$rows} cols={$cols} wrap=virtual>"
319 . $wgLang->recodeForEdit( $wpTextbox2 ) .
321 </textarea>" );
323 $wgOut->addHTML( "</form>\n" );
324 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
325 $wgOut->addHTML($previewhead);
326 $wgOut->addWikiText( $this->mArticle->preSaveTransform( $previewtext ) );
331 function blockedIPpage()
333 global $wgOut, $wgUser, $wgLang;
335 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
336 $wgOut->setRobotpolicy( "noindex,nofollow" );
337 $wgOut->setArticleFlag( false );
339 $id = $wgUser->blockedBy();
340 $reason = $wgUser->blockedFor();
342 $name = User::whoIs( $id );
343 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
344 ":{$name}|{$name}]]";
346 $text = str_replace( "$1", $link, wfMsg( "blockedtext" ) );
347 $text = str_replace( "$2", $reason, $text );
348 $wgOut->addWikiText( $text );
349 $wgOut->returnToMain( false );
354 function userNotLoggedInPage()
356 global $wgOut, $wgUser, $wgLang;
358 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
359 $wgOut->setRobotpolicy( "noindex,nofollow" );
360 $wgOut->setArticleFlag( false );
362 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
363 $wgOut->returnToMain( false );