r110101 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r110100‎ | r110101 | r110102 >
Date:05:19, 27 January 2012
Author:bawolff
Status:deferred
Tags:
Comment:
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".
Modified paths:
  • /trunk/extensions/PreviewFunctions (added) (history)
  • /trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.magic.php (added) (history)
  • /trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.php (added) (history)
  • /trunk/extensions/PreviewFunctions/PreviewFunctions.php (added) (history)
  • /trunk/extensions/PreviewFunctions/PreviewFunctions_body.php (added) (history)

Diff [purge]

Index: trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.magic.php
@@ -0,0 +1,7 @@
 2+<?php
 3+$magicWords = array();
 4+
 5+$magicWords['en'] = array(
 6+ 'ifpreview' => array( 0, 'ifpreview', 'onpreview' ),
 7+ 'previewwarning' => array( 0, 'addpreviewwarning' ),
 8+);
Property changes on: trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.magic.php
___________________________________________________________________
Added: svn:eol-style
19 + native
Index: trunk/extensions/PreviewFunctions/PreviewFunctions_body.php
@@ -0,0 +1,52 @@
 2+<?php
 3+class PreviewFunctions {
 4+
 5+ /**
 6+ * Register the parser hook.
 7+ * @param $parser Parser
 8+ */
 9+ public static function register( Parser $parser ) {
 10+ $parser->setFunctionHook( 'ifpreview', 'PreviewFunctions::ifPreview', Parser::SFH_OBJECT_ARGS );
 11+ $parser->setFunctionHook( 'previewwarning', 'PreviewFunctions::addWarning' );
 12+ return true;
 13+ }
 14+
 15+ public static function ifPreview( $parser, $frame, $args ) {
 16+ if ( $parser->getOptions()->getIsPreview() ) {
 17+ # Is a preview. Return first
 18+ return isset( $args[0] ) ? trim( $frame->expand( $args[0] ) ) : '';
 19+ } else {
 20+ # Otherwise return second arg.
 21+ return isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
 22+ }
 23+ }
 24+
 25+ public static function addWarning( $parser, $warning ) {
 26+ if ( $warning === '' ) return '';
 27+
 28+ // Not 100% sure if doing this right.
 29+ // Note, EditPage.php parses such warnings
 30+ // (but with a different parser object)
 31+
 32+ // Even with something like PPFrame::RECOVER_ORIG - tag extensions are still expanded.
 33+
 34+ // Not doing:
 35+ // $warning = $parser->mStripState->unstripGeneral( $warning );
 36+ // Because double parses things that should be treated as html.
 37+
 38+ // Based on a line in CoreParserFunctions::displaytitle.
 39+ // Can't just substitute a <nowiki>$1</nowiki> and then unstripNoWiki, since that doesn't work
 40+ // for other extensions that put nowiki content (Since some like $wgRawHtml's <html>
 41+ // would not like the tag escaping done by <nowiki>.
 42+ // I suppose I could look for the nowiki part of "UNIQ7d3e18c0572c78c3-nowiki-00000031-QINU"
 43+ // but thats super hacky. So just delete the strip items.
 44+ $warning = preg_replace( '/' . preg_quote( $parser->uniqPrefix(), '/' ) . '.*?'
 45+ . preg_quote( Parser::MARKER_SUFFIX, '/' ) . '/',
 46+ '',
 47+ $warning
 48+ );
 49+
 50+ $warning = Html::rawElement( 'div', array( 'class' => 'error mw-previewfunc-warning' ), $warning );
 51+ $parser->getOutput()->addWarning( $warning );
 52+ }
 53+}
Property changes on: trunk/extensions/PreviewFunctions/PreviewFunctions_body.php
___________________________________________________________________
Added: svn:eol-style
154 + native
Index: trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.php
@@ -0,0 +1,18 @@
 2+<?php
 3+/**
 4+ * Internationalisation file for extension PreviewFunctions
 5+ *
 6+ * @file
 7+ * @ingroup Extensions
 8+ */
 9+
 10+$messages = array();
 11+
 12+$messages['en'] = array(
 13+ 'previewfunctions-desc' => 'A set of parser functions to manipulate the page on preview',
 14+);
 15+
 16+
 17+$messages['qqq'] = array(
 18+ 'previewfunctions-desc' => '{{desc}}',
 19+);
Property changes on: trunk/extensions/PreviewFunctions/PreviewFunctions.i18n.php
___________________________________________________________________
Added: svn:eol-style
120 + native
Index: trunk/extensions/PreviewFunctions/PreviewFunctions.php
@@ -0,0 +1,47 @@
 2+<?php
 3+/**
 4+ * Extension to add parserfunction that get triggered during preview process
 5+ * Ex: {{#ifpreview:text during preview|text not during preview}}
 6+ *
 7+ * @author Brian Wolff <bawolff+ext _at_ gmail _dot_ com>
 8+ *
 9+ * Copyright © Brian Wolff 2012.
 10+ *
 11+ * This program is free software; you can redistribute it and/or
 12+ * modify it under the terms of the GNU General Public License
 13+ * as published by the Free Software Foundation; either version 2
 14+ * of the License, or (at your option) any later version.
 15+ *
 16+ * This program is distributed in the hope that it will be useful,
 17+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 18+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 19+ * GNU General Public License for more details.
 20+ *
 21+ * You should have received a copy of the GNU General Public License
 22+ * along with this program; if not, write to the Free Software
 23+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 24+ *
 25+ */
 26+
 27+if ( !defined( 'MEDIAWIKI' ) ) {
 28+ die( 'This file is a MediaWiki extension, it is not a valid entry point' );
 29+}
 30+
 31+
 32+$wgExtensionCredits['parserhook'][] = array(
 33+ 'path' => __FILE__,
 34+ 'name' => 'PreviewFunctions',
 35+ 'descriptionmsg' => 'previewfunctions-desc',
 36+ 'version' => 1,
 37+ 'url' => 'https://mediawiki.org/wiki/Extension:PreviewFunctions',
 38+ 'author' => '[https://mediawiki.org/wiki/User:Bawolff Brian Wolff]',
 39+);
 40+
 41+$dir = dirname(__FILE__) . '/';
 42+
 43+$wgExtensionMessagesFiles['PreviewFunctions'] = $dir . 'PreviewFunctions.i18n.php';
 44+$wgExtensionMessagesFiles['PreviewFunctionsMagic'] = $dir . 'PreviewFunctions.i18n.magic.php';
 45+
 46+$wgAutoloadClasses['PreviewFunctions'] = $dir . 'PreviewFunctions_body.php';
 47+
 48+$wgHooks['ParserFirstCallInit'][] = 'PreviewFunctions::register';
Property changes on: trunk/extensions/PreviewFunctions/PreviewFunctions.php
___________________________________________________________________
Added: svn:eol-style
149 + native

Follow-up revisions

RevisionCommit summaryAuthorDate
r110419r110101: Register extension for translatewiki.net.raymond19:28, 31 January 2012

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r100610* (bug 31921) Fix for r78201: magic words REVISIONDAY, REVISIONMONTH and REVI...ialex16:14, 24 October 2011

Status & tagging log