Revert r38686, per comments on CodeReview (in brief, the backslashes and such are...
[lhc/web/wiklou.git] / includes / EditPage.php
index e37b846..667e625 100644 (file)
@@ -110,7 +110,7 @@ class EditPage {
         * @private
         */
        function getContent( $def_text = '' ) {
-               global $wgOut, $wgRequest, $wgParser, $wgMessageCache;
+               global $wgOut, $wgRequest, $wgParser, $wgContLang, $wgMessageCache;
 
                wfProfileIn( __METHOD__ );
                # Get variables from query string :P
@@ -124,9 +124,12 @@ class EditPage {
                // For other non-existent articles, use preload text if any.
                if ( !$this->mTitle->exists() ) {
                        if ( $this->mTitle->getNamespace() == NS_MEDIAWIKI ) {
-                               $wgMessageCache->loadAllMessages();
                                # If this is a system message, get the default text.
-                               $text = wfMsgWeirdKey( $this->mTitle->getText() ) ;
+                               list( $message, $lang ) = $wgMessageCache->figureMessage( $wgContLang->lcfirst( $this->mTitle->getText() ) );
+                               $wgMessageCache->loadAllMessages( $lang );
+                               $text = wfMsgGetKey( $message, false, $lang, false );
+                               if( wfEmptyMsg( $message, $text ) )
+                                       $text = '';
                        } else {
                                # If requested, preload some text.
                                $text = $this->getPreloadedText( $preload );
@@ -231,93 +234,87 @@ class EditPage {
         *  and set $wgMetadataWhitelist to the *full* title of the template whitelist
         */
        function extractMetaDataFromArticle () {
-               global $wgUseMetadataEdit , $wgMetadataWhitelist , $wgLang ;
-               $this->mMetaData = '' ;
-               if ( !$wgUseMetadataEdit ) return ;
-               if ( $wgMetadataWhitelist == '' ) return ;
-               $s = '' ;
+               global $wgUseMetadataEdit, $wgMetadataWhitelist, $wgContLang;
+               $this->mMetaData = '';
+               if ( !$wgUseMetadataEdit ) return;
+               if ( $wgMetadataWhitelist == '' ) return;
+               $s = '';
                $t = $this->getContent();
 
                # MISSING : <nowiki> filtering
 
                # Categories and language links
-               $t = explode ( "\n" , $t ) ;
-               $catlow = strtolower ( $wgLang->getNsText ( NS_CATEGORY ) ) ;
-               $cat = $ll = array() ;
-               foreach ( $t AS $key => $x )
-               {
-                       $y = trim ( strtolower ( $x ) ) ;
-                       while ( substr ( $y , 0 , 2 ) == '[[' )
-                       {
-                               $y = explode ( ']]' , trim ( $x ) ) ;
-                               $first = array_shift ( $y ) ;
-                               $first = explode ( ':' , $first ) ;
-                               $ns = array_shift ( $first ) ;
-                               $ns = trim ( str_replace ( '[' , '' , $ns ) ) ;
-                               if ( strlen ( $ns ) == 2 OR strtolower ( $ns ) == $catlow )
-                               {
-                                       $add = '[[' . $ns . ':' . implode ( ':' , $first ) . ']]' ;
-                                       if ( strtolower ( $ns ) == $catlow ) $cat[] = $add ;
-                                       else $ll[] = $add ;
-                                       $x = implode ( ']]' , $y ) ;
-                                       $t[$key] = $x ;
-                                       $y = trim ( strtolower ( $x ) ) ;
+               $t = explode ( "\n" , $t );
+               $catlow = strtolower ( $wgContLang->getNsText( NS_CATEGORY ) );
+               $cat = $ll = array();
+               foreach ( $t AS $key => $x ) {
+                       $y = trim ( strtolower ( $x ) );
+                       while ( substr ( $y , 0 , 2 ) == '[[' ) {
+                               $y = explode ( ']]' , trim ( $x ) );
+                               $first = array_shift ( $y );
+                               $first = explode ( ':' , $first );
+                               $ns = array_shift ( $first );
+                               $ns = trim ( str_replace ( '[' , '' , $ns ) );
+                               if ( $wgContLang->getLanguageName( $ns ) || strtolower ( $ns ) == $catlow ) {
+                                       $add = '[[' . $ns . ':' . implode ( ':' , $first ) . ']]';
+                                       if ( strtolower ( $ns ) == $catlow ) $cat[] = $add;
+                                       else $ll[] = $add;
+                                       $x = implode ( ']]' , $y );
+                                       $t[$key] = $x;
+                                       $y = trim ( strtolower ( $x ) );
+                               } else {
+                                       $x = implode ( ']]' , $y );
+                                       $y = trim ( strtolower ( $x ) );
                                }
                        }
                }
-               if ( count ( $cat ) ) $s .= implode ( ' ' , $cat ) . "\n" ;
-               if ( count ( $ll ) ) $s .= implode ( ' ' , $ll ) . "\n" ;
-               $t = implode ( "\n" , $t ) ;
+               if ( count ( $cat ) ) $s .= implode ( ' ' , $cat ) . "\n";
+               if ( count ( $ll ) ) $s .= implode ( ' ' , $ll ) . "\n";
+               $t = implode ( "\n" , $t );
 
                # Load whitelist
                $sat = array () ; # stand-alone-templates; must be lowercase
-               $wl_title = Title::newFromText ( $wgMetadataWhitelist ) ;
-               $wl_article = new Article ( $wl_title ) ;
-               $wl = explode ( "\n" , $wl_article->getContent() ) ;
-               foreach ( $wl AS $x )
-               {
-                       $isentry = false ;
-                       $x = trim ( $x ) ;
-                       while ( substr ( $x , 0 , 1 ) == '*' )
-                       {
-                               $isentry = true ;
-                               $x = trim ( substr ( $x , 1 ) ) ;
+               $wl_title = Title::newFromText ( $wgMetadataWhitelist );
+               $wl_article = new Article ( $wl_title );
+               $wl = explode ( "\n" , $wl_article->getContent() );
+               foreach ( $wl AS $x ) {
+                       $isentry = false;
+                       $x = trim ( $x );
+                       while ( substr ( $x , 0 , 1 ) == '*' ) {
+                               $isentry = true;
+                               $x = trim ( substr ( $x , 1 ) );
                        }
-                       if ( $isentry )
-                       {
-                               $sat[] = strtolower ( $x ) ;
+                       if ( $isentry ) {
+                               $sat[] = strtolower ( $x );
                        }
 
                }
 
                # Templates, but only some
-               $t = explode ( '{{' , $t ) ;
+               $t = explode ( '{{' , $t );
                $tl = array () ;
-               foreach ( $t AS $key => $x )
-               {
-                       $y = explode ( '}}' , $x , 2 ) ;
-                       if ( count ( $y ) == 2 )
-                       {
-                               $z = $y[0] ;
-                               $z = explode ( '|' , $z ) ;
-                               $tn = array_shift ( $z ) ;
-                               if ( in_array ( strtolower ( $tn ) , $sat ) )
-                               {
-                                       $tl[] = '{{' . $y[0] . '}}' ;
-                                       $t[$key] = $y[1] ;
-                                       $y = explode ( '}}' , $y[1] , 2 ) ;
+               foreach ( $t AS $key => $x ) {
+                       $y = explode ( '}}' , $x , 2 );
+                       if ( count ( $y ) == 2 ) {
+                               $z = $y[0];
+                               $z = explode ( '|' , $z );
+                               $tn = array_shift ( $z );
+                               if ( in_array ( strtolower ( $tn ) , $sat ) ) {
+                                       $tl[] = '{{' . $y[0] . '}}';
+                                       $t[$key] = $y[1];
+                                       $y = explode ( '}}' , $y[1] , 2 );
                                }
-                               else $t[$key] = '{{' . $x ;
+                               else $t[$key] = '{{' . $x;
                        }
-                       else if ( $key != 0 ) $t[$key] = '{{' . $x ;
-                       else $t[$key] = $x ;
+                       else if ( $key != 0 ) $t[$key] = '{{' . $x;
+                       else $t[$key] = $x;
                }
-               if ( count ( $tl ) ) $s .= implode ( ' ' , $tl ) ;
-               $t = implode ( '' , $t ) ;
+               if ( count ( $tl ) ) $s .= implode ( ' ' , $tl );
+               $t = implode ( '' , $t );
 
-               $t = str_replace ( "\n\n\n" , "\n" , $t ) ;
-               $this->mArticle->mContent = $t ;
-               $this->mMetaData = $s ;
+               $t = str_replace ( "\n\n\n" , "\n" , $t );
+               $this->mArticle->mContent = $t;
+               $this->mMetaData = $s;
        }
 
        /* 
@@ -332,8 +329,8 @@ class EditPage {
                if ( $this->mTitle->isDeleted() ) {
                        $this->lastDelete = $this->getLastDelete();
                        if ( $this->lastDelete ) {
-                               $deletetime = $this->lastDelete->log_timestamp;
-                               if ( ($deletetime - $this->starttime) > 0 ) {
+                               $deleteTime = wfTimestamp( TS_MW, $this->lastDelete->log_timestamp );
+                               if ( $deleteTime > $this->starttime ) {
                                        $this->deletedSinceEdit = true;
                                }
                        }
@@ -378,18 +375,10 @@ class EditPage {
                        return;
                }
 
-               if ( wfReadOnly() ) {
-                       if ( $this->save ){
+               if ( wfReadOnly() && $this->save ) {
                                // Force preview
                                $this->save = false;
                                $this->preview = true;
-                       } elseif ( $this->preview || $this->diff ) {
-                               // A warning will be displayed instead
-                       } else {
-                               $this->readOnlyPage( $this->getContent() );
-                               wfProfileOut( __METHOD__ );
-                               return;
-                       }
                }
 
                $wgOut->addScriptFile( 'edit.js' );
@@ -577,7 +566,7 @@ class EditPage {
 
                        $this->scrolltop = $request->getIntOrNull( 'wpScrolltop' );
 
-                       if ( is_null( $this->edittime ) ) {
+                       if ( is_null($this->section) || !$this->edittime || !$this->starttime ) {
                                # If the form is incomplete, force to preview.
                                wfDebug( "$fname: Form data appears to be incomplete\n" );
                                wfDebug( "POST DATA: " . var_export( $_POST, true ) . "\n" );
@@ -704,7 +693,7 @@ class EditPage {
                        }
                }
                # Give a notice if the user is editing a deleted page...
-               if ( $this->mTitle->exists() ) {
+               if ( !$this->mTitle->exists() ) {
                        $this->showDeletionLog( $wgOut );
                }
        }
@@ -847,7 +836,6 @@ class EditPage {
                # If article is new, insert it.
                $aid = $this->mTitle->getArticleID( GAID_FOR_UPDATE );
                if ( 0 == $aid ) {
-
                        // Late check for create permission, just in case *PARANOIA*
                        if ( !$this->mTitle->userCan( 'create' ) ) {
                                wfDebug( "$fname: no create permission\n" );
@@ -857,8 +845,8 @@ class EditPage {
 
                        # Don't save a new article if it's blank.
                        if ( '' == $this->textbox1 ) {
-                                       wfProfileOut( $fname );
-                                       return self::AS_BLANK_ARTICLE;
+                               wfProfileOut( $fname );
+                               return self::AS_BLANK_ARTICLE;
                        }
 
                        // Run post-section-merge edit filter
@@ -918,7 +906,7 @@ class EditPage {
                }
 
                # Suppress edit conflict with self, except for section edits where merging is required.
-               if ( ( $this->section == '' ) && ( 0 != $userid ) && ( $this->mArticle->getUser() == $userid ) ) {
+               if ( $this->section == '' && $userid && $this->userWasLastToEdit($userid,$this->edittime) ) {
                        wfDebug( "EditPage::editForm Suppressing edit conflict, same user.\n" );
                        $this->isConflict = false;
                } else {
@@ -952,7 +940,7 @@ class EditPage {
                }
 
                # Handle the user preference to force summaries here, but not for null edits
-               if ( $this->section != 'new' && !$this->allowBlankSummary && 0 != strcmp($oldtext, $text) && 
+               if ( $this->section != 'new' && !$this->allowBlankSummary && 0 != strcmp($oldtext, $text) &&
                        !is_object( Title::newFromRedirect( $text ) ) # check if it's not a redirect
                ) {
                        if ( md5( $this->summary ) == $this->autoSumm ) {
@@ -1026,6 +1014,29 @@ class EditPage {
                return self::AS_END;
        }
        
+       /**
+        * Check if no edits were made by other users since
+        * the time a user started editing the page. Limit to
+        * 50 revisions for the sake of performance.
+        */
+       protected function userWasLastToEdit( $id, $edittime ) {
+               $dbw = wfGetDB( DB_MASTER );
+               $res = $dbw->select( 'revision',
+                       'rev_user',
+                       array( 
+                               'rev_page' => $this->mArticle->getId(),
+                               'rev_timestamp > '.$dbw->addQuotes( $dbw->timestamp($edittime) )
+                       ),
+                       __METHOD__,
+                       array( 'ORDER BY' => 'rev_timestamp ASC', 'LIMIT' => 50 ) );
+               while( $row = $res->fetchObject() ) {
+                       if( $row->rev_user != $id ) {
+                               return false;
+                       }
+               }
+               return true;
+       }
+       
        /**
         * Check given input text against $wgSpamRegex, and return the text of the first match.
         * @return mixed -- matching string or false
@@ -1051,11 +1062,8 @@ class EditPage {
         */
        function initialiseForm() {
                $this->edittime = $this->mArticle->getTimestamp();
-               $this->textbox1 = $this->getContent(false);
-               if ( $this->textbox1 === false) return false;
-
-               if ( !$this->mArticle->exists() && $this->mTitle->getNamespace() == NS_MEDIAWIKI )
-                       $this->textbox1 = wfMsgWeirdKey( $this->mTitle->getText() );
+               $this->textbox1 = $this->getContent( false );
+               if ( $this->textbox1 === false ) return false;
                wfProxyCheck();
                return true;
        }
@@ -1182,10 +1190,12 @@ class EditPage {
                } elseif ( $wgUser->isAnon() && $this->formtype != 'preview' ) {
                        $wgOut->wrapWikiMsg( '<div id="mw-anon-edit-warning">$1</div>', 'anoneditwarning' );
                } else {
-                       if ( $this->isCssJsSubpage && $this->formtype != 'preview' ) {
+                       if ( $this->isCssJsSubpage ) {
                                # Check the skin exists
                                if ( $this->isValidCssJsSubpage ) {
-                                       $wgOut->addWikiMsg( 'usercssjsyoucanpreview' );
+                                       if ( $this->formtype !== 'preview' ) {
+                                               $wgOut->addWikiMsg( 'usercssjsyoucanpreview' );
+                                       }
                                } else {
                                        $wgOut->addWikiMsg( 'userinvalidcssjstitle', $wgTitle->getSkinFromCssJsSubpage() );
                                }
@@ -1206,7 +1216,7 @@ class EditPage {
                                $noticeMsg = 'protectedpagewarning';
                                $classes[] = 'mw-textarea-protected';
                        }
-                       $wgOut->addHTML( "<div id='mw-edit-$noticeMsg'>\n" );
+                       $wgOut->addHTML( "<div class='mw-warning-with-logexcerpt'>\n" );
                        $wgOut->addWikiMsg( $noticeMsg );
                        LogEventsList::showLogExtract( $wgOut, 'protect', $this->mTitle->getPrefixedText(), '', 1 );
                        $wgOut->addHTML( "</div>\n" );
@@ -1231,7 +1241,7 @@ class EditPage {
                        $this->kblength = (int)(strlen( $this->textbox1 ) / 1024);
                }
                if ( $this->tooBig || $this->kblength > $wgMaxArticleSize ) {
-                       $wgOut->addHTML( "<div id='mw-edit-longpageerror'>\n" );
+                       $wgOut->addHTML( "<div class='error' id='mw-edit-longpageerror'>\n" );
                        $wgOut->addWikiMsg( 'longpageerror', $wgLang->formatNum( $this->kblength ), $wgLang->formatNum( $wgMaxArticleSize ) );
                        $wgOut->addHTML( "</div>\n" );
                } elseif ( $this->kblength > 29 ) {
@@ -1346,6 +1356,7 @@ class EditPage {
                        $ew = $wgUser->getOption( 'editwidth' );
                        if ( $ew ) $ew = " style=\"width:100%\"";
                        else $ew = '';
+                       $cols = $wgUser->getIntOption( 'cols' );
                        /* /ToDo */
                        $metadata = $top . "<textarea name='metadata' rows='3' cols='{$cols}'{$ew}>{$metadata}</textarea>" ;
                }
@@ -1651,9 +1662,8 @@ END
                                $previewtext = wfMsg('userjspreview');
                        }
                        $parserOptions->setTidy(true);
-                       $parserOutput = $wgParser->parse( $previewtext , $this->mTitle, $parserOptions );
-                       //$wgOut->addHTML( $parserOutput->mText );
-                       $previewHTML = '';
+                       $parserOutput = $wgParser->parse( $previewtext, $this->mTitle, $parserOptions );
+                       $previewHTML = $parserOutput->mText;
                } elseif ( $rt = Title::newFromRedirect( $this->textbox1 ) ) {
                        $previewHTML = $this->mArticle->viewRedirect( $rt, false );
                } else {
@@ -2346,17 +2356,29 @@ END
                global $wgUser;
                $loglist = new LogEventsList( $wgUser->getSkin(), $out );
                $pager = new LogPager( $loglist, 'delete', false, $this->mTitle->getPrefixedText() );
-               if ( $pager->getNumRows() > 0 ) {
-                       $out->addHtml( '<div id="mw-recreate-deleted-warn">' );
+               $count = $pager->getNumRows();
+               if ( $count > 0 ) {
+                       $pager->mLimit = 10;
+                       $out->addHtml( '<div class="mw-warning-with-logexcerpt">' );
                        $out->addWikiMsg( 'recreate-deleted-warn' );
                        $out->addHTML(
                                $loglist->beginLogEventsList() .
                                $pager->getBody() .
                                $loglist->endLogEventsList()
                        );
+                       if($count > 10){
+                               $out->addHtml( $wgUser->getSkin()->link(
+                                       SpecialPage::getTitleFor( 'Log' ),
+                                       wfMsgHtml( 'deletelog-fulllog' ),
+                                       array(),
+                                       array(
+                                               'type' => 'delete',
+                                               'page' => $this->mTitle->getPrefixedText() ) ) );
+                       }
                        $out->addHtml( '</div>' );
                        return true;
                }
+               
                return false;
        }