Corrected spelling error
[lhc/web/wiklou.git] / includes / EditPage.php
index 7e891a8..21dae85 100644 (file)
@@ -62,6 +62,7 @@ class EditPage {
        }
 
        function importFormData( &$request ) {
+               global $wgIsMySQL, $wgIsPg;
                # These fields need to be checked for encoding.
                # Also remove trailing whitespace, but don't remove _initial_
                # whitespace from the text boxes. This may be significant formatting.
@@ -70,7 +71,12 @@ class EditPage {
                $this->summary = trim( $request->getText( "wpSummary" ) );
 
                $this->edittime = $request->getVal( 'wpEdittime' );
-               if( !preg_match( '/^\d{14}$/', $this->edittime ) ) $this->edittime = "";
+               if ($wgIsMySQL) 
+                       if( !preg_match( '/^\d{14}$/', $this->edittime )) $this->edittime = "";
+               if ($wgIsPg)
+                       if ( !preg_match( '/^\d{4}-\d\d-\d\d \d\d:\d\d:\d\d$/', 
+                               $this->edittime ))
+                               $this->edittime = "";
 
                $this->preview = $request->getCheck( 'wpPreview' );
                $this->save = $request->wasPosted() && !$this->preview;
@@ -113,7 +119,7 @@ class EditPage {
                $sk = $wgUser->getSkin();
                $isConflict = false;
                // css / js subpages of user pages get a special treatment
-               $isCssJsSubpage = Namespace::getUser() == $wgTitle->getNamespace() and preg_match("/\\.(css|js)$/", $wgTitle->getText() );
+               $isCssJsSubpage = (Namespace::getUser() == $wgTitle->getNamespace() and preg_match("/\\.(css|js)$/", $wgTitle->getText() ));
 
                if(!$this->mTitle->getArticleID()) { # new article
                        $wgOut->addWikiText(wfmsg("newarticletext"));
@@ -130,9 +136,13 @@ class EditPage {
 
                if ( "save" == $formtype ) {
                        # Check for spam
-                       if ( $wgSpamRegex && preg_match( $wgSpamRegex, $wpTextbox1 ) ) {
-                                       sleep(10);
-                                       $wgOut->redirect( $this->mTitle->getFullURL() );
+                       if ( $wgSpamRegex && preg_match( $wgSpamRegex, $this->textbox1 ) ) {
+                                       if ( $wgUser->isSysop() ) {
+                                               $this->spamPage();
+                                       } else {
+                                               sleep(10);
+                                               $wgOut->redirect( $this->mTitle->getFullURL() );
+                                       }
                                        return;
                        }
                        if ( $wgUser->isBlocked() ) {
@@ -191,8 +201,33 @@ class EditPage {
                                }
                        }
                        if ( ! $isConflict ) {
-                               # All's well: update the article here
-                               if($this->mArticle->updateArticle( $text, $this->summary, $this->minoredit, $this->watchthis ))
+                               # All's well
+                               $sectionanchor = '';
+                               if( $this->section != '' ) {
+                                       # Try to get a section anchor from the section source, redirect to edited section if header found
+                                       # XXX: might be better to integrate this into Article::getTextOfLastEditWithSectionReplacedOrAdded
+                                       # for duplicate heading checking and maybe parsing
+                                       $hasmatch = preg_match( "/^ *([=]{1,6})(.*?)(\\1) *\\n/i", $this->textbox1, $matches );
+                                       # we can't deal with anchors, includes, html etc in the header for now, 
+                                       # headline would need to be parsed to improve this
+                                       #if($hasmatch and strlen($matches[2]) > 0 and !preg_match( "/[\\['{<>]/", $matches[2])) {
+                                       if($hasmatch and strlen($matches[2]) > 0) {
+                                               global $wgInputEncoding;
+                                               $headline = do_html_entity_decode( $matches[2], ENT_COMPAT, $wgInputEncoding );
+                                               # strip out HTML 
+                                               $headline = preg_replace( "/<.*?" . ">/","",$headline );
+                                               $headline = trim( $headline );
+                                               $sectionanchor = '#'.urlencode( str_replace(' ', '_', $headline ) );
+                                               $replacearray = array(
+                                                       '%3A' => ':',
+                                                       '%' => '.'
+                                               );
+                                               $sectionanchor = str_replace(array_keys($replacearray),array_values($replacearray),$sectionanchor);
+                                       }
+                               }
+       
+                               # update the article here
+                               if($this->mArticle->updateArticle( $text, $this->summary, $this->minoredit, $this->watchthis, '', $sectionanchor ))
                                        return;
                                else
                                        $isConflict = true;
@@ -247,8 +282,10 @@ class EditPage {
 
                if( wfReadOnly() ) {
                        $wgOut->addHTML( "<strong>" .
-                               wfMsg( "readonlywarning" ) .
-                               "</strong>" );
+                       wfMsg( "readonlywarning" ) .
+                       "</strong>" );
+               } else if ( $isCssJsSubpage and "preview" != $formtype) {
+                       $wgOut->addHTML( wfMsg( "usercssjsyoucanpreview" ));
                }
                if( $this->mTitle->isProtected() ) {
                        $wgOut->addHTML( "<strong>" . wfMsg( "protectedpagewarning" ) .
@@ -280,12 +317,12 @@ class EditPage {
                $save = wfMsg( "savearticle" );
                $prev = wfMsg( "showpreview" );
 
-               $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedURL(),
+               $cancel = $sk->makeKnownLink( $this->mTitle->getPrefixedText(),
                  wfMsg( "cancel" ) );
-               $edithelpurl = $sk->makeUrl( wfMsg( "edithelppage" ));
-               $edithelp = '<a onclick="window.open('.
-               "'$edithelpurl', 'helpwindow', 'width=610,height=400,left=10,top=10'".'); return false;" href="'.$edithelpurl.'">'.
-               wfMsg( "edithelp" ).'</a>';
+               $edithelpurl = $sk->makeUrl( wfMsg( 'edithelppage' ));
+               $edithelp = '<a target="helpwindow" href="'.$edithelpurl.'">'.
+                       htmlspecialchars( wfMsg( 'edithelp' ) ).'</a> '.
+                       htmlspecialchars( wfMsg( 'newwindow' ) );
                $copywarn = wfMsg( "copyrightwarning", $sk->makeKnownLink(
                  wfMsg( "copyrightpage" ) ) );
 
@@ -333,7 +370,6 @@ class EditPage {
                                $previewhead.="<h2>" . wfMsg( "previewconflict" ) .
                                  "</h2>\n";
                        }
-                       $previewtext = wfUnescapeHTML( $this->textbox1 );
 
                        $parserOptions = ParserOptions::newFromUser( $wgUser );
                        $parserOptions->setUseCategoryMagic( false );
@@ -350,7 +386,7 @@ class EditPage {
                                $parserOutput = $wgParser->parse( $previewtext , $wgTitle, $parserOptions );
                                $wgOut->addHTML( $parserOutput->mText );
                        } else {
-                               $parserOutput = $wgParser->parse( $this->mArticle->preSaveTransform( $previewtext ) ."\n\n",
+                               $parserOutput = $wgParser->parse( $this->mArticle->preSaveTransform( $this->textbox1 ) ."\n\n",
                                $wgTitle, $parserOptions );
                                $previewHTML = $parserOutput->mText;
 
@@ -358,6 +394,8 @@ class EditPage {
                                        $wgOut->addHTML($previewhead);
                                        $wgOut->addHTML($previewHTML);
                                }
+                               $wgOut->addCategoryLinks($parserOutput->getCategoryLinks());
+                               $wgOut->addLanguageLinks($parserOutput->getLanguageLinks());
                                $wgOut->addHTML( "<br style=\"clear:both;\" />\n" );
                        }
                }
@@ -389,12 +427,12 @@ htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1 ) ) .
 </textarea>
 <br />{$editsummary}
 {$checkboxhtml}
-<input tabindex='5' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
+<input tabindex='5' id='wpSave' type='submit' value=\"{$save}\" name=\"wpSave\" accesskey=\"".wfMsg('accesskey-save')."\"".
 " title=\"".wfMsg('tooltip-save')."\"/>
-<input tabindex='6' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
+<input tabindex='6' id='wpSave' type='submit' value=\"{$prev}\" name=\"wpPreview\" accesskey=\"".wfMsg('accesskey-preview')."\"".
 " title=\"".wfMsg('tooltip-preview')."\"/>
 <em>{$cancel}</em> | <em>{$edithelp}</em>
-<br /><br />{$copywarn}
+<br /><div id=\"editpage-copywarn\">{$copywarn}</div>
 <input type='hidden' value=\"" . htmlspecialchars( $this->section ) . "\" name=\"wpSection\" />
 <input type='hidden' value=\"{$this->edittime}\" name=\"wpEdittime\" />\n" );
 
@@ -404,7 +442,7 @@ htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1 ) ) .
                          wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
 
                        $wgOut->addHTML( "<h2>" . wfMsg( "yourtext" ) . "</h2>
-<textarea tabindex=6 name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
+<textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
 . htmlspecialchars( $wgLang->recodeForEdit( $this->textbox2 ) ) .
 "
 </textarea>" );
@@ -451,6 +489,18 @@ htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1 ) ) .
                $wgOut->returnToMain( false );
        }
 
+       function spamPage()
+       {
+               global $wgOut, $wgSpamRegex;
+               $wgOut->setPageTitle( wfMsg( "spamprotectiontitle" ) );
+               $wgOut->setRobotpolicy( "noindex,nofollow" );
+               $wgOut->setArticleRelated( false );
+
+               $wgOut->addWikiText( wfMsg( "spamprotectiontext" ) );
+               $wgOut->addWikiText( "<pre>".$wgSpamRegex."</pre>" );
+               $wgOut->returnToMain( false );
+       }
+
        # Forks processes to scan the originating IP for an open proxy server
        # MemCached can be used to skip IPs that have already been scanned
        function proxyCheck()
@@ -495,6 +545,7 @@ htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1 ) ) .
        }
 
        /* private */ function mergeChangesInto( &$text ){
+               global $wgIsPg;
                $oldDate = $this->edittime;
                $res = wfQuery("SELECT cur_text FROM cur WHERE cur_id=" .
                        $this->mTitle->getArticleID() . " FOR UPDATE", DB_WRITE);
@@ -503,10 +554,13 @@ htmlspecialchars( $wgLang->recodeForEdit( $this->textbox1 ) ) .
                $yourtext = $obj->cur_text;
                $ns = $this->mTitle->getNamespace();
                $title = wfStrencode( $this->mTitle->getDBkey() );
-               $res = wfQuery("SELECT old_text FROM old WHERE old_namespace = $ns AND ".
+               $oldtable=$wgIsPg?'"old"':'old';
+               $res = wfQuery("SELECT old_text,old_flags FROM $oldtable WHERE old_namespace = $ns AND ".
                  "old_title = '{$title}' AND old_timestamp = '{$oldDate}'", DB_WRITE);
                $obj = wfFetchObject($res);
-               if(wfMerge($obj->old_text, $text, $yourtext, $result)){
+               $oldText = Article::getRevisionText( $obj );
+               
+               if(wfMerge($oldText, $text, $yourtext, $result)){
                        $text = $result;
                        return true;
                } else {