A little refactoring of the input splitting/expansion:
[lhc/web/wiklou.git] / includes / EditPage.php
index c7d9445..61e58d2 100644 (file)
@@ -38,6 +38,8 @@ class EditPage {
        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;
@@ -335,12 +337,11 @@ class EditPage {
        function edit() {
                global $wgOut, $wgUser, $wgRequest, $wgTitle;
 
-               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,13 +351,28 @@ class EditPage {
 
                if( $this->live ) {
                        $this->livePreview();
-                       wfProfileOut( $fname );
+                       wfProfileOut( __METHOD__ );
+                       return;
+               }
+               
+               if( wfReadOnly() ) {
+                       $wgOut->readOnlyPage( $this->getContent() );
+                       wfProfileOut( __METHOD__ );
                        return;
                }
 
                $permErrors = $this->mTitle->getUserPermissionsErrors('edit', $wgUser);
-               if( !$this->mTitle->exists() )
-                       $permErrors += array_diff( $this->mTitle->getUserPermissionsErrors('create', $wgUser), $permErrors );
+               if( !$this->mTitle->exists() ) {
+                       # We can't use array_diff here, because that considers ANY TWO
+                       # ARRAYS TO BE EQUAL.  Thanks, PHP.
+                       $createErrors = $this->mTitle->getUserPermissionsErrors('create', $wgUser);
+                       foreach( $createErrors as $error ) {
+                               # in_array() actually *does* work as expected.
+                               if( !in_array( $error, $permErrors ) ) {
+                                       $permErrors[] = $error;
+                               }
+                       }
+               }
 
                # Ignore some permissions errors.
                $remove = array();
@@ -377,14 +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" );
+               if ( !empty($permErrors) ) {
+                       wfDebug( __METHOD__.": User can't edit\n" );
                        $wgOut->readOnlyPage( $this->getContent(), true, $permErrors );
-                       wfProfileOut( $fname );
+                       wfProfileOut( __METHOD__ );
                        return;
                } else {
                        if ( $this->save ) {
@@ -404,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
@@ -447,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;
                        }
                }
@@ -458,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() ) 
@@ -467,8 +481,8 @@ class EditPage {
                }
 
                $this->showEditForm();
-               wfProfileOut( "$fname-business-end" );
-               wfProfileOut( $fname );
+               wfProfileOut( __METHOD__."-business-end" );
+               wfProfileOut( __METHOD__ );
        }
 
        /**
@@ -673,7 +687,7 @@ 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;
 
@@ -687,6 +701,17 @@ class EditPage {
                        return self::AS_HOOK_ERROR;
                }
 
+               # Check image redirect
+               if ( $wgTitle->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 = '' ;
@@ -792,7 +817,7 @@ class EditPage {
                        $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;
@@ -936,7 +961,7 @@ class EditPage {
 
                # 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 {
@@ -996,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() );
@@ -1032,7 +1056,7 @@ class EditPage {
                                $wgOut->addWikiText( '<div id="mw-missingcommentheader">' . wfMsg( 'missingcommentheader' ) . '</div>' );
                        }
 
-                       if( !$this->hookError == '' ) {
+                       if( $this->hookError !== '' ) {
                                $wgOut->addWikiText( $this->hookError );
                        }
 
@@ -1056,9 +1080,9 @@ class EditPage {
                }
 
                if( wfReadOnly() ) {
-                       $wgOut->addWikiText( wfMsg( 'readonlywarning' ) );
+                       $wgOut->addHTML( '<div id="mw-read-only-warning">'.wfMsgWikiHTML( 'readonlywarning' ).'</div>' );
                } elseif( $wgUser->isAnon() && $this->formtype != 'preview' ) {
-                       $wgOut->addWikiText( wfMsg( 'anoneditwarning' ) );
+                       $wgOut->addHTML( '<div id="mw-anon-edit-warning">'.wfMsgWikiHTML( 'anoneditwarning' ).'</div>' );
                } else {
                        if( $this->isCssJsSubpage && $this->formtype != 'preview' ) {
                                # Check the skin exists
@@ -1080,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 );
@@ -1097,6 +1121,9 @@ class EditPage {
                        }
                        $wgOut->addWikiText( $notice );
                }
+               if( !$this->mTitle->exists() && $this->mTitle->getRestrictions( 'create' ) != array() ){
+                       $wgOut->addWikiText( wfMsg( 'titleprotectedwarning' ) );
+               }
 
                if ( $this->kblength === false ) {
                        $this->kblength = (int)(strlen( $this->textbox1 ) / 1024);
@@ -1341,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 );
@@ -1448,17 +1475,12 @@ END
 
                if ( $this->mTriedSave && !$this->mTokenOk ) {
                        if ( $this->mTokenOkExceptSuffix ) {
-                               $msg = 'token_suffix_mismatch';
+                               $note = wfMsg( 'token_suffix_mismatch' );
                        } else {
-                               $msg = 'session_fail_preview';
+                               $note = wfMsg( 'session_fail_preview' );
                        }
                } else {
-                       $msg = 'previewnote';
-               }
-               $previewhead = '<h2>' . htmlspecialchars( wfMsg( 'preview' ) ) . "</h2>\n" .
-                       "<div class='previewnote'>" . $wgOut->parse( wfMsg( $msg ) ) . "</div>\n";
-               if ( $this->isConflict ) {
-                       $previewhead.='<h2>' . htmlspecialchars( wfMsg( 'previewconflict' ) ) . "</h2>\n";
+                       $note = wfMsg( 'previewnote' );
                }
 
                $parserOptions = ParserOptions::newFromUser( $wgUser );
@@ -1484,8 +1506,7 @@ END
                        $parserOptions->setTidy(true);
                        $parserOutput = $wgParser->parse( $previewtext , $this->mTitle, $parserOptions );
                        $wgOut->addHTML( $parserOutput->mText );
-                       wfProfileOut( $fname );
-                       return $previewhead;
+                       $previewHTML = '';
                } else {
                        $toparse = $this->textbox1;
 
@@ -1497,6 +1518,7 @@ END
 
                        if ( $this->mMetaData != "" ) $toparse .= "\n" . $this->mMetaData ;
                        $parserOptions->setTidy(true);
+                       $parserOptions->enableLimitReport();
                        $parserOutput = $wgParser->parse( $this->mArticle->preSaveTransform( $toparse ) ."\n\n",
                                        $this->mTitle, $parserOptions );
 
@@ -1510,9 +1532,19 @@ END
                                foreach ( array_keys( $template ) as $dbk)
                                        $this->mPreviewTemplates[] = Title::makeTitle($ns, $dbk);
 
-                       wfProfileOut( $fname );
-                       return $previewhead . $previewHTML;
+                       if ( count( $parserOutput->getWarnings() ) ) {
+                               $note .= "\n\n" . implode( "\n\n", $parserOutput->getWarnings() );
+                       }
                }
+
+               $previewhead = '<h2>' . htmlspecialchars( wfMsg( 'preview' ) ) . "</h2>\n" .
+                       "<div class='previewnote'>" . $wgOut->parse( $note ) . "</div>\n";
+               if ( $this->isConflict ) {
+                       $previewhead.='<h2>' . htmlspecialchars( wfMsg( 'previewconflict' ) ) . "</h2>\n";
+               }
+
+               wfProfileOut( $fname );
+               return $previewhead . $previewHTML;
        }
 
        /**
@@ -1587,9 +1619,11 @@ END
                $wgOut->setRobotPolicy( 'noindex,nofollow' );
                $wgOut->setArticleRelated( false );
 
+               $wgOut->addHtml( '<div id="spamprotected">' );
                $wgOut->addWikiText( wfMsg( 'spamprotectiontext' ) );
                if ( $match )
-                       $wgOut->addWikiText( wfMsg( 'spamprotectionmatch', "<nowiki>{$match}</nowiki>" ) );
+                       $wgOut->addWikiText( wfMsg( 'spamprotectionmatch',wfEscapeWikiText( $match ) ) );
+               $wgOut->addHtml( '</div>' );
 
                $wgOut->returnToMain( false, $wgTitle );
        }
@@ -2140,10 +2174,10 @@ 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;
@@ -2175,6 +2209,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;
@@ -2195,6 +2233,10 @@ END
                        case self::AS_BLANK_ARTICLE:
                                $wgOut->redirect( $wgTitle->getFullURL() );
                                return false;
+
+                       case self::AS_IMAGE_REDIRECT_LOGGED:
+                               $wgOut->permissionRequired( 'upload' );
+                               return false;
                }
        }
 }