Fix article count update on page creation
[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
26 $fields = array( "wpTextbox1", "wpSummary", "wpTextbox2" );
27 wfCleanFormFields( $fields );
28
29 if ( ! $this->mTitle->userCanEdit() ) {
30 $this->mArticle->view();
31 return;
32 }
33 if ( $wgUser->isBlocked() ) {
34 $this->blockedIPpage();
35 return;
36 }
37 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
38 $this->userNotLoggedInPage();
39 return;
40 }
41 if ( wfReadOnly() ) {
42 if( isset( $wpSave ) or isset( $wpPreview ) ) {
43 $this->editForm( "preview" );
44 } else {
45 $wgOut->readOnlyPage( $this->mArticle->getContent() );
46 }
47 return;
48 }
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" );
56 }
57 }
58
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.
64
65 function submit()
66 {
67 global $wpSave, $wpPreview;
68 if ( ! isset( $wpPreview ) ) { $wpSave = 1; }
69
70 $this->edit();
71 }
72
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.
78
79 function editForm( $formtype )
80 {
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;
87
88 if(isset($wpSection)) { $section=$wpSection; } else { $wpSection=$section; }
89
90 $sk = $wgUser->getSkin();
91 $isConflict = false;
92 $wpTextbox1 = rtrim ( $wpTextbox1 ) ; # To avoid text getting longer on each preview
93
94 if(!$this->mTitle->getArticleID()) { # new article
95
96 $wgOut->addWikiText(wfmsg("newarticletext"));
97
98 }
99
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.
104
105 if ( "save" == $formtype ) {
106 if ( $wgUser->isBlocked() ) {
107 $this->blockedIPpage();
108 return;
109 }
110 if ( !$wgUser->getID() && $wgWhitelistEdit ) {
111 $this->userNotLoggedInPage();
112 return;
113 }
114 if ( wfReadOnly() ) {
115 $wgOut->readOnlyPage();
116 return;
117 }
118 # If article is new, insert it.
119
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;
129 }
130 $this->mArticle->insertNewArticle( $wpTextbox1, $wpSummary, $wpMinoredit, $wpWatchthis );
131 return;
132 }
133 # Article exists. Check for edit conflict.
134 # Don't check for conflict when appending a comment - this should always work
135
136 $this->mArticle->clear(); # Force reload of dates, etc.
137 if ( $section!="new" && ( $this->mArticle->getTimestamp() != $wpEdittime ) ) {
138 $isConflict = true;
139 }
140 $u = $wgUser->getID();
141
142 # Supress edit conflict with self
143
144 if ( ( 0 != $u ) && ( $this->mArticle->getUser() == $u ) ) {
145 $isConflict = false;
146 } else {
147 # switch from section editing to normal editing in edit conflict
148 if($isConflict) {
149 $section="";$wpSection="";
150 }
151
152 }
153 if ( ! $isConflict ) {
154 # All's well: update the article here
155 if($this->mArticle->updateArticle( $wpTextbox1, $wpSummary, $wpMinoredit, $wpWatchthis, $wpSection ))
156 return;
157 else
158 $isConflict = true;
159 }
160 }
161 # First time through: get contents, set time for conflict
162 # checking, etc.
163
164 if ( "initial" == $formtype ) {
165 $wpEdittime = $this->mArticle->getTimestamp();
166 $wpTextbox1 = $this->mArticle->getContent(true);
167 $wpSummary = "";
168 }
169 $wgOut->setRobotpolicy( "noindex,nofollow" );
170 $wgOut->setArticleFlag( false );
171
172 if ( $isConflict ) {
173 $s = wfMsg( "editconflict", $this->mTitle->getPrefixedText() );
174 $wgOut->setPageTitle( $s );
175 $wgOut->addHTML( wfMsg( "explainconflict" ) );
176
177 $wpTextbox2 = $wpTextbox1;
178 $wpTextbox1 = $this->mArticle->getContent(true);
179 $wpEdittime = $this->mArticle->getTimestamp();
180 } else {
181 $s = wfMsg( "editing", $this->mTitle->getPrefixedText() );
182
183 if($section!="") {
184 if($section=="new") {
185 $s.=wfMsg("commentedit");
186 } else {
187 $s.=wfMsg("sectionedit");
188 }
189 }
190 $wgOut->setPageTitle( $s );
191 if ( $oldid ) {
192 $this->mArticle->setOldSubtitle();
193 $wgOut->addHTML( wfMsg( "editingold" ) );
194 }
195 }
196
197 if( wfReadOnly() ) {
198 $wgOut->addHTML( "<strong>" .
199 wfMsg( "readonlywarning" ) .
200 "</strong>" );
201 }
202 if( $this->mTitle->isProtected() ) {
203 $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
204 "</strong><br />\n" );
205 }
206
207 $kblength = (int)(strlen( $wpTextbox1 ) / 1024);
208 if( $kblength > 29 ) {
209 $wgOut->addHTML( "<strong>" .
210 wfMsg( "longpagewarning", $kblength )
211 . "</strong>" );
212 }
213
214 $rows = $wgUser->getOption( "rows" );
215 $cols = $wgUser->getOption( "cols" );
216
217 $ew = $wgUser->getOption( "editwidth" );
218 if ( $ew ) $ew = " style=\"width:100%\"";
219 else $ew = "" ;
220
221 $q = "action=submit";
222 if ( "no" == $redirect ) { $q .= "&redirect=no"; }
223 $action = wfEscapeHTML( wfLocalUrl( $this->mTitle->getPrefixedURL(), $q ) );
224
225 $summary = wfMsg( "summary" );
226 $subject = wfMsg("subject");
227 $minor = wfMsg( "minoredit" );
228 $watchthis = wfMsg ("watchthis");
229 $save = wfMsg( "savearticle" );
230 $prev = wfMsg( "showpreview" );
231
232 $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedURL(),
233 wfMsg( "cancel" ) );
234 $edithelp = $sk->makeKnownLink( wfMsg( "edithelppage" ),
235 wfMsg( "edithelp" ) );
236 $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
237 wfMsg( "copyrightpage" ) ) );
238
239 $wpTextbox1 = wfEscapeHTML( $wpTextbox1 );
240 $wpTextbox2 = wfEscapeHTML( $wpTextbox2 );
241 $wpSummary = wfEscapeHTML( $wpSummary );
242
243 // activate checkboxes if user wants them to be always active
244 if (!$wpPreview && $wgUser->getOption("watchdefault")) $wpWatchthis=1;
245 if (!$wpPreview && $wgUser->getOption("minordefault")) $wpMinoredit=1;
246
247 // activate checkbox also if user is already watching the page,
248 // require wpWatchthis to be unset so that second condition is not
249 // checked unnecessarily
250 if (!$wpWatchthis && !$wpPreview && $this->mTitle->userIsWatching()) $wpWatchthis=1;
251
252 if ( 0 != $wgUser->getID() ) {
253 $checkboxhtml=
254 "<input tabindex=3 type=checkbox value=1 name='wpMinoredit'".($wpMinoredit?" checked":"")." id='wpMinoredit'>".
255 "<label for='wpMinoredit'>{$minor}</label>".
256 "<input tabindex=4 type=checkbox name='wpWatchthis'".($wpWatchthis?" checked":"")." id='wpWatchthis'>".
257 "<label for='wpWatchthis'>{$watchthis}</label><br>";
258
259 } else {
260 $checkboxhtml="";
261 }
262
263
264 if ( "preview" == $formtype) {
265
266 $previewhead="<h2>" . wfMsg( "preview" ) . "</h2>\n<p><large><center><font color=\"#cc0000\">" .
267 wfMsg( "note" ) . wfMsg( "previewnote" ) . "</font></center></large><P>\n";
268 if ( $isConflict ) {
269 $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
270 "</h2>\n";
271 }
272 $previewtext = wfUnescapeHTML( $wpTextbox1 );
273
274 if($wgUser->getOption("previewontop")) {
275 $wgOut->addHTML($previewhead);
276 $wgOut->addWikiText( $this->mArticle->preSaveTransform( $previewtext ) ."\n\n");
277 }
278 $wgOut->addHTML( "<br clear=\"all\" />\n" );
279 }
280
281 # if this is a comment, show a subject line at the top, which is also the edit summary.
282 # Otherwise, show a summary field at the bottom
283 if($section=="new") {
284
285 $commentsubject="{$subject}: <input tabindex=1 type=text value=\"{$wpSummary}\" name=\"wpSummary\" maxlength=200 size=60><br>";
286 } else {
287
288 $editsummary="{$summary}: <input tabindex=3 type=text value=\"{$wpSummary}\" name=\"wpSummary\" maxlength=200 size=60><br>";
289 }
290
291 $wgOut->addHTML( "
292 <form id=\"editform\" name=\"editform\" method=\"post\" action=\"$action\"
293 enctype=\"application/x-www-form-urlencoded\">
294 {$commentsubject}
295 <textarea tabindex=2 name=\"wpTextbox1\" rows={$rows}
296 cols={$cols}{$ew} wrap=\"virtual\">" .
297 $wgLang->recodeForEdit( $wpTextbox1 ) .
298 "
299 </textarea>
300 <br>{$editsummary}
301 {$checkboxhtml}
302 <input tabindex=5 type=submit value=\"{$save}\" name=\"wpSave\">
303 <input tabindex=6 type=submit value=\"{$prev}\" name=\"wpPreview\">
304 <em>{$cancel}</em> | <em>{$edithelp}</em>
305 <br><br>{$copywarn}
306 <input type=hidden value=\"{$section}\" name=\"wpSection\">
307 <input type=hidden value=\"{$wpEdittime}\" name=\"wpEdittime\">\n" );
308
309 if ( $isConflict ) {
310 $wgOut->addHTML( "<h2>" . wfMsg( "yourdiff" ) . "</h2>\n" );
311 DifferenceEngine::showDiff( $wpTextbox2, $wpTextbox1,
312 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
313
314 $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
315 <textarea tabindex=6 name=\"wpTextbox2\" rows={$rows} cols={$cols} wrap=virtual>"
316 . $wgLang->recodeForEdit( $wpTextbox2 ) .
317 "
318 </textarea>" );
319 }
320 $wgOut->addHTML( "</form>\n" );
321 if($formtype =="preview" && !$wgUser->getOption("previewontop")) {
322 $wgOut->addHTML($previewhead);
323 $wgOut->addWikiText( $this->mArticle->preSaveTransform( $previewtext ) );
324 }
325
326 }
327
328 function blockedIPpage()
329 {
330 global $wgOut, $wgUser, $wgLang;
331
332 $wgOut->setPageTitle( wfMsg( "blockedtitle" ) );
333 $wgOut->setRobotpolicy( "noindex,nofollow" );
334 $wgOut->setArticleFlag( false );
335
336 $id = $wgUser->blockedBy();
337 $reason = $wgUser->blockedFor();
338
339 $name = User::whoIs( $id );
340 $link = "[[" . $wgLang->getNsText( Namespace::getUser() ) .
341 ":{$name}|{$name}]]";
342
343 $wgOut->addWikiText( wfMsg( "blockedtext", $link, $reason ) );
344 $wgOut->returnToMain( false );
345 }
346
347
348
349 function userNotLoggedInPage()
350 {
351 global $wgOut, $wgUser, $wgLang;
352
353 $wgOut->setPageTitle( wfMsg( "whitelistedittitle" ) );
354 $wgOut->setRobotpolicy( "noindex,nofollow" );
355 $wgOut->setArticleFlag( false );
356
357 $wgOut->addWikiText( wfMsg( "whitelistedittext" ) );
358 $wgOut->returnToMain( false );
359 }
360
361
362 }
363
364 ?>