Removed comment that's no longer relevant
[lhc/web/wiklou.git] / includes / Title.php
index aa0ec5f..a3b4718 100644 (file)
@@ -1316,6 +1316,7 @@ class Title {
         */
        private function checkPermissionHooks( $action, $user, $errors, $doExpensiveQueries, $short ) {
                // Use getUserPermissionsErrors instead
+               $result = '';
                if ( !wfRunHooks( 'userCan', array( &$this, &$user, $action, &$result ) ) ) {
                        return $result ? array() : array( array( 'badaccess-group0' ) );
                }
@@ -2413,6 +2414,10 @@ class Title {
         * This clears some fields in this object, and clears any associated
         * keys in the "bad links" section of the link cache.
         *
+        * - This is called from Article::insertNewArticle() to allow
+        * loading of the new page_id. It's also called from
+        * Article::doDeleteArticle()
+        *
         * @param $newid \type{\int} the new Article ID
         */
        public function resetArticleID( $newid ) {
@@ -2804,7 +2809,6 @@ class Title {
                                }
                        }
                }
-               $db->freeResult( $res );
                return $retVal;
        }
 
@@ -3026,6 +3030,8 @@ class Title {
         * @return \type{\mixed} true on success, getUserPermissionsErrors()-like array on failure
         */
        public function moveTo( &$nt, $auth = true, $reason = '', $createRedirect = true ) {
+               global $wgContLang;
+
                $err = $this->isValidMoveOperation( $nt, $auth, $reason );
                if ( is_array( $err ) ) {
                        return $err;
@@ -3058,24 +3064,19 @@ class Title {
                }
                $redirid = $this->getArticleID();
 
-               // Category memberships include a sort key which may be customized.
-               // If it's left as the default (the page title), we need to update
-               // the sort key to match the new title.
-               //
-               // Be careful to avoid resetting cl_timestamp, which may disturb
-               // time-based lists on some sites.
-               //
-               // Warning -- if the sort key is *explicitly* set to the old title,
-               // we can't actually distinguish it from a default here, and it'll
-               // be set to the new title even though it really shouldn't.
-               // It'll get corrected on the next edit, but resetting cl_timestamp.
+               // Refresh the sortkey for this row.  Be careful to avoid resetting
+               // cl_timestamp, which may disturb time-based lists on some sites.
+               $prefix = $dbw->selectField(
+                       'categorylinks',
+                       'cl_sortkey_prefix',
+                       array( 'cl_from' => $pageid ),
+                       __METHOD__
+               );
                $dbw->update( 'categorylinks',
                        array(
-                               'cl_sortkey' => $nt->getPrefixedText(),
+                               'cl_sortkey' => $wgContLang->convertToSortkey( $nt->getCategorySortkey( $prefix ) ),
                                'cl_timestamp=cl_timestamp' ),
-                       array(
-                               'cl_from' => $pageid,
-                               'cl_sortkey' => $this->getPrefixedText() ),
+                       array( 'cl_from' => $pageid ),
                        __METHOD__ );
 
                if ( $protected ) {
@@ -3138,9 +3139,8 @@ class Title {
                        $u->doUpdate();
                }
                # Update message cache for interface messages
-               if ( $nt->getNamespace() == NS_MEDIAWIKI ) {
-                       global $wgMessageCache;
-
+               global $wgMessageCache;
+               if ( $this->getNamespace() == NS_MEDIAWIKI ) {
                        # @bug 17860: old article can be deleted, if this the case,
                        # delete it from message cache
                        if ( $this->getArticleID() === 0 ) {
@@ -3149,7 +3149,8 @@ class Title {
                                $oldarticle = new Article( $this );
                                $wgMessageCache->replace( $this->getDBkey(), $oldarticle->getContent() );
                        }
-
+               }
+               if ( $nt->getNamespace() == NS_MEDIAWIKI ) {
                        $newarticle = new Article( $nt );
                        $wgMessageCache->replace( $nt->getDBkey(), $newarticle->getContent() );
                }
@@ -3551,14 +3552,13 @@ class Title {
                         . " ORDER BY cl_sortkey";
 
                $res = $dbr->query( $sql );
+               $data = array();
 
                if ( $dbr->numRows( $res ) > 0 ) {
-                       foreach ( $res as $row )
+                       foreach ( $res as $row ) {
                                // $data[] = Title::newFromText($wgContLang->getNSText ( NS_CATEGORY ).':'.$row->cl_to);
                                $data[$wgContLang->getNSText( NS_CATEGORY ) . ':' . $row->cl_to] = $this->getFullText();
-                       $dbr->freeResult( $res );
-               } else {
-                       $data = array();
+                       }
                }
                return $data;
        }
@@ -3585,10 +3585,9 @@ class Title {
                                        }
                                }
                        }
-                       return $stack;
-               } else {
-                       return array();
                }
+
+               return $stack;
        }
 
 
@@ -3783,21 +3782,21 @@ class Title {
                        return true;  // any interwiki link might be viewable, for all we know
                }
                switch( $this->mNamespace ) {
-               case NS_MEDIA:
-               case NS_FILE:
-                       return (bool)wfFindFile( $this );  // file exists, possibly in a foreign repo
-               case NS_SPECIAL:
-                       return SpecialPage::exists( $this->getDBkey() );  // valid special page
-               case NS_MAIN:
-                       return $this->mDbkeyform == '';  // selflink, possibly with fragment
-               case NS_MEDIAWIKI:
-                       // If the page is form Mediawiki:message/lang, calling wfMsgWeirdKey causes
-                       // the full l10n of that language to be loaded. That takes much memory and
-                       // isn't needed. So we strip the language part away.
-                       list( $basename, /* rest */ ) = explode( '/', $this->mDbkeyform, 2 );
-                       return (bool)wfMsgWeirdKey( $basename );  // known system message
-               default:
-                       return false;
+                       case NS_MEDIA:
+                       case NS_FILE:
+                               return (bool)wfFindFile( $this );  // file exists, possibly in a foreign repo
+                       case NS_SPECIAL:
+                               return SpecialPage::exists( $this->getDBkey() );  // valid special page
+                       case NS_MAIN:
+                               return $this->mDbkeyform == '';  // selflink, possibly with fragment
+                       case NS_MEDIAWIKI:
+                               // If the page is form Mediawiki:message/lang, calling wfMsgWeirdKey causes
+                               // the full l10n of that language to be loaded. That takes much memory and
+                               // isn't needed. So we strip the language part away.
+                               list( $basename, /* rest */ ) = explode( '/', $this->mDbkeyform, 2 );
+                               return (bool)wfMsgWeirdKey( $basename );  // known system message
+                       default:
+                               return false;
                }
        }
 
@@ -3810,7 +3809,7 @@ class Title {
         * @return \type{\bool}
         */
        public function isKnown() {
-               return $this->exists() || $this->isAlwaysKnown();
+               return $this->isAlwaysKnown() || $this->exists();
        }
 
        /**
@@ -4062,7 +4061,6 @@ class Title {
                        __METHOD__
                );
 
-
                foreach ( $res as $row ) {
                        $redirs[] = self::newFromRow( $row );
                }
@@ -4139,20 +4137,23 @@ class Title {
        }
 
        /**
-        * Returns what the default sort key for categories would be, if
-        * {{defaultsort:}} isn't used.  This is the same as getText() for
-        * categories, and for everything if $wgCategoryPrefixedDefaultSortkey is
-        * false; otherwise it's the same as getPrefixedText().
+        * Returns the raw sort key to be used for categories, with the specified
+        * prefix.  This will be fed to Language::convertToSortkey() to get a
+        * binary sortkey that can be used for actual sorting.
         *
+        * @param $prefix string The prefix to be used, specified using
+        *   {{defaultsort:}} or like [[Category:Foo|prefix]].  Empty for no
+        *   prefix.
         * @return string
         */
-       public function getCategorySortkey() {
-               global $wgCategoryPrefixedDefaultSortkey;
-               if ( $this->getNamespace() == NS_CATEGORY
-               || !$wgCategoryPrefixedDefaultSortkey ) {
-                       return $this->getText();
-               } else {
-                       return $this->getPrefixedText();
+       public function getCategorySortkey( $prefix = '' ) {
+               $unprefixed = $this->getText();
+               if ( $prefix !== '' ) {
+                       # Separate with a null byte, so the unprefixed part is only used as
+                       # a tiebreaker when two pages have the exact same prefix -- null
+                       # sorts before everything else (hopefully).
+                       return "$prefix\0$unprefixed";
                }
+               return $unprefixed;
        }
 }