Merge "Use {{int:}} on MediaWiki:Blockedtext and MediaWiki:Autoblockedtext"
[lhc/web/wiklou.git] / includes / Title.php
index 8dda01f..b771477 100644 (file)
@@ -1027,12 +1027,11 @@ class Title implements LinkTarget {
         */
        public function getNsText() {
                if ( $this->isExternal() ) {
-                       // This probably shouldn't even happen,
-                       // but for interwiki transclusion it sometimes does.
-                       // Use the canonical namespaces if possible to try to
-                       // resolve a foreign namespace.
-                       if ( MWNamespace::exists( $this->mNamespace ) ) {
-                               return MWNamespace::getCanonicalName( $this->mNamespace );
+                       // This probably shouldn't even happen, except for interwiki transclusion.
+                       // If possible, use the canonical name for the foreign namespace.
+                       $nsText = MWNamespace::getCanonicalName( $this->mNamespace );
+                       if ( $nsText !== false ) {
+                               return $nsText;
                        }
                }
 
@@ -1278,15 +1277,15 @@ class Title implements LinkTarget {
        }
 
        /**
-        * Could this page contain custom CSS or JavaScript for the global UI.
-        * This is generally true for pages in the MediaWiki namespace having CONTENT_MODEL_CSS
-        * or CONTENT_MODEL_JAVASCRIPT.
+        * Could this MediaWiki namespace page contain custom CSS, JSON, or JavaScript for the
+        * global UI. This is generally true for pages in the MediaWiki namespace having
+        * CONTENT_MODEL_CSS, CONTENT_MODEL_JSON, or CONTENT_MODEL_JAVASCRIPT.
         *
-        * This method does *not* return true for per-user JS/CSS. Use isCssJsSubpage()
+        * This method does *not* return true for per-user JS/JSON/CSS. Use isUserConfigPage()
         * for that!
         *
-        * Note that this method should not return true for pages that contain and
-        * show "inactive" CSS or JS.
+        * Note that this method should not return true for pages that contain and show
+        * "inactive" CSS, JSON, or JS.
         *
         * @return bool
         * @since 1.31
@@ -1296,6 +1295,7 @@ class Title implements LinkTarget {
                        NS_MEDIAWIKI == $this->mNamespace
                        && (
                                $this->hasContentModel( CONTENT_MODEL_CSS )
+                               || $this->hasContentModel( CONTENT_MODEL_JSON )
                                || $this->hasContentModel( CONTENT_MODEL_JAVASCRIPT )
                        )
                );
@@ -1303,7 +1303,7 @@ class Title implements LinkTarget {
 
        /**
         * @return bool
-        * @deprecated Since 1.31; use ::isSiteConfigPage() instead
+        * @deprecated Since 1.31; use ::isSiteConfigPage() instead (which also checks for JSON pages)
         */
        public function isCssOrJsPage() {
                wfDeprecated( __METHOD__, '1.31' );
@@ -1313,7 +1313,7 @@ class Title implements LinkTarget {
        }
 
        /**
-        * Is this a "config" (.css or .js) sub-page of a user page?
+        * Is this a "config" (.css, .json, or .js) sub-page of a user page?
         *
         * @return bool
         * @since 1.31
@@ -1324,6 +1324,7 @@ class Title implements LinkTarget {
                        && $this->isSubpage()
                        && (
                                $this->hasContentModel( CONTENT_MODEL_CSS )
+                               || $this->hasContentModel( CONTENT_MODEL_JSON )
                                || $this->hasContentModel( CONTENT_MODEL_JAVASCRIPT )
                        )
                );
@@ -1331,7 +1332,7 @@ class Title implements LinkTarget {
 
        /**
         * @return bool
-        * @deprecated Since 1.31; use ::isUserConfigPage() instead
+        * @deprecated Since 1.31; use ::isUserConfigPage() instead (which also checks for JSON pages)
         */
        public function isCssJsSubpage() {
                wfDeprecated( __METHOD__, '1.31' );
@@ -1341,9 +1342,9 @@ class Title implements LinkTarget {
        }
 
        /**
-        * Trim down a .css or .js subpage title to get the corresponding skin name
+        * Trim down a .css, .json, or .js subpage title to get the corresponding skin name
         *
-        * @return string Containing skin name from .css or .js subpage title
+        * @return string Containing skin name from .css, .json, or .js subpage title
         * @since 1.31
         */
        public function getSkinFromConfigSubpage() {
@@ -1351,14 +1352,14 @@ class Title implements LinkTarget {
                $subpage = $subpage[count( $subpage ) - 1];
                $lastdot = strrpos( $subpage, '.' );
                if ( $lastdot === false ) {
-                       return $subpage; # Never happens: only called for names ending in '.css' or '.js'
+                       return $subpage; # Never happens: only called for names ending in '.css'/'.json'/'.js'
                }
                return substr( $subpage, 0, $lastdot );
        }
 
        /**
         * @deprecated Since 1.31; use ::getSkinFromConfigSubpage() instead
-        * @return string Containing skin name from .css or .js subpage title
+        * @return string Containing skin name from .css, .json, or .js subpage title
         */
        public function getSkinFromCssJsSubpage() {
                wfDeprecated( __METHOD__, '1.31' );
@@ -1389,7 +1390,21 @@ class Title implements LinkTarget {
        }
 
        /**
-        * Is this a .js subpage of a user page?
+        * Is this a JSON "config" sub-page of a user page?
+        *
+        * @return bool
+        * @since 1.31
+        */
+       public function isUserJsonConfigPage() {
+               return (
+                       NS_USER == $this->mNamespace
+                       && $this->isSubpage()
+                       && $this->hasContentModel( CONTENT_MODEL_JSON )
+               );
+       }
+
+       /**
+        * Is this a JS "config" sub-page of a user page?
         *
         * @return bool
         * @since 1.31
@@ -2302,7 +2317,7 @@ class Title implements LinkTarget {
        }
 
        /**
-        * Check CSS/JS sub-page permissions
+        * Check CSS/JSON/JS sub-page permissions
         *
         * @param string $action The action to check
         * @param User $user User to check
@@ -2313,7 +2328,7 @@ class Title implements LinkTarget {
         * @return array List of errors
         */
        private function checkUserConfigPermissions( $action, $user, $errors, $rigor, $short ) {
-               # Protect css/js subpages of user pages
+               # Protect css/json/js subpages of user pages
                # XXX: this might be better using restrictions
 
                if ( $action != 'patrol' ) {
@@ -2323,6 +2338,11 @@ class Title implements LinkTarget {
                                        && !$user->isAllowedAny( 'editmyusercss', 'editusercss' )
                                ) {
                                        $errors[] = [ 'mycustomcssprotected', $action ];
+                               } elseif (
+                                       $this->isUserJsonConfigPage()
+                                       && !$user->isAllowedAny( 'editmyuserjson', 'edituserjson' )
+                               ) {
+                                       $errors[] = [ 'mycustomjsonprotected', $action ];
                                } elseif (
                                        $this->isUserJsConfigPage()
                                        && !$user->isAllowedAny( 'editmyuserjs', 'edituserjs' )
@@ -2335,6 +2355,11 @@ class Title implements LinkTarget {
                                        && !$user->isAllowed( 'editusercss' )
                                ) {
                                        $errors[] = [ 'customcssprotected', $action ];
+                               } elseif (
+                                       $this->isUserJsonConfigPage()
+                                       && !$user->isAllowed( 'edituserjson' )
+                               ) {
+                                       $errors[] = [ 'customjsonprotected', $action ];
                                } elseif (
                                        $this->isUserJsConfigPage()
                                        && !$user->isAllowed( 'edituserjs' )
@@ -3810,6 +3835,8 @@ class Title implements LinkTarget {
                // If we are looking at a css/js user subpage, purge the action=raw.
                if ( $this->isUserJsConfigPage() ) {
                        $urls[] = $this->getInternalURL( 'action=raw&ctype=text/javascript' );
+               } elseif ( $this->isUserJsonConfigPage() ) {
+                       $urls[] = $this->getInternalURL( 'action=raw&ctype=application/json' );
                } elseif ( $this->isUserCssConfigPage() ) {
                        $urls[] = $this->getInternalURL( 'action=raw&ctype=text/css' );
                }
@@ -4767,14 +4794,12 @@ class Title implements LinkTarget {
         */
        public function getNamespaceKey( $prepend = 'nstab-' ) {
                global $wgContLang;
-               // Gets the subject namespace if this title
-               $namespace = MWNamespace::getSubject( $this->getNamespace() );
-               // Checks if canonical namespace name exists for namespace
-               if ( MWNamespace::exists( $this->getNamespace() ) ) {
-                       // Uses canonical namespace name
-                       $namespaceKey = MWNamespace::getCanonicalName( $namespace );
-               } else {
-                       // Uses text of namespace
+               // Gets the subject namespace of this title
+               $subjectNS = MWNamespace::getSubject( $this->getNamespace() );
+               // Prefer canonical namespace name for HTML IDs
+               $namespaceKey = MWNamespace::getCanonicalName( $subjectNS );
+               if ( $namespaceKey === false ) {
+                       // Fallback to localised text
                        $namespaceKey = $this->getSubjectNsText();
                }
                // Makes namespace key lowercase