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