3 * Contain the EditPage class
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
22 var $save = false, $preview = false, $diff = false;
23 var $minoredit = false, $watchthis = false;
24 var $textbox1 = '', $textbox2 = '', $summary = '';
25 var $edittime = '', $section = '';
32 function EditPage( $article ) {
33 $this->mArticle
=& $article;
35 $this->mTitle
=& $wgTitle;
39 * This is the function that extracts metadata from the article body on the first view.
40 * To turn the feature on, set $wgUseMetadataEdit = true ; in LocalSettings
41 * and set $wgMetadataWhitelist to the *full* title of the template whitelist
43 function extractMetaDataFromArticle () {
44 global $wgUseMetadataEdit , $wgMetadataWhitelist , $wgLang ;
45 $this->mMetaData
= '' ;
46 if ( !$wgUseMetadataEdit ) return ;
47 if ( $wgMetadataWhitelist == '' ) return ;
49 $t = $this->mArticle
->getContent ( true ) ;
51 # MISSING : <nowiki> filtering
53 # Categories and language links
54 $t = explode ( "\n" , $t ) ;
55 $catlow = strtolower ( $wgLang->getNsText ( NS_CATEGORY
) ) ;
56 $cat = $ll = array() ;
57 foreach ( $t AS $key => $x )
59 $y = trim ( strtolower ( $x ) ) ;
60 while ( substr ( $y , 0 , 2 ) == '[[' )
62 $y = explode ( ']]' , trim ( $x ) ) ;
63 $first = array_shift ( $y ) ;
64 $first = explode ( ':' , $first ) ;
65 $ns = array_shift ( $first ) ;
66 $ns = trim ( str_replace ( '[' , '' , $ns ) ) ;
67 if ( strlen ( $ns ) == 2 OR strtolower ( $ns ) == $catlow )
69 $add = '[[' . $ns . ':' . implode ( ':' , $first ) . ']]' ;
70 if ( strtolower ( $ns ) == $catlow ) $cat[] = $add ;
72 $x = implode ( ']]' , $y ) ;
74 $y = trim ( strtolower ( $x ) ) ;
78 if ( count ( $cat ) ) $s .= implode ( ' ' , $cat ) . "\n" ;
79 if ( count ( $ll ) ) $s .= implode ( ' ' , $ll ) . "\n" ;
80 $t = implode ( "\n" , $t ) ;
83 $sat = array () ; # stand-alone-templates; must be lowercase
84 $wl_title = Title
::newFromText ( $wgMetadataWhitelist ) ;
85 $wl_article = new Article ( $wl_title ) ;
86 $wl = explode ( "\n" , $wl_article->getContent(true) ) ;
91 while ( substr ( $x , 0 , 1 ) == '*' )
94 $x = trim ( substr ( $x , 1 ) ) ;
98 $sat[] = strtolower ( $x ) ;
103 # Templates, but only some
104 $t = explode ( '{{' , $t ) ;
106 foreach ( $t AS $key => $x )
108 $y = explode ( '}}' , $x , 2 ) ;
109 if ( count ( $y ) == 2 )
112 $z = explode ( '|' , $z ) ;
113 $tn = array_shift ( $z ) ;
114 if ( in_array ( strtolower ( $tn ) , $sat ) )
116 $tl[] = '{{' . $y[0] . '}}' ;
118 $y = explode ( '}}' , $y[1] , 2 ) ;
120 else $t[$key] = '{{' . $x ;
122 else if ( $key != 0 ) $t[$key] = '{{' . $x ;
125 if ( count ( $tl ) ) $s .= implode ( ' ' , $tl ) ;
126 $t = implode ( '' , $t ) ;
128 $t = str_replace ( "\n\n\n" , "\n" , $t ) ;
129 $this->mArticle
->mContent
= $t ;
130 $this->mMetaData
= $s ;
134 * This is the function that gets called for "action=edit".
137 global $wgOut, $wgUser, $wgRequest;
138 // this is not an article
139 $wgOut->setArticleFlag(false);
141 $this->importFormData( $wgRequest );
144 $this->livePreview();
148 if ( ! $this->mTitle
->userCanEdit() ) {
149 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ), true );
152 if ( !$this->preview
&& !$this->diff
&& $wgUser->isBlocked( !$this->save
) ) {
153 # When previewing, don't check blocked state - will get caught at save time.
154 # Also, check when starting edition is done against slave to improve performance.
155 $this->blockedIPpage();
158 if ( !$wgUser->isAllowed('edit') ) {
159 if ( $wgUser->isAnon() ) {
160 $this->userNotLoggedInPage();
163 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ), true );
167 if ( wfReadOnly() ) {
168 if( $this->save ||
$this->preview
) {
169 $this->editForm( 'preview' );
170 } else if ( $this->diff
) {
171 $this->editForm( 'diff' );
173 $wgOut->readOnlyPage( $this->mArticle
->getContent( true ) );
178 $this->editForm( 'save' );
179 } else if ( $this->preview
) {
180 $this->editForm( 'preview' );
181 } else if ( $this->diff
) {
182 $this->editForm( 'diff' );
183 } else { # First time through
184 if( $wgUser->getOption('previewonfirst')
185 or $this->mTitle
->getNamespace() == NS_CATEGORY
) {
186 $this->editForm( 'preview', true );
188 $this->extractMetaDataFromArticle () ;
189 $this->editForm( 'initial', true );
197 function importFormData( &$request ) {
198 if( $request->wasPosted() ) {
199 # These fields need to be checked for encoding.
200 # Also remove trailing whitespace, but don't remove _initial_
201 # whitespace from the text boxes. This may be significant formatting.
202 $this->textbox1
= rtrim( $request->getText( 'wpTextbox1' ) );
203 $this->textbox2
= rtrim( $request->getText( 'wpTextbox2' ) );
204 $this->mMetaData
= rtrim( $request->getText( 'metadata' ) );
205 $this->summary
= $request->getText( 'wpSummary' );
207 $this->edittime
= $request->getVal( 'wpEdittime' );
208 if( is_null( $this->edittime
) ) {
209 # If the form is incomplete, force to preview.
210 $this->preview
= true;
212 if( $this->tokenOk( $request ) ) {
213 # Some browsers will not report any submit button
214 # if the user hits enter in the comment box.
215 # The unmarked state will be assumed to be a save,
216 # if the form seems otherwise complete.
217 $this->preview
= $request->getCheck( 'wpPreview' );
218 $this->diff
= $request->getCheck( 'wpDiff' );
220 # Page might be a hack attempt posted from
221 # an external site. Preview instead of saving.
222 $this->preview
= true;
225 $this->save
= ! ( $this->preview
OR $this->diff
);
226 if( !preg_match( '/^\d{14}$/', $this->edittime
)) {
227 $this->edittime
= null;
230 $this->minoredit
= $request->getCheck( 'wpMinoredit' );
231 $this->watchthis
= $request->getCheck( 'wpWatchthis' );
233 # Not a posted form? Start with nothing.
234 $this->textbox1
= '';
235 $this->textbox2
= '';
236 $this->mMetaData
= '';
238 $this->edittime
= '';
239 $this->preview
= false;
242 $this->minoredit
= false;
243 $this->watchthis
= false;
246 $this->oldid
= $request->getInt( 'oldid' );
248 # Section edit can come from either the form or a link
249 $this->section
= $request->getVal( 'wpSection', $request->getVal( 'section' ) );
251 $this->live
= $request->getCheck( 'live' );
255 * Make sure the form isn't faking a user's credentials.
257 * @param WebRequest $request
261 function tokenOk( &$request ) {
263 if( $wgUser->isAnon() ) {
264 # Anonymous users may not have a session
265 # open. Don't tokenize.
268 return $wgUser->matchEditToken( $request->getVal( 'wpEditToken' ) );
277 * The edit form is self-submitting, so that when things like
278 * preview and edit conflicts occur, we get the same form back
279 * with the extra stuff added. Only when the final submission
280 * is made and all is well do we actually save and redirect to
281 * the newly-edited page.
283 * @param string $formtype Type of form either : save, initial, diff or preview
284 * @param bool $firsttime True to load form data from db
286 function editForm( $formtype, $firsttime = false ) {
287 global $wgOut, $wgUser;
288 global $wgLang, $wgContLang, $wgParser, $wgTitle;
289 global $wgAllowAnonymousMinor;
290 global $wgSpamRegex, $wgFilterCallback;
292 $sk = $wgUser->getSkin();
294 // css / js subpages of user pages get a special treatment
295 $isCssJsSubpage = $wgTitle->isCssJsSubpage();
298 if(!$this->mTitle
->getArticleID()) { # new article
299 $wgOut->addWikiText(wfmsg('newarticletext'));
302 if( $this->mTitle
->isTalkPage() ) {
303 $wgOut->addWikiText(wfmsg('talkpagetext'));
306 # Attempt submission here. This will check for edit conflicts,
307 # and redundantly check for locked database, blocked IPs, etc.
308 # that edit() already checked just in case someone tries to sneak
309 # in the back door with a hand-edited submission URL.
311 if ( 'save' == $formtype ) {
312 # Reintegrate metadata
313 if ( $this->mMetaData
!= '' ) $this->textbox1
.= "\n" . $this->mMetaData
;
314 $this->mMetaData
= '' ;
317 if ( $wgSpamRegex && preg_match( $wgSpamRegex, $this->textbox1
, $matches ) ) {
318 $this->spamPage ( $matches[0] );
321 if ( $wgFilterCallback && $wgFilterCallback( $this->mTitle
, $this->textbox1
, $this->section
) ) {
322 # Error messages or other handling should be performed by the filter function
325 if ( $wgUser->isBlocked( false ) ) {
326 # Check block state against master, thus 'false'.
327 $this->blockedIPpage();
331 if ( !$wgUser->isAllowed('edit') ) {
332 if ( $wgUser->isAnon() ) {
333 $this->userNotLoggedInPage();
337 $wgOut->readOnlyPage();
342 if ( wfReadOnly() ) {
343 $wgOut->readOnlyPage();
346 if ( $wgUser->pingLimiter() ) {
347 $wgOut->rateLimited();
351 # If article is new, insert it.
352 $aid = $this->mTitle
->getArticleID( GAID_FOR_UPDATE
);
354 # Don't save a new article if it's blank.
355 if ( ( '' == $this->textbox1
) ||
356 ( wfMsg( 'newarticletext' ) == $this->textbox1
) ) {
357 $wgOut->redirect( $this->mTitle
->getFullURL() );
360 if (wfRunHooks('ArticleSave', array(&$this->mArticle
, &$wgUser, &$this->textbox1
,
361 &$this->summary
, &$this->minoredit
, &$this->watchthis
, NULL)))
363 $this->mArticle
->insertNewArticle( $this->textbox1
, $this->summary
,
364 $this->minoredit
, $this->watchthis
);
365 wfRunHooks('ArticleSaveComplete', array(&$this->mArticle
, &$wgUser, $this->textbox1
,
366 $this->summary
, $this->minoredit
,
367 $this->watchthis
, NULL));
372 # Article exists. Check for edit conflict.
374 $this->mArticle
->clear(); # Force reload of dates, etc.
375 $this->mArticle
->forUpdate( true ); # Lock the article
377 if( ( $this->section
!= 'new' ) &&
378 ($this->mArticle
->getTimestamp() != $this->edittime
) ) {
381 $userid = $wgUser->getID();
384 wfDebug( "EditPage::editForm conflict! getting section '$this->section' for time '$this->edittime' (article time '" .
385 $this->mArticle
->getTimestamp() . "'\n" );
386 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
387 $this->section
, $this->textbox1
, $this->summary
, $this->edittime
);
390 wfDebug( "EditPage::editForm getting section '$this->section'\n" );
391 $text = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
392 $this->section
, $this->textbox1
, $this->summary
);
394 # Suppress edit conflict with self
396 if ( ( 0 != $userid ) && ( $this->mArticle
->getUser() == $userid ) ) {
397 wfDebug( "Suppressing edit conflict, same user.\n" );
400 # switch from section editing to normal editing in edit conflict
403 if( $this->mergeChangesInto( $text ) ){
404 // Successful merge! Maybe we should tell the user the good news?
406 wfDebug( "Suppressing edit conflict, successful merge.\n" );
409 $this->textbox1
= $text;
410 wfDebug( "Keeping edit conflict, failed merge.\n" );
414 if ( ! $isConflict ) {
417 if( $this->section
== 'new' ) {
418 if( $this->summary
!= '' ) {
419 $sectionanchor = $this->sectionAnchor( $this->summary
);
421 } elseif( $this->section
!= '' ) {
422 # Try to get a section anchor from the section source, redirect to edited section if header found
423 # XXX: might be better to integrate this into Article::getTextOfLastEditWithSectionReplacedOrAdded
424 # for duplicate heading checking and maybe parsing
425 $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1
, $matches );
426 # we can't deal with anchors, includes, html etc in the header for now,
427 # headline would need to be parsed to improve this
428 #if($hasmatch and strlen($matches[2]) > 0 and !preg_match( "/[\\['{<>]/", $matches[2])) {
429 if($hasmatch and strlen($matches[2]) > 0) {
430 $sectionanchor = $this->sectionAnchor( $matches[2] );
434 if (wfRunHooks('ArticleSave', array(&$this->mArticle
, &$wgUser, &$text,
435 &$this->summary
, &$this->minoredit
,
436 &$this->watchthis
, &$sectionanchor)))
438 # update the article here
439 if($this->mArticle
->updateArticle( $text, $this->summary
, $this->minoredit
,
440 $this->watchthis
, '', $sectionanchor ))
442 wfRunHooks('ArticleSaveComplete', array(&$this->mArticle
, &$wgUser, $text,
443 $this->summary
, $this->minoredit
,
444 $this->watchthis
, $sectionanchor));
452 # First time through: get contents, set time for conflict
455 if ( 'initial' == $formtype ||
$firsttime ) {
456 $this->edittime
= $this->mArticle
->getTimestamp();
457 $this->textbox1
= $this->mArticle
->getContent( true );
461 $wgOut->setRobotpolicy( 'noindex,nofollow' );
463 # Enabled article-related sidebar, toplinks, etc.
464 $wgOut->setArticleRelated( true );
467 $s = wfMsg( 'editconflict', $this->mTitle
->getPrefixedText() );
468 $wgOut->setPageTitle( $s );
469 $wgOut->addWikiText( wfMsg( 'explainconflict' ) );
471 $this->textbox2
= $this->textbox1
;
472 $this->textbox1
= $this->mArticle
->getContent( true );
473 $this->edittime
= $this->mArticle
->getTimestamp();
476 if( $this->section
!= '' ) {
477 if( $this->section
== 'new' ) {
478 $s = wfMsg('editingcomment', $this->mTitle
->getPrefixedText() );
480 $s = wfMsg('editingsection', $this->mTitle
->getPrefixedText() );
482 if(!$this->preview
) {
483 preg_match( "/^(=+)(.+)\\1/mi",
486 if( !empty( $matches[2] ) ) {
487 $this->summary
= "/* ". trim($matches[2])." */ ";
491 $s = wfMsg( 'editing', $this->mTitle
->getPrefixedText() );
493 $wgOut->setPageTitle( $s );
494 if ( !$this->checkUnicodeCompliantBrowser() ) {
495 $this->mArticle
->setOldSubtitle();
496 $wgOut->addWikiText( wfMsg( 'nonunicodebrowser') );
498 if ( $this->oldid
) {
499 $this->mArticle
->setOldSubtitle();
500 $wgOut->addWikiText( wfMsg( 'editingold' ) );
505 $wgOut->addWikiText( wfMsg( 'readonlywarning' ) );
506 } else if ( $isCssJsSubpage and 'preview' != $formtype) {
507 $wgOut->addWikiText( wfMsg( 'usercssjsyoucanpreview' ));
509 if( $this->mTitle
->isProtected('edit') ) {
510 $wgOut->addWikiText( wfMsg( 'protectedpagewarning' ) );
513 $kblength = (int)(strlen( $this->textbox1
) / 1024);
514 if( $kblength > 29 ) {
515 $wgOut->addWikiText( wfMsg( 'longpagewarning', $wgLang->formatNum( $kblength ) ) );
518 $rows = $wgUser->getOption( 'rows' );
519 $cols = $wgUser->getOption( 'cols' );
521 $ew = $wgUser->getOption( 'editwidth' );
522 if ( $ew ) $ew = " style=\"width:100%\"";
525 $q = 'action=submit';
526 #if ( "no" == $redirect ) { $q .= "&redirect=no"; }
527 $action = $this->mTitle
->escapeLocalURL( $q );
529 $summary = wfMsg('summary');
530 $subject = wfMsg('subject');
531 $minor = wfMsg('minoredit');
532 $watchthis = wfMsg ('watchthis');
533 $save = wfMsg('savearticle');
534 $prev = wfMsg('showpreview');
535 $diff = wfMsg('showdiff');
537 $cancel = $sk->makeKnownLink( $this->mTitle
->getPrefixedText(),
539 $edithelpurl = $sk->makeInternalOrExternalUrl( wfMsg( 'edithelppage' ));
540 $edithelp = '<a target="helpwindow" href="'.$edithelpurl.'">'.
541 htmlspecialchars( wfMsg( 'edithelp' ) ).'</a> '.
542 htmlspecialchars( wfMsg( 'newwindow' ) );
544 global $wgRightsText;
545 $copywarn = "<div id=\"editpage-copywarn\">\n" .
546 wfMsg( $wgRightsText ?
'copyrightwarning' : 'copyrightwarning2',
547 '[[' . wfMsgForContent( 'copyrightpage' ) . ']]',
548 $wgRightsText ) . "\n</div>";
550 if( $wgUser->getOption('showtoolbar') and !$isCssJsSubpage ) {
551 # prepare toolbar for edit buttons
552 $toolbar = $this->getEditToolbar();
557 // activate checkboxes if user wants them to be always active
558 if( !$this->preview
&& !$this->diff
) {
559 if( $wgUser->getOption( 'watchdefault' ) ) $this->watchthis
= true;
560 if( $wgUser->getOption( 'minordefault' ) ) $this->minoredit
= true;
562 // activate checkbox also if user is already watching the page,
563 // require wpWatchthis to be unset so that second condition is not
564 // checked unnecessarily
565 if( !$this->watchthis
&& $this->mTitle
->userIsWatching() ) $this->watchthis
= true;
570 if ( $wgUser->isLoggedIn() ||
$wgAllowAnonymousMinor ) {
572 "<input tabindex='3' type='checkbox' value='1' name='wpMinoredit'".($this->minoredit?
" checked='checked'":"").
573 " accesskey='".wfMsg('accesskey-minoredit')."' id='wpMinoredit' />".
574 "<label for='wpMinoredit' title='".wfMsg('tooltip-minoredit')."'>{$minor}</label>";
579 if ( $wgUser->isLoggedIn() ) {
580 $watchhtml = "<input tabindex='4' type='checkbox' name='wpWatchthis'".($this->watchthis?
" checked='checked'":"").
581 " accesskey='".wfMsg('accesskey-watch')."' id='wpWatchthis' />".
582 "<label for='wpWatchthis' title='".wfMsg('tooltip-watch')."'>{$watchthis}</label>";
585 $checkboxhtml = $minoredithtml . $watchhtml . '<br />';
587 $wgOut->addHTML( '<div id="wikiPreview">' );
588 if ( 'preview' == $formtype) {
589 $previewOutput = $this->getPreviewText( $isConflict, $isCssJsSubpage );
590 if ( $wgUser->getOption('previewontop' ) ) {
591 $wgOut->addHTML( $previewOutput );
592 if($this->mTitle
->getNamespace() == NS_CATEGORY
) {
593 $this->mArticle
->closeShowCategory();
595 $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
598 $wgOut->addHTML( '</div>' );
599 if ( 'diff' == $formtype ) {
600 if ( $wgUser->getOption('previewontop' ) ) {
601 $wgOut->addHTML( $this->getDiff() );
606 # if this is a comment, show a subject line at the top, which is also the edit summary.
607 # Otherwise, show a summary field at the bottom
608 $summarytext = htmlspecialchars( $wgContLang->recodeForEdit( $this->summary
) ); # FIXME
609 if( $this->section
== 'new' ) {
610 $commentsubject="{$subject}: <input tabindex='1' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
613 $commentsubject = '';
614 $editsummary="{$summary}: <input tabindex='2' type='text' value=\"$summarytext\" name=\"wpSummary\" maxlength='200' size='60' /><br />";
617 if( !$this->preview
&& !$this->diff
) {
618 # Don't select the edit box on preview; this interferes with seeing what's going on.
619 $wgOut->setOnloadHandler( 'document.editform.wpTextbox1.focus()' );
621 # Prepare a list of templates used by this page
623 $articleTemplates = $this->mArticle
->getUsedTemplates();
624 if ( count( $articleTemplates ) > 0 ) {
625 $templates = '<br />'. wfMsg( 'templatesused' ) . '<ul>';
626 foreach ( $articleTemplates as $tpl ) {
627 if ( $titleObj = Title
::makeTitle( NS_TEMPLATE
, $tpl ) ) {
628 $templates .= '<li>' . $sk->makeLinkObj( $titleObj ) . '</li>';
631 $templates .= '</ul>';
634 global $wgLivePreview, $wgStylePath;
636 * Live Preview lets us fetch rendered preview page content and
637 * add it to the page without refreshing the whole page.
638 * Set up the button for it; if not supported by the browser
639 * it will fall through to the normal form submission method.
641 if( $wgLivePreview ) {
642 global $wgJsMimeType;
643 $wgOut->addHTML( '<script type="'.$wgJsMimeType.'" src="' .
644 htmlspecialchars( $wgStylePath . '/common/preview.js' ) .
645 '"></script>' . "\n" );
646 $liveAction = $wgTitle->getLocalUrl( 'action=submit&wpPreview=true&live=true' );
647 $liveOnclick = 'onclick="return !livePreview('.
648 'getElementById(\'wikiPreview\'),' .
649 'editform.wpTextbox1.value,' .
650 htmlspecialchars( '"' . $liveAction . '"' ) . ')"';
655 global $wgUseMetadataEdit ;
656 if ( $wgUseMetadataEdit )
658 $metadata = $this->mMetaData
;
659 $metadata = htmlspecialchars( $wgContLang->recodeForEdit( $metadata ) ) ;
660 $helppage = Title
::newFromText ( wfmsg("metadata_page") ) ;
661 $top = str_replace ( "$1" , $helppage->getInternalURL() , wfmsg("metadata") ) ;
662 $metadata = $top . "<textarea name='metadata' rows='3' cols='{$cols}'{$ew}>{$metadata}</textarea>" ;
664 else $metadata = "" ;
667 $wgOut->addHTML( <<<END
669 <form id="editform" name="editform" method="post" action="$action"
670 enctype="multipart/form-data">
672 <textarea tabindex='1' accesskey="," name="wpTextbox1" rows='{$rows}'
675 . htmlspecialchars( $wgContLang->recodeForEdit( $this->textbox1
) ) .
681 <input tabindex='5' id='wpSave' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
682 " title=\"".wfMsg('tooltip-save')."\"/>
683 <input tabindex='6' id='wpPreview' type='submit' $liveOnclick value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
684 " title=\"".wfMsg('tooltip-preview')."\"/>
685 <input tabindex='7' id='wpDiff' type='submit' value=\"{$diff}\" name=\"wpDiff\" accesskey=\"".wfMsg('accesskey-diff')."\"".
686 " title=\"".wfMsg('tooltip-diff')."\"/>
687 <em>{$cancel}</em> | <em>{$edithelp}</em>{$templates}" );
688 $wgOut->addWikiText( $copywarn );
690 <input type='hidden' value=\"" . htmlspecialchars( $this->section
) . "\" name=\"wpSection\" />
691 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
693 if ( $wgUser->isLoggedIn() ) {
695 * To make it harder for someone to slip a user a page
696 * which submits an edit form to the wiki without their
697 * knowledge, a random token is associated with the login
698 * session. If it's not passed back with the submission,
699 * we won't save the page, or render user JavaScript and
702 $token = htmlspecialchars( $wgUser->editToken() );
704 <input type='hidden' value=\"$token\" name=\"wpEditToken\" />\n" );
709 require_once( "DifferenceEngine.php" );
710 $wgOut->addWikiText( '==' . wfMsg( "yourdiff" ) . '==' );
711 DifferenceEngine
::showDiff( $this->textbox2
, $this->textbox1
,
712 wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
714 $wgOut->addWikiText( '==' . wfMsg( "yourtext" ) . '==' );
715 $wgOut->addHTML( "<textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
716 . htmlspecialchars( $wgContLang->recodeForEdit( $this->textbox2
) ) .
720 $wgOut->addHTML( "</form>\n" );
721 if ( $formtype == 'preview' && !$wgUser->getOption( 'previewontop' ) ) {
722 $wgOut->addHTML( '<div id="wikiPreview">' . $previewOutput . '</div>' );
724 if ( $formtype == 'diff' && !$wgUser->getOption( 'previewontop' ) ) {
725 #$wgOut->addHTML( '<div id="wikiPreview">' . $difftext . '</div>' );
726 $wgOut->addHTML( $this->getDiff() );
733 function getPreviewText( $isConflict, $isCssJsSubpage ) {
734 global $wgOut, $wgUser, $wgTitle, $wgParser, $wgAllowDiffPreview, $wgEnableDiffPreviewPreference;
735 $previewhead = '<h2>' . htmlspecialchars( wfMsg( 'preview' ) ) . "</h2>\n" .
736 "<p class='previewnote'>" . htmlspecialchars( wfMsg( 'previewnote' ) ) . "</p>\n";
738 $previewhead.='<h2>' . htmlspecialchars( wfMsg( 'previewconflict' ) ) .
742 $parserOptions = ParserOptions
::newFromUser( $wgUser );
743 $parserOptions->setEditSection( false );
745 # don't parse user css/js, show message about preview
746 # XXX: stupid php bug won't let us use $wgTitle->isCssJsSubpage() here
748 if ( $isCssJsSubpage ) {
749 if(preg_match("/\\.css$/", $wgTitle->getText() ) ) {
750 $previewtext = wfMsg('usercsspreview');
751 } else if(preg_match("/\\.js$/", $wgTitle->getText() ) ) {
752 $previewtext = wfMsg('userjspreview');
754 $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
755 $wgOut->addHTML( $parserOutput->mText
);
758 # if user want to see preview when he edit an article
759 if( $wgUser->getOption('previewonfirst') and ($this->textbox1
== '')) {
760 $this->textbox1
= $this->mArticle
->getContent(true);
763 $toparse = $this->textbox1
;
764 if ( $this->mMetaData
!= "" ) $toparse .= "\n" . $this->mMetaData
;
766 $parserOutput = $wgParser->parse( $this->mArticle
->preSaveTransform( $toparse ) ."\n\n",
767 $wgTitle, $parserOptions );
769 $previewHTML = $parserOutput->mText
;
771 $wgOut->addCategoryLinks($parserOutput->getCategoryLinks());
772 $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
773 return $previewhead . $previewHTML;
780 function blockedIPpage() {
781 global $wgOut, $wgUser, $wgContLang, $wgIP;
783 $wgOut->setPageTitle( wfMsg( 'blockedtitle' ) );
784 $wgOut->setRobotpolicy( 'noindex,nofollow' );
785 $wgOut->setArticleRelated( false );
787 $id = $wgUser->blockedBy();
788 $reason = $wgUser->blockedFor();
791 if ( is_numeric( $id ) ) {
792 $name = User
::whoIs( $id );
796 $link = '[[' . $wgContLang->getNsText( NS_USER
) .
797 ":{$name}|{$name}]]";
799 $wgOut->addWikiText( wfMsg( 'blockedtext', $link, $reason, $ip, $name ) );
800 $wgOut->returnToMain( false );
806 function userNotLoggedInPage() {
809 $wgOut->setPageTitle( wfMsg( 'whitelistedittitle' ) );
810 $wgOut->setRobotpolicy( 'noindex,nofollow' );
811 $wgOut->setArticleRelated( false );
813 $wgOut->addWikiText( wfMsg( 'whitelistedittext' ) );
814 $wgOut->returnToMain( false );
820 function spamPage ( $match = false )
823 $wgOut->setPageTitle( wfMsg( 'spamprotectiontitle' ) );
824 $wgOut->setRobotpolicy( 'noindex,nofollow' );
825 $wgOut->setArticleRelated( false );
827 $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) );
829 $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$match}</nowiki>" ) );
831 $wgOut->returnToMain( false );
835 * Forks processes to scan the originating IP for an open proxy server
836 * MemCached can be used to skip IPs that have already been scanned
838 function proxyCheck() {
839 global $wgBlockOpenProxies, $wgProxyPorts, $wgProxyScriptPath;
840 global $wgIP, $wgUseMemCached, $wgMemc, $wgDBname, $wgProxyMemcExpiry;
842 if ( !$wgBlockOpenProxies ) {
848 if ( $wgUseMemCached ) {
849 $mcKey = $wgDBname.':proxy:ip:'.$wgIP;
850 $mcValue = $wgMemc->get( $mcKey );
858 $title = Title
::makeTitle( NS_SPECIAL
, 'Blockme' );
859 $iphash = md5( $wgIP . $wgProxyKey );
860 $url = $title->getFullURL( 'ip='.$iphash );
862 foreach ( $wgProxyPorts as $port ) {
863 $params = implode( ' ', array(
864 escapeshellarg( $wgProxyScriptPath ),
865 escapeshellarg( $wgIP ),
866 escapeshellarg( $port ),
867 escapeshellarg( $url )
869 exec( "php $params &>/dev/null &" );
872 if ( $wgUseMemCached ) {
873 $wgMemc->set( $mcKey, 1, $wgProxyMemcExpiry );
882 function mergeChangesInto( &$editText ){
883 $db =& wfGetDB( DB_MASTER
);
885 // This is the revision the editor started from
886 $baseRevision = Revision
::loadFromTimestamp(
887 $db, $this->mArticle
->mTitle
, $this->edittime
);
888 if( is_null( $baseRevision ) ) {
891 $baseText = $baseRevision->getText();
893 // The current state, we want to merge updates into it
894 $currentRevision = Revision
::loadFromTitle(
895 $db, $this->mArticle
->mTitle
);
896 if( is_null( $currentRevision ) ) {
899 $currentText = $currentRevision->getText();
901 if( wfMerge( $baseText, $editText, $currentText, $result ) ){
910 function checkUnicodeCompliantBrowser() {
911 global $wgBrowserBlackList;
912 $currentbrowser = $_SERVER["HTTP_USER_AGENT"];
913 foreach ( $wgBrowserBlackList as $browser ) {
914 if ( preg_match($browser, $currentbrowser) ) {
922 * Format an anchor fragment as it would appear for a given section name
923 * @param string $text
927 function sectionAnchor( $text ) {
928 $headline = Sanitizer
::decodeCharReferences( $text );
930 $headline = preg_replace( '/<.*?' . '>/', '', $headline );
931 $headline = trim( $headline );
932 $sectionanchor = '#' . urlencode( str_replace( ' ', '_', $headline ) );
933 $replacearray = array(
938 array_keys( $replacearray ),
939 array_values( $replacearray ),
944 * Shows a bulletin board style toolbar for common editing functions.
945 * It can be disabled in the user preferences.
946 * The necessary JavaScript code can be found in style/wikibits.js.
948 function getEditToolbar() {
949 global $wgStylePath, $wgLang, $wgMimeType, $wgJsMimeType;
952 * toolarray an array of arrays which each include the filename of
953 * the button image (without path), the opening tag, the closing tag,
954 * and optionally a sample text that is inserted between the two when no
955 * selection is highlighted.
956 * The tip text is shown when the user moves the mouse over the button.
958 * Already here are accesskeys (key), which are not used yet until someone
959 * can figure out a way to make them work in IE. However, we should make
960 * sure these keys are not defined on the edit page.
963 array( 'image'=>'button_bold.png',
966 'sample'=> wfMsg('bold_sample'),
967 'tip' => wfMsg('bold_tip'),
970 array( 'image'=>'button_italic.png',
973 'sample'=> wfMsg('italic_sample'),
974 'tip' => wfMsg('italic_tip'),
977 array( 'image'=>'button_link.png',
980 'sample'=> wfMsg('link_sample'),
981 'tip' => wfMsg('link_tip'),
984 array( 'image'=>'button_extlink.png',
987 'sample'=> wfMsg('extlink_sample'),
988 'tip' => wfMsg('extlink_tip'),
991 array( 'image'=>'button_headline.png',
994 'sample'=> wfMsg('headline_sample'),
995 'tip' => wfMsg('headline_tip'),
998 array( 'image'=>'button_image.png',
999 'open' => '[['.$wgLang->getNsText(NS_IMAGE
).":",
1001 'sample'=> wfMsg('image_sample'),
1002 'tip' => wfMsg('image_tip'),
1005 array( 'image' =>'button_media.png',
1006 'open' => '[['.$wgLang->getNsText(NS_MEDIA
).':',
1008 'sample'=> wfMsg('media_sample'),
1009 'tip' => wfMsg('media_tip'),
1012 array( 'image' =>'button_math.png',
1013 'open' => "\\<math\\>",
1014 'close' => "\\</math\\>",
1015 'sample'=> wfMsg('math_sample'),
1016 'tip' => wfMsg('math_tip'),
1019 array( 'image' =>'button_nowiki.png',
1020 'open' => "\\<nowiki\\>",
1021 'close' => "\\</nowiki\\>",
1022 'sample'=> wfMsg('nowiki_sample'),
1023 'tip' => wfMsg('nowiki_tip'),
1026 array( 'image' =>'button_sig.png',
1030 'tip' => wfMsg('sig_tip'),
1033 array( 'image' =>'button_hr.png',
1034 'open' => "\\n----\\n",
1037 'tip' => wfMsg('hr_tip'),
1041 $toolbar ="<script type='$wgJsMimeType'>\n/*<![CDATA[*/\n";
1043 $toolbar.="document.writeln(\"<div id='toolbar'>\");\n";
1044 foreach($toolarray as $tool) {
1046 $image=$wgStylePath.'/common/images/'.$tool['image'];
1047 $open=$tool['open'];
1048 $close=$tool['close'];
1049 $sample = wfEscapeJsString( $tool['sample'] );
1051 // Note that we use the tip both for the ALT tag and the TITLE tag of the image.
1052 // Older browsers show a "speedtip" type message only for ALT.
1053 // Ideally these should be different, realistically they
1054 // probably don't need to be.
1055 $tip = wfEscapeJsString( $tool['tip'] );
1057 #$key = $tool["key"];
1059 $toolbar.="addButton('$image','$tip','$open','$close','$sample');\n";
1062 $toolbar.="addInfobox('" . wfEscapeJsString( wfMsg( "infobox" ) ) .
1063 "','" . wfEscapeJsString( wfMsg( "infobox_alert" ) ) . "');\n";
1064 $toolbar.="document.writeln(\"</div>\");\n";
1066 $toolbar.="/*]]>*/\n</script>";
1071 * Output preview text only. This can be sucked into the edit page
1072 * via JavaScript, and saves the server time rendering the skin as
1073 * well as theoretically being more robust on the client (doesn't
1074 * disturb the edit box's undo history, won't eat your text on
1077 * @todo This doesn't include category or interlanguage links.
1078 * Would need to enhance it a bit, maybe wrap them in XML
1079 * or something... that might also require more skin
1080 * initialization, so check whether that's a problem.
1082 function livePreview() {
1085 header( 'Content-type: text/xml' );
1086 header( 'Cache-control: no-cache' );
1088 echo $this->getPreviewText( false, false );
1093 * Get a diff between the current contents of the edit box and the
1094 * version of the page we're editing from.
1096 * If this is a section edit, we'll replace the section as for final
1097 * save and then make a comparison.
1099 * @return string HTML
1101 function getDiff() {
1102 require_once( 'DifferenceEngine.php' );
1103 $oldtext = $this->mArticle
->fetchContent();
1104 $newtext = $this->mArticle
->getTextOfLastEditWithSectionReplacedOrAdded(
1105 $this->section
, $this->textbox1
, $this->summary
, $this->edittime
);
1106 $oldtitle = wfMsg( 'currentrev' );
1107 $newtitle = wfMsg( 'yourtext' );
1108 if ( $oldtext != wfMsg( 'noarticletext' ) ||
$newtext != '' ) {
1109 $difftext = DifferenceEngine
::getDiff( $oldtext, $newtext, $oldtitle, $newtitle );
1112 return '<div id="wikiDiff">' . $difftext . '</div>';