diff options
author | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
---|---|---|
committer | Yaco <franco@reevo.org> | 2020-06-04 11:01:00 -0300 |
commit | fc7369835258467bf97eb64f184b93691f9a9fd5 (patch) | |
tree | daabd60089d2dd76d9f5fb416b005fbe159c799d /www/wiki/includes/composer |
first commit
Diffstat (limited to 'www/wiki/includes/composer')
4 files changed, 208 insertions, 0 deletions
diff --git a/www/wiki/includes/composer/ComposerHookHandler.php b/www/wiki/includes/composer/ComposerHookHandler.php new file mode 100644 index 00000000..a1943be5 --- /dev/null +++ b/www/wiki/includes/composer/ComposerHookHandler.php @@ -0,0 +1,37 @@ +<?php + +use Composer\Package\Package; +use Composer\Script\Event; + +$GLOBALS['IP'] = __DIR__ . '/../../'; +require_once __DIR__ . '/../AutoLoader.php'; + +/** + * @license GNU GPL v2+ + * @author Jeroen De Dauw < jeroendedauw@gmail.com > + */ +class ComposerHookHandler { + + public static function onPreUpdate( Event $event ) { + self::handleChangeEvent( $event ); + } + + public static function onPreInstall( Event $event ) { + self::handleChangeEvent( $event ); + } + + private static function handleChangeEvent( Event $event ) { + $package = $event->getComposer()->getPackage(); + + if ( $package instanceof Package ) { + $packageModifier = new ComposerPackageModifier( + $package, + new ComposerVersionNormalizer(), + new MediaWikiVersionFetcher() + ); + + $packageModifier->setProvidesMediaWiki(); + } + } + +} diff --git a/www/wiki/includes/composer/ComposerPackageModifier.php b/www/wiki/includes/composer/ComposerPackageModifier.php new file mode 100644 index 00000000..168336b2 --- /dev/null +++ b/www/wiki/includes/composer/ComposerPackageModifier.php @@ -0,0 +1,62 @@ +<?php + +use Composer\Package\Link; +use Composer\Package\Package; +use Composer\Semver\Constraint\Constraint; + +/** + * @license GNU GPL v2+ + * @author Jeroen De Dauw < jeroendedauw@gmail.com > + */ +class ComposerPackageModifier { + + const MEDIAWIKI_PACKAGE_NAME = 'mediawiki/mediawiki'; + + protected $package; + protected $versionNormalizer; + protected $versionFetcher; + + public function __construct( Package $package, + ComposerVersionNormalizer $versionNormalizer, MediaWikiVersionFetcher $versionFetcher + ) { + $this->package = $package; + $this->versionNormalizer = $versionNormalizer; + $this->versionFetcher = $versionFetcher; + } + + public function setProvidesMediaWiki() { + $this->setLinkAsProvides( $this->newMediaWikiLink() ); + } + + private function setLinkAsProvides( Link $link ) { + $this->package->setProvides( [ $link ] ); + } + + private function newMediaWikiLink() { + $version = $this->getMediaWikiVersionConstraint(); + + $link = new Link( + '__root__', + self::MEDIAWIKI_PACKAGE_NAME, + $version, + 'provides', + $version->getPrettyString() + ); + + return $link; + } + + private function getMediaWikiVersionConstraint() { + $mvVersion = $this->versionFetcher->fetchVersion(); + $mvVersion = $this->versionNormalizer->normalizeSuffix( $mvVersion ); + + $version = new Constraint( + '==', + $this->versionNormalizer->normalizeLevelCount( $mvVersion ) + ); + $version->setPrettyString( $mvVersion ); + + return $version; + } + +} diff --git a/www/wiki/includes/composer/ComposerVendorHtaccessCreator.php b/www/wiki/includes/composer/ComposerVendorHtaccessCreator.php new file mode 100644 index 00000000..1e5efdf1 --- /dev/null +++ b/www/wiki/includes/composer/ComposerVendorHtaccessCreator.php @@ -0,0 +1,43 @@ +<?php +/** + * Copyright (C) 2017 Kunal Mehta <legoktm@member.fsf.org> + * + * 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. + * + */ + +/** + * Creates a .htaccess in the vendor/ directory + * to prevent web access. + * + * This class runs *outside* of the normal MediaWiki + * environment and cannot depend upon any MediaWiki + * code. + */ +class ComposerVendorHtaccessCreator { + + /** + * Handle post-install-cmd and post-update-cmd hooks + */ + public static function onEvent() { + $fname = dirname( dirname( __DIR__ ) ) . "/vendor/.htaccess"; + if ( file_exists( $fname ) ) { + // Already exists + return; + } + + file_put_contents( $fname, "Deny from all\n" ); + } +} diff --git a/www/wiki/includes/composer/ComposerVersionNormalizer.php b/www/wiki/includes/composer/ComposerVersionNormalizer.php new file mode 100644 index 00000000..2194bede --- /dev/null +++ b/www/wiki/includes/composer/ComposerVersionNormalizer.php @@ -0,0 +1,66 @@ +<?php + +/** + * @license GNU GPL v2+ + * @author Jeroen De Dauw < jeroendedauw@gmail.com > + */ +class ComposerVersionNormalizer { + + /** + * Ensures there is a dash in between the version and the stability suffix. + * + * Examples: + * - 1.23RC => 1.23-RC + * - 1.23alpha => 1.23-alpha + * - 1.23alpha3 => 1.23-alpha3 + * - 1.23-beta => 1.23-beta + * + * @param string $version + * + * @return string + * @throws InvalidArgumentException + */ + public function normalizeSuffix( $version ) { + if ( !is_string( $version ) ) { + throw new InvalidArgumentException( '$version must be a string' ); + } + + return preg_replace( '/^(\d[\d\.]*)([a-zA-Z]+)(\d*)$/', '$1-$2$3', $version, 1 ); + } + + /** + * Ensures the version has four levels. + * Version suffixes are supported, as long as they start with a dash. + * + * Examples: + * - 1.19 => 1.19.0.0 + * - 1.19.2.3 => 1.19.2.3 + * - 1.19-alpha => 1.19.0.0-alpha + * - 1337 => 1337.0.0.0 + * + * @param string $version + * + * @return string + * @throws InvalidArgumentException + */ + public function normalizeLevelCount( $version ) { + if ( !is_string( $version ) ) { + throw new InvalidArgumentException( '$version must be a string' ); + } + + $dashPosition = strpos( $version, '-' ); + + if ( $dashPosition !== false ) { + $suffix = substr( $version, $dashPosition ); + $version = substr( $version, 0, $dashPosition ); + } + + $version = implode( '.', array_pad( explode( '.', $version ), 4, '0' ) ); + + if ( $dashPosition !== false ) { + $version .= $suffix; + } + + return $version; + } +} |