Fixing a PHP warning about passing variables by reference from the function call...
[lhc/web/wiklou.git] / includes / EditPage.php
1 <?php
2 /**
3 * Contain the EditPage class
4 * @package MediaWiki
5 */
6
7 /**
8 * Splitting edit page/HTML interface from Article...
9 * The actual database and text munging is still in Article,
10 * but it should get easier to call those from alternate
11 * interfaces.
12 *
13 * @package MediaWiki
14 */
15
16 class EditPage {
17 var $mArticle;
18 var $mTitle;
19 var $mMetaData = '';
20 var $isConflict = false;
21 var $isCssJsSubpage = false;
22 var $deletedSinceEdit = false;
23 var $formtype;
24 var $firsttime;
25 var $lastDelete;
26 var $mTokenOk = false;
27 var $mTriedSave = false;
28 var $tooBig = false;
29 var $kblength = false;
30 var $missingComment = false;
31 var $missingSummary = false;
32 var $allowBlankSummary = false;
33 var $autoSumm = '';
34 var $hookError = '';
35 var $mPreviewTemplates;
36
37 # Form values
38 var $save = false, $preview = false, $diff = false;
39 var $minoredit = false, $watchthis = false, $recreate = false;
40 var $textbox1 = '', $textbox2 = '', $summary = '';
41 var $edittime = '', $section = '', $starttime = '';
42 var $oldid = 0, $editintro = '', $scrolltop = null;
43
44 # Placeholders for text injection by hooks (must be HTML)
45 # extensions should take care to _append_ to the present value
46 public $editFormPageTop; // Before even the preview
47 public $editFormTextTop;
48 public $editFormTextAfterWarn;
49 public $editFormTextAfterTools;
50 public $editFormTextBottom;
51
52 /**
53 * @todo document
54 * @param $article
55 */
56 function EditPage( $article ) {
57 $this->mArticle =& $article;
58 global $wgTitle;
59 $this->mTitle =& $wgTitle;
60
61 # Placeholders for text injection by hooks (empty per default)
62 $this->editFormPageTop =
63 $this->editFormTextTop =
64 $this->editFormTextAfterWarn =
65 $this->editFormTextAfterTools =
66 $this->editFormTextBottom = "";
67 }
68
69 /**
70 * Fetch initial editing page content.
71 */
72 private function getContent() {
73 global $wgRequest, $wgParser;
74
75 # Get variables from query string :P
76 $section = $wgRequest->getVal( 'section' );
77 $preload = $wgRequest->getVal( 'preload' );
78 $undo = $wgRequest->getVal( 'undo' );
79
80 wfProfileIn( __METHOD__ );
81
82 $text = '';
83 if( !$this->mTitle->exists() ) {
84
85 # If requested, preload some text.
86 $text = $this->getPreloadedText( $preload );
87
88 # We used to put MediaWiki:Newarticletext here if
89 # $text was empty at this point.
90 # This is now shown above the edit box instead.
91 } else {
92 // FIXME: may be better to use Revision class directly
93 // But don't mess with it just yet. Article knows how to
94 // fetch the page record from the high-priority server,
95 // which is needed to guarantee we don't pick up lagged
96 // information.
97
98 $text = $this->mArticle->getContent();
99
100 if ( $undo > 0 ) {
101 #Undoing a specific edit overrides section editing; section-editing
102 # doesn't work with undoing.
103 $undorev = Revision::newFromId($undo);
104 $oldrev = $undorev->getPrevious();
105
106 #Sanity check, make sure it's the right page.
107 # Otherwise, $text will be left as-is.
108 if ($undorev->getPage() == $this->mArticle->getID()) {
109 $undorev_text = $undorev->getText();
110 $oldrev_text = $oldrev->getText();
111 $currev_text = $text;
112
113 $result = wfMerge($undorev_text, $oldrev_text, $currev_text, $text);
114
115 if (!$result) {
116 #Undoing failed. Bailing out with regular revision text.
117 $text = $currev_text;
118
119 #Give a warning
120 $this->editFormPageTop .= "<h2>" . wfMsg('undofailed') . "</h2>\n" .
121 '<p><strong class="error">'.wfMsg('explainundofailed').'</strong></p>';
122 }
123 }
124 }
125 else if( $section != '' ) {
126 if( $section == 'new' ) {
127 $text = $this->getPreloadedText( $preload );
128 } else {
129 $text = $wgParser->getSection( $text, $section );
130 }
131 }
132 }
133
134 wfProfileOut( __METHOD__ );
135 return $text;
136 }
137
138 /**
139 * Get the contents of a page from its title and remove includeonly tags
140 *
141 * @param $preload String: the title of the page.
142 * @return string The contents of the page.
143 */
144 private function getPreloadedText($preload) {
145 if ( $preload === '' )
146 return '';
147 else {
148 $preloadTitle = Title::newFromText( $preload );
149 if ( isset( $preloadTitle ) && $preloadTitle->userCanRead() ) {
150 $rev=Revision::newFromTitle($preloadTitle);
151 if ( is_object( $rev ) ) {
152 $text = $rev->getText();
153 // TODO FIXME: AAAAAAAAAAA, this shouldn't be implementing
154 // its own mini-parser! -ævar
155 $text = preg_replace( '~</?includeonly>~', '', $text );
156 return $text;
157 } else
158 return '';
159 }
160 }
161 }
162
163 /**
164 * This is the function that extracts metadata from the article body on the first view.
165 * To turn the feature on, set $wgUseMetadataEdit = true ; in LocalSettings
166 * and set $wgMetadataWhitelist to the *full* title of the template whitelist
167 */
168 function extractMetaDataFromArticle () {
169 global $wgUseMetadataEdit , $wgMetadataWhitelist , $wgLang ;
170 $this->mMetaData = '' ;
171 if ( !$wgUseMetadataEdit ) return ;
172 if ( $wgMetadataWhitelist == '' ) return ;
173 $s = '' ;
174 $t = $this->getContent();
175
176 # MISSING : <nowiki> filtering
177
178 # Categories and language links
179 $t = explode ( "\n" , $t ) ;
180 $catlow = strtolower ( $wgLang->getNsText ( NS_CATEGORY ) ) ;
181 $cat = $ll = array() ;
182 foreach ( $t AS $key => $x )
183 {
184 $y = trim ( strtolower ( $x ) ) ;
185 while ( substr ( $y , 0 , 2 ) == '[[' )
186 {
187 $y = explode ( ']]' , trim ( $x ) ) ;
188 $first = array_shift ( $y ) ;
189 $first = explode ( ':' , $first ) ;
190 $ns = array_shift ( $first ) ;
191 $ns = trim ( str_replace ( '[' , '' , $ns ) ) ;
192 if ( strlen ( $ns ) == 2 OR strtolower ( $ns ) == $catlow )
193 {
194 $add = '[[' . $ns . ':' . implode ( ':' , $first ) . ']]' ;
195 if ( strtolower ( $ns ) == $catlow ) $cat[] = $add ;
196 else $ll[] = $add ;
197 $x = implode ( ']]' , $y ) ;
198 $t[$key] = $x ;
199 $y = trim ( strtolower ( $x ) ) ;
200 }
201 }
202 }
203 if ( count ( $cat ) ) $s .= implode ( ' ' , $cat ) . "\n" ;
204 if ( count ( $ll ) ) $s .= implode ( ' ' , $ll ) . "\n" ;
205 $t = implode ( "\n" , $t ) ;
206
207 # Load whitelist
208 $sat = array () ; # stand-alone-templates; must be lowercase
209 $wl_title = Title::newFromText ( $wgMetadataWhitelist ) ;
210 $wl_article = new Article ( $wl_title ) ;
211 $wl = explode ( "\n" , $wl_article->getContent() ) ;
212 foreach ( $wl AS $x )
213 {
214 $isentry = false ;
215 $x = trim ( $x ) ;
216 while ( substr ( $x , 0 , 1 ) == '*' )
217 {
218 $isentry = true ;
219 $x = trim ( substr ( $x , 1 ) ) ;
220 }
221 if ( $isentry )
222 {
223 $sat[] = strtolower ( $x ) ;
224 }
225
226 }
227
228 # Templates, but only some
229 $t = explode ( '{{' , $t ) ;
230 $tl = array () ;
231 foreach ( $t AS $key => $x )
232 {
233 $y = explode ( '}}' , $x , 2 ) ;
234 if ( count ( $y ) == 2 )
235 {
236 $z = $y[0] ;
237 $z = explode ( '|' , $z ) ;
238 $tn = array_shift ( $z ) ;
239 if ( in_array ( strtolower ( $tn ) , $sat ) )
240 {
241 $tl[] = '{{' . $y[0] . '}}' ;
242 $t[$key] = $y[1] ;
243 $y = explode ( '}}' , $y[1] , 2 ) ;
244 }
245 else $t[$key] = '{{' . $x ;
246 }
247 else if ( $key != 0 ) $t[$key] = '{{' . $x ;
248 else $t[$key] = $x ;
249 }
250 if ( count ( $tl ) ) $s .= implode ( ' ' , $tl ) ;
251 $t = implode ( '' , $t ) ;
252
253 $t = str_replace ( "\n\n\n" , "\n" , $t ) ;
254 $this->mArticle->mContent = $t ;
255 $this->mMetaData = $s ;
256 }
257
258 function submit() {
259 $this->edit();
260 }
261
262 /**
263 * This is the function that gets called for "action=edit". It
264 * sets up various member variables, then passes execution to
265 * another function, usually showEditForm()
266 *
267 * The edit form is self-submitting, so that when things like
268 * preview and edit conflicts occur, we get the same form back
269 * with the extra stuff added. Only when the final submission
270 * is made and all is well do we actually save and redirect to
271 * the newly-edited page.
272 */
273 function edit() {
274 global $wgOut, $wgUser, $wgRequest, $wgTitle;
275 global $wgEmailConfirmToEdit;
276
277 if ( ! wfRunHooks( 'AlternateEdit', array( &$this ) ) )
278 return;
279
280 $fname = 'EditPage::edit';
281 wfProfileIn( $fname );
282 wfDebug( "$fname: enter\n" );
283
284 // this is not an article
285 $wgOut->setArticleFlag(false);
286
287 $this->importFormData( $wgRequest );
288 $this->firsttime = false;
289
290 if( $this->live ) {
291 $this->livePreview();
292 wfProfileOut( $fname );
293 return;
294 }
295
296 if ( ! $this->mTitle->userCanEdit() ) {
297 wfDebug( "$fname: user can't edit\n" );
298 $wgOut->readOnlyPage( $this->getContent(), true );
299 wfProfileOut( $fname );
300 return;
301 }
302 wfDebug( "$fname: Checking blocks\n" );
303 if ( !$this->preview && !$this->diff && $wgUser->isBlockedFrom( $this->mTitle, !$this->save ) ) {
304 # When previewing, don't check blocked state - will get caught at save time.
305 # Also, check when starting edition is done against slave to improve performance.
306 wfDebug( "$fname: user is blocked\n" );
307 $this->blockedPage();
308 wfProfileOut( $fname );
309 return;
310 }
311 if ( !$wgUser->isAllowed('edit') ) {
312 if ( $wgUser->isAnon() ) {
313 wfDebug( "$fname: user must log in\n" );
314 $this->userNotLoggedInPage();
315 wfProfileOut( $fname );
316 return;
317 } else {
318 wfDebug( "$fname: read-only page\n" );
319 $wgOut->readOnlyPage( $this->getContent(), true );
320 wfProfileOut( $fname );
321 return;
322 }
323 }
324 if ($wgEmailConfirmToEdit && !$wgUser->isEmailConfirmed()) {
325 wfDebug("$fname: user must confirm e-mail address\n");
326 $this->userNotConfirmedPage();
327 wfProfileOut($fname);
328 return;
329 }
330 if ( !$this->mTitle->userCanCreate() && !$this->mTitle->exists() ) {
331 wfDebug( "$fname: no create permission\n" );
332 $this->noCreatePermission();
333 wfProfileOut( $fname );
334 return;
335 }
336 if ( wfReadOnly() ) {
337 wfDebug( "$fname: read-only mode is engaged\n" );
338 if( $this->save || $this->preview ) {
339 $this->formtype = 'preview';
340 } else if ( $this->diff ) {
341 $this->formtype = 'diff';
342 } else {
343 $wgOut->readOnlyPage( $this->getContent() );
344 wfProfileOut( $fname );
345 return;
346 }
347 } else {
348 if ( $this->save ) {
349 $this->formtype = 'save';
350 } else if ( $this->preview ) {
351 $this->formtype = 'preview';
352 } else if ( $this->diff ) {
353 $this->formtype = 'diff';
354 } else { # First time through
355 $this->firsttime = true;
356 if( $this->previewOnOpen() ) {
357 $this->formtype = 'preview';
358 } else {
359 $this->extractMetaDataFromArticle () ;
360 $this->formtype = 'initial';
361 }
362 }
363 }
364
365 wfProfileIn( "$fname-business-end" );
366
367 $this->isConflict = false;
368 // css / js subpages of user pages get a special treatment
369 $this->isCssJsSubpage = $wgTitle->isCssJsSubpage();
370 $this->isValidCssJsSubpage = $wgTitle->isValidCssJsSubpage();
371
372 /* Notice that we can't use isDeleted, because it returns true if article is ever deleted
373 * no matter it's current state
374 */
375 $this->deletedSinceEdit = false;
376 if ( $this->edittime != '' ) {
377 /* Note that we rely on logging table, which hasn't been always there,
378 * but that doesn't matter, because this only applies to brand new
379 * deletes. This is done on every preview and save request. Move it further down
380 * to only perform it on saves
381 */
382 if ( $this->mTitle->isDeleted() ) {
383 $this->lastDelete = $this->getLastDelete();
384 if ( !is_null($this->lastDelete) ) {
385 $deletetime = $this->lastDelete->log_timestamp;
386 if ( ($deletetime - $this->starttime) > 0 ) {
387 $this->deletedSinceEdit = true;
388 }
389 }
390 }
391 }
392
393 if(!$this->mTitle->getArticleID() && ('initial' == $this->formtype || $this->firsttime )) { # new article
394 $this->showIntro();
395 }
396 if( $this->mTitle->isTalkPage() ) {
397 $wgOut->addWikiText( wfMsg( 'talkpagetext' ) );
398 }
399
400 # Attempt submission here. This will check for edit conflicts,
401 # and redundantly check for locked database, blocked IPs, etc.
402 # that edit() already checked just in case someone tries to sneak
403 # in the back door with a hand-edited submission URL.
404
405 if ( 'save' == $this->formtype ) {
406 if ( !$this->attemptSave() ) {
407 wfProfileOut( "$fname-business-end" );
408 wfProfileOut( $fname );
409 return;
410 }
411 }
412
413 # First time through: get contents, set time for conflict
414 # checking, etc.
415 if ( 'initial' == $this->formtype || $this->firsttime ) {
416 $this->initialiseForm();
417 if( !$this->mTitle->getArticleId() )
418 wfRunHooks( 'EditFormPreloadText', array( &$this->textbox1, &$this->mTitle ) );
419 }
420
421 $this->showEditForm();
422 wfProfileOut( "$fname-business-end" );
423 wfProfileOut( $fname );
424 }
425
426 /**
427 * Return true if this page should be previewed when the edit form
428 * is initially opened.
429 * @return bool
430 * @private
431 */
432 function previewOnOpen() {
433 global $wgUser;
434 return $this->section != 'new' &&
435 ( ( $wgUser->getOption( 'previewonfirst' ) && $this->mTitle->exists() ) ||
436 ( $this->mTitle->getNamespace() == NS_CATEGORY &&
437 !$this->mTitle->exists() ) );
438 }
439
440 /**
441 * @todo document
442 * @param $request
443 */
444 function importFormData( &$request ) {
445 global $wgLang, $wgUser;
446 $fname = 'EditPage::importFormData';
447 wfProfileIn( $fname );
448
449 if( $request->wasPosted() ) {
450 # These fields need to be checked for encoding.
451 # Also remove trailing whitespace, but don't remove _initial_
452 # whitespace from the text boxes. This may be significant formatting.
453 $this->textbox1 = $this->safeUnicodeInput( $request, 'wpTextbox1' );
454 $this->textbox2 = $this->safeUnicodeInput( $request, 'wpTextbox2' );
455 $this->mMetaData = rtrim( $request->getText( 'metadata' ) );
456 # Truncate for whole multibyte characters. +5 bytes for ellipsis
457 $this->summary = $wgLang->truncate( $request->getText( 'wpSummary' ), 250 );
458
459 $this->edittime = $request->getVal( 'wpEdittime' );
460 $this->starttime = $request->getVal( 'wpStarttime' );
461
462 $this->scrolltop = $request->getIntOrNull( 'wpScrolltop' );
463
464 if( is_null( $this->edittime ) ) {
465 # If the form is incomplete, force to preview.
466 wfDebug( "$fname: Form data appears to be incomplete\n" );
467 wfDebug( "POST DATA: " . var_export( $_POST, true ) . "\n" );
468 $this->preview = true;
469 } else {
470 /* Fallback for live preview */
471 $this->preview = $request->getCheck( 'wpPreview' ) || $request->getCheck( 'wpLivePreview' );
472 $this->diff = $request->getCheck( 'wpDiff' );
473
474 // Remember whether a save was requested, so we can indicate
475 // if we forced preview due to session failure.
476 $this->mTriedSave = !$this->preview;
477
478 if ( $this->tokenOk( $request ) ) {
479 # Some browsers will not report any submit button
480 # if the user hits enter in the comment box.
481 # The unmarked state will be assumed to be a save,
482 # if the form seems otherwise complete.
483 wfDebug( "$fname: Passed token check.\n" );
484 } else if ( $this->diff ) {
485 # Failed token check, but only requested "Show Changes".
486 wfDebug( "$fname: Failed token check; Show Changes requested.\n" );
487 } else {
488 # Page might be a hack attempt posted from
489 # an external site. Preview instead of saving.
490 wfDebug( "$fname: Failed token check; forcing preview\n" );
491 $this->preview = true;
492 }
493 }
494 $this->save = ! ( $this->preview OR $this->diff );
495 if( !preg_match( '/^\d{14}$/', $this->edittime )) {
496 $this->edittime = null;
497 }
498
499 if( !preg_match( '/^\d{14}$/', $this->starttime )) {
500 $this->starttime = null;
501 }
502
503 $this->recreate = $request->getCheck( 'wpRecreate' );
504
505 $this->minoredit = $request->getCheck( 'wpMinoredit' );
506 $this->watchthis = $request->getCheck( 'wpWatchthis' );
507
508 # Don't force edit summaries when a user is editing their own user or talk page
509 if( ( $this->mTitle->mNamespace == NS_USER || $this->mTitle->mNamespace == NS_USER_TALK ) && $this->mTitle->getText() == $wgUser->getName() ) {
510 $this->allowBlankSummary = true;
511 } else {
512 $this->allowBlankSummary = $request->getBool( 'wpIgnoreBlankSummary' );
513 }
514
515 $this->autoSumm = $request->getText( 'wpAutoSummary' );
516 } else {
517 # Not a posted form? Start with nothing.
518 wfDebug( "$fname: Not a posted form.\n" );
519 $this->textbox1 = '';
520 $this->textbox2 = '';
521 $this->mMetaData = '';
522 $this->summary = '';
523 $this->edittime = '';
524 $this->starttime = wfTimestampNow();
525 $this->preview = false;
526 $this->save = false;
527 $this->diff = false;
528 $this->minoredit = false;
529 $this->watchthis = false;
530 $this->recreate = false;
531 }
532
533 $this->oldid = $request->getInt( 'oldid' );
534
535 # Section edit can come from either the form or a link
536 $this->section = $request->getVal( 'wpSection', $request->getVal( 'section' ) );
537
538 $this->live = $request->getCheck( 'live' );
539 $this->editintro = $request->getText( 'editintro' );
540
541 wfProfileOut( $fname );
542 }
543
544 /**
545 * Make sure the form isn't faking a user's credentials.
546 *
547 * @param $request WebRequest
548 * @return bool
549 * @private
550 */
551 function tokenOk( &$request ) {
552 global $wgUser;
553 if( $wgUser->isAnon() ) {
554 # Anonymous users may not have a session
555 # open. Don't tokenize.
556 $this->mTokenOk = true;
557 } else {
558 $this->mTokenOk = $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
559 }
560 return $this->mTokenOk;
561 }
562
563 /** */
564 function showIntro() {
565 global $wgOut, $wgUser;
566 $addstandardintro=true;
567 if($this->editintro) {
568 $introtitle=Title::newFromText($this->editintro);
569 if(isset($introtitle) && $introtitle->userCanRead()) {
570 $rev=Revision::newFromTitle($introtitle);
571 if($rev) {
572 $wgOut->addSecondaryWikiText($rev->getText());
573 $addstandardintro=false;
574 }
575 }
576 }
577 if($addstandardintro) {
578 if ( $wgUser->isLoggedIn() )
579 $wgOut->addWikiText( wfMsg( 'newarticletext' ) );
580 else
581 $wgOut->addWikiText( wfMsg( 'newarticletextanon' ) );
582 }
583 }
584
585 /**
586 * Attempt submission
587 * @return bool false if output is done, true if the rest of the form should be displayed
588 */
589 function attemptSave() {
590 global $wgSpamRegex, $wgFilterCallback, $wgUser, $wgOut;
591 global $wgMaxArticleSize;
592
593 $fname = 'EditPage::attemptSave';
594 wfProfileIn( $fname );
595 wfProfileIn( "$fname-checks" );
596
597 # Reintegrate metadata
598 if ( $this->mMetaData != '' ) $this->textbox1 .= "\n" . $this->mMetaData ;
599 $this->mMetaData = '' ;
600
601 # Check for spam
602 $matches = array();
603 if ( $wgSpamRegex && preg_match( $wgSpamRegex, $this->textbox1, $matches ) ) {
604 $this->spamPage ( $matches[0] );
605 wfProfileOut( "$fname-checks" );
606 wfProfileOut( $fname );
607 return false;
608 }
609 if ( $wgFilterCallback && $wgFilterCallback( $this->mTitle, $this->textbox1, $this->section ) ) {
610 # Error messages or other handling should be performed by the filter function
611 wfProfileOut( $fname );
612 wfProfileOut( "$fname-checks" );
613 return false;
614 }
615 if ( !wfRunHooks( 'EditFilter', array( $this, $this->textbox1, $this->section, &$this->hookError ) ) ) {
616 # Error messages etc. could be handled within the hook...
617 wfProfileOut( $fname );
618 wfProfileOut( "$fname-checks" );
619 return false;
620 } elseif( $this->hookError != '' ) {
621 # ...or the hook could be expecting us to produce an error
622 wfProfileOut( "$fname-checks " );
623 wfProfileOut( $fname );
624 return true;
625 }
626 if ( $wgUser->isBlockedFrom( $this->mTitle, false ) ) {
627 # Check block state against master, thus 'false'.
628 $this->blockedPage();
629 wfProfileOut( "$fname-checks" );
630 wfProfileOut( $fname );
631 return false;
632 }
633 $this->kblength = (int)(strlen( $this->textbox1 ) / 1024);
634 if ( $this->kblength > $wgMaxArticleSize ) {
635 // Error will be displayed by showEditForm()
636 $this->tooBig = true;
637 wfProfileOut( "$fname-checks" );
638 wfProfileOut( $fname );
639 return true;
640 }
641
642 if ( !$wgUser->isAllowed('edit') ) {
643 if ( $wgUser->isAnon() ) {
644 $this->userNotLoggedInPage();
645 wfProfileOut( "$fname-checks" );
646 wfProfileOut( $fname );
647 return false;
648 }
649 else {
650 $wgOut->readOnlyPage();
651 wfProfileOut( "$fname-checks" );
652 wfProfileOut( $fname );
653 return false;
654 }
655 }
656
657 if ( wfReadOnly() ) {
658 $wgOut->readOnlyPage();
659 wfProfileOut( "$fname-checks" );
660 wfProfileOut( $fname );
661 return false;
662 }
663 if ( $wgUser->pingLimiter() ) {
664 $wgOut->rateLimited();
665 wfProfileOut( "$fname-checks" );
666 wfProfileOut( $fname );
667 return false;
668 }
669
670 # If the article has been deleted while editing, don't save it without
671 # confirmation
672 if ( $this->deletedSinceEdit && !$this->recreate ) {
673 wfProfileOut( "$fname-checks" );
674 wfProfileOut( $fname );
675 return true;
676 }
677
678 wfProfileOut( "$fname-checks" );
679
680 # If article is new, insert it.
681 $aid = $this->mTitle->getArticleID( GAID_FOR_UPDATE );
682 if ( 0 == $aid ) {
683 // Late check for create permission, just in case *PARANOIA*
684 if ( !$this->mTitle->userCanCreate() ) {
685 wfDebug( "$fname: no create permission\n" );
686 $this->noCreatePermission();
687 wfProfileOut( $fname );
688 return;
689 }
690
691 # Don't save a new article if it's blank.
692 if ( ( '' == $this->textbox1 ) ) {
693 $wgOut->redirect( $this->mTitle->getFullURL() );
694 wfProfileOut( $fname );
695 return false;
696 }
697
698 $isComment=($this->section=='new');
699 $this->mArticle->insertNewArticle( $this->textbox1, $this->summary,
700 $this->minoredit, $this->watchthis, false, $isComment);
701
702 wfProfileOut( $fname );
703 return false;
704 }
705
706 # Article exists. Check for edit conflict.
707
708 $this->mArticle->clear(); # Force reload of dates, etc.
709 $this->mArticle->forUpdate( true ); # Lock the article
710
711 if( $this->mArticle->getTimestamp() != $this->edittime ) {
712 $this->isConflict = true;
713 if( $this->section == 'new' ) {
714 if( $this->mArticle->getUserText() == $wgUser->getName() &&
715 $this->mArticle->getComment() == $this->summary ) {
716 // Probably a duplicate submission of a new comment.
717 // This can happen when squid resends a request after
718 // a timeout but the first one actually went through.
719 wfDebug( "EditPage::editForm duplicate new section submission; trigger edit conflict!\n" );
720 } else {
721 // New comment; suppress conflict.
722 $this->isConflict = false;
723 wfDebug( "EditPage::editForm conflict suppressed; new section\n" );
724 }
725 }
726 }
727 $userid = $wgUser->getID();
728
729 if ( $this->isConflict) {
730 wfDebug( "EditPage::editForm conflict! getting section '$this->section' for time '$this->edittime' (article time '" .
731 $this->mArticle->getTimestamp() . "'\n" );
732 $text = $this->mArticle->replaceSection( $this->section, $this->textbox1, $this->summary, $this->edittime);
733 }
734 else {
735 wfDebug( "EditPage::editForm getting section '$this->section'\n" );
736 $text = $this->mArticle->replaceSection( $this->section, $this->textbox1, $this->summary);
737 }
738 if( is_null( $text ) ) {
739 wfDebug( "EditPage::editForm activating conflict; section replace failed.\n" );
740 $this->isConflict = true;
741 $text = $this->textbox1;
742 }
743
744 # Suppress edit conflict with self, except for section edits where merging is required.
745 if ( ( $this->section == '' ) && ( 0 != $userid ) && ( $this->mArticle->getUser() == $userid ) ) {
746 wfDebug( "Suppressing edit conflict, same user.\n" );
747 $this->isConflict = false;
748 } else {
749 # switch from section editing to normal editing in edit conflict
750 if($this->isConflict) {
751 # Attempt merge
752 if( $this->mergeChangesInto( $text ) ){
753 // Successful merge! Maybe we should tell the user the good news?
754 $this->isConflict = false;
755 wfDebug( "Suppressing edit conflict, successful merge.\n" );
756 } else {
757 $this->section = '';
758 $this->textbox1 = $text;
759 wfDebug( "Keeping edit conflict, failed merge.\n" );
760 }
761 }
762 }
763
764 if ( $this->isConflict ) {
765 wfProfileOut( $fname );
766 return true;
767 }
768
769 $oldtext = $this->mArticle->getContent();
770
771 # Handle the user preference to force summaries here, but not for null edits
772 if( $this->section != 'new' && !$this->allowBlankSummary && $wgUser->getOption( 'forceeditsummary')
773 && 0 != strcmp($oldtext, $text) && !Article::getRedirectAutosummary( $text )) {
774 if( md5( $this->summary ) == $this->autoSumm ) {
775 $this->missingSummary = true;
776 wfProfileOut( $fname );
777 return( true );
778 }
779 }
780
781 #And a similar thing for new sections
782 if( $this->section == 'new' && !$this->allowBlankSummary && $wgUser->getOption( 'forceeditsummary' ) ) {
783 if (trim($this->summary) == '') {
784 $this->missingSummary = true;
785 wfProfileOut( $fname );
786 return( true );
787 }
788 }
789
790 # All's well
791 wfProfileIn( "$fname-sectionanchor" );
792 $sectionanchor = '';
793 if( $this->section == 'new' ) {
794 if ( $this->textbox1 == '' ) {
795 $this->missingComment = true;
796 return true;
797 }
798 if( $this->summary != '' ) {
799 $sectionanchor = $this->sectionAnchor( $this->summary );
800 }
801 } elseif( $this->section != '' ) {
802 # Try to get a section anchor from the section source, redirect to edited section if header found
803 # XXX: might be better to integrate this into Article::replaceSection
804 # for duplicate heading checking and maybe parsing
805 $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1, $matches );
806 # we can't deal with anchors, includes, html etc in the header for now,
807 # headline would need to be parsed to improve this
808 if($hasmatch and strlen($matches[2]) > 0) {
809 $sectionanchor = $this->sectionAnchor( $matches[2] );
810 }
811 }
812 wfProfileOut( "$fname-sectionanchor" );
813
814 // Save errors may fall down to the edit form, but we've now
815 // merged the section into full text. Clear the section field
816 // so that later submission of conflict forms won't try to
817 // replace that into a duplicated mess.
818 $this->textbox1 = $text;
819 $this->section = '';
820
821 // Check for length errors again now that the section is merged in
822 $this->kblength = (int)(strlen( $text ) / 1024);
823 if ( $this->kblength > $wgMaxArticleSize ) {
824 $this->tooBig = true;
825 wfProfileOut( $fname );
826 return true;
827 }
828
829 # update the article here
830 if( $this->mArticle->updateArticle( $text, $this->summary, $this->minoredit,
831 $this->watchthis, '', $sectionanchor ) ) {
832 wfProfileOut( $fname );
833 return false;
834 } else {
835 $this->isConflict = true;
836 }
837 wfProfileOut( $fname );
838 return true;
839 }
840
841 /**
842 * Initialise form fields in the object
843 * Called on the first invocation, e.g. when a user clicks an edit link
844 */
845 function initialiseForm() {
846 $this->edittime = $this->mArticle->getTimestamp();
847 $this->textbox1 = $this->getContent();
848 $this->summary = '';
849 if ( !$this->mArticle->exists() && $this->mArticle->mTitle->getNamespace() == NS_MEDIAWIKI )
850 $this->textbox1 = wfMsgWeirdKey( $this->mArticle->mTitle->getText() ) ;
851 wfProxyCheck();
852 }
853
854 /**
855 * Send the edit form and related headers to $wgOut
856 * @param $formCallback Optional callable that takes an OutputPage
857 * parameter; will be called during form output
858 * near the top, for captchas and the like.
859 */
860 function showEditForm( $formCallback=null ) {
861 global $wgOut, $wgUser, $wgLang, $wgContLang, $wgMaxArticleSize;
862
863 $fname = 'EditPage::showEditForm';
864 wfProfileIn( $fname );
865
866 $sk =& $wgUser->getSkin();
867
868 wfRunHooks( 'EditPage::showEditForm:initial', array( &$this ) ) ;
869
870 $wgOut->setRobotpolicy( 'noindex,nofollow' );
871
872 # Enabled article-related sidebar, toplinks, etc.
873 $wgOut->setArticleRelated( true );
874
875 if ( $this->isConflict ) {
876 $s = wfMsg( 'editconflict', $this->mTitle->getPrefixedText() );
877 $wgOut->setPageTitle( $s );
878 $wgOut->addWikiText( wfMsg( 'explainconflict' ) );
879
880 $this->textbox2 = $this->textbox1;
881 $this->textbox1 = $this->getContent();
882 $this->edittime = $this->mArticle->getTimestamp();
883 } else {
884
885 if( $this->section != '' ) {
886 if( $this->section == 'new' ) {
887 $s = wfMsg('editingcomment', $this->mTitle->getPrefixedText() );
888 } else {
889 $s = wfMsg('editingsection', $this->mTitle->getPrefixedText() );
890 $matches = array();
891 if( !$this->summary && !$this->preview && !$this->diff ) {
892 preg_match( "/^(=+)(.+)\\1/mi",
893 $this->textbox1,
894 $matches );
895 if( !empty( $matches[2] ) ) {
896 $this->summary = "/* ". trim($matches[2])." */ ";
897 }
898 }
899 }
900 } else {
901 $s = wfMsg( 'editing', $this->mTitle->getPrefixedText() );
902 }
903 $wgOut->setPageTitle( $s );
904
905 if ( $this->missingComment ) {
906 $wgOut->addWikiText( wfMsg( 'missingcommenttext' ) );
907 }
908
909 if( $this->missingSummary && $this->section != 'new' ) {
910 $wgOut->addWikiText( wfMsg( 'missingsummary' ) );
911 }
912
913 if( $this->missingSummary && $this->section == 'new' ) {
914 $wgOut->addWikiText( wfMsg( 'missingcommentheader' ) );
915 }
916
917 if( !$this->hookError == '' ) {
918 $wgOut->addWikiText( $this->hookError );
919 }
920
921 if ( !$this->checkUnicodeCompliantBrowser() ) {
922 $wgOut->addWikiText( wfMsg( 'nonunicodebrowser') );
923 }
924 if ( isset( $this->mArticle )
925 && isset( $this->mArticle->mRevision )
926 && !$this->mArticle->mRevision->isCurrent() ) {
927 $this->mArticle->setOldSubtitle( $this->mArticle->mRevision->getId() );
928 $wgOut->addWikiText( wfMsg( 'editingold' ) );
929 }
930 }
931
932 if( wfReadOnly() ) {
933 $wgOut->addWikiText( wfMsg( 'readonlywarning' ) );
934 } elseif( $wgUser->isAnon() && $this->formtype != 'preview' ) {
935 $wgOut->addWikiText( wfMsg( 'anoneditwarning' ) );
936 } else {
937 if( $this->isCssJsSubpage && $this->formtype != 'preview' ) {
938 # Check the skin exists
939 if( $this->isValidCssJsSubpage ) {
940 $wgOut->addWikiText( wfMsg( 'usercssjsyoucanpreview' ) );
941 } else {
942 $wgOut->addWikiText( wfMsg( 'userinvalidcssjstitle', $this->mTitle->getSkinFromCssJsSubpage() ) );
943 }
944 }
945 }
946
947 if( $this->mTitle->isProtected( 'edit' ) ) {
948 # Is the protection due to the namespace, e.g. interface text?
949 if( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
950 # Yes; remind the user
951 $notice = wfMsg( 'editinginterface' );
952 } elseif( $this->mTitle->isSemiProtected() ) {
953 # No; semi protected
954 $notice = wfMsg( 'semiprotectedpagewarning' );
955 if( wfEmptyMsg( 'semiprotectedpagewarning', $notice ) || $notice == '-' ) {
956 $notice = '';
957 }
958 } else {
959 # No; regular protection
960 $notice = wfMsg( 'protectedpagewarning' );
961 }
962 $wgOut->addWikiText( $notice );
963 }
964
965 if ( $this->kblength === false ) {
966 $this->kblength = (int)(strlen( $this->textbox1 ) / 1024);
967 }
968 if ( $this->tooBig || $this->kblength > $wgMaxArticleSize ) {
969 $wgOut->addWikiText( wfMsg( 'longpageerror', $wgLang->formatNum( $this->kblength ), $wgMaxArticleSize ) );
970 } elseif( $this->kblength > 29 ) {
971 $wgOut->addWikiText( wfMsg( 'longpagewarning', $wgLang->formatNum( $this->kblength ) ) );
972 }
973
974 #need to parse the preview early so that we know which templates are used,
975 #otherwise users with "show preview after edit box" will get a blank list
976 $previewOutput = $this->getPreviewText();
977
978 $rows = $wgUser->getIntOption( 'rows' );
979 $cols = $wgUser->getIntOption( 'cols' );
980
981 $ew = $wgUser->getOption( 'editwidth' );
982 if ( $ew ) $ew = " style=\"width:100%\"";
983 else $ew = '';
984
985 $q = 'action=submit';
986 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
987 $action = $this->mTitle->escapeLocalURL( $q );
988
989 $summary = wfMsg('summary');
990 $subject = wfMsg('subject');
991 $minor = wfMsgExt('minoredit', array('parseinline'));
992 $watchthis = wfMsgExt('watchthis', array('parseinline'));
993
994 $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedText(),
995 wfMsgExt('cancel', array('parseinline')) );
996 $edithelpurl = Skin::makeInternalOrExternalUrl( wfMsgForContent( 'edithelppage' ));
997 $edithelp = '<a target="helpwindow" href="'.$edithelpurl.'">'.
998 htmlspecialchars( wfMsg( 'edithelp' ) ).'</a> '.
999 htmlspecialchars( wfMsg( 'newwindow' ) );
1000
1001 global $wgRightsText;
1002 $copywarn = "<div id=\"editpage-copywarn\">\n" .
1003 wfMsg( $wgRightsText ? 'copyrightwarning' : 'copyrightwarning2',
1004 '[[' . wfMsgForContent( 'copyrightpage' ) . ']]',
1005 $wgRightsText ) . "\n</div>";
1006
1007 if( $wgUser->getOption('showtoolbar') and !$this->isCssJsSubpage ) {
1008 # prepare toolbar for edit buttons
1009 $toolbar = $this->getEditToolbar();
1010 } else {
1011 $toolbar = '';
1012 }
1013
1014 // activate checkboxes if user wants them to be always active
1015 if( !$this->preview && !$this->diff ) {
1016 # Sort out the "watch" checkbox
1017 if( $wgUser->getOption( 'watchdefault' ) ) {
1018 # Watch all edits
1019 $this->watchthis = true;
1020 } elseif( $wgUser->getOption( 'watchcreations' ) && !$this->mTitle->exists() ) {
1021 # Watch creations
1022 $this->watchthis = true;
1023 } elseif( $this->mTitle->userIsWatching() ) {
1024 # Already watched
1025 $this->watchthis = true;
1026 }
1027
1028 if( $wgUser->getOption( 'minordefault' ) ) $this->minoredit = true;
1029 }
1030
1031 $minoredithtml = '';
1032
1033 if ( $wgUser->isAllowed('minoredit') ) {
1034 $minoredithtml =
1035 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?" checked='checked'":"").
1036 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />\n".
1037 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>\n";
1038 }
1039
1040 $watchhtml = '';
1041
1042 if ( $wgUser->isLoggedIn() ) {
1043 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".
1044 ($this->watchthis?" checked='checked'":"").
1045 " accesskey=\"".htmlspecialchars(wfMsg('accesskey-watch'))."\" id='wpWatchthis' />\n".
1046 "<label for='wpWatchthis' title=\"" .
1047 htmlspecialchars(wfMsg('tooltip-watch'))."\">{$watchthis}</label>\n";
1048 }
1049
1050 $checkboxhtml = $minoredithtml . $watchhtml;
1051
1052 $wgOut->addHTML( $this->editFormPageTop );
1053
1054 if ( $wgUser->getOption( 'previewontop' ) ) {
1055
1056 if ( 'preview' == $this->formtype ) {
1057 $this->showPreview( $previewOutput );
1058 } else {
1059 $wgOut->addHTML( '<div id="wikiPreview"></div>' );
1060 }
1061
1062 if ( 'diff' == $this->formtype ) {
1063 $wgOut->addHTML( $this->getDiff() );
1064 }
1065 }
1066
1067
1068 $wgOut->addHTML( $this->editFormTextTop );
1069
1070 # if this is a comment, show a subject line at the top, which is also the edit summary.
1071 # Otherwise, show a summary field at the bottom
1072 $summarytext = htmlspecialchars( $wgContLang->recodeForEdit( $this->summary ) ); # FIXME
1073 if( $this->section == 'new' ) {
1074 $commentsubject="<span id='wpSummaryLabel'><label for='wpSummary'>{$subject}:</label></span>\n<div class='editOptions'>\n<input tabindex='1' type='text' value=\"$summarytext\" name='wpSummary' id='wpSummary' maxlength='200' size='60' /><br />";
1075 $editsummary = '';
1076 $subjectpreview = $summarytext && $this->preview ? "<div class=\"mw-summary-preview\">".wfMsg('subject-preview').':'.$sk->commentBlock( $this->summary, $this->mTitle )."</div>\n" : '';
1077 $summarypreview = '';
1078 } else {
1079 $commentsubject = '';
1080 $editsummary="<span id='wpSummaryLabel'><label for='wpSummary'>{$summary}:</label></span>\n<div class='editOptions'>\n<input tabindex='2' type='text' value=\"$summarytext\" name='wpSummary' id='wpSummary' maxlength='200' size='60' /><br />";
1081 $summarypreview = $summarytext && $this->preview ? "<div class=\"mw-summary-preview\">".wfMsg('summary-preview').':'.$sk->commentBlock( $this->summary, $this->mTitle )."</div>\n" : '';
1082 $subjectpreview = '';
1083 }
1084
1085 # Set focus to the edit box on load, except on preview or diff, where it would interfere with the display
1086 if( !$this->preview && !$this->diff ) {
1087 $wgOut->setOnloadHandler( 'document.editform.wpTextbox1.focus()' );
1088 }
1089 $templates = ($this->preview || $this->section) ? $this->mPreviewTemplates : $this->mArticle->getUsedTemplates();
1090 $formattedtemplates = $sk->formatTemplates( $templates, $this->preview, $this->section != '');
1091
1092 global $wgUseMetadataEdit ;
1093 if ( $wgUseMetadataEdit ) {
1094 $metadata = $this->mMetaData ;
1095 $metadata = htmlspecialchars( $wgContLang->recodeForEdit( $metadata ) ) ;
1096 $top = wfMsgWikiHtml( 'metadata_help' );
1097 $metadata = $top . "<textarea name='metadata' rows='3' cols='{$cols}'{$ew}>{$metadata}</textarea>" ;
1098 }
1099 else $metadata = "" ;
1100
1101 $hidden = '';
1102 $recreate = '';
1103 if ($this->deletedSinceEdit) {
1104 if ( 'save' != $this->formtype ) {
1105 $wgOut->addWikiText( wfMsg('deletedwhileediting'));
1106 } else {
1107 // Hide the toolbar and edit area, use can click preview to get it back
1108 // Add an confirmation checkbox and explanation.
1109 $toolbar = '';
1110 $hidden = 'type="hidden" style="display:none;"';
1111 $recreate = $wgOut->parse( wfMsg( 'confirmrecreate', $this->lastDelete->user_name , $this->lastDelete->log_comment ));
1112 $recreate .=
1113 "<br /><input tabindex='1' type='checkbox' value='1' name='wpRecreate' id='wpRecreate' />".
1114 "<label for='wpRecreate' title='".wfMsg('tooltip-recreate')."'>". wfMsg('recreate')."</label>";
1115 }
1116 }
1117
1118 $temp = array(
1119 'id' => 'wpSave',
1120 'name' => 'wpSave',
1121 'type' => 'submit',
1122 'tabindex' => '5',
1123 'value' => wfMsg('savearticle'),
1124 'accesskey' => wfMsg('accesskey-save'),
1125 'title' => wfMsg('tooltip-save'),
1126 );
1127 $buttons['save'] = wfElement('input', $temp, '');
1128 $temp = array(
1129 'id' => 'wpDiff',
1130 'name' => 'wpDiff',
1131 'type' => 'submit',
1132 'tabindex' => '7',
1133 'value' => wfMsg('showdiff'),
1134 'accesskey' => wfMsg('accesskey-diff'),
1135 'title' => wfMsg('tooltip-diff'),
1136 );
1137 $buttons['diff'] = wfElement('input', $temp, '');
1138
1139 global $wgLivePreview;
1140 if ( $wgLivePreview && $wgUser->getOption( 'uselivepreview' ) ) {
1141 $temp = array(
1142 'id' => 'wpPreview',
1143 'name' => 'wpPreview',
1144 'type' => 'submit',
1145 'tabindex' => '6',
1146 'value' => wfMsg('showpreview'),
1147 'accesskey' => '',
1148 'title' => wfMsg('tooltip-preview'),
1149 'style' => 'display: none;',
1150 );
1151 $buttons['preview'] = wfElement('input', $temp, '');
1152 $temp = array(
1153 'id' => 'wpLivePreview',
1154 'name' => 'wpLivePreview',
1155 'type' => 'submit',
1156 'tabindex' => '6',
1157 'value' => wfMsg('showlivepreview'),
1158 'accesskey' => wfMsg('accesskey-preview'),
1159 'title' => '',
1160 'onclick' => $this->doLivePreviewScript(),
1161 );
1162 $buttons['live'] = wfElement('input', $temp, '');
1163 } else {
1164 $temp = array(
1165 'id' => 'wpPreview',
1166 'name' => 'wpPreview',
1167 'type' => 'submit',
1168 'tabindex' => '6',
1169 'value' => wfMsg('showpreview'),
1170 'accesskey' => wfMsg('accesskey-preview'),
1171 'title' => wfMsg('tooltip-preview'),
1172 );
1173 $buttons['preview'] = wfElement('input', $temp, '');
1174 $buttons['live'] = '';
1175 }
1176
1177 $safemodehtml = $this->checkUnicodeCompliantBrowser()
1178 ? ""
1179 : "<input type='hidden' name=\"safemode\" value='1' />\n";
1180
1181 $wgOut->addHTML( <<<END
1182 {$toolbar}
1183 <form id="editform" name="editform" method="post" action="$action" enctype="multipart/form-data">
1184 END
1185 );
1186
1187 if( is_callable( $formCallback ) ) {
1188 call_user_func_array( $formCallback, array( &$wgOut ) );
1189 }
1190
1191 // Put these up at the top to ensure they aren't lost on early form submission
1192 $wgOut->addHTML( "
1193 <input type='hidden' value=\"" . htmlspecialchars( $this->section ) . "\" name=\"wpSection\" />
1194 <input type='hidden' value=\"{$this->starttime}\" name=\"wpStarttime\" />\n
1195 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n
1196 <input type='hidden' value=\"{$this->scrolltop}\" name=\"wpScrolltop\" id=\"wpScrolltop\" />\n" );
1197
1198 $wgOut->addHTML( <<<END
1199 $recreate
1200 {$commentsubject}
1201 {$subjectpreview}
1202 <textarea tabindex='1' accesskey="," name="wpTextbox1" id="wpTextbox1" rows='{$rows}'
1203 cols='{$cols}'{$ew} $hidden>
1204 END
1205 . htmlspecialchars( $this->safeUnicodeOutput( $this->textbox1 ) ) .
1206 "
1207 </textarea>
1208 " );
1209
1210 $wgOut->addWikiText( $copywarn );
1211 $wgOut->addHTML( $this->editFormTextAfterWarn );
1212 $wgOut->addHTML( "
1213 {$metadata}
1214 {$editsummary}
1215 {$summarypreview}
1216 {$checkboxhtml}
1217 {$safemodehtml}
1218 ");
1219
1220 $wgOut->addHTML(
1221 "<div class='editButtons'>
1222 {$buttons['save']}
1223 {$buttons['preview']}
1224 {$buttons['live']}
1225 {$buttons['diff']}
1226 <span class='editHelp'>{$cancel} | {$edithelp}</span>
1227 </div><!-- editButtons -->
1228 </div><!-- editOptions -->");
1229
1230 $wgOut->addWikiText( wfMsgForContent( 'edittools' ) );
1231 $wgOut->addHTML( $this->editFormTextAfterTools );
1232
1233 $wgOut->addHTML( "
1234 <div class='templatesUsed'>
1235 {$formattedtemplates}
1236 </div>
1237 " );
1238
1239 if ( $wgUser->isLoggedIn() ) {
1240 /**
1241 * To make it harder for someone to slip a user a page
1242 * which submits an edit form to the wiki without their
1243 * knowledge, a random token is associated with the login
1244 * session. If it's not passed back with the submission,
1245 * we won't save the page, or render user JavaScript and
1246 * CSS previews.
1247 */
1248 $token = htmlspecialchars( $wgUser->editToken() );
1249 $wgOut->addHTML( "\n<input type='hidden' value=\"$token\" name=\"wpEditToken\" />\n" );
1250 }
1251
1252 # If a blank edit summary was previously provided, and the appropriate
1253 # user preference is active, pass a hidden tag here. This will stop the
1254 # user being bounced back more than once in the event that a summary
1255 # is not required.
1256 if( $this->missingSummary ) {
1257 $wgOut->addHTML( "<input type=\"hidden\" name=\"wpIgnoreBlankSummary\" value=\"1\" />\n" );
1258 }
1259
1260 # For a bit more sophisticated detection of blank summaries, hash the
1261 # automatic one and pass that in a hidden field.
1262 $autosumm = $this->autoSumm ? $this->autoSumm : md5( $this->summary );
1263 $wgOut->addHtml( wfHidden( 'wpAutoSummary', $autosumm ) );
1264
1265 if ( $this->isConflict ) {
1266 $wgOut->addWikiText( '==' . wfMsg( "yourdiff" ) . '==' );
1267
1268 $de = new DifferenceEngine( $this->mTitle );
1269 $de->setText( $this->textbox2, $this->textbox1 );
1270 $de->showDiff( wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
1271
1272 $wgOut->addWikiText( '==' . wfMsg( "yourtext" ) . '==' );
1273 $wgOut->addHTML( "<textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
1274 . htmlspecialchars( $this->safeUnicodeOutput( $this->textbox2 ) ) . "\n</textarea>" );
1275 }
1276 $wgOut->addHTML( $this->editFormTextBottom );
1277 $wgOut->addHTML( "</form>\n" );
1278 if ( !$wgUser->getOption( 'previewontop' ) ) {
1279
1280 if ( $this->formtype == 'preview') {
1281 $this->showPreview( $previewOutput );
1282 } else {
1283 $wgOut->addHTML( '<div id="wikiPreview"></div>' );
1284 }
1285
1286 if ( $this->formtype == 'diff') {
1287 $wgOut->addHTML( $this->getDiff() );
1288 }
1289
1290 }
1291
1292 wfProfileOut( $fname );
1293 }
1294
1295 /**
1296 * Append preview output to $wgOut.
1297 * Includes category rendering if this is a category page.
1298 *
1299 * @param string $text The HTML to be output for the preview.
1300 */
1301 private function showPreview( $text ) {
1302 global $wgOut;
1303
1304 $wgOut->addHTML( '<div id="wikiPreview">' );
1305 if($this->mTitle->getNamespace() == NS_CATEGORY) {
1306 $this->mArticle->openShowCategory();
1307 }
1308 $wgOut->addHTML( $text );
1309 if($this->mTitle->getNamespace() == NS_CATEGORY) {
1310 $this->mArticle->closeShowCategory();
1311 }
1312 $wgOut->addHTML( '</div>' );
1313 }
1314
1315 /**
1316 * Live Preview lets us fetch rendered preview page content and
1317 * add it to the page without refreshing the whole page.
1318 * If not supported by the browser it will fall through to the normal form
1319 * submission method.
1320 *
1321 * This function outputs a script tag to support live preview, and
1322 * returns an onclick handler which should be added to the attributes
1323 * of the preview button
1324 */
1325 function doLivePreviewScript() {
1326 global $wgStylePath, $wgJsMimeType, $wgStyleVersion, $wgOut, $wgTitle;
1327 $wgOut->addHTML( '<script type="'.$wgJsMimeType.'" src="' .
1328 htmlspecialchars( "$wgStylePath/common/preview.js?$wgStyleVersion" ) .
1329 '"></script>' . "\n" );
1330 $liveAction = $wgTitle->getLocalUrl( 'action=submit&wpPreview=true&live=true' );
1331 return "return !livePreview(" .
1332 "getElementById('wikiPreview')," .
1333 "editform.wpTextbox1.value," .
1334 '"' . $liveAction . '"' . ")";
1335 }
1336
1337 function getLastDelete() {
1338 $dbr =& wfGetDB( DB_SLAVE );
1339 $fname = 'EditPage::getLastDelete';
1340 $res = $dbr->select(
1341 array( 'logging', 'user' ),
1342 array( 'log_type',
1343 'log_action',
1344 'log_timestamp',
1345 'log_user',
1346 'log_namespace',
1347 'log_title',
1348 'log_comment',
1349 'log_params',
1350 'user_name', ),
1351 array( 'log_namespace' => $this->mTitle->getNamespace(),
1352 'log_title' => $this->mTitle->getDBkey(),
1353 'log_type' => 'delete',
1354 'log_action' => 'delete',
1355 'user_id=log_user' ),
1356 $fname,
1357 array( 'LIMIT' => 1, 'ORDER BY' => 'log_timestamp DESC' ) );
1358
1359 if($dbr->numRows($res) == 1) {
1360 while ( $x = $dbr->fetchObject ( $res ) )
1361 $data = $x;
1362 $dbr->freeResult ( $res ) ;
1363 } else {
1364 $data = null;
1365 }
1366 return $data;
1367 }
1368
1369 /**
1370 * @todo document
1371 */
1372 function getPreviewText() {
1373 global $wgOut, $wgUser, $wgTitle, $wgParser;
1374
1375 $fname = 'EditPage::getPreviewText';
1376 wfProfileIn( $fname );
1377
1378 if ( $this->mTriedSave && !$this->mTokenOk ) {
1379 $msg = 'session_fail_preview';
1380 } else {
1381 $msg = 'previewnote';
1382 }
1383 $previewhead = '<h2>' . htmlspecialchars( wfMsg( 'preview' ) ) . "</h2>\n" .
1384 "<div class='previewnote'>" . $wgOut->parse( wfMsg( $msg ) ) . "</div>\n";
1385 if ( $this->isConflict ) {
1386 $previewhead.='<h2>' . htmlspecialchars( wfMsg( 'previewconflict' ) ) . "</h2>\n";
1387 }
1388
1389 $parserOptions = ParserOptions::newFromUser( $wgUser );
1390 $parserOptions->setEditSection( false );
1391
1392 global $wgRawHtml;
1393 if( $wgRawHtml && !$this->mTokenOk ) {
1394 // Could be an offsite preview attempt. This is very unsafe if
1395 // HTML is enabled, as it could be an attack.
1396 return $wgOut->parse( "<div class='previewnote'>" .
1397 wfMsg( 'session_fail_preview_html' ) . "</div>" );
1398 }
1399
1400 # don't parse user css/js, show message about preview
1401 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
1402
1403 if ( $this->isCssJsSubpage ) {
1404 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
1405 $previewtext = wfMsg('usercsspreview');
1406 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
1407 $previewtext = wfMsg('userjspreview');
1408 }
1409 $parserOptions->setTidy(true);
1410 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
1411 $wgOut->addHTML( $parserOutput->mText );
1412 wfProfileOut( $fname );
1413 return $previewhead;
1414 } else {
1415 $toparse = $this->textbox1;
1416
1417 # If we're adding a comment, we need to show the
1418 # summary as the headline
1419 if($this->section=="new" && $this->summary!="") {
1420 $toparse="== {$this->summary} ==\n\n".$toparse;
1421 }
1422
1423 if ( $this->mMetaData != "" ) $toparse .= "\n" . $this->mMetaData ;
1424 $parserOptions->setTidy(true);
1425 $parserOutput = $wgParser->parse( $this->mArticle->preSaveTransform( $toparse ) ."\n\n",
1426 $wgTitle, $parserOptions );
1427
1428 $previewHTML = $parserOutput->getText();
1429 $wgOut->addParserOutputNoText( $parserOutput );
1430
1431 foreach ( $parserOutput->getTemplates() as $ns => $template)
1432 foreach ( array_keys( $template ) as $dbk)
1433 $this->mPreviewTemplates[] = Title::makeTitle($ns, $dbk);
1434
1435 wfProfileOut( $fname );
1436 return $previewhead . $previewHTML;
1437 }
1438 }
1439
1440 /**
1441 * Call the stock "user is blocked" page
1442 */
1443 function blockedPage() {
1444 global $wgOut, $wgUser;
1445 $wgOut->blockedPage( false ); # Standard block notice on the top, don't 'return'
1446
1447 # If the user made changes, preserve them when showing the markup
1448 # (This happens when a user is blocked during edit, for instance)
1449 $first = $this->firsttime || ( !$this->save && $this->textbox1 == '' );
1450 if( $first ) {
1451 $source = $this->mTitle->exists() ? $this->getContent() : false;
1452 } else {
1453 $source = $this->textbox1;
1454 }
1455
1456 # Spit out the source or the user's modified version
1457 if( $source !== false ) {
1458 $rows = $wgUser->getOption( 'rows' );
1459 $cols = $wgUser->getOption( 'cols' );
1460 $attribs = array( 'id' => 'wpTextbox1', 'name' => 'wpTextbox1', 'cols' => $cols, 'rows' => $rows, 'readonly' => 'readonly' );
1461 $wgOut->addHtml( '<hr />' );
1462 $wgOut->addWikiText( wfMsg( $first ? 'blockedoriginalsource' : 'blockededitsource', $this->mTitle->getPrefixedText() ) );
1463 $wgOut->addHtml( wfOpenElement( 'textarea', $attribs ) . htmlspecialchars( $source ) . wfCloseElement( 'textarea' ) );
1464 }
1465 }
1466
1467 /**
1468 * Produce the stock "please login to edit pages" page
1469 */
1470 function userNotLoggedInPage() {
1471 global $wgUser, $wgOut;
1472 $skin = $wgUser->getSkin();
1473
1474 $loginTitle = SpecialPage::getTitleFor( 'Userlogin' );
1475 $loginLink = $skin->makeKnownLinkObj( $loginTitle, wfMsgHtml( 'loginreqlink' ), 'returnto=' . $this->mTitle->getPrefixedUrl() );
1476
1477 $wgOut->setPageTitle( wfMsg( 'whitelistedittitle' ) );
1478 $wgOut->setRobotPolicy( 'noindex,nofollow' );
1479 $wgOut->setArticleRelated( false );
1480
1481 $wgOut->addHtml( wfMsgWikiHtml( 'whitelistedittext', $loginLink ) );
1482 $wgOut->returnToMain( false, $this->mTitle->getPrefixedUrl() );
1483 }
1484
1485 /**
1486 * Creates a basic error page which informs the user that
1487 * they have to validate their email address before being
1488 * allowed to edit.
1489 */
1490 function userNotConfirmedPage() {
1491 global $wgOut;
1492
1493 $wgOut->setPageTitle( wfMsg( 'confirmedittitle' ) );
1494 $wgOut->setRobotPolicy( 'noindex,nofollow' );
1495 $wgOut->setArticleRelated( false );
1496
1497 $wgOut->addWikiText( wfMsg( 'confirmedittext' ) );
1498 $wgOut->returnToMain( false );
1499 }
1500
1501 /**
1502 * Produce the stock "your edit contains spam" page
1503 *
1504 * @param $match Text which triggered one or more filters
1505 */
1506 function spamPage( $match = false ) {
1507 global $wgOut;
1508
1509 $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) );
1510 $wgOut->setRobotPolicy( 'noindex,nofollow' );
1511 $wgOut->setArticleRelated( false );
1512
1513 $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) );
1514 if ( $match )
1515 $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$match}</nowiki>" ) );
1516
1517 $wgOut->returnToMain( false );
1518 }
1519
1520 /**
1521 * @private
1522 * @todo document
1523 */
1524 function mergeChangesInto( &$editText ){
1525 $fname = 'EditPage::mergeChangesInto';
1526 wfProfileIn( $fname );
1527
1528 $db =& wfGetDB( DB_MASTER );
1529
1530 // This is the revision the editor started from
1531 $baseRevision = Revision::loadFromTimestamp(
1532 $db, $this->mArticle->mTitle, $this->edittime );
1533 if( is_null( $baseRevision ) ) {
1534 wfProfileOut( $fname );
1535 return false;
1536 }
1537 $baseText = $baseRevision->getText();
1538
1539 // The current state, we want to merge updates into it
1540 $currentRevision = Revision::loadFromTitle(
1541 $db, $this->mArticle->mTitle );
1542 if( is_null( $currentRevision ) ) {
1543 wfProfileOut( $fname );
1544 return false;
1545 }
1546 $currentText = $currentRevision->getText();
1547
1548 $result = '';
1549 if( wfMerge( $baseText, $editText, $currentText, $result ) ){
1550 $editText = $result;
1551 wfProfileOut( $fname );
1552 return true;
1553 } else {
1554 wfProfileOut( $fname );
1555 return false;
1556 }
1557 }
1558
1559 /**
1560 * Check if the browser is on a blacklist of user-agents known to
1561 * mangle UTF-8 data on form submission. Returns true if Unicode
1562 * should make it through, false if it's known to be a problem.
1563 * @return bool
1564 * @private
1565 */
1566 function checkUnicodeCompliantBrowser() {
1567 global $wgBrowserBlackList;
1568 if( empty( $_SERVER["HTTP_USER_AGENT"] ) ) {
1569 // No User-Agent header sent? Trust it by default...
1570 return true;
1571 }
1572 $currentbrowser = $_SERVER["HTTP_USER_AGENT"];
1573 foreach ( $wgBrowserBlackList as $browser ) {
1574 if ( preg_match($browser, $currentbrowser) ) {
1575 return false;
1576 }
1577 }
1578 return true;
1579 }
1580
1581 /**
1582 * Format an anchor fragment as it would appear for a given section name
1583 * @param string $text
1584 * @return string
1585 * @private
1586 */
1587 function sectionAnchor( $text ) {
1588 $headline = Sanitizer::decodeCharReferences( $text );
1589 # strip out HTML
1590 $headline = preg_replace( '/<.*?' . '>/', '', $headline );
1591 $headline = trim( $headline );
1592 $sectionanchor = '#' . urlencode( str_replace( ' ', '_', $headline ) );
1593 $replacearray = array(
1594 '%3A' => ':',
1595 '%' => '.'
1596 );
1597 return str_replace(
1598 array_keys( $replacearray ),
1599 array_values( $replacearray ),
1600 $sectionanchor );
1601 }
1602
1603 /**
1604 * Shows a bulletin board style toolbar for common editing functions.
1605 * It can be disabled in the user preferences.
1606 * The necessary JavaScript code can be found in style/wikibits.js.
1607 */
1608 function getEditToolbar() {
1609 global $wgStylePath, $wgContLang, $wgJsMimeType;
1610
1611 /**
1612 * toolarray an array of arrays which each include the filename of
1613 * the button image (without path), the opening tag, the closing tag,
1614 * and optionally a sample text that is inserted between the two when no
1615 * selection is highlighted.
1616 * The tip text is shown when the user moves the mouse over the button.
1617 *
1618 * Already here are accesskeys (key), which are not used yet until someone
1619 * can figure out a way to make them work in IE. However, we should make
1620 * sure these keys are not defined on the edit page.
1621 */
1622 $toolarray=array(
1623 array( 'image'=>'button_bold.png',
1624 'open' => "\'\'\'",
1625 'close' => "\'\'\'",
1626 'sample'=> wfMsg('bold_sample'),
1627 'tip' => wfMsg('bold_tip'),
1628 'key' => 'B'
1629 ),
1630 array( 'image'=>'button_italic.png',
1631 'open' => "\'\'",
1632 'close' => "\'\'",
1633 'sample'=> wfMsg('italic_sample'),
1634 'tip' => wfMsg('italic_tip'),
1635 'key' => 'I'
1636 ),
1637 array( 'image'=>'button_link.png',
1638 'open' => '[[',
1639 'close' => ']]',
1640 'sample'=> wfMsg('link_sample'),
1641 'tip' => wfMsg('link_tip'),
1642 'key' => 'L'
1643 ),
1644 array( 'image'=>'button_extlink.png',
1645 'open' => '[',
1646 'close' => ']',
1647 'sample'=> wfMsg('extlink_sample'),
1648 'tip' => wfMsg('extlink_tip'),
1649 'key' => 'X'
1650 ),
1651 array( 'image'=>'button_headline.png',
1652 'open' => "\\n== ",
1653 'close' => " ==\\n",
1654 'sample'=> wfMsg('headline_sample'),
1655 'tip' => wfMsg('headline_tip'),
1656 'key' => 'H'
1657 ),
1658 array( 'image'=>'button_image.png',
1659 'open' => '[['.$wgContLang->getNsText(NS_IMAGE).":",
1660 'close' => ']]',
1661 'sample'=> wfMsg('image_sample'),
1662 'tip' => wfMsg('image_tip'),
1663 'key' => 'D'
1664 ),
1665 array( 'image' =>'button_media.png',
1666 'open' => '[['.$wgContLang->getNsText(NS_MEDIA).':',
1667 'close' => ']]',
1668 'sample'=> wfMsg('media_sample'),
1669 'tip' => wfMsg('media_tip'),
1670 'key' => 'M'
1671 ),
1672 array( 'image' =>'button_math.png',
1673 'open' => "<math>",
1674 'close' => "<\\/math>",
1675 'sample'=> wfMsg('math_sample'),
1676 'tip' => wfMsg('math_tip'),
1677 'key' => 'C'
1678 ),
1679 array( 'image' =>'button_nowiki.png',
1680 'open' => "<nowiki>",
1681 'close' => "<\\/nowiki>",
1682 'sample'=> wfMsg('nowiki_sample'),
1683 'tip' => wfMsg('nowiki_tip'),
1684 'key' => 'N'
1685 ),
1686 array( 'image' =>'button_sig.png',
1687 'open' => '--~~~~',
1688 'close' => '',
1689 'sample'=> '',
1690 'tip' => wfMsg('sig_tip'),
1691 'key' => 'Y'
1692 ),
1693 array( 'image' =>'button_hr.png',
1694 'open' => "\\n----\\n",
1695 'close' => '',
1696 'sample'=> '',
1697 'tip' => wfMsg('hr_tip'),
1698 'key' => 'R'
1699 )
1700 );
1701 $toolbar = "<div id='toolbar'>\n";
1702 $toolbar.="<script type='$wgJsMimeType'>\n/*<![CDATA[*/\n";
1703
1704 foreach($toolarray as $tool) {
1705
1706 $image=$wgStylePath.'/common/images/'.$tool['image'];
1707 $open=$tool['open'];
1708 $close=$tool['close'];
1709 $sample = wfEscapeJsString( $tool['sample'] );
1710
1711 // Note that we use the tip both for the ALT tag and the TITLE tag of the image.
1712 // Older browsers show a "speedtip" type message only for ALT.
1713 // Ideally these should be different, realistically they
1714 // probably don't need to be.
1715 $tip = wfEscapeJsString( $tool['tip'] );
1716
1717 #$key = $tool["key"];
1718
1719 $toolbar.="addButton('$image','$tip','$open','$close','$sample');\n";
1720 }
1721
1722 $toolbar.="/*]]>*/\n</script>";
1723 $toolbar.="\n</div>";
1724 return $toolbar;
1725 }
1726
1727 /**
1728 * Output preview text only. This can be sucked into the edit page
1729 * via JavaScript, and saves the server time rendering the skin as
1730 * well as theoretically being more robust on the client (doesn't
1731 * disturb the edit box's undo history, won't eat your text on
1732 * failure, etc).
1733 *
1734 * @todo This doesn't include category or interlanguage links.
1735 * Would need to enhance it a bit, maybe wrap them in XML
1736 * or something... that might also require more skin
1737 * initialization, so check whether that's a problem.
1738 */
1739 function livePreview() {
1740 global $wgOut;
1741 $wgOut->disable();
1742 header( 'Content-type: text/xml' );
1743 header( 'Cache-control: no-cache' );
1744 # FIXME
1745 echo $this->getPreviewText( );
1746 /* To not shake screen up and down between preview and live-preview */
1747 echo "<br style=\"clear:both;\" />\n";
1748 }
1749
1750
1751 /**
1752 * Get a diff between the current contents of the edit box and the
1753 * version of the page we're editing from.
1754 *
1755 * If this is a section edit, we'll replace the section as for final
1756 * save and then make a comparison.
1757 *
1758 * @return string HTML
1759 */
1760 function getDiff() {
1761 $oldtext = $this->mArticle->fetchContent();
1762 $newtext = $this->mArticle->replaceSection(
1763 $this->section, $this->textbox1, $this->summary, $this->edittime );
1764 $newtext = $this->mArticle->preSaveTransform( $newtext );
1765 $oldtitle = wfMsgExt( 'currentrev', array('parseinline') );
1766 $newtitle = wfMsgExt( 'yourtext', array('parseinline') );
1767 if ( $oldtext !== false || $newtext != '' ) {
1768 $de = new DifferenceEngine( $this->mTitle );
1769 $de->setText( $oldtext, $newtext );
1770 $difftext = $de->getDiff( $oldtitle, $newtitle );
1771 } else {
1772 $difftext = '';
1773 }
1774
1775 return '<div id="wikiDiff">' . $difftext . '</div>';
1776 }
1777
1778 /**
1779 * Filter an input field through a Unicode de-armoring process if it
1780 * came from an old browser with known broken Unicode editing issues.
1781 *
1782 * @param WebRequest $request
1783 * @param string $field
1784 * @return string
1785 * @private
1786 */
1787 function safeUnicodeInput( $request, $field ) {
1788 $text = rtrim( $request->getText( $field ) );
1789 return $request->getBool( 'safemode' )
1790 ? $this->unmakesafe( $text )
1791 : $text;
1792 }
1793
1794 /**
1795 * Filter an output field through a Unicode armoring process if it is
1796 * going to an old browser with known broken Unicode editing issues.
1797 *
1798 * @param string $text
1799 * @return string
1800 * @private
1801 */
1802 function safeUnicodeOutput( $text ) {
1803 global $wgContLang;
1804 $codedText = $wgContLang->recodeForEdit( $text );
1805 return $this->checkUnicodeCompliantBrowser()
1806 ? $codedText
1807 : $this->makesafe( $codedText );
1808 }
1809
1810 /**
1811 * A number of web browsers are known to corrupt non-ASCII characters
1812 * in a UTF-8 text editing environment. To protect against this,
1813 * detected browsers will be served an armored version of the text,
1814 * with non-ASCII chars converted to numeric HTML character references.
1815 *
1816 * Preexisting such character references will have a 0 added to them
1817 * to ensure that round-trips do not alter the original data.
1818 *
1819 * @param string $invalue
1820 * @return string
1821 * @private
1822 */
1823 function makesafe( $invalue ) {
1824 // Armor existing references for reversability.
1825 $invalue = strtr( $invalue, array( "&#x" => "&#x0" ) );
1826
1827 $bytesleft = 0;
1828 $result = "";
1829 $working = 0;
1830 for( $i = 0; $i < strlen( $invalue ); $i++ ) {
1831 $bytevalue = ord( $invalue{$i} );
1832 if( $bytevalue <= 0x7F ) { //0xxx xxxx
1833 $result .= chr( $bytevalue );
1834 $bytesleft = 0;
1835 } elseif( $bytevalue <= 0xBF ) { //10xx xxxx
1836 $working = $working << 6;
1837 $working += ($bytevalue & 0x3F);
1838 $bytesleft--;
1839 if( $bytesleft <= 0 ) {
1840 $result .= "&#x" . strtoupper( dechex( $working ) ) . ";";
1841 }
1842 } elseif( $bytevalue <= 0xDF ) { //110x xxxx
1843 $working = $bytevalue & 0x1F;
1844 $bytesleft = 1;
1845 } elseif( $bytevalue <= 0xEF ) { //1110 xxxx
1846 $working = $bytevalue & 0x0F;
1847 $bytesleft = 2;
1848 } else { //1111 0xxx
1849 $working = $bytevalue & 0x07;
1850 $bytesleft = 3;
1851 }
1852 }
1853 return $result;
1854 }
1855
1856 /**
1857 * Reverse the previously applied transliteration of non-ASCII characters
1858 * back to UTF-8. Used to protect data from corruption by broken web browsers
1859 * as listed in $wgBrowserBlackList.
1860 *
1861 * @param string $invalue
1862 * @return string
1863 * @private
1864 */
1865 function unmakesafe( $invalue ) {
1866 $result = "";
1867 for( $i = 0; $i < strlen( $invalue ); $i++ ) {
1868 if( ( substr( $invalue, $i, 3 ) == "&#x" ) && ( $invalue{$i+3} != '0' ) ) {
1869 $i += 3;
1870 $hexstring = "";
1871 do {
1872 $hexstring .= $invalue{$i};
1873 $i++;
1874 } while( ctype_xdigit( $invalue{$i} ) && ( $i < strlen( $invalue ) ) );
1875
1876 // Do some sanity checks. These aren't needed for reversability,
1877 // but should help keep the breakage down if the editor
1878 // breaks one of the entities whilst editing.
1879 if ((substr($invalue,$i,1)==";") and (strlen($hexstring) <= 6)) {
1880 $codepoint = hexdec($hexstring);
1881 $result .= codepointToUtf8( $codepoint );
1882 } else {
1883 $result .= "&#x" . $hexstring . substr( $invalue, $i, 1 );
1884 }
1885 } else {
1886 $result .= substr( $invalue, $i, 1 );
1887 }
1888 }
1889 // reverse the transform that we made for reversability reasons.
1890 return strtr( $result, array( "&#x0" => "&#x" ) );
1891 }
1892
1893 function noCreatePermission() {
1894 global $wgOut;
1895 $wgOut->setPageTitle( wfMsg( 'nocreatetitle' ) );
1896 $wgOut->addWikiText( wfMsg( 'nocreatetext' ) );
1897 }
1898
1899 }
1900
1901 ?>