use the one central list of talk namespaces, don't make your own every time you need one
[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
112 $sk = $wgUser->getSkin();
113 $isConflict = false;
114
115 if(!$this->mTitle->getArticleID()) { # new article
116 $wgOut->addWikiText(wfmsg("newarticletext"));
117 }
118
119 if( Namespace::isTalk( $this->mTitle->getNamespace() ) ) {
120 $wgOut->addWikiText(wfmsg("talkpagetext"));
121 }
122
123 # Attempt submission here. This will check for edit conflicts,
124 # and redundantly check for locked database, blocked IPs, etc.
125 # that edit() already checked just in case someone tries to sneak
126 # in the back door with a hand-edited submission URL.
127
128 if ( "save" == $formtype ) {
129 if ( $wgUser->isBlocked() ) {
130 $this->blockedIPpage();
131 return;
132 }
133 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
134 $this->userNotLoggedInPage();
135 return;
136 }
137 if ( wfReadOnly() ) {
138 $wgOut->readOnlyPage();
139 return;
140 }
141 # If article is new, insert it.
142
143 $aid = $this->mTitle->getArticleID();
144 if ( 0 == $aid ) {
145 # Don't save a new article if it's blank.
146 if ( ( "" == $this->textbox1 ) ||
147 ( wfMsg( "newarticletext" ) == $this->textbox1 ) ) {
148 $wgOut->redirect( $this->mTitle->getFullURL() );
149 return;
150 }
151 $this->mArticle->insertNewArticle( $this->textbox1, $this->summary, $this->minoredit, $this->watchthis );
152 return;
153 }
154 # Article exists. Check for edit conflict.
155 # Don't check for conflict when appending a comment - this should always work
156
157 $this->mArticle->clear(); # Force reload of dates, etc.
158 if( ( $this->section != "new" ) &&
159 ( $this->mArticle->getTimestamp() != $this->edittime ) ) {
160 $isConflict = true;
161 }
162 $userid = $wgUser->getID();
163
164 # Suppress edit conflict with self
165
166 if ( ( 0 != $userid ) && ( $this->mArticle->getUser() == $userid ) ) {
167 $isConflict = false;
168 } else {
169 # switch from section editing to normal editing in edit conflict
170 # FIXME: This is confusing. In theory we should attempt to merge, finding
171 # the equivalent section if it's unchanged and avoid the conflict.
172 if($isConflict) {
173 $this->section = "";
174 }
175 }
176 if ( ! $isConflict ) {
177 # All's well: update the article here
178 if($this->mArticle->updateArticle( $this->textbox1, $this->summary, $this->minoredit, $this->watchthis, $this->section ))
179 return;
180 else
181 $isConflict = true;
182 }
183 }
184 # First time through: get contents, set time for conflict
185 # checking, etc.
186
187 if ( "initial" == $formtype ) {
188 $this->edittime = $this->mArticle->getTimestamp();
189 $this->textbox1 = $this->mArticle->getContent(true);
190 $this->summary = "";
191 }
192 $wgOut->setRobotpolicy( "noindex,nofollow" );
193
194 # Enabled article-related sidebar, toplinks, etc.
195 $wgOut->setArticleRelated( true );
196
197 if ( $isConflict ) {
198 $s = wfMsg( "editconflict", $this->mTitle->getPrefixedText() );
199 $wgOut->setPageTitle( $s );
200 $wgOut->addHTML( wfMsg( "explainconflict" ) );
201
202 $this->textbox2 = $this->textbox1;
203 $this->textbox1 = $this->mArticle->getContent(true);
204 $this->edittime = $this->mArticle->getTimestamp();
205 } else {
206 $s = wfMsg( "editing", $this->mTitle->getPrefixedText() );
207
208 if( $this->section != "" ) {
209 if( $this->section == "new" ) {
210 $s.=wfMsg("commentedit");
211 } else {
212 $s.=wfMsg("sectionedit");
213 }
214 }
215 $wgOut->setPageTitle( $s );
216 if ( $this->oldid ) {
217 $this->mArticle->setOldSubtitle();
218 $wgOut->addHTML( wfMsg( "editingold" ) );
219 }
220 }
221
222 if( wfReadOnly() ) {
223 $wgOut->addHTML( "<strong>" .
224 wfMsg( "readonlywarning" ) .
225 "</strong>" );
226 }
227 if( $this->mTitle->isProtected() ) {
228 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
229 "</strong><br />\n" );
230 }
231
232 $kblength = (int)(strlen( $this->textbox1 ) / 1024);
233 if( $kblength > 29 ) {
234 $wgOut->addHTML( "<strong>" .
235 wfMsg( "longpagewarning", $kblength )
236 . "</strong>" );
237 }
238
239 $rows = $wgUser->getOption( "rows" );
240 $cols = $wgUser->getOption( "cols" );
241
242 $ew = $wgUser->getOption( "editwidth" );
243 if ( $ew ) $ew = " style=\"width:100%\"";
244 else $ew = "" ;
245
246 $q = "action=submit";
247 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
248 $action = $this->mTitle->escapeLocalURL( $q );
249
250 $summary = wfMsg( "summary" );
251 $subject = wfMsg("subject");
252 $minor = wfMsg( "minoredit" );
253 $watchthis = wfMsg ("watchthis");
254 $save = wfMsg( "savearticle" );
255 $prev = wfMsg( "showpreview" );
256
257 $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedURL(),
258 wfMsg( "cancel" ) );
259 $edithelp = $sk->makeKnownLink( wfMsg( "edithelppage" ),
260 wfMsg( "edithelp" ) );
261 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
262 wfMsg( "copyrightpage" ) ) );
263
264 if($wgUser->getOption("showtoolbar")) {
265 // prepare toolbar for edit buttons
266 $toolbar=$sk->getEditToolbar();
267 }
268
269 // activate checkboxes if user wants them to be always active
270 if( !$this->preview ) {
271 if( $wgUser->getOption( "watchdefault" ) ) $this->watchthis = true;
272 if( $wgUser->getOption( "minordefault" ) ) $this->minoredit = true;
273
274 // activate checkbox also if user is already watching the page,
275 // require wpWatchthis to be unset so that second condition is not
276 // checked unnecessarily
277 if( !$this->watchthis && $this->mTitle->userIsWatching() ) $this->watchthis = true;
278 }
279
280 $minoredithtml = "";
281
282 if ( 0 != $wgUser->getID() || $wgAllowAnonymousMinor ) {
283 $minoredithtml =
284 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?" checked":"")." id='wpMinoredit'>".
285 "<label for='wpMinoredit'>{$minor}</label>";
286 }
287
288 $watchhtml = "";
289
290 if ( 0 != $wgUser->getID() ) {
291 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?" checked":"")." id='wpWatchthis'>".
292 "<label for='wpWatchthis'>{$watchthis}</label>";
293 }
294
295 $checkboxhtml = $minoredithtml . $watchhtml . "<br>";
296
297 if ( "preview" == $formtype) {
298 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
299 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large><p>\n";
300 if ( $isConflict ) {
301 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
302 "</h2>\n";
303 }
304 $previewtext = wfUnescapeHTML( $this->textbox1 );
305
306 $parserOptions = ParserOptions::newFromUser( $wgUser );
307 $parserOptions->setUseCategoryMagic( false );
308 $parserOptions->setEditSection( false );
309 $parserOptions->setEditSectionOnRightClick( false );
310 $parserOutput = $wgParser->parse( $this->mArticle->preSaveTransform( $previewtext ) ."\n\n",
311 $wgTitle, $parserOptions );
312 $previewHTML = $parserOutput->mText;
313
314 if($wgUser->getOption("previewontop")) {
315 $wgOut->addHTML($previewhead);
316 $wgOut->addHTML($previewHTML);
317 }
318 $wgOut->addHTML( "<br clear=\"all\" />\n" );
319 }
320
321 # if this is a comment, show a subject line at the top, which is also the edit summary.
322 # Otherwise, show a summary field at the bottom
323 $summarytext = htmlspecialchars( $wgLang->recodeForEdit( $this->summary ) ); # FIXME
324 if( $this->section == "new" ) {
325 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60'><br>";
326 $editsummary = "";
327 } else {
328 $commentsubject = "";
329 $editsummary="{$summary}: <input tabindex='3' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60'><br>";
330 }
331
332 if( !$this->preview ) {
333 # Don't select the edit box on preview; this interferes with seeing what's going on.
334 $wgOut->setOnloadHandler( "document.editform.wpTextbox1.focus()" );
335 }
336 $wgOut->addHTML( "
337 {$toolbar}
338 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
339 enctype=\"application/x-www-form-urlencoded\">
340 {$commentsubject}
341 <textarea tabindex='2' name=\"wpTextbox1\" rows='{$rows}'
342 cols='{$cols}'{$ew} wrap=\"virtual\">" .
343 htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1 ) ) .
344 "
345 </textarea>
346 <br>{$editsummary}
347 {$checkboxhtml}
348 <input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"s\">
349 <input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"p\">
350 <em>{$cancel}</em> | <em>{$edithelp}</em>
351 <br><br>{$copywarn}
352 <input type=hidden value=\"" . htmlspecialchars( $this->section ) . "\" name=\"wpSection\">
353 <input type=hidden value=\"{$this->edittime}\" name=\"wpEdittime\">\n" );
354
355 if ( $isConflict ) {
356 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
357 DifferenceEngine::showDiff( $wpTextbox2, $wpTextbox1,
358 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
359
360 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
361 <textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
362 . htmlspecialchars( $wgLang->recodeForEdit( $wpTextbox2 ) ) .
363 "
364 </textarea>" );
365 }
366 $wgOut->addHTML( "</form>\n" );
367 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
368 $wgOut->addHTML($previewhead);
369 $wgOut->addHTML($previewHTML);
370 }
371
372 }
373
374 function blockedIPpage()
375 {
376 global $wgOut, $wgUser, $wgLang, $wgIP;
377
378 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
379 $wgOut->setRobotpolicy( "noindex,nofollow" );
380 $wgOut->setArticleRelated( false );
381
382 $id = $wgUser->blockedBy();
383 $reason = $wgUser->blockedFor();
384 $ip = $wgIP;
385
386 $name = User::whoIs( $id );
387 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
388 ":{$name}|{$name}]]";
389
390 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason, $ip ) );
391 $wgOut->returnToMain( false );
392 }
393
394
395
396 function userNotLoggedInPage()
397 {
398 global $wgOut, $wgUser, $wgLang;
399
400 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
401 $wgOut->setRobotpolicy( "noindex,nofollow" );
402 $wgOut->setArticleRelated( false );
403
404 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
405 $wgOut->returnToMain( false );
406 }
407
408
409 }
410
411 ?>