Don't hang up page views with jobs
[lhc/web/wiklou.git] / includes / Wiki.php
index 04618e0..c87f078 100644 (file)
@@ -43,7 +43,7 @@ class MediaWiki {
         * Initialization of ... everything
         * Performs the request too
         *
-        * @param $title Title
+        * @param $title Title ($wgTitle)
         * @param $article Article
         * @param $output OutputPage
         * @param $user User
@@ -60,6 +60,7 @@ class MediaWiki {
                        } elseif( is_string( $new_article ) ) {
                                $output->redirect( $new_article );
                        } else {
+                               wfProfileOut( __METHOD__ );
                                throw new MWException( "Shouldn't happen: MediaWiki::initializeArticle() returned neither an object nor a URL" );
                        }
                }
@@ -251,12 +252,12 @@ 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:
+                               return new ImagePage( $title );
+                       case NS_CATEGORY:
+                               return new CategoryPage( $title );
+                       default:
+                               return new Article( $title );
                }
        }
 
@@ -264,7 +265,7 @@ class MediaWiki {
         * Initialize the object to be known as $wgArticle for "standard" actions
         * Create an Article object for the page, following redirects if needed.
         *
-        * @param $title Title
+        * @param $title Title ($wgTitle)
         * @param $request WebRequest
         * @return mixed an Article, or a string to redirect to another URL
         */
@@ -274,24 +275,26 @@ class MediaWiki {
                $action = $this->getVal( 'action' );
                $article = self::articleFromTitle( $title );
                
-               wfDebug("Article: ".$title->getPrefixedText()."\n");
-               
                // Namespace might change when using redirects
                // Check for redirects ...
                $file = $title->getNamespace() == NS_IMAGE ? $article->getFile() : null;
                if( ( $action == 'view' || $action == 'render' )        // ... for actions that show content
-                                       && !$request->getVal( 'oldid' ) &&                      // ... and are not old revisions
+                                       && !$request->getVal( 'oldid' ) &&    // ... and are not old revisions
                                        $request->getVal( 'redirect' ) != 'no' &&       // ... unless explicitly told not to
-                                       // ... and the article is not an image page with associated file
-                                       !( is_object( $file ) && $file->exists() &&
-                                                       !$file->getRedirected() ) ) { // ... unless it is really an image redirect
+                                       // ... and the article is not a non-redirect image page with associated file
+                                       !( is_object( $file ) && $file->exists() && !$file->getRedirected() ) ) {
+
+                       # Give extensions a change to ignore/handle redirects as needed
+                       $ignoreRedirect = $target = false;
+                       wfRunHooks( 'InitializeArticleMaybeRedirect', array( &$title, &$request, &$ignoreRedirect, &$target ) );
 
                        $dbr = wfGetDB( DB_SLAVE );
                        $article->loadPageData( $article->pageDataFromTitle( $dbr, $title ) );
 
                        // Follow redirects only for... redirects
-                       if( $article->isRedirect() ) {
-                               $target = $article->followRedirect();
+                       if( !$ignoreRedirect && $article->isRedirect() ) {
+                               # Is the target already set by an extension?
+                               $target = $target ? $target : $article->followRedirect();
                                if( is_string( $target ) ) {
                                        if( !$this->getVal( 'DisableHardRedirects' ) ) {
                                                // we'll need to redirect
@@ -303,9 +306,7 @@ class MediaWiki {
                                        // Rewrite environment to redirected article
                                        $rarticle = self::articleFromTitle( $target );
                                        $rarticle->loadPageData( $rarticle->pageDataFromTitle( $dbr, $target ) );
-                                       if ( $rarticle->getTitle()->exists() || 
-                                                               ( is_object( $file ) && 
-                                                               !$file->isLocal() ) ) {
+                                       if ( $rarticle->exists() || ( is_object( $file ) && !$file->isLocal() ) ) {
                                                $rarticle->setRedirectedFrom( $title );
                                                $article = $rarticle;
                                                $title = $target;
@@ -327,12 +328,16 @@ class MediaWiki {
         */
        function finalCleanup ( &$deferredUpdates, &$output ) {
                wfProfileIn( __METHOD__ );
-               $this->doUpdates( $deferredUpdates );
-               $this->doJobs();
                # Now commit any transactions, so that unreported errors after output() don't roll back the whole thing
                $factory = wfGetLBFactory();
-               $factory->shutdown();
+               $factory->commitMasterChanges();
+               # Output everything!
                $output->output();
+               # Do any deferred jobs
+               $this->doUpdates( $deferredUpdates );
+               $this->doJobs();
+               # Commit and close up!
+               $factory->shutdown();
                wfProfileOut( __METHOD__ );
        }
 
@@ -418,7 +423,7 @@ class MediaWiki {
        function performAction( &$output, &$article, &$title, &$user, &$request ) {
                wfProfileIn( __METHOD__ );
 
-               if ( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request ) ) ) {
+               if ( !wfRunHooks( 'MediaWikiPerformAction', array( $output, $article, $title, $user, $request, $this ) ) ) {
                        wfProfileOut( __METHOD__ );
                        return;
                }
@@ -455,21 +460,20 @@ class MediaWiki {
                                if( !$this->getVal( 'EnableDublinCoreRdf' ) ) {
                                        wfHttpError( 403, 'Forbidden', wfMsg( 'nodublincore' ) );
                                } else {
-                                       require_once( 'includes/Metadata.php' );
-                                       wfDublinCoreRdf( $article );
+                                       $rdf = new DublinCoreRdf( $article );
+                                       $rdf->show();
                                }
                                break;
                        case 'creativecommons':
                                if( !$this->getVal( 'EnableCreativeCommonsRdf' ) ) {
                                        wfHttpError( 403, 'Forbidden', wfMsg( 'nocreativecommons' ) );
                                } else {
-                                       require_once( 'includes/Metadata.php' );
-                                       wfCreativeCommonsRdf( $article );
+                                       $rdf = new CreativeCommonsRdf( $article );
+                                       $rdf->show();
                                }
                                break;
                        case 'credits':
-                               require_once( 'includes/Credits.php' );
-                               showCreditsPage( $article );
+                               Credits::showPage( $article );
                                break;
                        case 'submit':
                                if( session_id() == '' ) {