Noella94 has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/402143 )
Change subject: Breaking down the MassMessage class into three classes ...................................................................... Breaking down the MassMessage class into three classes Second task of Outreachy/15 project (see here: T178431). Devided MassMessage class into MassMessage, MassMessageUrl and MassMessageGetDB Bug: T178215 Change-Id: I374decb1108530561851930c0c2d288ae612ff65 --- M extension.json M includes/ApiQueryMMSites.php M includes/MassMessage.php A includes/MassMessageGetDB.php M includes/MassMessageTargets.php A includes/MassMessageUrl.php M includes/SpecialEditMassMessageList.php M includes/content/MassMessageListContent.php M includes/content/MassMessageListContentHandler.php M includes/job/MassMessageJob.php M tests/phpunit/MassMessageTest.php 11 files changed, 128 insertions(+), 103 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MassMessage refs/changes/43/402143/1 diff --git a/extension.json b/extension.json index 91c79ec..117aaa0 100644 --- a/extension.json +++ b/extension.json @@ -211,6 +211,8 @@ "MediaWiki\\MassMessage\\ApiQueryMMSites": "includes/ApiQueryMMSites.php", "MediaWiki\\MassMessage\\MassMessage": "includes/MassMessage.php", "MediaWiki\\MassMessage\\MassMessageTargets": "includes/MassMessageTargets.php", + "MediaWiki\\MassMessage\\MassMessageGetDB": "includes/MassMessageGetDB.php", + "MediaWiki\\MassMessage\\MassMessageUrl": "includes/MassMessageUrl.php", "MediaWiki\\MassMessage\\SpecialMassMessage": "includes/SpecialMassMessage.php", "MediaWiki\\MassMessage\\SpecialCreateMassMessageList": "includes/SpecialCreateMassMessageList.php", "MediaWiki\\MassMessage\\SpecialEditMassMessageList": "includes/SpecialEditMassMessageList.php", diff --git a/includes/ApiQueryMMSites.php b/includes/ApiQueryMMSites.php index 050d74b..a48d901 100644 --- a/includes/ApiQueryMMSites.php +++ b/includes/ApiQueryMMSites.php @@ -16,7 +16,7 @@ $params = $this->extractRequestParams(); $term = strtolower( $params['term'] ); - $sites = array_keys( MassMessage::getDatabases() ); + $sites = array_keys( MassMessageGetDB::getDatabases() ); sort( $sites ); $matches = []; foreach ( $sites as $site ) { diff --git a/includes/MassMessage.php b/includes/MassMessage.php index 6bedf19..2e46044 100644 --- a/includes/MassMessage.php +++ b/includes/MassMessage.php @@ -26,25 +26,6 @@ class MassMessage { - /** - * Function to follow redirects - * - * @param Title $title - * @return Title|null null if the page is an interwiki redirect - */ - public static function followRedirect( Title $title ) { - if ( !$title->isRedirect() ) { - return $title; - } - $wikipage = WikiPage::factory( $title ); - - $target = $wikipage->followRedirect(); - if ( $target instanceof Title ) { - return $target; - } else { - return null; // Interwiki redirect - } - } /** * Sets up the messenger account for our use if it hasn't been already. @@ -67,70 +48,6 @@ return $user; } - /** - * Returns the basic hostname and port using wfParseUrl - * @param string $url - * @return string - */ - public static function getBaseUrl( $url ) { - static $mapping = []; - - if ( isset( $mapping[$url] ) ) { - return $mapping[$url]; - } - - $parse = wfParseUrl( $url ); - $mapping[$url] = $parse['host']; - if ( isset( $parse['port'] ) ) { - $mapping[$url] .= ':' . $parse['port']; - } - return $mapping[$url]; - } - - /** - * Get a mapping from site domains to database names - * Requires $wgConf to be set up properly - * Tries to read from cache if possible - * @return array - */ - public static function getDatabases() { - global $wgConf, $wgMemc; - static $mapping = null; - if ( $mapping === null ) { - $key = wfGlobalCacheKey( 'massmessage:urltodb' ); - $data = $wgMemc->get( $key ); - if ( $data === false ) { - $dbs = $wgConf->getLocalDatabases(); - $mapping = []; - foreach ( $dbs as $dbname ) { - $url = WikiMap::getWiki( $dbname )->getCanonicalServer(); - $site = self::getBaseUrl( $url ); - $mapping[$site] = $dbname; - } - $wgMemc->set( $key, $mapping, 60 * 60 ); - } else { - $mapping = $data; - } - } - return $mapping; - } - - /** - * Get database name from URL hostname - * @param string $host - * @return string - */ - public static function getDBName( $host ) { - global $wgMassMessageWikiAliases; - $mapping = self::getDatabases(); - if ( isset( $mapping[$host] ) ) { - return $mapping[$host]; - } - if ( isset( $wgMassMessageWikiAliases[$host] ) ) { - return $wgMassMessageWikiAliases[$host]; - } - return null; // Couldn't find anything - } /** * Verify that parser function data is valid and return processed data as an array @@ -147,10 +64,10 @@ $data = [ 'title' => $page, 'site' => trim( $site ) ]; if ( $data['site'] === '' ) { - $data['site'] = self::getBaseUrl( $wgCanonicalServer ); + $data['site'] = MassMessageUrl::getBaseUrl( $wgCanonicalServer ); $data['wiki'] = wfWikiID(); } else { - $data['wiki'] = self::getDBName( $data['site'] ); + $data['wiki'] = MassMessageGetDB::getDBName( $data['site'] ); if ( $data['wiki'] === null ) { return self::parserError( 'massmessage-parse-badurl', $site ); } @@ -274,7 +191,7 @@ return 'massmessage-spamlist-doesnotexist'; } else { // Page exists, follow a redirect if possible - $target = self::followRedirect( $spamlist ); + $target = MassMessageUrl::followRedirect( $spamlist ); if ( $target === null || !$target->exists() ) { return 'massmessage-spamlist-invalid'; // Interwiki redirect or non-existent page. } else { diff --git a/includes/MassMessageGetDB.php b/includes/MassMessageGetDB.php new file mode 100644 index 0000000..bebd0d7 --- /dev/null +++ b/includes/MassMessageGetDB.php @@ -0,0 +1,55 @@ +<?php +namespace MediaWiki\MassMessage; + +use WikiMap; + +class MassMessageGetDB { + + /** + * Get a mapping from site domains to database names + * Requires $wgConf to be set up properly + * Tries to read from cache if possible + * @return array + */ + public static function getDatabases() { + global $wgConf, $wgMemc; + static $mapping = null; + if ( $mapping === null ) { + $key = wfGlobalCacheKey( 'massmessage:urltodb' ); + $data = $wgMemc->get( $key ); + if ( $data === false ) { + $dbs = $wgConf->getLocalDatabases(); + $mapping = []; + foreach ( $dbs as $dbname ) { + $url = WikiMap::getWiki( $dbname )->getCanonicalServer(); + $site = MassMessageUrl::getBaseUrl( $url ); + $mapping[$site] = $dbname; + } + $wgMemc->set( $key, $mapping, 60 * 60 ); + } else { + $mapping = $data; + } + } + return $mapping; + } + + /** + * Get database name from URL hostname + * @param string $host + * @return string + */ + public static function getDBName( $host ) { + global $wgMassMessageWikiAliases; + $mapping = self::getDatabases(); + if ( isset( $mapping[$host] ) ) { + return $mapping[$host]; + } + if ( isset( $wgMassMessageWikiAliases[$host] ) ) { + return $wgMassMessageWikiAliases[$host]; + } + return null; // Couldn't find anything + } + +} + +?> \ No newline at end of file diff --git a/includes/MassMessageTargets.php b/includes/MassMessageTargets.php index c4e1966..495c677 100644 --- a/includes/MassMessageTargets.php +++ b/includes/MassMessageTargets.php @@ -90,7 +90,7 @@ $title = Title::makeTitle( $wgNamespacesToConvert[$title->getNamespace()], $title->getText() ); } - $title = MassMessage::followRedirect( $title ); + $title = MassMessageUrl::followRedirect( $title ); if ( $title === null ) { continue; // Interwiki redirect } @@ -118,7 +118,7 @@ $targets[] = [ 'title' => $member->getPrefixedText(), 'wiki' => wfWikiID(), - 'site' => MassMessage::getBaseUrl( $wgCanonicalServer ), + 'site' => MassMessageUrl::getBaseUrl( $wgCanonicalServer ), ]; } return $targets; @@ -135,9 +135,9 @@ $targets = Revision::newFromTitle( $spamlist )->getContent()->getValidTargets(); foreach ( $targets as &$target ) { if ( array_key_exists( 'site', $target ) ) { - $target['wiki'] = MassMessage::getDBName( $target['site'] ); + $target['wiki'] = MassMessageGetDB::getDBName( $target['site'] ); } else { - $target['site'] = MassMessage::getBaseUrl( $wgCanonicalServer ); + $target['site'] = MassMessageUrl::getBaseUrl( $wgCanonicalServer ); $target['wiki'] = wfWikiId(); } } diff --git a/includes/MassMessageUrl.php b/includes/MassMessageUrl.php new file mode 100644 index 0000000..4569248 --- /dev/null +++ b/includes/MassMessageUrl.php @@ -0,0 +1,51 @@ +<?php +namespace MediaWiki\MassMessage; + +use WikiPage; +use Title; + +class MassMessageUrl { + + /** + * Function to follow redirects + * + * @param Title $title + * @return Title|null null if the page is an interwiki redirect + */ + public static function followRedirect( Title $title ) { + if ( !$title->isRedirect() ) { + return $title; + } + $wikipage = WikiPage::factory( $title ); + + $target = $wikipage->followRedirect(); + if ( $target instanceof Title ) { + return $target; + } else { + return null; // Interwiki redirect + } + } + + /** + * Returns the basic hostname and port using wfParseUrl + * @param string $url + * @return string + */ + public static function getBaseUrl( $url ) { + static $mapping = []; + + if ( isset( $mapping[$url] ) ) { + return $mapping[$url]; + } + + $parse = wfParseUrl( $url ); + $mapping[$url] = $parse['host']; + if ( isset( $parse['port'] ) ) { + $mapping[$url] .= ':' . $parse['port']; + } + return $mapping[$url]; + } + +} + +?> \ No newline at end of file diff --git a/includes/SpecialEditMassMessageList.php b/includes/SpecialEditMassMessageList.php index cfb7451..77e1e28 100644 --- a/includes/SpecialEditMassMessageList.php +++ b/includes/SpecialEditMassMessageList.php @@ -187,7 +187,7 @@ if ( $this->rev ) { // Instructions - if ( $wgAllowGlobalMessaging && count( MassMessage::getDatabases() ) > 1 ) { + if ( $wgAllowGlobalMessaging && count( MassMessageGetDB::getDatabases() ) > 1 ) { $headerKey = 'massmessage-edit-headermulti'; } else { $headerKey = 'massmessage-edit-header'; diff --git a/includes/content/MassMessageListContent.php b/includes/content/MassMessageListContent.php index 5c48c4b..57166a1 100644 --- a/includes/content/MassMessageListContent.php +++ b/includes/content/MassMessageListContent.php @@ -97,7 +97,7 @@ foreach ( $targets as $target ) { if ( !array_key_exists( 'site', $target ) || $wgAllowGlobalMessaging - && MassMessage::getDBName( $target['site'] ) !== null + && MassMessageGetDB::getDBName( $target['site'] ) !== null ) { $validTargets[] = $target; } @@ -120,7 +120,7 @@ } elseif ( strpos( $target['title'], '@' ) !== false ) { // List the site if it'd otherwise be ambiguous $targetStrings[] = $target['title'] . '@' - . MassMessage::getBaseUrl( $wgCanonicalServer ); + . MassMessageUrl::getBaseUrl( $wgCanonicalServer ); } else { $targetStrings[] = $target['title']; } @@ -321,12 +321,12 @@ $html .= Html::element( 'label', [ 'for' => 'mw-massmessage-addtitle' ], wfMessage( 'massmessage-content-addtitle' )->inLanguage( $lang )->text() ); $html .= Html::input( 'title', '', 'text', [ 'id' => 'mw-massmessage-addtitle' ] ); - if ( $wgAllowGlobalMessaging && count( MassMessage::getDatabases() ) > 1 ) { + if ( $wgAllowGlobalMessaging && count( MassMessageGetDB::getDatabases() ) > 1 ) { $html .= Html::element( 'label', [ 'for' => 'mw-massmessage-addsite' ], wfMessage( 'massmessage-content-addsite' )->inLanguage( $lang )->text() ); $html .= Html::input( 'site', '', 'text', [ 'id' => 'mw-massmessage-addsite', - 'placeholder' => MassMessage::getBaseUrl( $wgCanonicalServer ) + 'placeholder' => MassMessageUrl::getBaseUrl( $wgCanonicalServer ) ] ); } $html .= Html::input( 'submit', diff --git a/includes/content/MassMessageListContentHandler.php b/includes/content/MassMessageListContentHandler.php index 1c9f1d4..f5b2014 100644 --- a/includes/content/MassMessageListContentHandler.php +++ b/includes/content/MassMessageListContentHandler.php @@ -157,8 +157,8 @@ $result['title'] = $title->getPrefixedText(); // Use the canonical form. } - if ( $site !== null && $site !== MassMessage::getBaseUrl( $wgCanonicalServer ) ) { - if ( !$wgAllowGlobalMessaging || MassMessage::getDBName( $site ) === null ) { + if ( $site !== null && $site !== MassMessageUrl::getBaseUrl( $wgCanonicalServer ) ) { + if ( !$wgAllowGlobalMessaging || MassMessageGetDB::getDBName( $site ) === null ) { $result['errors'][] = 'invalidsite'; } else { $result['site'] = $site; diff --git a/includes/job/MassMessageJob.php b/includes/job/MassMessageJob.php index add5853..a2ffb4f 100644 --- a/includes/job/MassMessageJob.php +++ b/includes/job/MassMessageJob.php @@ -112,7 +112,7 @@ if ( isset( $wgNamespacesToConvert[$title->getNamespace()] ) ) { $title = Title::makeTitle( $wgNamespacesToConvert[$title->getNamespace()], $title->getText() ); } - $title = MassMessage::followRedirect( $title ) ?: $title; // Try to follow redirects + $title = MassMessageUrl::followRedirect( $title ) ?: $title; // Try to follow redirects if ( !$title->isTalkPage() && !in_array( $title->getNamespace(), $wgNamespacesToPostIn ) ) { $this->logLocalSkip( 'skipbadns' ); $title = null; diff --git a/tests/phpunit/MassMessageTest.php b/tests/phpunit/MassMessageTest.php index 654b8a1..6192f1d 100644 --- a/tests/phpunit/MassMessageTest.php +++ b/tests/phpunit/MassMessageTest.php @@ -27,7 +27,7 @@ * @param string $expected */ public function testGetDBName( $url, $expected ) { - $dbname = MassMessage::getDBName( $url ); + $dbname = MassMessageGetDB::getDBName( $url ); $this->assertEquals( $expected, $dbname ); } @@ -51,7 +51,7 @@ * @param string $expected expected value */ public function testGetBaseUrl( $url, $expected ) { - $output = MassMessage::getBaseUrl( $url ); + $output = MassMessageUrl::getBaseUrl( $url ); $this->assertEquals( $expected, $output ); } @@ -86,11 +86,11 @@ $this->assertEquals( $title2->getFullText(), - MassMessage::followRedirect( $title )->getFullText() + MassMessageUrl::followRedirect( $title )->getFullText() ); $this->assertEquals( $title2->getFullText(), - MassMessage::followRedirect( $title2 )->getFullText() + MassMessageUrl::followRedirect( $title2 )->getFullText() ); } } -- To view, visit https://gerrit.wikimedia.org/r/402143 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I374decb1108530561851930c0c2d288ae612ff65 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/MassMessage Gerrit-Branch: master Gerrit-Owner: Noella94 <tekenoell...@gmail.com> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits