diff --git a/AddBodyClass.hooks.php b/AddBodyClass.hooks.php new file mode 100644 index 0000000..01f9eb5 --- /dev/null +++ b/AddBodyClass.hooks.php @@ -0,0 +1,77 @@ + + Copyright 2016 Will Stott + + 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, see . + */ + +$wgCategoriesAsBodyClasses = false; + +class AddBodyClassHooks { + + static protected $classes = ''; + + static function setup(&$parser) + { + $parser->setFunctionHook('addbodyclass', 'AddBodyClassHooks::on_parse'); + + return true; + } + + static function on_parse(&$parser, $param1 = '') + { + $output = ''; + if ($param1 !== '') { + /* if parser cache is used, the page is parsed only once, so we + need to store our data in the cached text */ + $output = 'ADD_BODY_CLASS_BEGIN ' + . htmlspecialchars($param1) + . ' ADD_BODY_CLASS_END'; + } + return $output; + } + + static function on_output_before_html(&$out, &$text) + { + if (($found = strpos($text, 'ADD_BODY_CLASS_BEGIN')) !== false) { + if (preg_match_all("/ADD_BODY_CLASS_BEGIN (.*?) ADD_BODY_CLASS_END/", + $text, $matches, PREG_SET_ORDER)) { + foreach ($matches as $m) { + self::$classes .= ' '.$m[1]; + $text = str_replace($m[0], "", $text); + } + } + } + return true; + } + + static function add_attrs($out, $sk, &$bodyAttrs) + { + global $wgCategoriesAsBodyClasses; + + if (self::$classes !== '') { + $bodyAttrs['class'] .= self::$classes; + } + + if ($wgCategoriesAsBodyClasses) { + foreach ($out->getCategories() as $categoryName) { + $safeCategoryName = str_replace(array('.', ' '), '_', $categoryName); + $bodyAttrs['class'] .= ' cat-' . $safeCategoryName . ' icat-' . strtolower($safeCategoryName); + } + } + return true; + } + +} diff --git a/AddBodyClass.i18n.php b/AddBodyClass.i18n.php deleted file mode 100644 index 82dc70f..0000000 --- a/AddBodyClass.i18n.php +++ /dev/null @@ -1,10 +0,0 @@ - 'Add Body Class', - 'addbodyclass_desc' => 'Add class attributes to the body tag ', -); diff --git a/AddBodyClass.php b/AddBodyClass.php index 627c755..33c24a2 100644 --- a/AddBodyClass.php +++ b/AddBodyClass.php @@ -1,93 +1,15 @@ - Copyright 2016 Will Stott - - 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, see . - */ - -$wgExtensionCredits['parserhook'][] = array( - 'path' => __FILE__, - 'name' => 'AddBodyClass', - 'author' => 'Povilas Kanapickas & Will Stott', - 'descriptionmsg' => 'addbodyclass_desc', - 'url' => 'https://github.com/p12tic/AddBodyClass', - 'version' => '1.2', -); - -$wgExtensionMessagesFiles['AddBodyClassMagic'] = dirname( __FILE__ ) . '/' . 'AddBodyClass.i18n.magic.php'; -$wgExtensionMessagesFiles['AddBodyClass'] = dirname( __FILE__ ) . '/' . 'AddBodyClass.i18n.php'; - -$wgHooks['ParserFirstCallInit'][] = 'AddBodyClass::setup'; -$wgHooks['OutputPageBodyAttributes'][] = 'AddBodyClass::add_attrs'; -$wgHooks['OutputPageBeforeHTML'][] = 'AddBodyClass::on_output_before_html'; - -$wgCategoriesAsBodyClasses = false; - -class AddBodyClass { - - static protected $classes = ''; - - static function setup(&$parser) - { - $parser->setFunctionHook('addbodyclass', 'AddBodyClass::on_parse'); - - return true; - } - - static function on_parse(&$parser, $param1 = '') - { - $output = ''; - if ($param1 !== '') { - /* if parser cache is used, the page is parsed only once, so we - need to store our data in the cached text */ - $output = 'ADD_BODY_CLASS_BEGIN ' - . htmlspecialchars($param1) - . ' ADD_BODY_CLASS_END'; - } - return $output; - } - - static function on_output_before_html(&$out, &$text) - { - if (($found = strpos($text, 'ADD_BODY_CLASS_BEGIN')) !== false) { - if (preg_match_all("/ADD_BODY_CLASS_BEGIN (.*?) ADD_BODY_CLASS_END/", - $text, $matches, PREG_SET_ORDER)) { - foreach ($matches as $m) { - self::$classes .= ' '.$m[1]; - $text = str_replace($m[0], "", $text); - } - } - } - return true; - } - - static function add_attrs($out, $sk, &$bodyAttrs) - { - global $wgCategoriesAsBodyClasses; - - if (self::$classes !== '') { - $bodyAttrs['class'] .= self::$classes; - } - - if ($wgCategoriesAsBodyClasses) { - foreach ($out->getCategories() as $categoryName) { - $safeCategoryName = str_replace(array('.', ' '), '_', $categoryName); - $bodyAttrs['class'] .= ' cat-' . $safeCategoryName . ' icat-' . strtolower($safeCategoryName); - } - } - return true; - } - +if ( function_exists( 'wfLoadExtension' ) ) { + wfLoadExtension( 'AddBodyClass' ); + // Keep i18n globals so mergeMessageFileList.php doesn't break + $wgMessagesDirs['AddBodyClass'] = __DIR__ . '/i18n'; + $wgExtensionMessagesFiles['AddBodyClassMagic'] = __DIR__ . '/' . 'AddBodyClass.i18n.magic.php'; + wfWarn( + 'Deprecated PHP entry point used for the AddBodyClass extension. ' . + 'Please use wfLoadExtension instead, ' . + 'see https://www.mediawiki.org/wiki/Extension_registration for more details.' + ); + return; +} else { + die( 'This version of the AddBodyClass extension requires MediaWiki 1.25+' ); } diff --git a/extension.json b/extension.json new file mode 100644 index 0000000..4458617 --- /dev/null +++ b/extension.json @@ -0,0 +1,29 @@ +{ + "name": "AddBodyClass", + "version": "1.2", + "author": [ + "Povilas Kanapickas", + "Will Stott" + ], + "url": "http://www.mediawiki.org/wiki/Extension:AddBodyClass", + "descriptionmsg": "addbodyclass-desc", + "type": "parserhook", + "ExtensionMessagesFiles": { + "AddBodyClassMagic": "AddBodyClass.i18n.magic.php" + }, + "MessagesDirs": { + "AddBodyClass": [ + "i18n" + ] + }, + "AutoloadClasses": { + "AddBodyClassHooks": "AddBodyClass.hooks.php" + }, + "license-name": "GPL-2.0-or-later", + "Hooks": { + "ParserFirstCallInit": "AddBodyClassHooks::setup", + "OutputPageBodyAttributes": "AddBodyClassHooks::add_attrs", + "OutputPageBeforeHTML": "AddBodyClassHooks::on_output_before_html" + }, + "manifest_version": 1 +} diff --git a/i18n/en.json b/i18n/en.json new file mode 100644 index 0000000..8d342ff --- /dev/null +++ b/i18n/en.json @@ -0,0 +1,6 @@ +{ + "@metadata": { + "authors": [] + }, + "addbodyclass-desc": "Add class attributes to the body tag " +}