https://www.mediawiki.org/wiki/Special:Code/MediaWiki/101942
Revision: 101942
Author: danwe
Date: 2011-11-04 01:29:27 +0000 (Fri, 04 Nov 2011)
Log Message:
-----------
Changes and bugfixes for release 0.7 + distributed under ISC license from now on
Modified Paths:
--------------
trunk/extensions/HashTables/HashTables.php
trunk/extensions/HashTables/README
trunk/extensions/HashTables/RELEASE-NOTES
Added Paths:
-----------
trunk/extensions/HashTables/COPYING
Added: trunk/extensions/HashTables/COPYING
===================================================================
--- trunk/extensions/HashTables/COPYING (rev 0)
+++ trunk/extensions/HashTables/COPYING 2011-11-04 01:29:27 UTC (rev 101942)
@@ -0,0 +1,13 @@
+Copyright (c) 2010 - 2011 by Daniel Werner < [email protected] >
+
+Permission to use, copy, modify, and/or distribute this software for any
+purpose with or without fee is hereby granted, provided that the above
+copyright notice and this permission notice appear in all copies.
+
+THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
\ No newline at end of file
Property changes on: trunk/extensions/HashTables/COPYING
___________________________________________________________________
Added: svn:eol-style
+ native
Modified: trunk/extensions/HashTables/HashTables.php
===================================================================
--- trunk/extensions/HashTables/HashTables.php 2011-11-04 01:28:52 UTC (rev
101941)
+++ trunk/extensions/HashTables/HashTables.php 2011-11-04 01:29:27 UTC (rev
101942)
@@ -4,8 +4,9 @@
* Defines a subset of parser functions to handle hash tables. Inspired by the
ArrayExtension
* (http://www.mediawiki.org/wiki/Extension:ArrayExtension).
*
- * @version: 0.6.4 alpha
+ * @version: 0.7
* @author: Daniel Werner < [email protected] >
+ * @license: ISC license
*
* Documentation: http://www.mediawiki.org/wiki/Extension:HashTables
* Support: http://www.mediawiki.org/wiki/Extension_talk:HashTables
@@ -16,8 +17,8 @@
*
* @ToDo:
* ======
- * - binding hash tables instance to each initialized parser instead of having
one global one.
- * Considering about:
+ * - binding one hash tables instance per initialized parser instead of having
one global one.
+ * Thinking about:
* - Sort function
* - Search function
*
@@ -51,13 +52,13 @@
class ExtHashTables {
/**
- * Version of the HashTables extension.
+ * Version of the 'HashTables' extension.
*
* @since 0.6.1
*
* @var string
*/
- const VERSION = '0.6.4 alpha';
+ const VERSION = '0.7';
/**
* @since 0.1
@@ -135,25 +136,29 @@
* Returns the size of a hash table. Returns '' if the table doesn't
exist.
*/
function hashsize( &$parser, $hashId) {
- if( !isset($hashId) || !$this->hashExists($hashId) )
+ if( ! isset($hashId) || ! $this->hashExists($hashId) ) {
return '';
-
+ }
return count( $this->mHashTables[ $hashId ] );
}
/**
- * Returns "1" or 'yes' (if set) if the hash item key 'key' exists
inside the hash table 'hashId'.
- * Otherwise the output will be a void string or 'no' (if set).
- */
- function hashkeyexists( &$parser, $hashId, $key = '', $yes = '1', $no = ''
) {
+ * Returns "1" or the third parameter (if set) if the hash item key
'key' exists inside the hash
+ * table 'hashId'. Otherwise the output will be a void string or the
fourth (if set).
+ */
+ function hashkeyexists( Parser &$parser, PPFrame $frame, $args ) {
+ $hashId = trim( $frame->expand( $args[0] ) );
+ $key = isset( $args[1] ) ? trim( $frame->expand( $args[1] ) ) : '';
+
// get hash or null:
$hash = $this->getHash( $hashId );
+ // only expand the one argument needed:
if( $hash !== null && array_key_exists( $key, $hash ) ) {
- return $yes;
+ return isset( $args[2] ) ? trim( $frame->expand(
$args[2] ) ) : '1'; // true '1'
}
else {
- return $no;
+ return isset( $args[3] ) ? trim( $frame->expand(
$args[3] ) ) : ''; // false ''
}
}
@@ -162,7 +167,7 @@
* Syntax:
* {{#hashprint:hashID |seperator |keyPattern |valuePattern |subject
|printOrderArrayId}}
*/
- function hashprint( Parser &$parser, PPFrame $frame, $args) {
+ function hashprint( Parser &$parser, PPFrame $frame, $args ) {
if( ! isset( $args[0] ) ) {
return '';
}
@@ -175,10 +180,15 @@
// parameter validation:
$seperator = isset( $args[1] ) ? trim( $frame->expand(
$args[1] ) ) : ', ';
+ /*
+ * PPFrame::NO_ARGS and PPFrame::NO_TEMPLATES for expansion
make a lot of sense here since the patterns getting replaced
+ * in $subject before $subject is being parsed. So any template
or argument influence in the patterns wouldn't make any
+ * sense in any sane scenario.
+ */
$keyPattern = isset( $args[2] ) ? trim( $frame->expand(
$args[2], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : '';
$valuePattern = isset( $args[3] ) ? trim( $frame->expand(
$args[3], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : '@@@@';
$subject = isset( $args[4] ) ? trim( $frame->expand(
$args[4], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : '@@@@';
- $printOrderArrayId = isset( $args[5] ) ? trim( $frame->expand(
$args[5], PPFrame::NO_ARGS | PPFrame::NO_TEMPLATES ) ) : null;
+ $printOrderArrayId = isset( $args[5] ) ? trim( $frame->expand(
$args[5] ) ) : null;
if( $printOrderArrayId !== null ) {
global $wgArrayExtension;
@@ -228,27 +238,24 @@
* Define an hash filled with all given parameters of the current
template.
* In case there are no parameters, the hash will be void.
*/
- function parameterstohash( &$parser, $frame, $args) {
- if( !isset($args[0]) )
+ function parameterstohash( &$parser, PPFrame $frame, $args) {
+ if( ! isset( $args[0] ) ) {
return '';
-
- $hashId = trim( $frame->expand($args[0]) );
-
- // in case the page is not used as template i.e. when displayed
by it's own...
- if( !( $frame instanceof PPTemplateFrame_Dom ) )
- {
- $this->mHashTables[ $hashId ] = array(); // create
empty hash table
+ }
+ $hashId = trim( $frame->expand( $args[0] ) );
+ $this->mHashTables[ $hashId ] = array(); // create empty hash
table
+
+ // in case the page is not used as template i.e. when displayed
on its own
+ if( ! $frame->isTemplate() ) {
return '';
- }
-
+ }
+
$templateArgs = $frame->getArguments();
-
- foreach ( $templateArgs as $argName => $argVal )
- {
+
+ foreach ( $templateArgs as $argName => $argVal ) {
$this->mHashTables[ $hashId ][ trim( $argName ) ] =
trim( $argVal );
- }
- return '';
-
+ }
+ return '';
}
/**
@@ -257,8 +264,9 @@
*/
function hashreset( &$parser, $frame, $args) {
// reset all hash tables if no specific tables are given:
- if( !isset( $args[0] ) || ( $args[0] === '' && count( $args )
== 1 ) )
+ if( !isset( $args[0] ) || ( $args[0] === '' && count( $args )
== 1 ) ) {
$this->mHashTables = array();
+ }
else {
foreach ( $args as $arg )
{
@@ -324,10 +332,10 @@
* {{#hashmerge:hashID |hash1 |hash2 |... |hash n}}
*/
function hashmerge( &$parser, $frame, $args) {
- $this->multiHashOperation( $frame, $args, __FUNCTION__ );
+ $this->multiHashOperation( $frame, $args, __FUNCTION__, true );
return '';
}
- private function multi_hashmerge( $hash1, $hash2 ) {
+ private function multi_hashmerge( $hash1, $hash2 = array() ) {
return array_merge( $hash1, $hash2 );
}
@@ -337,8 +345,8 @@
* Syntax:
* {{#hashmix:hashID |hash1 |hash2 |... |hash n}}
*/
- function hashmix( &$parser, $frame, $args) {
- $this->multiHashOperation( $frame, $args, __FUNCTION__ );
+ function hashmix( &$parser, $frame, $args) {
+ $this->multiHashOperation( $frame, $args, __FUNCTION__, false );
return '';
}
private function multi_hashmix( $hash1, $hash2 ) {
@@ -357,7 +365,7 @@
* {{#hashdiff:hashID |hash1 |hash2 |... |hash n}}
*/
function hashdiff( &$parser, $frame, $args) {
- $this->multiHashOperation( $frame, $args, __FUNCTION__ );
+ $this->multiHashOperation( $frame, $args, __FUNCTION__, false );
return '';
}
private function multi_hashdiff( $hash1, $hash2 ) {
@@ -372,7 +380,7 @@
* {{#hashintersect:hashID |hash1 |hash2 |... |hash n}}
*/
function hashintersect( &$parser, $frame, $args) {
- $this->multiHashOperation( $frame, $args, __FUNCTION__ );
+ $this->multiHashOperation( $frame, $args, __FUNCTION__, false );
return '';
}
private function multi_hashintersect( $hash1, $hash2 ) {
@@ -482,16 +490,18 @@
$templateCall = '{{' . $template;
foreach ($params as $paramKey => $paramValue){
- // replace '}' and '|' to avoid template call
manipulation
- $paramValue = str_replace( array( '}', '|' ), array(
'}', $pipeReplacer ), $paramValue );
+ // replace '{', '}' and '|' to avoid template call
manipulation
+ $paramValue = str_replace( array( '{', '}', '|' ),
array( '{', '}', $pipeReplacer ), $paramValue );
$templateCall .= "|$paramKey=$paramValue";
}
$templateCall .= '}}';
-
+
+ // parse template call:
$result = $parser->preprocessToDom( $templateCall,
$frame->isTemplate() ? Parser::PTD_FOR_INCLUSION : 0 );
$result = trim( $frame->expand( $result ) );
- return array( $result , 'noparse' => false, 'isHTML' => false );
+ // we don't have to set 'noparse' to false since parsing is
done above
+ return array( $result , 'noparse' => true, 'isHTML' => false );
}
/* ============================ */
@@ -506,7 +516,7 @@
* Returns a value within a hash. If key or hash doesn't exist, this
will return null
* or another predefined default.
*
- * @since 0.6.4
+ * @since 0.7
*
* @param string $hashId
* @param string $key
@@ -585,15 +595,18 @@
* $hash1 and $hash2 which will perform an action between $hash1 and
hash2 which will
* result into a new $hash1. There can be 1 to n $hash2 in the whole
process.
*
- * @param $frame
- * @param $args
- * @param $operationFunc name of the function calling this. There must
be a counterpart
+ * @param $frame PPFrame
+ * @param $args array
+ * @param $operationFunc string name of the function calling this.
There must be a counterpart
* function with prefix 'multi_' which should have two
parameters. Both parameters
* will receive a hash (array), the function must return the
result hash of the
* processing.
+ * @param $runFuncOnSingleHash boolean whether the $operationFunc
function should be run in case
+ * only one hash table id is given. If not, the original hash
will end up in the new hash.
*/
- protected function multiHashOperation( $frame, $args ,$operationFunc ) {
+ protected function multiHashOperation( PPFrame $frame, array $args,
$operationFunc, $runFuncOnSingleHash = true ) {
$lastHash = null;
+ $operationRan = false;
$finalHashId = trim( $frame->expand( $args[0] ) );
$operationFunc = 'multi_' . $operationFunc;
@@ -614,13 +627,18 @@
else {
// second or later hash table, process
with previous:
$lastHash = $this->{ $operationFunc }(
$lastHash, $argHash ); // perform action between last and current hash
+ $operationRan = true;
}
}
}
- // in case no array were given:
+ // in case no hash was given at all:
if( $lastHash === null ) {
$lastHash = array();
}
+ // if the operation didn't run because there was only one or no
array:
+ if( $operationRan == false && $runFuncOnSingleHash ) {
+ $lastHash = $this->{ $operationFunc }( $lastHash );
+ }
$this->mHashTables[ $finalHashId ] = $lastHash;
}
@@ -638,7 +656,7 @@
-function efHashTablesParserFirstCallInit( &$parser ) {
+function efHashTablesParserFirstCallInit( Parser &$parser ) {
global $wgHashTables, $wgArrayExtension;
$wgHashTables = new ExtHashTables();
@@ -646,7 +664,7 @@
$parser->setFunctionHook( 'hashdefine', array( &$wgHashTables,
'hashdefine' ) );
$parser->setFunctionHook( 'hashvalue', array( &$wgHashTables,
'hashvalue' ) );
$parser->setFunctionHook( 'hashsize', array( &$wgHashTables,
'hashsize' ) );
- $parser->setFunctionHook( 'hashkeyexists', array( &$wgHashTables,
'hashkeyexists' ) );
+ $parser->setFunctionHook( 'hashkeyexists', array( &$wgHashTables,
'hashkeyexists' ), SFH_OBJECT_ARGS );
$parser->setFunctionHook( 'hashprint', array( &$wgHashTables,
'hashprint' ), SFH_OBJECT_ARGS );
$parser->setFunctionHook( 'parameterstohash', array( &$wgHashTables,
'parameterstohash' ), SFH_OBJECT_ARGS );
$parser->setFunctionHook( 'hashtotemplate', array( &$wgHashTables,
'hashtotemplate' ), SFH_OBJECT_ARGS );
Modified: trunk/extensions/HashTables/README
===================================================================
--- trunk/extensions/HashTables/README 2011-11-04 01:28:52 UTC (rev 101941)
+++ trunk/extensions/HashTables/README 2011-11-04 01:29:27 UTC (rev 101942)
@@ -7,6 +7,7 @@
* Website: http://www.mediawiki.org/wiki/Extension:HashTables
* Author: Daniel Werner < [email protected] >
+* License: ISC license
== Installation ==
@@ -24,4 +25,4 @@
If you have bug reports or feature requests, please add them to the
''HashTables''
Talk page [0]. You can also send them to Daniel Werner < [email protected] >
-[0] http://www.mediawiki.org/w/index.php?title=Extension_talk:Maps
+[0] http://www.mediawiki.org/w/index.php?title=Extension_talk:HashTables
Modified: trunk/extensions/HashTables/RELEASE-NOTES
===================================================================
--- trunk/extensions/HashTables/RELEASE-NOTES 2011-11-04 01:28:52 UTC (rev
101941)
+++ trunk/extensions/HashTables/RELEASE-NOTES 2011-11-04 01:29:27 UTC (rev
101942)
@@ -1,10 +1,17 @@
Changelog:
==========
- * trunk, version 0.6.4 alpha
+ * November 3, version 0.7
+ - buggy behavior in 'hashkeyexists' which led to expanding both arguments
'yes' and 'no' in
+ case custom arguments were given is solved. Now only the one actual case
is being expanded.
+ - 'hashtotemplate' now changes '{' to '{' since '{{' made some
problems in some cases.
+ - 'hashprint' last parameter for print order now evaluates templates and
arguments as it should
+ always have done.
+ - 'hashmerge' on single hash will re-asign its numeric keys in ascending
order instead of doing nothing.
- using hook 'ParserFirstCallInit' now.
- some gernal cleanup done.
- put into wikimedia.org svn
(http://svn.wikimedia.org/svnroot/mediawiki/trunk/extensions/HashTables/)
+ - distributed under ISC license.
* August 3, 2011 version 0.6.3
- minor bug in 'parameterstohash' function solved.
@@ -12,7 +19,7 @@
* March 29, 2011 version 0.6.2
- 'hashtotemplate' has a new parameter to define a string which will
replace pipes '|'. Can be
useful to preserve links.
-
+
* January 24, 2011 version 0.6.1
- Constant VERSION and function getHash() added to class ExtHashTables.
_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs