https://www.mediawiki.org/wiki/Special:Code/MediaWiki/110101
Revision: 110101 Author: bawolff Date: 2012-01-27 05:19:47 +0000 (Fri, 27 Jan 2012) Log Message: ----------- New extension that implements some preview related functions - {{#onpreview:previewing|non-preview view}} Not sure if this is an evil idea. But people seem to "want" to do such things (see 'pedia's abuse of {{REVISIONTIMESTAMP}} and discussion on r100610) Also implements a {{#addpreviewwarning:Warning}} where people can make warnings like "You used template X wrong". Added Paths: ----------- trunk/extensions/PreviewFunctions/ trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.magic.php trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.php trunk/extensions/PreviewFunctions/PreviewFunctions.php trunk/extensions/PreviewFunctions/PreviewFunctions_body.php Added: trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.magic.php =================================================================== --- trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.magic.php (rev 0) +++ trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.magic.php 2012-01-27 05:19:47 UTC (rev 110101) @@ -0,0 +1,7 @@ +<?php +$magicWords = array(); + +$magicWords['en'] = array( + 'ifpreview' => array( 0, 'ifpreview', 'onpreview' ), + 'previewwarning' => array( 0, 'addpreviewwarning' ), +); Property changes on: trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.magic.php ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.php =================================================================== --- trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.php (rev 0) +++ trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.php 2012-01-27 05:19:47 UTC (rev 110101) @@ -0,0 +1,18 @@ +<?php +/** + * Internationalisation file for extension PreviewFunctions + * + * @file + * @ingroup Extensions + */ + +$messages = array(); + +$messages['en'] = array( + 'previewfunctions-desc' => 'A set of parser functions to manipulate the page on preview', +); + + +$messages['qqq'] = array( + 'previewfunctions-desc' => '{{desc}}', +); Property changes on: trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.php ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/extensions/PreviewFunctions/PreviewFunctions.php =================================================================== --- trunk/extensions/PreviewFunctions/PreviewFunctions.php (rev 0) +++ trunk/extensions/PreviewFunctions/PreviewFunctions.php 2012-01-27 05:19:47 UTC (rev 110101) @@ -0,0 +1,47 @@ +<?php +/** + * Extension to add parserfunction that get triggered during preview process + * Ex: {{#ifpreview:text during preview|text not during preview}} + * + * @author Brian Wolff <bawolff+ext _at_ gmail _dot_ com> + * + * Copyright © Brian Wolff 2012. + * + * 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. + * + */ + +if ( !defined( 'MEDIAWIKI' ) ) { + die( 'This file is a MediaWiki extension, it is not a valid entry point' ); +} + + +$wgExtensionCredits['parserhook'][] = array( + 'path' => __FILE__, + 'name' => 'PreviewFunctions', + 'descriptionmsg' => 'previewfunctions-desc', + 'version' => 1, + 'url' => 'https://mediawiki.org/wiki/Extension:PreviewFunctions', + 'author' => '[https://mediawiki.org/wiki/User:Bawolff Brian Wolff]', +); + +$dir = dirname(__FILE__) . '/'; + +$wgExtensionMessagesFiles['PreviewFunctions'] = $dir . 'PreviewFunctions.i18n.php'; +$wgExtensionMessagesFiles['PreviewFunctionsMagic'] = $dir . 'PreviewFunctions.i18n.magic.php'; + +$wgAutoloadClasses['PreviewFunctions'] = $dir . 'PreviewFunctions_body.php'; + +$wgHooks['ParserFirstCallInit'][] = 'PreviewFunctions::register'; Property changes on: trunk/extensions/PreviewFunctions/PreviewFunctions.php ___________________________________________________________________ Added: svn:eol-style + native Added: trunk/extensions/PreviewFunctions/PreviewFunctions_body.php =================================================================== --- trunk/extensions/PreviewFunctions/PreviewFunctions_body.php (rev 0) +++ trunk/extensions/PreviewFunctions/PreviewFunctions_body.php 2012-01-27 05:19:47 UTC (rev 110101) @@ -0,0 +1,52 @@ +<?php +class PreviewFunctions { + + /** + * Register the parser hook. + * @param $parser Parser + */ + public static function register( Parser $parser ) { + $parser->setFunctionHook( 'ifpreview', 'PreviewFunctions::ifPreview', Parser::SFH_OBJECT_ARGS ); + $parser->setFunctionHook( 'previewwarning', 'PreviewFunctions::addWarning' ); + return true; + } + + public static function ifPreview( $parser, $frame, $args ) { + if ( $parser->getOptions()->getIsPreview() ) { + # Is a preview. Return first + return isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : ''; + } else { + # Otherwise return second arg. + return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : ''; + } + } + + public static function addWarning( $parser, $warning ) { + if ( $warning === '' ) return ''; + + // Not 100% sure if doing this right. + // Note, EditPage.php parses such warnings + // (but with a different parser object) + + // Even with something like PPFrame::RECOVER_ORIG - tag extensions are still expanded. + + // Not doing: + // $warning = $parser->mStripState->unstripGeneral( $warning ); + // Because double parses things that should be treated as html. + + // Based on a line in CoreParserFunctions::displaytitle. + // Can't just substitute a <nowiki>$1</nowiki> and then unstripNoWiki, since that doesn't work + // for other extensions that put nowiki content (Since some like $wgRawHtml's <html> + // would not like the tag escaping done by <nowiki>. + // I suppose I could look for the nowiki part of "UNIQ7d3e18c0572c78c3-nowiki-00000031-QINU" + // but thats super hacky. So just delete the strip items. + $warning = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?' + . preg_quote( Parser::MARKER_SUFFIX, '/' ) . '/', + '', + $warning + ); + + $warning = Html::rawElement( 'div', array( 'class' => 'error mw-previewfunc-warning' ), $warning ); + $parser->getOutput()->addWarning( $warning ); + } +} Property changes on: trunk/extensions/PreviewFunctions/PreviewFunctions_body.php ___________________________________________________________________ Added: svn:eol-style + native _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs