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