fix synxtax
[lhc/web/wiklou.git] / includes / Title.php
index 1653067..8a9d3ee 100644 (file)
@@ -207,6 +207,9 @@ class Title {
         * Make an array of titles from an array of IDs 
         */
        public static function newFromIDs( $ids ) {
+               if ( !count( $ids ) ) {
+                       return array();
+               }
                $dbr = wfGetDB( DB_SLAVE );
                $res = $dbr->select( 'page', array( 'page_namespace', 'page_title' ),
                        'page_id IN (' . $dbr->makeList( $ids ) . ')', __METHOD__ );
@@ -265,7 +268,12 @@ class Title {
         * @return Title the new object
         */
        public static function newMainPage() {
-               return Title::newFromText( wfMsgForContent( 'mainpage' ) );
+               $title = Title::newFromText( wfMsgForContent( 'mainpage' ) );
+               // Don't give fatal errors if the message is broken
+               if ( !$title ) {
+                       $title = Title::newFromText( 'Main Page' );
+               }
+               return $title;
        }
 
        /**
@@ -670,7 +678,7 @@ class Title {
         */
        public function getBaseText() {
                global $wgNamespacesWithSubpages;
-               if( isset( $wgNamespacesWithSubpages[ $this->mNamespace ] ) && $wgNamespacesWithSubpages[ $this->mNamespace ] ) {
+               if( !empty( $wgNamespacesWithSubpages[$this->mNamespace] ) ) {
                        $parts = explode( '/', $this->getText() );
                        # Don't discard the real title if there's no subpage involved
                        if( count( $parts ) > 1 )
@@ -794,16 +802,15 @@ class Title {
                } else {
                        $dbkey = wfUrlencode( $this->getPrefixedDBkey() );
                        if ( $query == '' ) {
-                               if($variant!=false && $wgContLang->hasVariants()){
-                                       if($wgVariantArticlePath==false) {
+                               if( $variant != false && $wgContLang->hasVariants() ) {
+                                       if( $wgVariantArticlePath == false ) {
                                                $variantArticlePath =  "$wgScript?title=$1&variant=$2"; // default
                                        } else {
                                                $variantArticlePath = $wgVariantArticlePath;
                                        }
                                        $url = str_replace( '$2', urlencode( $variant ), $variantArticlePath );
                                        $url = str_replace( '$1', $dbkey, $url  );
-                               }
-                               else {
+                               } else {
                                        $url = str_replace( '$1', $dbkey, $wgArticlePath );
                                }
                        } else {
@@ -1025,8 +1032,9 @@ class Title {
        }
 
        /**
-        * Can $user perform $action on this page?  This *does not* check throttles
-        * (User::pingLimiter()) yet; check that manually to be sure.
+        * Can $user perform $action on this page?
+        *
+        * FIXME: This *does not* check throttles (User::pingLimiter()).
         *
         * @param string $action action that permission needs to be checked for
         * @param bool $doExpensiveQueries Set this to false to avoid doing unnecessary queries.
@@ -1037,12 +1045,6 @@ class Title {
 
                global $wgContLang;
                global $wgLang;
-
-               if ( wfReadOnly() && $action != 'read' ) {
-                       global $wgReadOnly;
-                       $errors[] = array( 'readonlytext', $wgReadOnly );
-               }
-
                global $wgEmailConfirmToEdit, $wgUser;
 
                if ( $wgEmailConfirmToEdit && !$user->isEmailConfirmed() ) {
@@ -1129,6 +1131,16 @@ class Title {
                        else if ($result === false )
                                $errors[] = array('badaccess-group0'); # a generic "We don't want them to do that"
                }
+               if ($doExpensiveQueries && !wfRunHooks( 'getUserPermissionsErrorsExpensive', array( &$this, &$user, $action, &$result ) ) ) {
+                       if ($result != array() && is_array($result) && !is_array($result[0]))
+                               $errors[] = $result; # A single array representing an error
+                       else if (is_array($result) && is_array($result[0]))
+                               $errors = array_merge( $errors, $result ); # A nested array representing multiple errors
+                       else if ($result != '' && $result != null && $result !== true && $result !== false)
+                               $errors[] = array($result); # A string representing a message-id
+                       else if ($result === false )
+                               $errors[] = array('badaccess-group0'); # a generic "We don't want them to do that"
+               }
 
                if( NS_SPECIAL == $this->mNamespace ) {
                        $errors[] = array('ns-specialprotected');
@@ -1262,7 +1274,7 @@ class Title {
 
                $dbr = wfGetDB( DB_SLAVE );
                $res = $dbr->select( 'protected_titles', '*', 
-                       array ('pt_namespace' => $this->getNamespace(), 'pt_title' => $this->getDBKey()) );
+                       array ('pt_namespace' => $this->getNamespace(), 'pt_title' => $this->getDBkey()) );
 
                if ($row = $dbr->fetchRow( $res )) {
                        return $row;
@@ -1280,7 +1292,7 @@ class Title {
                        return true;
                }
 
-               list ($namespace, $title) = array( $this->getNamespace(), $this->getDBKey() );
+               list ($namespace, $title) = array( $this->getNamespace(), $this->getDBkey() );
 
                $dbw = wfGetDB( DB_MASTER );
 
@@ -1322,7 +1334,7 @@ class Title {
                $dbw = wfGetDB( DB_MASTER );
 
                $dbw->delete( 'protected_titles', 
-                       array ('pt_namespace' => $this->getNamespace(), 'pt_title' => $this->getDBKey()), __METHOD__ );
+                       array ('pt_namespace' => $this->getNamespace(), 'pt_title' => $this->getDBkey()), __METHOD__ );
        }
 
        /**
@@ -1369,8 +1381,12 @@ class Title {
         * @todo fold these checks into userCan()
         */
        public function userCanRead() {
-               global $wgUser;
-
+               global $wgUser, $wgGroupPermissions;
+               
+               # Shortcut for public wikis, allows skipping quite a bit of code path
+               if ($wgGroupPermissions['*']['read'])
+                       return true;
+               
                $result = null;
                wfRunHooks( 'userCan', array( &$this, &$wgUser, 'read', &$result ) );
                if ( $result !== null ) {
@@ -1418,7 +1434,7 @@ class Title {
                         * and check again
                         */
                        if( $this->getNamespace() == NS_SPECIAL ) {
-                               $name = $this->getDBKey();
+                               $name = $this->getDBkey();
                                list( $name, /* $subpage */) = SpecialPage::resolveAliasWithSubpage( $name );
                                if ( $name === false ) {
                                        # Invalid special page, but we show standard login required message
@@ -2021,7 +2037,9 @@ class Title {
                       strpos( $dbkey, './' ) === 0  ||
                       strpos( $dbkey, '../' ) === 0 ||
                       strpos( $dbkey, '/./' ) !== false ||
-                      strpos( $dbkey, '/../' ) !== false ) )
+                      strpos( $dbkey, '/../' ) !== false  ||
+                      substr( $dbkey, -2 ) == '/.' ||
+                      substr( $dbkey, -3 ) == '/..' ) )
                {
                        return false;
                }
@@ -2146,7 +2164,7 @@ class Title {
                        array(
                                "{$prefix}_from=page_id",
                                "{$prefix}_namespace" => $this->getNamespace(),
-                               "{$prefix}_title"     => $this->getDbKey() ),
+                               "{$prefix}_title"     => $this->getDBkey() ),
                        'Title::getLinksTo',
                        $options );
 
@@ -2296,10 +2314,14 @@ class Title {
                        return 'badarticleerror';
                }
 
-               if ( $auth && (
-                               !$this->userCan( 'edit' ) || !$nt->userCan( 'edit' ) ||
-                               !$this->userCan( 'move' ) || !$nt->userCan( 'move' ) ) ) {
-                       return 'protectedpage';
+               if ( $auth ) {
+                       global $wgUser;
+                       $errors = array_merge($this->getUserPermissionsErrors('move', $wgUser),
+                                       $this->getUserPermissionsErrors('edit', $wgUser),
+                                       $nt->getUserPermissionsErrors('move', $wgUser),
+                                       $nt->getUserPermissionsErrors('edit', $wgUser));
+                       if($errors !== array())
+                               return $errors[0][0];
                }
 
                global $wgUser;
@@ -2410,6 +2432,8 @@ class Title {
                # Update message cache for interface messages
                if( $nt->getNamespace() == NS_MEDIAWIKI ) {
                        global $wgMessageCache;
+                       $oldarticle = new Article( $this );
+                       $wgMessageCache->replace( $this->getDBkey(), $oldarticle->getContent() );
                        $newarticle = new Article( $nt );
                        $wgMessageCache->replace( $nt->getDBkey(), $newarticle->getContent() );
                }
@@ -2442,13 +2466,25 @@ class Title {
                $newid = $nt->getArticleID();
                $oldid = $this->getArticleID();
                $dbw = wfGetDB( DB_MASTER );
-               $linkCache =& LinkCache::singleton();
 
                # Delete the old redirect. We don't save it to history since
                # by definition if we've got here it's rather uninteresting.
                # We have to remove it so that the next step doesn't trigger
                # a conflict on the unique namespace+title index...
                $dbw->delete( 'page', array( 'page_id' => $newid ), $fname );
+               if ( !$dbw->cascadingDeletes() ) {
+                       $dbw->delete( 'revision', array( 'rev_page' => $newid ), __METHOD__ );
+                       global $wgUseTrackbacks;
+                       if ($wgUseTrackbacks)
+                               $dbw->delete( 'trackbacks', array( 'tb_page' => $newid ), __METHOD__ );
+                       $dbw->delete( 'pagelinks', array( 'pl_from' => $newid ), __METHOD__ );
+                       $dbw->delete( 'imagelinks', array( 'il_from' => $newid ), __METHOD__ );
+                       $dbw->delete( 'categorylinks', array( 'cl_from' => $newid ), __METHOD__ );
+                       $dbw->delete( 'templatelinks', array( 'tl_from' => $newid ), __METHOD__ );
+                       $dbw->delete( 'externallinks', array( 'el_from' => $newid ), __METHOD__ );
+                       $dbw->delete( 'langlinks', array( 'll_from' => $newid ), __METHOD__ );
+                       $dbw->delete( 'redirect', array( 'rd_from' => $newid ), __METHOD__ );
+               }
 
                # Save a null revision in the page's history notifying of the move
                $nullRevision = Revision::newNullRevision( $dbw, $oldid, $comment, true );
@@ -2465,7 +2501,7 @@ class Title {
                        /* WHERE */ array( 'page_id' => $oldid ),
                        $fname
                );
-               $linkCache->clearLink( $nt->getPrefixedDBkey() );
+               $nt->resetArticleID( $oldid );
 
                # Recreate the redirect, this time in the other direction.
                if($createRedirect || !$wgUser->isAllowed('suppressredirect'))
@@ -2480,7 +2516,6 @@ class Title {
                                'text'    => $redirectText ) );
                        $redirectRevision->insertOn( $dbw );
                        $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
-                       $linkCache->clearLink( $this->getPrefixedDBkey() );
 
                        # Now, we record the link from the redirect to the new title.
                        # It should have no other outgoing links...
@@ -2489,8 +2524,10 @@ class Title {
                                array(
                                        'pl_from'      => $newid,
                                        'pl_namespace' => $nt->getNamespace(),
-                                       'pl_title'     => $nt->getDbKey() ),
+                                       'pl_title'     => $nt->getDBkey() ),
                                $fname );
+               } else {
+                       $this->resetArticleID( 0 );
                }
                
                # Log the move
@@ -2524,13 +2561,12 @@ class Title {
                $oldid = $this->getArticleID();
                $dbw = wfGetDB( DB_MASTER );
                $now = $dbw->timestamp();
-               $linkCache =& LinkCache::singleton();
 
                # Save a null revision in the page's history notifying of the move
                $nullRevision = Revision::newNullRevision( $dbw, $oldid, $comment, true );
                $nullRevId = $nullRevision->insertOn( $dbw );
 
-               # Rename cur entry
+               # Rename page entry
                $dbw->update( 'page',
                        /* SET */ array(
                                'page_touched'   => $now,
@@ -2541,8 +2577,7 @@ class Title {
                        /* WHERE */ array( 'page_id' => $oldid ),
                        $fname
                );
-
-               $linkCache->clearLink( $nt->getPrefixedDBkey() );
+               $nt->resetArticleID( $oldid );
 
                if($createRedirect || !$wgUser->isAllowed('suppressredirect'))
                {
@@ -2557,7 +2592,7 @@ class Title {
                                'text'    => $redirectText ) );
                        $redirectRevision->insertOn( $dbw );
                        $redirectArticle->updateRevisionOn( $dbw, $redirectRevision, 0 );
-                       $linkCache->clearLink( $this->getPrefixedDBkey() );
+
                        # Record the just-created redirect's linking to the page
                        $dbw->insert( 'pagelinks',
                                array(
@@ -2565,6 +2600,8 @@ class Title {
                                        'pl_namespace' => $nt->getNamespace(),
                                        'pl_title'     => $nt->getDBkey() ),
                                $fname );
+               } else {
+                       $this->resetArticleID( 0 );
                }
 
                # Log the move
@@ -2765,7 +2802,7 @@ class Title {
                // Note: === is necessary for proper matching of number-like titles.
                return $this->getInterwiki() === $title->getInterwiki()
                        && $this->getNamespace() == $title->getNamespace()
-                       && $this->getDbkey() === $title->getDbkey();
+                       && $this->getDBkey() === $title->getDBkey();
        }
        
        /**