Fix for bug 13004, in which the Postgres full-text search has too many results,
[lhc/web/wiklou.git] / includes / EditPage.php
index cd028e2..0cdc21c 100644 (file)
  * usually the same, but they are now allowed to be different.
  */
 class EditPage {
-       const AS_SUCCESS_UPDATE                         = 200;
+       const AS_SUCCESS_UPDATE                 = 200;
        const AS_SUCCESS_NEW_ARTICLE            = 201;
-       const AS_HOOK_ERROR                                     = 210;
-       const AS_FILTERING                                      = 211;
+       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_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_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;
+       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_EXCEEDED      = 229;
+       const AS_OK                             = 230;
+       const AS_END                            = 231;
+       const AS_SPAM_ERROR                     = 232;
+       const AS_IMAGE_REDIRECT_ANON            = 233;
+       const AS_IMAGE_REDIRECT_LOGGED          = 234;
 
        var $mArticle;
        var $mTitle;
@@ -86,7 +88,6 @@ class EditPage {
         * @param $article
         */
        function EditPage( $article ) {
-               global $wgTitle;
                $this->mArticle =& $article;
                $this->mTitle = $article->getTitle();
 
@@ -101,8 +102,9 @@ class EditPage {
        
        /**
         * Fetch initial editing page content.
+        * @private
         */
-       private function getContent( $def_text = '' ) {
+       function getContent( $def_text = '' ) {
                global $wgOut, $wgRequest, $wgParser;
 
                # Get variables from query string :P
@@ -333,14 +335,13 @@ class EditPage {
         * the newly-edited page.
         */
        function edit() {
-               global $wgOut, $wgUser, $wgRequest, $wgTitle;
+               global $wgOut, $wgUser, $wgRequest;
 
-               if ( ! wfRunHooks( 'AlternateEdit', array( &$this ) ) )
+               if ( !wfRunHooks( 'AlternateEdit', array( &$this ) ) )
                        return;
 
-               $fname = 'EditPage::edit';
-               wfProfileIn( $fname );
-               wfDebug( "$fname: enter\n" );
+               wfProfileIn( __METHOD__ );
+               wfDebug( __METHOD__.": enter\n" );
 
                // this is not an article
                $wgOut->setArticleFlag(false);
@@ -350,7 +351,13 @@ class EditPage {
 
                if( $this->live ) {
                        $this->livePreview();
-                       wfProfileOut( $fname );
+                       wfProfileOut( __METHOD__ );
+                       return;
+               }
+               
+               if( wfReadOnly() ) {
+                       $wgOut->readOnlyPage( $this->getContent() );
+                       wfProfileOut( __METHOD__ );
                        return;
                }
 
@@ -386,13 +393,12 @@ class EditPage {
                                }
                        }
                }
-               # 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" );
+                       wfDebug( __METHOD__.": User can't edit\n" );
                        $wgOut->readOnlyPage( $this->getContent(), true, $permErrors );
-                       wfProfileOut( $fname );
+                       wfProfileOut( __METHOD__ );
                        return;
                } else {
                        if ( $this->save ) {
@@ -412,7 +418,7 @@ class EditPage {
                        }
                }
 
-               wfProfileIn( "$fname-business-end" );
+               wfProfileIn( __METHOD__."-business-end" );
 
                $this->isConflict = false;
                // css / js subpages of user pages get a special treatment
@@ -455,8 +461,8 @@ class EditPage {
 
                if ( 'save' == $this->formtype ) {
                        if ( !$this->attemptSave() ) {
-                               wfProfileOut( "$fname-business-end" );
-                               wfProfileOut( $fname );
+                               wfProfileOut( __METHOD__."-business-end" );
+                               wfProfileOut( __METHOD__ );
                                return;
                        }
                }
@@ -466,8 +472,8 @@ class EditPage {
                if ( 'initial' == $this->formtype || $this->firsttime ) {
                        if ($this->initialiseForm() === false) {
                                $this->noSuchSectionPage();
-                               wfProfileOut( "$fname-business-end" );
-                               wfProfileOut( $fname );
+                               wfProfileOut( __METHOD__."-business-end" );
+                               wfProfileOut( __METHOD__ );
                                return;
                        }
                        if( !$this->mTitle->getArticleId() ) 
@@ -475,8 +481,8 @@ class EditPage {
                }
 
                $this->showEditForm();
-               wfProfileOut( "$fname-business-end" );
-               wfProfileOut( $fname );
+               wfProfileOut( __METHOD__."-business-end" );
+               wfProfileOut( __METHOD__ );
        }
 
        /**
@@ -681,9 +687,9 @@ class EditPage {
         * Attempt submission (no UI)
         * @return one of the constants describing the result
         */
-       function internalAttemptSave( &$result ) {
+       function internalAttemptSave( &$result, $bot = false ) {
                global $wgSpamRegex, $wgFilterCallback, $wgUser, $wgOut, $wgParser;
-               global $wgMaxArticleSize, $wgTitle;
+               global $wgMaxArticleSize;
 
                $fname = 'EditPage::attemptSave';
                wfProfileIn( $fname );
@@ -695,6 +701,17 @@ class EditPage {
                        return self::AS_HOOK_ERROR;
                }
 
+               # Check image redirect
+               if ( $this->mTitle->getNamespace() == NS_IMAGE &&
+                       Title::newFromRedirect( $this->textbox1 ) instanceof Title &&
+                       !$wgUser->isAllowed( 'upload' ) ) {
+                               if( $wgUser->isAnon() ) {
+                                       return self::AS_IMAGE_REDIRECT_ANON;
+                               } else {
+                                       return self::AS_IMAGE_REDIRECT_LOGGED;
+                               }
+               }
+
                # Reintegrate metadata
                if ( $this->mMetaData != '' ) $this->textbox1 .= "\n" . $this->mMetaData ;
                $this->mMetaData = '' ;
@@ -724,7 +741,7 @@ class EditPage {
                        wfProfileOut( $fname );
                        return self::AS_HOOK_ERROR_EXPECTED;
                }
-               if ( $wgUser->isBlockedFrom( $wgTitle, false ) ) {
+               if ( $wgUser->isBlockedFrom( $this->mTitle, false ) ) {
                        # Check block state against master, thus 'false'.
                        wfProfileOut( "$fname-checks" );
                        wfProfileOut( $fname );
@@ -785,7 +802,7 @@ class EditPage {
                        }
 
                        # Don't save a new article if it's blank.
-                       if ( ( '' == $this->textbox1 ) ) {
+                       if ( '' == $this->textbox1 ) {
                                        wfProfileOut( $fname );
                                        return self::AS_BLANK_ARTICLE;
                        }
@@ -794,13 +811,13 @@ class EditPage {
                        if ( !wfRunHooks( 'EditFilterMerged', array( $this, $this->textbox1, &$this->hookError ) ) ) {
                                # Error messages etc. could be handled within the hook...
                                wfProfileOut( $fname );
-                               return false;
+                               return self::AS_HOOK_ERROR;
                        }
 
                        $isComment = ( $this->section == 'new' );
                        
                        $this->mArticle->insertNewArticle( $this->textbox1, $this->summary,
-                               $this->minoredit, $this->watchthis, false, $isComment);
+                               $this->minoredit, $this->watchthis, false, $isComment, $bot);
 
                        wfProfileOut( $fname );
                        return self::AS_SUCCESS_NEW_ARTICLE;
@@ -877,7 +894,7 @@ class EditPage {
                if ( !wfRunHooks( 'EditFilterMerged', array( $this, $text, &$this->hookError ) ) ) {
                        # Error messages etc. could be handled within the hook...
                        wfProfileOut( $fname );
-                       return false;
+                       return self::AS_HOOK_ERROR;
                }
 
                # Handle the user preference to force summaries here, but not for null edits
@@ -939,12 +956,12 @@ class EditPage {
                if ( $this->kblength > $wgMaxArticleSize ) {
                        $this->tooBig = true;
                        wfProfileOut( $fname );
-                       return self::AS_MAX_ARTICLE_SIZE_EXCEDED;
+                       return self::AS_MAX_ARTICLE_SIZE_EXCEEDED;
                }
 
                # update the article here
                if( $this->mArticle->updateArticle( $text, $this->summary, $this->minoredit,
-                       $this->watchthis, '', $sectionanchor ) ) {
+                       $this->watchthis, $bot, $sectionanchor ) ) {
                        wfProfileOut( $fname );
                        return self::AS_SUCCESS_UPDATE;
                } else {
@@ -1004,7 +1021,6 @@ class EditPage {
                        $this->textbox1 = $this->getContent();
                        $this->edittime = $this->mArticle->getTimestamp();
                } else {
-
                        if( $this->section != '' ) {
                                if( $this->section == 'new' ) {
                                        $s = wfMsg('editingcomment', $wgTitle->getPrefixedText() );
@@ -1040,7 +1056,7 @@ class EditPage {
                                $wgOut->addWikiText( '<div id="mw-missingcommentheader">' . wfMsg( 'missingcommentheader' ) . '</div>' );
                        }
 
-                       if( !$this->hookError == '' ) {
+                       if( $this->hookError !== '' ) {
                                $wgOut->addWikiText( $this->hookError );
                        }
 
@@ -1088,7 +1104,7 @@ class EditPage {
                                if( wfEmptyMsg( 'semiprotectedpagewarning', $notice ) || $notice == '-' )
                                        $notice = '';
                        } else {
-                       # Then it must be protected based on static groups (regular)
+                               # Then it must be protected based on static groups (regular)
                                $notice = wfMsg( 'protectedpagewarning' );
                        }
                        $wgOut->addWikiText( $notice );
@@ -1352,7 +1368,7 @@ END
                        $de->showDiff( wfMsg( "yourtext" ), wfMsg( "storedversion" ) );
 
                        $wgOut->addWikiText( '==' . wfMsg( "yourtext" ) . '==' );
-                       $wgOut->addHTML( "<textarea tabindex=6 id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}' wrap='virtual'>"
+                       $wgOut->addHTML( "<textarea tabindex='6' id='wpTextbox2' name=\"wpTextbox2\" rows='{$rows}' cols='{$cols}'>"
                                . htmlspecialchars( $this->safeUnicodeOutput( $this->textbox2 ) ) . "\n</textarea>" );
                }
                $wgOut->addHTML( $this->editFormTextBottom );
@@ -1510,7 +1526,12 @@ END
                        $wgOut->addParserOutputNoText( $parserOutput );
                        
                        # ParserOutput might have altered the page title, so reset it
-                       $wgOut->setPageTitle( wfMsg( 'editing', $wgTitle->getPrefixedText() ) );                        
+                       # Also, use the title defined by DISPLAYTITLE magic word when present
+                       if( ( $dt = $parserOutput->getDisplayTitle() ) !== false ) {
+                               $wgOut->setPageTitle( wfMsg( 'editing', $dt ) );
+                       } else {
+                               $wgOut->setPageTitle( wfMsg( 'editing', $wgTitle->getPrefixedText() ) );                        
+                       }
 
                        foreach ( $parserOutput->getTemplates() as $ns => $template)
                                foreach ( array_keys( $template ) as $dbk)
@@ -1945,7 +1966,8 @@ END
                        'title'     => wfMsg( 'tooltip-diff' ).' ['.wfMsg( 'accesskey-diff' ).']',
                );
                $buttons['diff'] = wfElement('input', $temp, '');
-
+               
+               wfRunHooks( 'EditPageBeforeEditButtons', array( &$this, &$buttons ) );
                return $buttons;
        }
 
@@ -2158,27 +2180,26 @@ END
         * @return bool false if output is done, true if the rest of the form should be displayed
         */
        function attemptSave() {
-               global $wgUser, $wgOut, $wgTitle;
+               global $wgUser, $wgOut, $wgTitle, $wgRequest;
 
                $resultDetails = false;
-               $value = $this->internalAttemptSave( $resultDetails );
+               $value = $this->internalAttemptSave( $resultDetails, $wgUser->isAllowed('bot') && $wgRequest->getBool('bot', true) );
                
                if( $value == self::AS_SUCCESS_UPDATE || $value == self::AS_SUCCESS_NEW_ARTICLE ) {
                        $this->didSave = true;
                }
 
-               switch ($value)
-               {
+               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_MAX_ARTICLE_SIZE_EXCEEDED:
                        case self::AS_END:
                                return true;
-                       
+
                        case self::AS_HOOK_ERROR:
                        case self::AS_FILTERING:
                        case self::AS_SUCCESS_NEW_ARTICLE:
@@ -2193,6 +2214,10 @@ END
                                $this->blockedPage();
                                return false;
 
+                       case self::AS_IMAGE_REDIRECT_ANON:
+                               $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
+                               return false;
+
                        case self::AS_READ_ONLY_PAGE_ANON:
                                $this->userNotLoggedInPage();
                                return false;
@@ -2213,6 +2238,10 @@ END
                        case self::AS_BLANK_ARTICLE:
                                $wgOut->redirect( $wgTitle->getFullURL() );
                                return false;
+
+                       case self::AS_IMAGE_REDIRECT_LOGGED:
+                               $wgOut->permissionRequired( 'upload' );
+                               return false;
                }
        }
 }