https://www.mediawiki.org/wiki/Special:Code/MediaWiki/115373

Revision: 115373
Author:   yaron
Date:     2012-05-14 14:24:47 +0000 (Mon, 14 May 2012)
Log Message:
-----------
Added new parser function, #drilldownlink, based on patch from MWJames (bug 
report 36693). Also, some very minor formatting changes.

Modified Paths:
--------------
    trunk/extensions/SemanticDrilldown/SemanticDrilldown.php
    trunk/extensions/SemanticDrilldown/includes/SD_Utils.php
    
trunk/extensions/SemanticDrilldown/languages/SemanticDrilldown.i18n.magic.php

Added Paths:
-----------
    trunk/extensions/SemanticDrilldown/includes/SD_ParserFunctions.php

Modified: trunk/extensions/SemanticDrilldown/SemanticDrilldown.php
===================================================================
--- trunk/extensions/SemanticDrilldown/SemanticDrilldown.php    2012-05-13 
22:51:32 UTC (rev 115372)
+++ trunk/extensions/SemanticDrilldown/SemanticDrilldown.php    2012-05-14 
14:24:47 UTC (rev 115373)
@@ -60,12 +60,14 @@
 $wgAutoloadClasses['SDFilterValue'] = $sdgIP . '/includes/SD_FilterValue.php';
 $wgAutoloadClasses['SDAppliedFilter'] = $sdgIP . 
'/includes/SD_AppliedFilter.php';
 $wgAutoloadClasses['SDPageSchemas'] = $sdgIP . '/includes/SD_PageSchemas.php';
+$wgAutoloadClasses['SDParserFunctions'] = $sdgIP . 
'/includes/SD_ParserFunctions.php';
 
 $wgHooks['smwInitProperties'][] = 'sdfInitProperties';
 $wgHooks['AdminLinks'][] = 'SDUtils::addToAdminLinks';
 $wgHooks['MagicWordwgVariableIDs'][] = 'SDUtils::addMagicWordVariableIDs';
 $wgHooks['ParserBeforeTidy'][] = 'SDUtils::handleShowAndHide';
 $wgHooks['PageSchemasRegisterHandlers'][] = 'SDPageSchemas::registerClass';
+$wgHooks['ParserFirstCallInit'][] = 'SDParserFunctions::registerFunctions';
 
 $wgPageProps['hidefromdrilldown'] = 'Whether or not the page is set as 
HIDEFROMDRILLDOWN';
 $wgPageProps['showindrilldown'] = 'Whether or not the page is set as 
SHOWINDRILLDOWN';

Added: trunk/extensions/SemanticDrilldown/includes/SD_ParserFunctions.php
===================================================================
--- trunk/extensions/SemanticDrilldown/includes/SD_ParserFunctions.php          
                (rev 0)
+++ trunk/extensions/SemanticDrilldown/includes/SD_ParserFunctions.php  
2012-05-14 14:24:47 UTC (rev 115373)
@@ -0,0 +1,88 @@
+<?php
+/**
+ * Parser functions for Semantic Drilldown
+ *
+ * @file
+ * @ingroup SD
+ *
+ * The following parser function is defined: #drilldownlink
+ *
+ * #drilldownlink links to the page Special:BrowseData, with a query string
+ * dictated by the parameters.
+ *
+ * {{#drilldownlink:category=|subcategory=|single|link text=|tooltip=|filter=}}
+ *
+ * @author Yaron Koren
+ * @author mwjames
+ */
+
+class SDParserFunctions {
+
+       static function registerFunctions( &$parser ) {
+               $parser->setFunctionHook( 'drilldownlink', array( 
'SDParserFunctions', 'renderDrilldownLink' ) );
+               return true;
+       }
+
+       static function renderDrilldownLink( &$parser ) {
+               $params = func_get_args();
+               array_shift( $params );
+
+               $specialPage = SDUtils::getSpecialPage( 'BrowseData' );
+
+               // Set defaults.
+               $inQueryArr = array();
+               $inLinkStr = $category = $classStr = $inTooltip = '';
+
+               // Parameters
+               foreach ( $params as $i => $param ) {
+
+                       $elements = explode( '=', $param, 2 );
+
+                       // set param_name and value
+                       if ( count( $elements ) > 1 ) {
+                               $param_name = trim( $elements[0] );
+
+                               // parse (and sanitize) parameter values
+                               $value = trim( $parser->recursiveTagParse( 
$elements[1] ) );
+                       } else {
+                               $param_name = null;
+
+                               // parse (and sanitize) parameter values
+                               $value = trim( $parser->recursiveTagParse( 
$param ) );
+                       }
+
+                       if ( $param_name == 'category' || $param_name == 'cat' 
) {
+                               $category = $value;
+                       } elseif ( $param_name == 'subcategory' || $param_name 
== 'subcat' ) {
+                               parse_str( '_subcat=' . $value , $arr);
+                               $inQueryArr = array_merge( $inQueryArr, $arr );
+                       } elseif ( $param_name == 'link text' ) {
+                               $inLinkStr = $value;
+                       } elseif ( $param_name == 'tooltip' ) {
+                               $inTooltip = Sanitizer::decodeCharReferences( 
$value );
+                       } elseif ( $param_name == null && $value == 'single' ) {
+                               parse_str( '_single' , $arr);
+                               $inQueryArr = array_merge( $inQueryArr, $arr );
+                       } elseif ( $param_name == 'filters' ) {
+                               $inQueryStr = str_replace( '&amp;', '%26', 
$value );
+                               parse_str( $inQueryStr, $arr );
+                               $inQueryArr = array_merge( $inQueryArr, $arr );
+                       }
+               }
+
+               $link_url = $specialPage->getTitle()->getLocalURL() . 
"/{$category}";
+               $link_url = str_replace( ' ', '_', $link_url );
+               if ( ! empty( $inQueryArray ) ) {
+                       $link_url .= ( strstr( $link_url, '?' ) ) ? '&' : '?';
+                       $link_url .= str_replace( '+', '%20', http_build_query( 
$inQueryArr, '', '&' ) );
+               }
+
+               $inLinkStr = ( empty($inLinkStr) ? $category : $inLinkStr ) ;
+               $link = Html::rawElement( 'a', array( 'href' => $link_url, 
'class' => $classStr, 'title' => $inTooltip ), $inLinkStr );
+
+               // hack to remove newline from beginning of output, thanks to
+               // 
http://jimbojw.com/wiki/index.php?title=Raw_HTML_Output_from_a_MediaWiki_Parser_Function
+               return $parser->insertStripItem( $link , $parser->mStripState );
+       }
+
+}

