* Proper redirect deletion
[lhc/web/wiklou.git] / includes / Wiki.php
index 430dc63..7945c7a 100644 (file)
@@ -8,10 +8,8 @@ class MediaWiki {
        var $GET; /* Stores the $_GET variables at time of creation, can be changed */
        var $params = array();
 
-       /**
-        * Constructor
-        */
-       function MediaWiki () {
+       /** Constructor. It just save the $_GET variable */
+       function __construct() {
                $this->GET = $_GET;
        }
 
@@ -58,6 +56,18 @@ class MediaWiki {
                return $article;
        }
 
+       function checkMaxLag( $maxLag ) {
+               global $wgLoadBalancer;
+               list( $host, $lag ) = $wgLoadBalancer->getMaxLag();
+               if ( $lag > $maxLag ) {
+                       wfMaxlagError( $host, $lag, $maxLag );
+                       return false;
+               } else {
+                       return true;
+               }
+       }
+
+
        /**
         * Checks some initial queries
         * Note that $title here is *not* a Title object, but a string!
@@ -84,6 +94,14 @@ class MediaWiki {
                                $lang->findVariantLink( $title, $ret );
 
                }
+               if ( ( $oldid = $request->getInt( 'oldid' ) )
+                       && ( is_null( $ret ) || $ret->getNamespace() != NS_SPECIAL ) ) {
+                       // Allow oldid to override a changed or missing title.
+                       $rev = Revision::newFromId( $oldid );
+                       if( $rev ) {
+                               $ret = $rev->getTitle();
+                       }
+               }
                return $ret ;
        }
 
@@ -92,16 +110,14 @@ class MediaWiki {
         */
        function preliminaryChecks ( &$title, &$output, $request ) {
 
-               # Debug statement for user levels
-               // print_r($wgUser);
-
-               $search = $request->getText( 'search' );
-               if( !is_null( $search ) && $search !== '' ) {
+               if( $request->getCheck( 'search' ) ) {
                        // Compatibility with old search URLs which didn't use Special:Search
+                       // Just check for presence here, so blank requests still
+                       // show the search page when using ugly URLs (bug 8054).
+                       
                        // Do this above the read whitelist check for security...
                        $title = SpecialPage::getTitleFor( 'Search' );
                }
-               $this->setVal( 'Search', $search );
 
                # If the user is not logged in, the Namespace:title of the article must be in
                # the Read array in order for the user to see it. (We have to check here to
@@ -121,13 +137,8 @@ class MediaWiki {
                global $wgRequest;
                wfProfileIn( 'MediaWiki::initializeSpecialCases' );
 
-               $search = $this->getVal('Search');
                $action = $this->getVal('Action');
-               if( !$this->getVal('DisableInternalSearch') && !is_null( $search ) && $search !== '' ) {
-                       require_once( 'includes/SpecialSearch.php' );
-                       $title = SpecialPage::getTitleFor( 'Search' );
-                       wfSpecialSearch();
-               } else if( !$title or $title->getDBkey() == '' ) {
+               if( !$title or $title->getDBkey() == '' ) {
                        $title = SpecialPage::getTitleFor( 'Badtitle' );
                        # Die now before we mess up $wgArticle and the skin stops working
                        throw new ErrorPageError( 'badtitle', 'badtitletext' );
@@ -144,13 +155,13 @@ class MediaWiki {
                                $title = SpecialPage::getTitleFor( 'Badtitle' );
                                throw new ErrorPageError( 'badtitle', 'badtitletext' );
                        }
-               } else if ( ( $action == 'view' ) &&
+               } else if ( ( $action == 'view' ) && !$wgRequest->wasPosted() && 
                        (!isset( $this->GET['title'] ) || $title->getPrefixedDBKey() != $this->GET['title'] ) &&
                        !count( array_diff( array_keys( $this->GET ), array( 'action', 'title' ) ) ) )
                {
                        $targetUrl = $title->getFullURL();
                        // Redirect to canonical url, make it a 301 to allow caching
-                       global $wgServer, $wgUsePathInfo;
+                       global $wgUsePathInfo;
                        if( $targetUrl == $wgRequest->getFullRequestURL() ) {
                                $message = "Redirect loop detected!\n\n" .
                                        "This means the wiki got confused about what page was " .
@@ -195,7 +206,7 @@ class MediaWiki {
         * @param Title $title
         * @return Article
         */
-       function articleFromTitle( $title ) {
+       static function articleFromTitle( $title ) {
                $article = null;
                wfRunHooks('ArticleFromTitle', array( &$title, &$article ) );
                if ( $article ) {
@@ -208,12 +219,17 @@ class MediaWiki {
                }
 
                switch( $title->getNamespace() ) {
-               case NS_IMAGE:
-                       return new ImagePage( $title );
-               case NS_CATEGORY:
-                       return new CategoryPage( $title );
-               default:
-                       return new Article( $title );
+                       case NS_IMAGE:
+                               $file = RepoGroup::singleton()->findFile( $title->getText() );
+                               if( $file && $file->getRedirectedFrom() ) {
+                                       return new Article( $title );
+                               } else {
+                                       return new ImagePage( $title );
+                               }
+                       case NS_CATEGORY:
+                               return new CategoryPage( $title );
+                       default:
+                               return new Article( $title );
                }
        }
 
@@ -233,10 +249,10 @@ class MediaWiki {
                $article = $this->articleFromTitle( $title );
 
                // Namespace might change when using redirects
-               if( $action == 'view' && !$request->getVal( 'oldid' ) &&
+               if( ( $action == 'view' || $action == 'render' ) && !$request->getVal( 'oldid' ) &&
                                                $request->getVal( 'redirect' ) != 'no' ) {
 
-                       $dbr =& wfGetDB(DB_SLAVE);
+                       $dbr = wfGetDB(DB_SLAVE);
                        $article->loadPageData($article->pageDataFromTitle($dbr, $title));
 
                        /* Follow redirects only for... redirects */
@@ -290,7 +306,7 @@ class MediaWiki {
         */
        function doUpdates ( &$updates ) {
                wfProfileIn( 'MediaWiki::doUpdates' );
-               $dbw =& wfGetDB( DB_MASTER );
+               $dbw = wfGetDB( DB_MASTER );
                foreach( $updates as $up ) {
                        $up->doUpdate();
 
@@ -352,6 +368,11 @@ class MediaWiki {
 
                wfProfileIn( 'MediaWiki::performAction' );
 
+               if ( !wfRunHooks('MediaWikiPerformAction', array($output, $article, $title, $user, $request)) ) {
+                       wfProfileOut( 'MediaWiki::performAction' );
+                       return;
+               }
+
                $action = $this->getVal('Action');
                if( in_array( $action, $this->getVal('DisabledActions',array()) ) ) {
                        /* No such action; this will switch to the default case */
@@ -401,28 +422,31 @@ class MediaWiki {
                                showCreditsPage( $article );
                                break;
                        case 'submit':
-                               if( !$this->getVal( 'CommandLineMode' ) && !$request->checkSessionCookie() ) {
+                               if( session_id() == '' ) {
                                        /* Send a cookie so anons get talk message notifications */
-                                       User::SetupSession();
+                                       wfSetupSession();
                                }
                                /* Continue... */
                        case 'edit':
-                               $internal = $request->getVal( 'internaledit' );
-                               $external = $request->getVal( 'externaledit' );
-                               $section = $request->getVal( 'section' );
-                               $oldid = $request->getVal( 'oldid' );
-                               if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
-                                  $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
-                                       $editor = new EditPage( $article );
-                                       $editor->submit();
-                               } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
-                                       $mode = $request->getVal( 'mode' );
-                                       $extedit = new ExternalEdit( $article, $mode );
-                                       $extedit->edit();
+                               if( wfRunHooks( 'CustomEditor', array( $article, $user ) ) ) {
+                                       $internal = $request->getVal( 'internaledit' );
+                                       $external = $request->getVal( 'externaledit' );
+                                       $section = $request->getVal( 'section' );
+                                       $oldid = $request->getVal( 'oldid' );
+                                       if( !$this->getVal( 'UseExternalEditor' ) || $action=='submit' || $internal ||
+                                          $section || $oldid || ( !$user->getOption( 'externaleditor' ) && !$external ) ) {
+                                               $editor = new EditPage( $article );
+                                               $editor->submit();
+                                       } elseif( $this->getVal( 'UseExternalEditor' ) && ( $external || $user->getOption( 'externaleditor' ) ) ) {
+                                               $mode = $request->getVal( 'mode' );
+                                               $extedit = new ExternalEdit( $article, $mode );
+                                               $extedit->edit();
+                                       }
                                }
                                break;
                        case 'history':
-                               if( $_SERVER['REQUEST_URI'] == $title->getInternalURL( 'action=history' ) ) {
+                               global $wgRequest;
+                               if( $wgRequest->getFullRequestURL() == $title->getInternalURL( 'action=history' ) ) {
                                        $output->setSquidMaxage( $this->getVal( 'SquidMaxage' ) );
                                }
                                $history = new PageHistory( $article );
@@ -443,4 +467,4 @@ class MediaWiki {
 
 }; /* End of class MediaWiki */
 
-?>
+