Mainframe98 has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/331335 )
Change subject: Initial Commit ...................................................................... Initial Commit This commit adds the first version of the PageLanguageApi extension, based on the MediaWiki Api page API:PageLanguage, which was written for an patch that was never merged. Change-Id: I4564f03822cecc43ea4d7c9db7ead7c04759c92f --- A COPYING A LICENSE A PageLanguageApi.php A README A extension.json A i18n/en.json A i18n/nl.json A i18n/qqq.json A includes/api/PageLanguageApi.php 9 files changed, 376 insertions(+), 0 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/PageLanguageApi refs/changes/35/331335/1 diff --git a/COPYING b/COPYING new file mode 100644 index 0000000..8ab948f --- /dev/null +++ b/COPYING @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 Klaas Skelte van der Werf + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..8ab948f --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2016 Klaas Skelte van der Werf + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/PageLanguageApi.php b/PageLanguageApi.php new file mode 100644 index 0000000..c539814 --- /dev/null +++ b/PageLanguageApi.php @@ -0,0 +1,33 @@ +<?php +/** + * Created using PhpStorm. + * + * Documentation for the extension can be found on https://www.mediawiki.org/wiki/Extension:PageLanguageAPI + * Documentation for the API can be found on https://www.mediawiki.org/wiki/API:PageLanguage + * + * @file + * @ingroup Extensions + * @author Klaas Skelte van der Werf(Mainframe98), 2016 + * @version 0.0.1.0 + * @license MIT + */ +/* + * This file is kept for 1.24 backwards compatibility. 1.25+ uses extension.json + * Editors are encouraged to edit that file instead and ensure that this file is updated accordingly + */ + +$wgExtensionCredits['api'][] = array( + 'path' => __FILE__, + 'name' => 'PageLanguageApi', + 'author' => 'Klaas Skelte van der Werf', + 'url' => 'https://www.mediawiki.org/wiki/Extension:PageLanguageApi', + 'descriptionmsg' => 'pagelanguageapi-desc', + 'version' => '1.0.0.0', + 'license-name' => "MIT", + 'type' => 'api' +); +/* Setup */ +$wgMessagesDirs['PageLanguageApi'] = __DIR__ . '/i18n'; + +$wgAutoloadClasses['PageLanguageApi'] = __DIR__ . '/includes/api/PageLanguageApi.php'; +$wgAPIModules['pagelang'] = 'PageLanguageApi'; \ No newline at end of file diff --git a/README b/README new file mode 100644 index 0000000..30d66cc --- /dev/null +++ b/README @@ -0,0 +1,23 @@ +== Installation instructions == +Note: you can also find these on https://www.mediawiki.org/wiki/Extension:PageLanguageApi + +To use this extension, you need three things: +* $wgPageLanguageUseDB needs to be set to true, which is set to default to false +* The 'pagelang' must be assigned to any group. DefaultSettings.php recommends sysops +* Finally, you need to add wfLoadExtension( 'PageLanguageApi' ); to your LocalSettings.php file +* If you are using 1.24, add require_once "$IP/extensions/PageLanguageApi/PageLanguageApi.php"; +to your LocalSettings.php file instead + +== Usage == +This is in part available on https://www.mediawiki.org/wiki/API:PageLanguage, but you are recommended to +navigate to https://www.mediawiki.org/wiki/Extension:PageLanguageApi/API instead for correct documentation +as the former was written for a patch that never was merged. + +Anyway, below is a brief summary: + +There are four parameters used by this module, which is accessible by using action=pagelang: +# titles - A list of titles, separated by a pipe (|) or U+001F for 1.28 +# pageids - A list of pageids, separated by a pipe (|) or U+001F for 1.28 +# lang - The language code to change the new pages to. This parameter can be omitted to indicate that +the wikis default language should be used +# token - A csrf token \ No newline at end of file diff --git a/extension.json b/extension.json new file mode 100644 index 0000000..1c8bbc0 --- /dev/null +++ b/extension.json @@ -0,0 +1,25 @@ +{ + "name": "PageLanguageApi", + "namemsg": "pagelanguageapi-extensionname", + "version": "1.0.0.0", + "author": "Klaas Skelte van der Werf", + "url": "https://www.mediawiki.org/wiki/Extension:PageLanguageApi", + "descriptionmsg": "pagelanguageapi-desc", + "license-name": "MIT", + "type": "api", + "APIModules": { + "pagelang": "PageLanguageApi" + }, + "MessagesDirs": { + "PageLanguageApi": [ + "i18n" + ] + }, + "AutoloadClasses": { + "PageLanguageApi": "includes/api/PageLanguageApi.php" + }, + "manifest_version": 1, + "requires": { + "MediaWiki": ">= 1.24.0" + } +} \ No newline at end of file diff --git a/i18n/en.json b/i18n/en.json new file mode 100644 index 0000000..5088b21 --- /dev/null +++ b/i18n/en.json @@ -0,0 +1,12 @@ +{ + "pagelanguageapi-desc": "Allows to change the page language through the API", + "pagelanguageapi-extensionname": "PageLanguageApi", + "apisampleoutput-pagelang": "Change the page language for the page Main Page to Frisian with the token 123ABC+\\", + "apihelp-pagelang-description": "Change the page language for a given set of pages.", + "apihelp-pagelang-param-titles": "A list of titles to work on.", + "apihelp-pagelang-param-pageids": "A list of page IDs to work on.", + "apihelp-pagelang-param-lang": "The new language code. Omit for the wikis default language.", + "pagelanguageapi-disabled": "The pagelang module has been disabled.", + "pagelanguageapi-permissiondenied": "Permission denied.", + "badlanguage-code": "Invalid language code." +} \ No newline at end of file diff --git a/i18n/nl.json b/i18n/nl.json new file mode 100644 index 0000000..dae14bd --- /dev/null +++ b/i18n/nl.json @@ -0,0 +1,12 @@ +{ + "pagelanguageapi-desc": "Bied de mogelijkheid om de paginataal aan te passen door middel van de API", + "pagelanguageapi-extensionname": "PaginaTaalApi", + "apisampleoutput-pagelang": "Wijzig de paginataal voor de pagina Main Page naar Fries met het token 123ABC+\\", + "apihelp-pagelang-description": "Wijzig de paginataal voor een gegeven aantal pagina's.", + "apihelp-pagelang-param-titles": "Een lijst met titels.", + "apihelp-pagelang-param-pageids": "Een lijst met pagina IDs.", + "apihelp-pagelang-param-lang": "De nieuwe taalcode. Laat weg om de standaard taalcode van de wiki's te gebruiken.", + "pagelanguageapi-disabled": "De pagelang module is uitgeschakeld.", + "pagelanguageapi-permissiondenied": "Toegang geweigerd.", + "badlanguage-code": "Ongeldige taalcode." +} \ No newline at end of file diff --git a/i18n/qqq.json b/i18n/qqq.json new file mode 100644 index 0000000..013d6b0 --- /dev/null +++ b/i18n/qqq.json @@ -0,0 +1,12 @@ +{ + "pagelanguage-desc": "The description used in the extension credits", + "pagelanguageapi-extensionname": "The localized name of the extension", + "apisampleoutput-pagelang": "Example for the request api.php?action=pagelang&titles=Main%20Page&lang=fy&token=123ABC+\\", + "apihelp-pagelang-description": "The description used in the autogenerated api help", + "apihelp-pagelang-param-titles": "Parameter description for the parameter titles", + "apihelp-pagelang-param-pageids": "Parameter description for the parameter pageids", + "apihelp-pagelang-param-lang": "Parameter description for the parameter lang", + "pagelanguageapi-disabled": "API error code displayed when the page language module is disabled", + "pagelanguageapi-permissiondenied": "API error code displayed when the user does not have the pagelang right", + "badlanguage-code": "API error code displayed when the language code is invalid" +} \ No newline at end of file diff --git a/includes/api/PageLanguageApi.php b/includes/api/PageLanguageApi.php new file mode 100644 index 0000000..2a587c7 --- /dev/null +++ b/includes/api/PageLanguageApi.php @@ -0,0 +1,217 @@ +<?php + +if ( !defined( 'MEDIAWIKI' ) ) { + die(); +} + +/** + * Created using PhpStorm. + * + * Module to change the Page Language + * Code based on this unmerged Patch: https://gerrit.wikimedia.org/r/#/c/143025/ + * which was created by Kunal Grover + * + * @ingroup API + * @ingroup Extensions + */ +class PageLanguageApi extends ApiBase { + + /* + * This method performs all the magic whenever the api is called with action=pagelang + */ + public function execute() { + //Exit with error moduledisabled if the change page language feature is disabled + if ( !$GLOBALS['wgPageLanguageUseDB'] ) { + // Deprecated since 1.29, use ApiBase::dieWithError() instead, retained for backwards compatibility for now + // Using wfMessage to allow usage of i18n files, as to prevent duplication errors + $this->dieUsage( wfMessage( 'pagelanguageapi-disabled' ), 'moduledisabled' ); + } + + $apiResult = $this->getResult(); + $user = $this->getUser(); + + // Exit with error permissiondenied if the user does not have the right pagelang + if ( !$user->isAllowed( 'pagelang' ) ) { + // Deprecated since 1.29, use ApiBase::dieWithError() instead, retained for backwards compatibility for now + // Using wfMessage to allow usage of i18n files, as to prevent duplication errors + $this->dieUsage( wfMessage( 'pagelanguageapi-permissiondenied' ), 'permissiondenied' ); + } + + // Get the language code provided by the user + $languagecode = $this->getMain()->getVal( 'lang' ); + // Check if the code is specified, and when it is, check if it is valid + if ( $languagecode != null and !Language::isKnownLanguageTag( $languagecode ) ) { + // This works, since ::isValidBuiltInCode and ::isValidCode don't + // Using wfMessage to allow usage of i18n files, as to prevent duplication errors + $this->dieUsage( wfMessage( 'badlanguage-code' ), 'badlanguage-code' ); + } + + // Get the pages provided by the user + $titles = $this->getPageSet(); + // Populate the object with titles + $titles->execute(); + // Gets the invalid titles + $invalidtitles = $titles->getInvalidTitlesAndRevisions( array( 'invalidTitles', 'special', 'missingIds', 'interwikiTitles' ) ); + + // Get the special page object, used for the actual changing + $specialpage = SpecialPageFactory::getPage('PageLanguage'); + + // Create an array with the information needed by the special page + $dataarray['language'] = $languagecode; + + // If the language code hasn't been set, add selectoption with value 1 to indicate the + // page should use the wiki's default language + if ( !$languagecode ) { + $dataarray['selectoption'] = 1; + } + + $r = []; + // Loop through all titles + foreach ( $titles->getGoodTitles() as $title ) { + // Add the title info to the output + ApiQueryBase::addTitleInfo( $r, $title ); + // Add the page title to the data array + $dataarray['pagename'] = $title->getTitleValue()->getText(); + + $status = $specialpage->onSubmit( $dataarray ); + if ( $status ) { + $r['status'] = 'success'; + } else { + $r['status'] = 'no changes'; + } + $invalidtitles[] = $r; + } + + $apiResult->setIndexedTagName( $invalidtitles, 'page' ); + $apiResult->addValue( null, $this->getModuleName(), $invalidtitles ); + + $values = $titles->getNormalizedTitlesAsResult( $apiResult ); + if ( $values ) { + $apiResult->addValue( null, 'normalized', $values ); + } + $values = $titles->getConvertedTitlesAsResult( $apiResult ); + if ( $values ) { + $apiResult->addValue( null, 'converted', $values ); + } + $values = $titles->getRedirectTitlesAsResult( $apiResult ); + if ( $values ) { + $apiResult->addValue( null, 'redirects', $values ); + } + } + + /** + * Get a cached instance of an ApiPageSet object + * @return ApiPageSet + */ + private function getPageSet() { + if ( !isset( $this->mPageSet ) ) { + $this->mPageSet = new ApiPageSet( $this ); + } + + return $this->mPageSet; + } + + /** + * Ensures that the module must be posted + * @return bool + */ + public function mustBePosted() { + return true; + } + + /** + * Specifies that and which token the module requires + * @return string + */ + public function needsToken() { + return 'csrf'; + } + + /** + * Specifies that the module is a write module + * @return bool + */ + public function isWriteMode() { + return true; + } + + /** + * The parameters accepted by this module + * @return array + */ + public function getAllowedParams() { + return array_merge( parent::getAllowedParams(), array( + 'titles' => array ( + ApiBase::PARAM_ISMULTI => true, + ApiBase::PARAM_TYPE => 'string' + ), + 'pageids' => array ( + ApiBase::PARAM_ISMULTI => true, + ApiBase::PARAM_TYPE => 'integer' + ), + 'lang' => array ( + ApiBase::PARAM_TYPE => 'string' + ) + ) ); + } + + /** + * Example usage used in the autogenerated API help + * @return array + */ + public function getExamplesMessages() { + return array( + // apisampleoutput-pagelang is the key to the i18n message explaining the example + 'api.php?action=pagelang&titles=Main%20Page&lang=fy&token=123ABC+\\' => 'apisampleoutput-pagelang' + ); + } + + /** + * Helpurl displayed in either Special:Version and/or the autogenerated API help + * @return string + */ + public function getHelpUrls() { + return 'https://www.mediawiki.org/wiki/API:PageLanguage'; + } + + // ---- \\ + // Deprecated elements kept for backwards compatibility with 1.24 \\ + // These should be updated accordingly to changes made to the newer elements \\ + // ---- \\ + + /** + * Example usage used in the autogenerated API help, for 1.24 + * Deprecated in favor of the method above, getExamplesMessages() + * @return array|bool|string + */ + public function getExamples() { + return array( + // apisampleoutput-pagelang is the key to the i18n message explaining the example + 'api.php?action=pagelang&titles=Main%20Page&lang=fy&token=123ABC+\\' => wfMessage('apisampleoutput-pagelang')->plain() + ); + } + + /** + * The description of the module used in the autogenerated API help, for 1.24 + * Available in i18/xxx.json as apihelp-pagelang-description + * @return string + */ + public function getDescription() { + //return 'Change the page language for a given set of pages.'; + return wfMessage('apihelp-pagelang-description')->plain(); + } + + /** + * The descriptions of the parameters of this module used in the autogenerated API help, for 1.24 + * Available in i18n/xxx.json as apihelp-pagelang-param-titles, apihelp-pagelang-param-pageids + * and apihelp-pagelang-param-lang + * @return array + */ + public function getParamDescription() { + return array_merge( parent::getParamDescription(), array( + 'titles' => wfMessage('apihelp-pagelang-param-titles')->plain(), + 'pageids' => wfMessage('apihelp-pagelang-param-pageids')->plain(), + 'lang' => wfMessage('apihelp-pagelang-param-lang')->plain() + ) ); + } +} \ No newline at end of file -- To view, visit https://gerrit.wikimedia.org/r/331335 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I4564f03822cecc43ea4d7c9db7ead7c04759c92f Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/PageLanguageApi Gerrit-Branch: master Gerrit-Owner: Mainframe98 <[email protected]> _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
