Setup: Move wgActionPath logic to PathRouter
authorTimo Tijhof <krinklemail@gmail.com>
Sat, 31 Aug 2019 22:05:50 +0000 (23:05 +0100)
committerKrinkle <krinklemail@gmail.com>
Wed, 4 Sep 2019 20:29:03 +0000 (20:29 +0000)
This is only relevant when processing page views or when constructing
Title urls with an 'action' query. Pretty important stuff, and worth
optimising for if we had to choose, but we can defer it in this case
without slowing it down, which is better for everything else.

It also means we don't mutate configuration (beyond setting whole values
as dynamic defaults), which seems desirable, and makes the overall behaviour
easier to test. Handling absence of 'view' should be PathRouter's
responsibility, not Setup.

Bug: T189966
Change-Id: I9c1eea2dcea74be0e283eb2b175268315ced1793

includes/PathRouter.php
includes/Setup.php
includes/Title.php
includes/WebRequest.php

index 2882e66..4d7bd38 100644 (file)
@@ -401,4 +401,22 @@ class PathRouter {
 
                return $value;
        }
+
+       /**
+        * @internal For use by Title and WebRequest only.
+        * @param array $actionPaths
+        * @param string $articlePath
+        * @return string[]|false
+        */
+       public static function getActionPaths( array $actionPaths, $articlePath ) {
+               if ( !$actionPaths ) {
+                       return false;
+               }
+               // Processing of urls for this feature requires that 'view' is set.
+               // By default, set it to the pretty article path.
+               if ( !isset( $actionPaths['view'] ) ) {
+                       $actionPaths['view'] = $articlePath;
+               }
+               return $actionPaths;
+       }
 }
index cc9a3f9..b5540d4 100644 (file)
@@ -156,12 +156,6 @@ if ( $wgArticlePath === false ) {
        }
 }
 
-if ( !empty( $wgActionPaths ) && !isset( $wgActionPaths['view'] ) ) {
-       // 'view' is assumed the default action path everywhere in the code
-       // but is rarely filled in $wgActionPaths
-       $wgActionPaths['view'] = $wgArticlePath;
-}
-
 if ( $wgResourceBasePath === null ) {
        $wgResourceBasePath = $wgScriptPath;
 }
index 547b28c..b641448 100644 (file)
@@ -2068,16 +2068,18 @@ class Title implements LinkTarget, IDBAccessObject {
                                $url = false;
                                $matches = [];
 
-                               if ( !empty( $wgActionPaths )
+                               $articlePaths = PathRouter::getActionPaths( $wgActionPaths, $wgArticlePath );
+
+                               if ( $articlePaths
                                        && preg_match( '/^(.*&|)action=([^&]*)(&(.*)|)$/', $query, $matches )
                                ) {
                                        $action = urldecode( $matches[2] );
-                                       if ( isset( $wgActionPaths[$action] ) ) {
+                                       if ( isset( $articlePaths[$action] ) ) {
                                                $query = $matches[1];
                                                if ( isset( $matches[4] ) ) {
                                                        $query .= $matches[4];
                                                }
-                                               $url = str_replace( '$1', $dbkey, $wgActionPaths[$action] );
+                                               $url = str_replace( '$1', $dbkey, $articlePaths[$action] );
                                                if ( $query != '' ) {
                                                        $url = wfAppendQuery( $url, $query );
                                                }
index 9b8f5a6..7b14667 100644 (file)
@@ -162,8 +162,9 @@ class WebRequest {
                        }
 
                        global $wgActionPaths;
-                       if ( $wgActionPaths ) {
-                               $router->add( $wgActionPaths, [ 'action' => '$key' ] );
+                       $articlePaths = PathRouter::getActionPaths( $wgActionPaths, $wgArticlePath );
+                       if ( $articlePaths ) {
+                               $router->add( $articlePaths, [ 'action' => '$key' ] );
                        }
 
                        global $wgVariantArticlePath;