Modified: trunk/extensions/SemanticDrilldown/includes/SD_Utils.php
===================================================================
--- trunk/extensions/SemanticDrilldown/includes/SD_Utils.php    2012-05-13 
22:51:32 UTC (rev 115372)
+++ trunk/extensions/SemanticDrilldown/includes/SD_Utils.php    2012-05-14 
14:24:47 UTC (rev 115373)
@@ -6,6 +6,7 @@
  */
 
 class SDUtils {
+
        /**
         * Helper function to handle getPropertyValues() in both SMW 1.6
         * and earlier versions.
@@ -389,10 +390,12 @@
        <input type="hidden" name="$action" />
 
 END;
-               if ( $is_minor_edit )
-                       $text .= '    <input type="hidden" name="wpMinoredit">' 
. "\n";
-               if ( $watch_this )
-                       $text .= '    <input type="hidden" name="wpWatchthis">' 
. "\n";
+               if ( $is_minor_edit ) {
+                       $text .= '      <input type="hidden" 
name="wpMinoredit">' . "\n";
+               }
+               if ( $watch_this ) {
+                       $text .= '      <input type="hidden" 
name="wpWatchthis">' . "\n";
+               }
                $text .= <<<END
        </form>
        <script type="text/javascript">
@@ -413,6 +416,18 @@
        }
 
        /**
+        * Set the actual value of the magic words
+        */
+       static function addMagicWordLanguage( &$magicWords, $langCode ) {
+               switch( $langCode ) {
+               default:
+                       $magicWords['MAG_HIDEFROMDRILLDOWN'] = array( 0, 
'__HIDEFROMDRILLDOWN__' );
+                       $magicWords['MAG_SHOWINDRILLDOWN'] = array( 0, 
'__SHOWINDRILLDOWN__' );
+               }
+               return true;
+       }
+
+       /**
         * Set values in the page_props table based on the presence of the
         * 'HIDEFROMDRILLDOWN' and 'SHOWINDRILLDOWN' magic words in a page
         */
@@ -429,6 +444,22 @@
                return true;
        }
 
+       /**
+        * Compatibility helper function.
+        * Since 1.18 SpecialPageFactory::getPage should be used.
+        * SpecialPage::getPage is deprecated in 1.18.
+        *
+        * @since 2.3.3
+        *
+        * @param string $pageName
+        *
+        * @return SpecialPage|null
+        */
+       public static function getSpecialPage( $pageName ) {
+               $hasFactory = class_exists( 'SpecialPageFactory' ) && 
method_exists( 'SpecialPageFactory', 'getPage' );
+               return $hasFactory ? SpecialPageFactory::getPage( $pageName ) : 
SpecialPage::getPage( $pageName );
+       }
+
        public static function addToAdminLinks( &$admin_links_tree ) {
                $browse_search_section = $admin_links_tree->getSection( wfMsg( 
'adminlinks_browsesearch' ) );
                $sd_row = new ALRow( 'sd' );

Modified: 
trunk/extensions/SemanticDrilldown/languages/SemanticDrilldown.i18n.magic.php
===================================================================
--- 
trunk/extensions/SemanticDrilldown/languages/SemanticDrilldown.i18n.magic.php   
    2012-05-13 22:51:32 UTC (rev 115372)
+++ 
trunk/extensions/SemanticDrilldown/languages/SemanticDrilldown.i18n.magic.php   
    2012-05-14 14:24:47 UTC (rev 115373)
@@ -8,4 +8,5 @@
 $magicWords['en'] = array(
        'MAG_HIDEFROMDRILLDOWN' => array( 0, '__HIDEFROMDRILLDOWN__' ),
        'MAG_SHOWINDRILLDOWN' => array( 0, '__SHOWINDRILLDOWN__' ),
+       'drilldownlink' => array( 0, 'drilldownlink' ),
 );


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to