* Wont work
[lhc/web/wiklou.git] / includes / EditPage.php
index 7a2a498..b3170da 100644 (file)
  * interfaces.
  */
 class EditPage {
+       const AS_SUCCESS_UPDATE                         = 200;
+       const AS_SUCCESS_NEW_ARTICLE            = 201;
+       const AS_HOOK_ERROR                                     = 210;
+       const AS_FILTERING                                      = 211;
+       const AS_HOOK_ERROR_EXPECTED            = 212;
+       const AS_BLOCKED_PAGE_FOR_USER          = 215;
+       const AS_CONTENT_TOO_BIG                        = 216;
+       const AS_USER_CANNOT_EDIT                       = 217;
+       const AS_READ_ONLY_PAGE_ANON            = 218;
+       const AS_READ_ONLY_PAGE_LOGGED          = 219;
+       const AS_READ_ONLY_PAGE                         = 220;
+       const AS_RATE_LIMITED                           = 221;
+       const AS_ARTICLE_WAS_DELETED            = 222;
+       const AS_NO_CREATE_PERMISSION           = 223;
+       const AS_BLANK_ARTICLE                          = 224;
+       const AS_CONFLICT_DETECTED                      = 225;
+       const AS_SUMMARY_NEEDED                         = 226;
+       const AS_TEXTBOX_EMPTY                          = 228;
+       const AS_MAX_ARTICLE_SIZE_EXCEDED       = 229;
+       const AS_OK                                                     = 230;
+       const AS_END                                            = 231;
+       const AS_SPAM_ERROR                                     = 232;
+
        var $mArticle;
        var $mTitle;
        var $mMetaData = '';
@@ -298,7 +321,6 @@ class EditPage {
         */
        function edit() {
                global $wgOut, $wgUser, $wgRequest, $wgTitle;
-               global $wgEmailConfirmToEdit;
 
                if ( ! wfRunHooks( 'AlternateEdit', array( &$this ) ) )
                        return;
@@ -319,57 +341,38 @@ class EditPage {
                        return;
                }
 
-               if ( ! $this->mTitle->userCan( 'edit' ) ) {
-                       wfDebug( "$fname: user can't edit\n" );
-                       $wgOut->readOnlyPage( $this->getContent(), true );
-                       wfProfileOut( $fname );
-                       return;
-               }
-               wfDebug( "$fname: Checking blocks\n" );
-               if ( !$this->preview && !$this->diff && $wgUser->isBlockedFrom( $this->mTitle, !$this->save ) ) {
-                       # When previewing, don't check blocked state - will get caught at save time.
-                       # Also, check when starting edition is done against slave to improve performance.
-                       wfDebug( "$fname: user is blocked\n" );
-                       $this->blockedPage();
-                       wfProfileOut( $fname );
-                       return;
-               }
-               if ( !$wgUser->isAllowed('edit') ) {
-                       if ( $wgUser->isAnon() ) {
-                               wfDebug( "$fname: user must log in\n" );
-                               $this->userNotLoggedInPage();
-                               wfProfileOut( $fname );
-                               return;
-                       } else {
-                               wfDebug( "$fname: read-only page\n" );
-                               $wgOut->readOnlyPage( $this->getContent(), true );
-                               wfProfileOut( $fname );
-                               return;
+               $permErrors = $this->mTitle->getUserPermissionsErrors('edit', $wgUser);
+               if( !$this->mTitle->exists() )
+                       $permErrors += array_diff( $this->mTitle->getUserPermissionsErrors('create', $wgUser), $permErrors );
+
+               # Ignore some permissions errors.
+               $remove = array();
+               foreach( $permErrors as $error ) {
+                       if ($this->preview || $this->diff &&
+                               ($error[0] == 'blockedtext' || $error[0] == 'autoblockedtext'))
+                       {
+                               // Don't worry about blocks when previewing/diffing
+                               $remove[] = $error;
+                       }
+
+                       if ($error[0] == 'readonlytext')
+                       {
+                               if ($this->edit) {
+                                       $this->formtype = 'preview';
+                               } elseif ($this->save || $this->preview || $this->diff) {
+                                       $remove[] = $error;
+                               }
                        }
                }
-               if ($wgEmailConfirmToEdit && !$wgUser->isEmailConfirmed()) {
-                       wfDebug("$fname: user must confirm e-mail address\n");
-                       $this->userNotConfirmedPage();
-                       wfProfileOut($fname);
-                       return;
-               }
-               if ( !$this->mTitle->userCan( 'create' ) && !$this->mTitle->exists() ) {
-                       wfDebug( "$fname: no create permission\n" );
-                       $this->noCreatePermission();
+               # array_diff returns elements in $permErrors that are not in $remove.
+               $permErrors = array_diff( $permErrors, $remove );
+
+               if ( !empty($permErrors) )
+               {
+                       wfDebug( "$fname: User can't edit\n" );
+                       $wgOut->readOnlyPage( $this->getContent(), true, $permErrors );
                        wfProfileOut( $fname );
                        return;
-               }
-               if ( wfReadOnly() ) {
-                       wfDebug( "$fname: read-only mode is engaged\n" );
-                       if( $this->save || $this->preview ) {
-                               $this->formtype = 'preview';
-                       } else if ( $this->diff ) {
-                               $this->formtype = 'diff';
-                       } else {
-                               $wgOut->readOnlyPage( $this->getContent() );
-                               wfProfileOut( $fname );
-                               return;
-                       }
                } else {
                        if ( $this->save ) {
                                $this->formtype = 'save';
@@ -471,7 +474,7 @@ class EditPage {
                } elseif( $this->section == 'new' ) {
                        // Nothing *to* preview for new sections
                        return false;
-               } elseif( $this->mTitle->exists() && $wgUser->getOption( 'previewonfirst' ) ) {
+               } elseif( ( $wgRequest->getVal( 'preload' ) !== '' || $this->mTitle->exists() ) && $wgUser->getOption( 'previewonfirst' ) ) {
                        // Standard preference behaviour
                        return true;
                } elseif( !$this->mTitle->exists() && $this->mTitle->getNamespace() == NS_CATEGORY ) {
@@ -567,9 +570,10 @@ class EditPage {
                        $this->summary   = '';
                        $this->edittime  = '';
                        $this->starttime = wfTimestampNow();
+                       $this->edit      = false;
                        $this->preview   = false;
                        $this->save      = false;
-                       $this->diff      = false;
+                       $this->diff      = false;
                        $this->minoredit = false;
                        $this->watchthis = false;
                        $this->recreate  = false;
@@ -638,11 +642,11 @@ class EditPage {
        }
 
        /**
-        * Attempt submission
-        * @return bool false if output is done, true if the rest of the form should be displayed
+        * Attempt submission (no UI)
+        * @return one of the constants describing the result
         */
-       function attemptSave() {
-               global $wgSpamRegex, $wgFilterCallback, $wgUser, $wgOut;
+       function internalAttemptSave( &$result ) {
+               global $wgSpamRegex, $wgFilterCallback, $wgUser, $wgOut, $wgParser;
                global $wgMaxArticleSize;
 
                $fname = 'EditPage::attemptSave';
@@ -652,7 +656,7 @@ class EditPage {
                if( !wfRunHooks( 'EditPage::attemptSave', array( &$this ) ) )
                {
                        wfDebug( "Hook 'EditPage::attemptSave' aborted article saving" );
-                       return false;
+                       return self::AS_HOOK_ERROR;
                }
 
                # Reintegrate metadata
@@ -662,34 +666,33 @@ class EditPage {
                # Check for spam
                $matches = array();
                if ( $wgSpamRegex && preg_match( $wgSpamRegex, $this->textbox1, $matches ) ) {
-                       $this->spamPage ( $matches[0] );
+                       $result['spam'] = $matches[0];
                        wfProfileOut( "$fname-checks" );
                        wfProfileOut( $fname );
-                       return false;
+                       return self::AS_SPAM_ERROR;
                }
                if ( $wgFilterCallback && $wgFilterCallback( $this->mTitle, $this->textbox1, $this->section ) ) {
                        # Error messages or other handling should be performed by the filter function
                        wfProfileOut( $fname );
                        wfProfileOut( "$fname-checks" );
-                       return false;
+                       return self::AS_FILTERING;
                }
                if ( !wfRunHooks( 'EditFilter', array( $this, $this->textbox1, $this->section, &$this->hookError ) ) ) {
                        # Error messages etc. could be handled within the hook...
                        wfProfileOut( $fname );
                        wfProfileOut( "$fname-checks" );
-                       return false;
+                       return self::AS_HOOK_ERROR;
                } elseif( $this->hookError != '' ) {
                        # ...or the hook could be expecting us to produce an error
                        wfProfileOut( "$fname-checks " );
                        wfProfileOut( $fname );
-                       return true;
+                       return self::AS_HOOK_ERROR_EXPECTED;
                }
                if ( $wgUser->isBlockedFrom( $this->mTitle, false ) ) {
                        # Check block state against master, thus 'false'.
-                       $this->blockedPage();
                        wfProfileOut( "$fname-checks" );
                        wfProfileOut( $fname );
-                       return false;
+                       return self::AS_BLOCKED_PAGE_FOR_USER;
                }
                $this->kblength = (int)(strlen( $this->textbox1 ) / 1024);
                if ( $this->kblength > $wgMaxArticleSize ) {
@@ -697,35 +700,31 @@ class EditPage {
                        $this->tooBig = true;
                        wfProfileOut( "$fname-checks" );
                        wfProfileOut( $fname );
-                       return true;
+                       return self::AS_CONTENT_TOO_BIG;
                }
 
                if ( !$wgUser->isAllowed('edit') ) {
                        if ( $wgUser->isAnon() ) {
-                               $this->userNotLoggedInPage();
                                wfProfileOut( "$fname-checks" );
                                wfProfileOut( $fname );
-                               return false;
+                               return self::AS_READ_ONLY_PAGE_ANON;
                        }
                        else {
-                               $wgOut->readOnlyPage();
                                wfProfileOut( "$fname-checks" );
                                wfProfileOut( $fname );
-                               return false;
+                               return self::AS_READ_ONLY_PAGE_LOGGED;
                        }
                }
 
                if ( wfReadOnly() ) {
-                       $wgOut->readOnlyPage();
                        wfProfileOut( "$fname-checks" );
                        wfProfileOut( $fname );
-                       return false;
+                       return self::AS_READ_ONLY_PAGE;
                }
                if ( $wgUser->pingLimiter() ) {
-                       $wgOut->rateLimited();
                        wfProfileOut( "$fname-checks" );
                        wfProfileOut( $fname );
-                       return false;
+                       return self::AS_RATE_LIMITED;
                }
 
                # If the article has been deleted while editing, don't save it without
@@ -733,7 +732,7 @@ class EditPage {
                if ( $this->deletedSinceEdit && !$this->recreate ) {
                        wfProfileOut( "$fname-checks" );
                        wfProfileOut( $fname );
-                       return true;
+                       return self::AS_ARTICLE_WAS_DELETED;
                }
 
                wfProfileOut( "$fname-checks" );
@@ -745,16 +744,14 @@ class EditPage {
                        // Late check for create permission, just in case *PARANOIA*
                        if ( !$this->mTitle->userCan( 'create' ) ) {
                                wfDebug( "$fname: no create permission\n" );
-                               $this->noCreatePermission();
                                wfProfileOut( $fname );
-                               return;
+                               return self::AS_NO_CREATE_PERMISSION;
                        }
 
                        # Don't save a new article if it's blank.
                        if ( ( '' == $this->textbox1 ) ) {
-                                       $wgOut->redirect( $this->mTitle->getFullURL() );
                                        wfProfileOut( $fname );
-                                       return false;
+                                       return self::AS_BLANK_ARTICLE;
                        }
 
                        $isComment=($this->section=='new');
@@ -762,7 +759,7 @@ class EditPage {
                                $this->minoredit, $this->watchthis, false, $isComment);
 
                        wfProfileOut( $fname );
-                       return false;
+                       return self::AS_SUCCESS_NEW_ARTICLE;
                }
 
                # Article exists. Check for edit conflict.
@@ -827,7 +824,7 @@ class EditPage {
 
                if ( $this->isConflict ) {
                        wfProfileOut( $fname );
-                       return true;
+                       return self::AS_CONFLICT_DETECTED;
                }
 
                $oldtext = $this->mArticle->getContent();
@@ -838,7 +835,7 @@ class EditPage {
                        if( md5( $this->summary ) == $this->autoSumm ) {
                                $this->missingSummary = true;
                                wfProfileOut( $fname );
-                               return( true );
+                               return self::AS_SUMMARY_NEEDED;
                        }
                }
 
@@ -847,7 +844,7 @@ class EditPage {
                        if (trim($this->summary) == '') {
                                $this->missingSummary = true;
                                wfProfileOut( $fname );
-                               return( true );
+                               return self::AS_SUMMARY_NEEDED;
                        }
                }
 
@@ -857,10 +854,14 @@ class EditPage {
                if( $this->section == 'new' ) {
                        if ( $this->textbox1 == '' ) {
                                $this->missingComment = true;
-                               return true;
+                               return self::AS_TEXTBOX_EMPTY;
                        }
                        if( $this->summary != '' ) {
-                               $sectionanchor = $this->sectionAnchor( $this->summary );
+                               $sectionanchor = $wgParser->guessSectionNameFromWikiText( $this->summary );
+                               # This is a new section, so create a link to the new section
+                               # in the revision summary.
+                               $cleanSummary = $wgParser->stripSectionName( $this->summary );
+                               $this->summary = wfMsgForContent( 'newsectionsummary', $cleanSummary );
                        }
                } elseif( $this->section != '' ) {
                        # Try to get a section anchor from the section source, redirect to edited section if header found
@@ -870,7 +871,7 @@ class EditPage {
                        # 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) {
-                               $sectionanchor = $this->sectionAnchor( $matches[2] );
+                               $sectionanchor = $wgParser->guessSectionNameFromWikiText( $matches[2] );
                        }
                }
                wfProfileOut( "$fname-sectionanchor" );
@@ -887,19 +888,19 @@ class EditPage {
                if ( $this->kblength > $wgMaxArticleSize ) {
                        $this->tooBig = true;
                        wfProfileOut( $fname );
-                       return true;
+                       return self::AS_MAX_ARTICLE_SIZE_EXCEDED;
                }
 
                # update the article here
                if( $this->mArticle->updateArticle( $text, $this->summary, $this->minoredit,
                        $this->watchthis, '', $sectionanchor ) ) {
                        wfProfileOut( $fname );
-                       return false;
+                       return self::AS_SUCCESS_UPDATE;
                } else {
                        $this->isConflict = true;
                }
                wfProfileOut( $fname );
-               return true;
+               return self::AS_END;
        }
 
        /**
@@ -964,7 +965,10 @@ class EditPage {
                                                        $this->textbox1,
                                                        $matches );
                                                if( !empty( $matches[2] ) ) {
-                                                       $this->summary = "/* ". trim($matches[2])." */ ";
+                                                       global $wgParser;
+                                                       $this->summary = "/* " . 
+                                                               $wgParser->stripSectionName(trim($matches[2])) . 
+                                                               " */ ";
                                                }
                                        }
                                }
@@ -974,15 +978,15 @@ class EditPage {
                        $wgOut->setPageTitle( $s );
 
                        if ( $this->missingComment ) {
-                               $wgOut->addWikiText( wfMsg( 'missingcommenttext' ) );
+                               $wgOut->addWikiText( '<div id="mw-missingcommenttext">' . wfMsg( 'missingcommenttext' ) . '</div>' );
                        }
 
                        if( $this->missingSummary && $this->section != 'new' ) {
-                               $wgOut->addWikiText( wfMsg( 'missingsummary' ) );
+                               $wgOut->addWikiText( '<div id="mw-missingsummary">' . wfMsg( 'missingsummary' ) . '</div>' );
                        }
 
                        if( $this->missingSummary && $this->section == 'new' ) {
-                               $wgOut->addWikiText( wfMsg( 'missingcommentheader' ) );
+                               $wgOut->addWikiText( '<div id="mw-missingcommentheader">' . wfMsg( 'missingcommentheader' ) . '</div>' );
                        }
 
                        if( !$this->hookError == '' ) {
@@ -1040,9 +1044,10 @@ class EditPage {
                        if ( count($cascadeSources) > 0 ) {
                                # Explain, and list the titles responsible
                                $notice = wfMsgExt( 'cascadeprotectedwarning', array('parsemag'), count($cascadeSources) ) . "\n";
-                               foreach( $cascadeSources as $id => $page )
+                               foreach( $cascadeSources as $page ) {
                                        $notice .= '* [[:' . $page->getPrefixedText() . "]]\n";
                                }
+                       }
                        $wgOut->addWikiText( $notice );
                }
 
@@ -1323,6 +1328,7 @@ END
                if($this->mTitle->getNamespace() == NS_CATEGORY) {
                        $this->mArticle->openShowCategory();
                }
+               wfRunHooks( 'OutputPageBeforeHTML',array( &$wgOut, &$text ) );
                $wgOut->addHTML( $text );
                if($this->mTitle->getNamespace() == NS_CATEGORY) {
                        $this->mArticle->closeShowCategory();
@@ -1346,8 +1352,7 @@ END
                        htmlspecialchars( "$wgStylePath/common/preview.js?$wgStyleVersion" ) .
                        '"></script>' . "\n" );
                $liveAction = $wgTitle->getLocalUrl( 'action=submit&wpPreview=true&live=true' );
-               return "return !livePreview(" .
-                       "getElementById('wikiPreview')," .
+               return "return !lpDoPreview(" .
                        "editform.wpTextbox1.value," .
                        '"' . $liveAction . '"' . ")";
        }
@@ -1504,23 +1509,7 @@ END
                $wgOut->setArticleRelated( false );
 
                $wgOut->addHtml( wfMsgWikiHtml( 'whitelistedittext', $loginLink ) );
-               $wgOut->returnToMain( false, $this->mTitle->getPrefixedUrl() );
-       }
-
-       /**
-        * Creates a basic error page which informs the user that
-        * they have to validate their email address before being
-        * allowed to edit.
-        */
-       function userNotConfirmedPage() {
-               global $wgOut;
-
-               $wgOut->setPageTitle( wfMsg( 'confirmedittitle' ) );
-               $wgOut->setRobotPolicy( 'noindex,nofollow' );
-               $wgOut->setArticleRelated( false );
-
-               $wgOut->addWikiText( wfMsg( 'confirmedittext' ) );
-               $wgOut->returnToMain( false );
+               $wgOut->returnToMain( false, $this->mTitle );
        }
 
        /**
@@ -1535,7 +1524,7 @@ END
                $wgOut->setArticleRelated( false );
 
                $wgOut->addWikiText( wfMsg( 'nosuchsectiontext', $this->section ) );
-               $wgOut->returnToMain( false, $this->mTitle->getPrefixedUrl() );
+               $wgOut->returnToMain( false, $this->mTitle );
        }
 
        /**
@@ -1554,7 +1543,7 @@ END
                if ( $match )
                        $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$match}</nowiki>" ) );
 
-               $wgOut->returnToMain( false );
+               $wgOut->returnToMain( false, $this->mTitle );
        }
 
        /**
@@ -1618,6 +1607,14 @@ END
                return true;
        }
 
+       /**
+        * @deprecated use $wgParser->stripSectionName()
+        */
+       function pseudoParseSectionAnchor( $text ) {
+               global $wgParser;
+               return $wgParser->stripSectionName( $text );
+       }
+
        /**
         * Format an anchor fragment as it would appear for a given section name
         * @param string $text
@@ -1625,19 +1622,8 @@ END
         * @private
         */
        function sectionAnchor( $text ) {
-               $headline = Sanitizer::decodeCharReferences( $text );
-               # strip out HTML
-               $headline = preg_replace( '/<.*?' . '>/', '', $headline );
-               $headline = trim( $headline );
-               $sectionanchor = '#' . urlencode( str_replace( ' ', '_', $headline ) );
-               $replacearray = array(
-                       '%3A' => ':',
-                       '%' => '.'
-               );
-               return str_replace(
-                       array_keys( $replacearray ),
-                       array_values( $replacearray ),
-                       $sectionanchor );
+               global $wgParser;
+               return $wgParser->guessSectionNameFromWikiText( $text );
        }
 
        /**
@@ -1915,12 +1901,15 @@ END
                header( 'Content-type: text/xml; charset=utf-8' );
                header( 'Cache-control: no-cache' );
 
+               $previewText = $this->getPreviewText();
+               #$categories = $skin->getCategoryLinks();
+
                $s =
                '<?xml version="1.0" encoding="UTF-8" ?>' . "\n" .
-               Xml::openElement( 'livepreview' ) .
-               Xml::element( 'preview', null, $this->getPreviewText() ) .
-               Xml::element( 'br', array( 'style' => 'clear: both;' ) ) .
-               Xml::closeElement( 'livepreview' );
+               Xml::tags( 'livepreview', null,
+                       Xml::element( 'preview', null, $previewText )
+                       #.      Xml::element( 'category', null, $categories )
+               );
                echo $s;
        }
 
@@ -2094,10 +2083,65 @@ END
                        $out->addWikiText( wfMsg( 'recreate-deleted-warn' ) );
                        $viewer = new LogViewer( $reader );
                        $viewer->showList( $out );
-                       $out->addHtml( '</div>' );                      
-               }                               
+                       $out->addHtml( '</div>' );
+               }
        }
-       
-}
 
+       /**
+        * Attempt submission
+        * @return bool false if output is done, true if the rest of the form should be displayed
+        */
+       function attemptSave() {
+               global $wgUser, $wgOut;
+
+               $resultDetails = false;
+               $value = $this->internalAttemptSave( $resultDetails );
+               switch ($value)
+               {
+                       case self::AS_HOOK_ERROR_EXPECTED:
+                       case self::AS_CONTENT_TOO_BIG:
+                       case self::AS_ARTICLE_WAS_DELETED:
+                       case self::AS_CONFLICT_DETECTED:
+                       case self::AS_SUMMARY_NEEDED:
+                       case self::AS_TEXTBOX_EMPTY:
+                       case self::AS_MAX_ARTICLE_SIZE_EXCEDED:
+                       case self::AS_END:
+                               return true;
+                       
+                       case self::AS_HOOK_ERROR:
+                       case self::AS_FILTERING:
+                       case self::AS_SUCCESS_NEW_ARTICLE:
+                       case self::AS_SUCCESS_UPDATE:
+                               return false;
 
+                       case self::AS_SPAM_ERROR:
+                               $this->spamPage ( $resultDetails['spam'] );
+                               return false;
+
+                       case self::AS_BLOCKED_PAGE_FOR_USER:
+                               $this->blockedPage();
+                               return false;
+
+                       case self::AS_READ_ONLY_PAGE_ANON:
+                               $this->userNotLoggedInPage();
+                               return false;
+
+                       case self::AS_READ_ONLY_PAGE_LOGGED:
+                       case self::AS_READ_ONLY_PAGE:
+                               $wgOut->readOnlyPage();
+                               return false;
+
+                       case self::AS_RATE_LIMITED:
+                               $wgOut->rateLimited();
+                               return false;
+
+                       case self::AS_NO_CREATE_PERMISSION;
+                               $this->noCreatePermission();
+                               return;
+                       
+                       case self::AS_BLANK_ARTICLE:
+                               $wgOut->redirect( $this->mTitle->getFullURL() );
+                               return false;
+               }
+       }
+}