Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 77 additions & 0 deletions AddBodyClass.hooks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php
/*
Copyright 2012 Povilas Kanapickas <tir5c3@yahoo.co.uk>
Copyright 2016 Will Stott <willstott101@gmail.com>

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 <http://www.gnu.org/licenses/>.
*/

$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;
}

}
10 changes: 0 additions & 10 deletions AddBodyClass.i18n.php

This file was deleted.

104 changes: 13 additions & 91 deletions AddBodyClass.php
Original file line number Diff line number Diff line change
@@ -1,93 +1,15 @@
<?php
/*
Copyright 2012 Povilas Kanapickas <tir5c3@yahoo.co.uk>
Copyright 2016 Will Stott <willstott101@gmail.com>

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 <http://www.gnu.org/licenses/>.
*/

$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+' );
}
29 changes: 29 additions & 0 deletions extension.json
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 6 additions & 0 deletions i18n/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"@metadata": {
"authors": []
},
"addbodyclass-desc": "Add class attributes to the body tag "
}