Fix sort for proper index
[mediawiki.git] / includes / EditPage.php
blob43aafdf3686b92f2a77f853849e90a22e10f7433
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 = wfMsg( "editconflict", $this->mTitle->getPrefixedText() );
175 $wgOut->setPageTitle( $s );
176 $wgOut->addHTML( wfMsg( "explainconflict" ) );
178 $wpTextbox2 = $wpTextbox1;
179 $wpTextbox1 = $this->mArticle->getContent(true);
180 $wpEdittime = $this->mArticle->getTimestamp();
181 } else {
182 $s = wfMsg( "editing", $this->mTitle->getPrefixedText() );
184 if($section!="") {
185 if($section=="new") {
186 $s.=wfMsg("commentedit");
187 } else {
188 $s.=wfMsg("sectionedit");
191 $wgOut->setPageTitle( $s );
192 if ( $oldid ) {
193 $this->mArticle->setOldSubtitle();
194 $wgOut->addHTML( wfMsg( "editingold" ) );
198 if( wfReadOnly() ) {
199 $wgOut->addHTML( "<strong>" .
200 wfMsg( "readonlywarning" ) .
201 "</strong>" );
203 if( $this->mTitle->isProtected() ) {
204 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
205 "</strong><br />\n" );
208 $kblength = (int)(strlen( $wpTextbox1 ) / 1024);
209 if( $kblength > 29 ) {
210 $wgOut->addHTML( "<strong>" .
211 wfMsg( "longpagewarning", $kblength )
212 . "</strong>" );
215 $rows = $wgUser->getOption( "rows" );
216 $cols = $wgUser->getOption( "cols" );
218 $ew = $wgUser->getOption( "editwidth" );
219 if ( $ew ) $ew = " style=\"width:100%\"";
220 else $ew = "" ;
222 $q = "action=submit";
223 if ( "no" == $redirect ) { $q .= "&redirect=no"; }
224 $action = wfEscapeHTML( wfLocalUrl( $this->mTitle->getPrefixedURL(), $q ) );
226 $summary = wfMsg( "summary" );
227 $subject = wfMsg("subject");
228 $minor = wfMsg( "minoredit" );
229 $watchthis = wfMsg ("watchthis");
230 $save = wfMsg( "savearticle" );
231 $prev = wfMsg( "showpreview" );
233 $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedURL(),
234 wfMsg( "cancel" ) );
235 $edithelp = $sk->makeKnownLink( wfMsg( "edithelppage" ),
236 wfMsg( "edithelp" ) );
237 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
238 wfMsg( "copyrightpage" ) ) );
240 $wpTextbox1 = wfEscapeHTML( $wpTextbox1 );
241 $wpTextbox2 = wfEscapeHTML( $wpTextbox2 );
242 $wpSummary = wfEscapeHTML( $wpSummary );
244 // activate checkboxes if user wants them to be always active
245 if (!$wpPreview && $wgUser->getOption("watchdefault")) $wpWatchthis=1;
246 if (!$wpPreview && $wgUser->getOption("minordefault")) $wpMinoredit=1;
248 // activate checkbox also if user is already watching the page,
249 // require wpWatchthis to be unset so that second condition is not
250 // checked unnecessarily
251 if (!$wpWatchthis && !$wpPreview && $this->mTitle->userIsWatching()) $wpWatchthis=1;
253 if ( 0 != $wgUser->getID() ) {
254 $checkboxhtml=
255 "<input tabindex=3 type=checkbox value=1 name='wpMinoredit'".($wpMinoredit?" checked":"")." id='wpMinoredit'>".
256 "<label for='wpMinoredit'>{$minor}</label>".
257 "<input tabindex=4 type=checkbox name='wpWatchthis'".($wpWatchthis?" checked":"")." id='wpWatchthis'>".
258 "<label for='wpWatchthis'>{$watchthis}</label><br>";
260 } else {
261 $checkboxhtml="";
265 if ( "preview" == $formtype) {
267 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
268 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large><P>\n";
269 if ( $isConflict ) {
270 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
271 "</h2>\n";
273 $previewtext = wfUnescapeHTML( $wpTextbox1 );
275 if($wgUser->getOption("previewontop")) {
276 $wgOut->addHTML($previewhead);
277 $wgOut->addWikiText( $this->mArticle->preSaveTransform( $previewtext ) ."\n\n");
279 $wgOut->addHTML( "<br clear=\"all\" />\n" );
282 # if this is a comment, show a subject line at the top, which is also the edit summary.
283 # Otherwise, show a summary field at the bottom
284 if($section=="new") {
286 $commentsubject="{$subject}: <input tabindex=1 type=text value=\"{$wpSummary}\" name=\"wpSummary\" maxlength=200 size=60><br>";
287 } else {
289 $editsummary="{$summary}: <input tabindex=3 type=text value=\"{$wpSummary}\" name=\"wpSummary\" maxlength=200 size=60><br>";
292 $wgOut->addHTML( "
293 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
294 enctype=\"application/x-www-form-urlencoded\">
295 {$commentsubject}
296 <textarea tabindex=2 name=\"wpTextbox1\" rows={$rows}
297 cols={$cols}{$ew} wrap=\"virtual\">" .
298 $wgLang->recodeForEdit( $wpTextbox1 ) .
300 </textarea>
301 <br>{$editsummary}
302 {$checkboxhtml}
303 <input tabindex=5 type=submit value=\"{$save}\" name=\"wpSave\">
304 <input tabindex=6 type=submit value=\"{$prev}\" name=\"wpPreview\">
305 <em>{$cancel}</em> | <em>{$edithelp}</em>
306 <br><br>{$copywarn}
307 <input type=hidden value=\"{$section}\" name=\"wpSection\">
308 <input type=hidden value=\"{$wpEdittime}\" name=\"wpEdittime\">\n" );
310 if ( $isConflict ) {
311 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
312 DifferenceEngine::showDiff( $wpTextbox2, $wpTextbox1,
313 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
315 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
316 <textarea tabindex=6 name=\"wpTextbox2\" rows={$rows} cols={$cols} wrap=virtual>"
317 . $wgLang->recodeForEdit( $wpTextbox2 ) .
319 </textarea>" );
321 $wgOut->addHTML( "</form>\n" );
322 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
323 $wgOut->addHTML($previewhead);
324 $wgOut->addWikiText( $this->mArticle->preSaveTransform( $previewtext ) );
329 function blockedIPpage()
331 global $wgOut, $wgUser, $wgLang;
333 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
334 $wgOut->setRobotpolicy( "noindex,nofollow" );
335 $wgOut->setArticleFlag( false );
337 $id = $wgUser->blockedBy();
338 $reason = $wgUser->blockedFor();
340 $name = User::whoIs( $id );
341 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
342 ":{$name}|{$name}]]";
344 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason ) );
345 $wgOut->returnToMain( false );
350 function userNotLoggedInPage()
352 global $wgOut, $wgUser, $wgLang;
354 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
355 $wgOut->setRobotpolicy( "noindex,nofollow" );
356 $wgOut->setArticleFlag( false );
358 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
359 $wgOut->returnToMain( false );