GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • View File
  • Edit File
  • Delete File
  • View Transforms
  • Details

This view

  • Original page
  • New address

GeezerVison is showing phabricator.wikimedia.org — ⇪ tagHookPST.patch

Page MenuHomePhabricator
Log In
Files F2120

tagHookPST.patch
Public
Actions

  • View File
  • Edit File
  • Delete File
  • View Transforms
Authored By
• bzimport
Nov 21 2014, 8:36 PM2014-11-21 20:36:06 (UTC+0)
Size
4 KB
Referenced Files
None
Subscribers
None

tagHookPST.patch
View Options

Index: includes/parser/Parser.php
===================================================================
--- includes/parser/Parser.php (revision 108087)
+++ includes/parser/Parser.php (working copy)
@@ -100,6 +100,7 @@
# Persistent:
var $mTagHooks = array();
+ var $mTagPSTHooks = array();
var $mTransparentTagHooks = array();
var $mFunctionHooks = array();
var $mFunctionSynonyms = array( 0 => array(), 1 => array() );
@@ -3727,6 +3728,25 @@
extract( $flags );
}
} else {
+
+ if ( $this->ot['wiki'] ) {
+ //we're in preSaveTransform, allow the tag extension to transform its contents
+ $lcname = strtolower( $name );
+ $attributes = Sanitizer::decodeTagAttributes( $attrText );
+ if ( isset( $params['attributes'] ) ) {
+ $attributes = $attributes + $params['attributes'];
+ }
+
+ if ( isset( $this->mTagPSTHooks[$lcname] ) ) {
+ # Workaround for PHP bug 35229 and similar
+ if ( !is_callable( $this->mTagPSTHooks[$lcname] ) ) {
+ throw new MWException( "Tag PST hook for $lcname is not callable\n" );
+ }
+ $content = call_user_func_array( $this->mTagPSTHooks[$lcname],
+ array( $content, $attributes, $this, $frame ) );
+ }
+ }
+
if ( is_null( $attrText ) ) {
$attrText = '';
}
@@ -4583,6 +4603,26 @@
return $oldVal;
}
+/**
+ * As setHook(), but the callback set here will be called during preSaveTransform
+ * This allows extension tags to do pre-save transformations on their input,
+ *
+ * @param $tag Mixed: the tag to use, e.g. 'hook' for <hook>
+ * @param $callback Mixed: the callback function (and object) to use for the tag
+ * @return The old value of the mTagPSTHooks array associated with the hook
+ */
+ public function setPSTHook( $tag, $callback ) {
+ $tag = strtolower( $tag );
+ if ( preg_match( '/[<>\r\n]/', $tag, $m ) ) throw new MWException( "Invalid character {$m[0]} in setPSTHook('$tag', ...) call" );
+ $oldVal = isset( $this->mTagPSTHooks[$tag] ) ? $this->mTagPSTHooks[$tag] : null;
+ $this->mTagPSTHooks[$tag] = $callback;
+ if ( !in_array( $tag, $this->mStripList ) ) {
+ $this->mStripList[] = $tag;
+ }
+
+ return $oldVal;
+ }
+
/**
* As setHook(), but letting the contents be parsed.
*
@@ -4614,6 +4654,7 @@
function clearTagHooks() {
$this->mTagHooks = array();
$this->mFunctionTagHooks = array();
+ $this->mTagPSTHooks = array();
$this->mStripList = $this->mDefaultStripList;
}
@@ -5155,7 +5196,8 @@
* @return array
*/
function getTags() {
- return array_merge( array_keys( $this->mTransparentTagHooks ), array_keys( $this->mTagHooks ), array_keys( $this->mFunctionTagHooks ) );
+ return array_unique( array_merge( array_keys( $this->mTransparentTagHooks ), array_keys( $this->mTagHooks ),
+ array_keys( $this->mFunctionTagHooks ), array_keys( $this->mTagPSTHooks ) ) );
}
/**
Index: includes/parser/CoreTagHooks.php
===================================================================
--- includes/parser/CoreTagHooks.php (revision 108087)
+++ includes/parser/CoreTagHooks.php (working copy)
@@ -19,6 +19,7 @@
$parser->setHook( 'pre', array( __CLASS__, 'pre' ) );
$parser->setHook( 'nowiki', array( __CLASS__, 'nowiki' ) );
$parser->setHook( 'gallery', array( __CLASS__, 'gallery' ) );
+ $parser->setPSTHook( 'gallery', array( __CLASS__, 'galleryPST' ) );
if ( $wgRawHtml ) {
$parser->setHook( 'html', array( __CLASS__, 'html' ) );
}
@@ -102,4 +103,41 @@
static function gallery( $content, $attributes, $parser ) {
return $parser->renderImageGallery( $content, $attributes );
}
+
+ /**
+ * Core parser tag preSaveTransform hook function for 'gallery'.
+ *
+ * This function applies preSaveTransform to the image descriptions.
+ *
+ * @param string $content
+ * @param array $attributes
+ * @param Parser $parser
+ * @return string wikitext
+ */
+ static function galleryPST( $content, $attributes, $parser ) {
+
+ $lines = StringUtils::explode( "\n", $content );
+ $transformed = array();
+ foreach ( $lines as $line ) {
+ # match lines like these:
+ # Image:someimage.jpg|This is some image
+ $matches = array();
+ preg_match( "/^([^|]+)(\\|(.*))?$/", $line, $matches );
+ # Skip empty lines
+ if ( count( $matches ) == 0 ) {
+ $transformed[] = $line;
+ continue;
+ }
+
+ if ( isset( $matches[3] ) ) {
+ //pre-save transform the second match, which is wikitext
+ $presaved = $parser->preSaveTransform( $matches[2], $parser->getTitle(), $parser->getOptions()->getUser(), $parser->getOptions(), false );
+ $transformed[] = $matches[1] . $presaved;// $matches[2];
+ } else {
+ $transformed[] = $line;
+ }
+
+ }
+ return implode( "\n", $transformed );
+ }
}

File Metadata

  • Details
  • Attached
Mime Type
text/x-diff
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1697
Default Alt Text
tagHookPST.patch (4 KB)
Attached ToMode
T4700: Pre-save transform skips extensions using wikitext (gallery, references, footnotes, Cite, status indicators, pipe trick, subst, signatures)Attached
Detach File

Event Timeline

Log In to Comment
Content licensed under Creative Commons Attribution-ShareAlike (CC BY-SA) 4.0 unless otherwise noted; code licensed under GNU General Public License (GPL) 2.0 or later and other open source licenses. By using this site, you agree to the Terms of Use, Privacy Policy, and Code of Conduct. · Wikimedia Foundation · Privacy Policy · Code of Conduct · Terms of Use · Disclaimer · CC-BY-SA · GPL · Credits
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.