2014-06-26 14:29:31 +00:00
|
|
|
<?php
|
|
|
|
|
/**
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
*/
|
|
|
|
|
|
2022-05-06 09:09:56 +00:00
|
|
|
namespace MediaWiki\ResourceLoader;
|
|
|
|
|
|
resourceloader: Tweak RL\FilePath handling in packageFiles
ResourceLoader's FilePath is designed to allow a FileModule
to include files that exist outside of the module's localBasePath,
to allow skins and extensions to extend core MediaWiki modules.
This is accomplished by having the FileModule use the FilePath's
localBasePath instead, in FileModule::getLocalPath.
(Similarly for remoteBasePath, but these are going out of fashion.)
However, the code processing 'packageFiles' did not handle this right:
it used FilePath's localBasePath when it appeared as a 'file',
but not when it was returned by a 'callback' or 'versionCallback',
using the FileModule's localBasePath instead in that case. Most
existing uses of FilePath in 'packageFiles' required the same base
path as the module, so it was convenient to avoid providing it twice.
To keep that convenience (already relied on by some extensions too)
while also allowing skins and extensions to add files from their own
directories to existing modules, the code processing 'packageFiles'
now uses FilePath's base paths in all cases, but they are optional,
and will fall back to the FileModule's paths when not provided.
Follow-up to 2890bca27db3636348b1a052e9e4d23a6bb1e272.
Change-Id: I38a0761ae4633a567b685b52c1d73b6ce280ffb7
2022-05-24 00:42:21 +00:00
|
|
|
use RuntimeException;
|
|
|
|
|
|
2014-06-26 14:29:31 +00:00
|
|
|
/**
|
2022-05-27 23:15:55 +00:00
|
|
|
* A path to a bundled file (such as JavaScript or CSS), along with a remote and local base path.
|
resourceloader: Tweak RL\FilePath handling in packageFiles
ResourceLoader's FilePath is designed to allow a FileModule
to include files that exist outside of the module's localBasePath,
to allow skins and extensions to extend core MediaWiki modules.
This is accomplished by having the FileModule use the FilePath's
localBasePath instead, in FileModule::getLocalPath.
(Similarly for remoteBasePath, but these are going out of fashion.)
However, the code processing 'packageFiles' did not handle this right:
it used FilePath's localBasePath when it appeared as a 'file',
but not when it was returned by a 'callback' or 'versionCallback',
using the FileModule's localBasePath instead in that case. Most
existing uses of FilePath in 'packageFiles' required the same base
path as the module, so it was convenient to avoid providing it twice.
To keep that convenience (already relied on by some extensions too)
while also allowing skins and extensions to add files from their own
directories to existing modules, the code processing 'packageFiles'
now uses FilePath's base paths in all cases, but they are optional,
and will fall back to the FileModule's paths when not provided.
Follow-up to 2890bca27db3636348b1a052e9e4d23a6bb1e272.
Change-Id: I38a0761ae4633a567b685b52c1d73b6ce280ffb7
2022-05-24 00:42:21 +00:00
|
|
|
*
|
2022-05-27 23:15:55 +00:00
|
|
|
* This is for use with FileModule. Base path may be `null`, which indicates that the
|
|
|
|
|
* path is expanded relative to the corresponding base path of the FileModule object instead.
|
2019-09-14 04:32:54 +00:00
|
|
|
*
|
|
|
|
|
* @ingroup ResourceLoader
|
|
|
|
|
* @since 1.17
|
2014-06-26 14:29:31 +00:00
|
|
|
*/
|
2022-05-06 09:09:56 +00:00
|
|
|
class FilePath {
|
resourceloader: Tweak RL\FilePath handling in packageFiles
ResourceLoader's FilePath is designed to allow a FileModule
to include files that exist outside of the module's localBasePath,
to allow skins and extensions to extend core MediaWiki modules.
This is accomplished by having the FileModule use the FilePath's
localBasePath instead, in FileModule::getLocalPath.
(Similarly for remoteBasePath, but these are going out of fashion.)
However, the code processing 'packageFiles' did not handle this right:
it used FilePath's localBasePath when it appeared as a 'file',
but not when it was returned by a 'callback' or 'versionCallback',
using the FileModule's localBasePath instead in that case. Most
existing uses of FilePath in 'packageFiles' required the same base
path as the module, so it was convenient to avoid providing it twice.
To keep that convenience (already relied on by some extensions too)
while also allowing skins and extensions to add files from their own
directories to existing modules, the code processing 'packageFiles'
now uses FilePath's base paths in all cases, but they are optional,
and will fall back to the FileModule's paths when not provided.
Follow-up to 2890bca27db3636348b1a052e9e4d23a6bb1e272.
Change-Id: I38a0761ae4633a567b685b52c1d73b6ce280ffb7
2022-05-24 00:42:21 +00:00
|
|
|
/** @var string|null Local base path */
|
2022-12-29 16:56:06 +00:00
|
|
|
protected ?string $localBasePath;
|
2014-06-26 14:29:31 +00:00
|
|
|
|
resourceloader: Tweak RL\FilePath handling in packageFiles
ResourceLoader's FilePath is designed to allow a FileModule
to include files that exist outside of the module's localBasePath,
to allow skins and extensions to extend core MediaWiki modules.
This is accomplished by having the FileModule use the FilePath's
localBasePath instead, in FileModule::getLocalPath.
(Similarly for remoteBasePath, but these are going out of fashion.)
However, the code processing 'packageFiles' did not handle this right:
it used FilePath's localBasePath when it appeared as a 'file',
but not when it was returned by a 'callback' or 'versionCallback',
using the FileModule's localBasePath instead in that case. Most
existing uses of FilePath in 'packageFiles' required the same base
path as the module, so it was convenient to avoid providing it twice.
To keep that convenience (already relied on by some extensions too)
while also allowing skins and extensions to add files from their own
directories to existing modules, the code processing 'packageFiles'
now uses FilePath's base paths in all cases, but they are optional,
and will fall back to the FileModule's paths when not provided.
Follow-up to 2890bca27db3636348b1a052e9e4d23a6bb1e272.
Change-Id: I38a0761ae4633a567b685b52c1d73b6ce280ffb7
2022-05-24 00:42:21 +00:00
|
|
|
/** @var string|null Remote base path */
|
2022-12-29 16:56:06 +00:00
|
|
|
protected ?string $remoteBasePath;
|
2014-06-26 14:29:31 +00:00
|
|
|
|
2020-06-14 18:40:02 +00:00
|
|
|
/** @var string Path to the file */
|
2022-12-29 16:56:06 +00:00
|
|
|
protected string $path;
|
2014-06-26 14:29:31 +00:00
|
|
|
|
|
|
|
|
/**
|
2021-06-08 23:34:03 +00:00
|
|
|
* @param string $path Relative path to the file, no leading slash.
|
resourceloader: Tweak RL\FilePath handling in packageFiles
ResourceLoader's FilePath is designed to allow a FileModule
to include files that exist outside of the module's localBasePath,
to allow skins and extensions to extend core MediaWiki modules.
This is accomplished by having the FileModule use the FilePath's
localBasePath instead, in FileModule::getLocalPath.
(Similarly for remoteBasePath, but these are going out of fashion.)
However, the code processing 'packageFiles' did not handle this right:
it used FilePath's localBasePath when it appeared as a 'file',
but not when it was returned by a 'callback' or 'versionCallback',
using the FileModule's localBasePath instead in that case. Most
existing uses of FilePath in 'packageFiles' required the same base
path as the module, so it was convenient to avoid providing it twice.
To keep that convenience (already relied on by some extensions too)
while also allowing skins and extensions to add files from their own
directories to existing modules, the code processing 'packageFiles'
now uses FilePath's base paths in all cases, but they are optional,
and will fall back to the FileModule's paths when not provided.
Follow-up to 2890bca27db3636348b1a052e9e4d23a6bb1e272.
Change-Id: I38a0761ae4633a567b685b52c1d73b6ce280ffb7
2022-05-24 00:42:21 +00:00
|
|
|
* @param string|null $localBasePath Base path to prepend when generating a local path.
|
|
|
|
|
* @param string|null $remoteBasePath Base path to prepend when generating a remote path.
|
2021-06-08 23:34:03 +00:00
|
|
|
* Should not have a trailing slash unless at web document root.
|
2014-06-26 14:29:31 +00:00
|
|
|
*/
|
2022-12-29 16:56:06 +00:00
|
|
|
public function __construct( string $path, ?string $localBasePath = null, ?string $remoteBasePath = null ) {
|
2014-06-26 14:29:31 +00:00
|
|
|
$this->path = $path;
|
|
|
|
|
$this->localBasePath = $localBasePath;
|
|
|
|
|
$this->remoteBasePath = $remoteBasePath;
|
|
|
|
|
}
|
|
|
|
|
|
resourceloader: Tweak RL\FilePath handling in packageFiles
ResourceLoader's FilePath is designed to allow a FileModule
to include files that exist outside of the module's localBasePath,
to allow skins and extensions to extend core MediaWiki modules.
This is accomplished by having the FileModule use the FilePath's
localBasePath instead, in FileModule::getLocalPath.
(Similarly for remoteBasePath, but these are going out of fashion.)
However, the code processing 'packageFiles' did not handle this right:
it used FilePath's localBasePath when it appeared as a 'file',
but not when it was returned by a 'callback' or 'versionCallback',
using the FileModule's localBasePath instead in that case. Most
existing uses of FilePath in 'packageFiles' required the same base
path as the module, so it was convenient to avoid providing it twice.
To keep that convenience (already relied on by some extensions too)
while also allowing skins and extensions to add files from their own
directories to existing modules, the code processing 'packageFiles'
now uses FilePath's base paths in all cases, but they are optional,
and will fall back to the FileModule's paths when not provided.
Follow-up to 2890bca27db3636348b1a052e9e4d23a6bb1e272.
Change-Id: I38a0761ae4633a567b685b52c1d73b6ce280ffb7
2022-05-24 00:42:21 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
* @throws RuntimeException If the base path was not provided. You must either provide the base
|
|
|
|
|
* path in the constructor, or use getPath() instead and add the base path from a FileModule.
|
|
|
|
|
*/
|
2022-12-29 16:56:06 +00:00
|
|
|
public function getLocalPath(): string {
|
resourceloader: Tweak RL\FilePath handling in packageFiles
ResourceLoader's FilePath is designed to allow a FileModule
to include files that exist outside of the module's localBasePath,
to allow skins and extensions to extend core MediaWiki modules.
This is accomplished by having the FileModule use the FilePath's
localBasePath instead, in FileModule::getLocalPath.
(Similarly for remoteBasePath, but these are going out of fashion.)
However, the code processing 'packageFiles' did not handle this right:
it used FilePath's localBasePath when it appeared as a 'file',
but not when it was returned by a 'callback' or 'versionCallback',
using the FileModule's localBasePath instead in that case. Most
existing uses of FilePath in 'packageFiles' required the same base
path as the module, so it was convenient to avoid providing it twice.
To keep that convenience (already relied on by some extensions too)
while also allowing skins and extensions to add files from their own
directories to existing modules, the code processing 'packageFiles'
now uses FilePath's base paths in all cases, but they are optional,
and will fall back to the FileModule's paths when not provided.
Follow-up to 2890bca27db3636348b1a052e9e4d23a6bb1e272.
Change-Id: I38a0761ae4633a567b685b52c1d73b6ce280ffb7
2022-05-24 00:42:21 +00:00
|
|
|
if ( $this->localBasePath === null ) {
|
|
|
|
|
throw new RuntimeException( 'Base path was not provided' );
|
|
|
|
|
}
|
|
|
|
|
return "{$this->localBasePath}/{$this->path}";
|
2014-06-26 14:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
resourceloader: Tweak RL\FilePath handling in packageFiles
ResourceLoader's FilePath is designed to allow a FileModule
to include files that exist outside of the module's localBasePath,
to allow skins and extensions to extend core MediaWiki modules.
This is accomplished by having the FileModule use the FilePath's
localBasePath instead, in FileModule::getLocalPath.
(Similarly for remoteBasePath, but these are going out of fashion.)
However, the code processing 'packageFiles' did not handle this right:
it used FilePath's localBasePath when it appeared as a 'file',
but not when it was returned by a 'callback' or 'versionCallback',
using the FileModule's localBasePath instead in that case. Most
existing uses of FilePath in 'packageFiles' required the same base
path as the module, so it was convenient to avoid providing it twice.
To keep that convenience (already relied on by some extensions too)
while also allowing skins and extensions to add files from their own
directories to existing modules, the code processing 'packageFiles'
now uses FilePath's base paths in all cases, but they are optional,
and will fall back to the FileModule's paths when not provided.
Follow-up to 2890bca27db3636348b1a052e9e4d23a6bb1e272.
Change-Id: I38a0761ae4633a567b685b52c1d73b6ce280ffb7
2022-05-24 00:42:21 +00:00
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
* @throws RuntimeException If the base path was not provided. You must either provide the base
|
|
|
|
|
* path in the constructor, or use getPath() instead and add the base path from a FileModule.
|
|
|
|
|
*/
|
2022-12-29 16:56:06 +00:00
|
|
|
public function getRemotePath(): string {
|
resourceloader: Tweak RL\FilePath handling in packageFiles
ResourceLoader's FilePath is designed to allow a FileModule
to include files that exist outside of the module's localBasePath,
to allow skins and extensions to extend core MediaWiki modules.
This is accomplished by having the FileModule use the FilePath's
localBasePath instead, in FileModule::getLocalPath.
(Similarly for remoteBasePath, but these are going out of fashion.)
However, the code processing 'packageFiles' did not handle this right:
it used FilePath's localBasePath when it appeared as a 'file',
but not when it was returned by a 'callback' or 'versionCallback',
using the FileModule's localBasePath instead in that case. Most
existing uses of FilePath in 'packageFiles' required the same base
path as the module, so it was convenient to avoid providing it twice.
To keep that convenience (already relied on by some extensions too)
while also allowing skins and extensions to add files from their own
directories to existing modules, the code processing 'packageFiles'
now uses FilePath's base paths in all cases, but they are optional,
and will fall back to the FileModule's paths when not provided.
Follow-up to 2890bca27db3636348b1a052e9e4d23a6bb1e272.
Change-Id: I38a0761ae4633a567b685b52c1d73b6ce280ffb7
2022-05-24 00:42:21 +00:00
|
|
|
if ( $this->remoteBasePath === null ) {
|
|
|
|
|
throw new RuntimeException( 'Base path was not provided' );
|
2021-06-08 23:34:03 +00:00
|
|
|
}
|
|
|
|
|
if ( $this->remoteBasePath === '/' ) {
|
|
|
|
|
// In document root
|
|
|
|
|
// Don't insert another slash (T284391).
|
|
|
|
|
return $this->remoteBasePath . $this->path;
|
|
|
|
|
}
|
|
|
|
|
return "{$this->remoteBasePath}/{$this->path}";
|
2014-06-26 14:29:31 +00:00
|
|
|
}
|
|
|
|
|
|
resourceloader: Tweak RL\FilePath handling in packageFiles
ResourceLoader's FilePath is designed to allow a FileModule
to include files that exist outside of the module's localBasePath,
to allow skins and extensions to extend core MediaWiki modules.
This is accomplished by having the FileModule use the FilePath's
localBasePath instead, in FileModule::getLocalPath.
(Similarly for remoteBasePath, but these are going out of fashion.)
However, the code processing 'packageFiles' did not handle this right:
it used FilePath's localBasePath when it appeared as a 'file',
but not when it was returned by a 'callback' or 'versionCallback',
using the FileModule's localBasePath instead in that case. Most
existing uses of FilePath in 'packageFiles' required the same base
path as the module, so it was convenient to avoid providing it twice.
To keep that convenience (already relied on by some extensions too)
while also allowing skins and extensions to add files from their own
directories to existing modules, the code processing 'packageFiles'
now uses FilePath's base paths in all cases, but they are optional,
and will fall back to the FileModule's paths when not provided.
Follow-up to 2890bca27db3636348b1a052e9e4d23a6bb1e272.
Change-Id: I38a0761ae4633a567b685b52c1d73b6ce280ffb7
2022-05-24 00:42:21 +00:00
|
|
|
/** @return string|null */
|
2022-12-29 16:56:06 +00:00
|
|
|
public function getLocalBasePath(): ?string {
|
2017-03-17 02:14:05 +00:00
|
|
|
return $this->localBasePath;
|
|
|
|
|
}
|
|
|
|
|
|
resourceloader: Tweak RL\FilePath handling in packageFiles
ResourceLoader's FilePath is designed to allow a FileModule
to include files that exist outside of the module's localBasePath,
to allow skins and extensions to extend core MediaWiki modules.
This is accomplished by having the FileModule use the FilePath's
localBasePath instead, in FileModule::getLocalPath.
(Similarly for remoteBasePath, but these are going out of fashion.)
However, the code processing 'packageFiles' did not handle this right:
it used FilePath's localBasePath when it appeared as a 'file',
but not when it was returned by a 'callback' or 'versionCallback',
using the FileModule's localBasePath instead in that case. Most
existing uses of FilePath in 'packageFiles' required the same base
path as the module, so it was convenient to avoid providing it twice.
To keep that convenience (already relied on by some extensions too)
while also allowing skins and extensions to add files from their own
directories to existing modules, the code processing 'packageFiles'
now uses FilePath's base paths in all cases, but they are optional,
and will fall back to the FileModule's paths when not provided.
Follow-up to 2890bca27db3636348b1a052e9e4d23a6bb1e272.
Change-Id: I38a0761ae4633a567b685b52c1d73b6ce280ffb7
2022-05-24 00:42:21 +00:00
|
|
|
/** @return string|null */
|
2022-12-29 16:56:06 +00:00
|
|
|
public function getRemoteBasePath(): ?string {
|
2017-03-17 02:14:05 +00:00
|
|
|
return $this->remoteBasePath;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 18:40:02 +00:00
|
|
|
/** @return string */
|
2022-12-29 16:56:06 +00:00
|
|
|
public function getPath(): string {
|
2014-06-26 14:29:31 +00:00
|
|
|
return $this->path;
|
|
|
|
|
}
|
2023-03-29 10:27:03 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set the base path if it has not already been set.
|
|
|
|
|
*
|
|
|
|
|
* @param string $localBasePath
|
|
|
|
|
* @param string $remoteBasePath
|
|
|
|
|
*/
|
|
|
|
|
public function initBasePaths( string $localBasePath, string $remoteBasePath ) {
|
|
|
|
|
if ( $this->localBasePath === null ) {
|
|
|
|
|
$this->localBasePath = $localBasePath;
|
|
|
|
|
}
|
|
|
|
|
if ( $this->remoteBasePath === null ) {
|
|
|
|
|
$this->remoteBasePath = $remoteBasePath;
|
|
|
|
|
}
|
|
|
|
|
}
|
2014-06-26 14:29:31 +00:00
|
|
|
}
|