Kji has submitted this change and it was merged.
Change subject: Reimplemented titleicons for efficiency.
......................................................................
Reimplemented titleicons for efficiency.
The api for titleicon retrieval has been removed. Titleicons are now
retrieved directly from the appropriate property instead of via custom api
call.
Change-Id: I9067393f63e43e8eefb3d4b724f2d2d96c8a26b5
---
D ApiHBGetTitleIcons.php
M HierarchyBuilder.class.php
M HierarchyBuilder.php
3 files changed, 97 insertions(+), 199 deletions(-)
Approvals:
Kji: Verified; Looks good to me, approved
diff --git a/ApiHBGetTitleIcons.php b/ApiHBGetTitleIcons.php
deleted file mode 100644
index fffb826..0000000
--- a/ApiHBGetTitleIcons.php
+++ /dev/null
@@ -1,133 +0,0 @@
-<?php
-/*
- * Copyright (c) 2014 The MITRE Corporation
- *
- * Permission is hereby granted, free of charge, to any person obtaining a
- * copy of this software and associated documentation files (the "Software"),
- * to deal in the Software without restriction, including without limitation
- * the rights to use, copy, modify, merge, publish, distribute, sublicense,
- * and/or sell copies of the Software, and to permit persons to whom the
- * Software is furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
- * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
- * DEALINGS IN THE SOFTWARE.
- */
-
-class ApiHBGetTitleIcons extends ApiBase {
- public function __construct( $main, $action ) {
- parent::__construct( $main, $action );
- }
-
- public function execute() {
- $pageTitle = $this->getMain()->getVal( 'pageTitle' );
- $titleIconProperty = $this->getMain()->getVal(
'titleIconProperty' );
-
- //global $TitleIcon_TitleIconPropertyName;
- $myTitleIconName = $titleIconProperty;
-
- $pageNameWithSpaces = str_replace( '_', ' ', $pageTitle );
- $titleIconWithSpaces = str_replace( '+', ' ', $myTitleIconName
);
-
- $api = new ApiMain(
- new DerivativeRequest(
- $this->getRequest(),
- array(
- 'action' => 'askargs',
- 'conditions' => $pageTitle,
- 'printouts' => $titleIconWithSpaces
- )
- ),
- false
- );
-
- $api->execute();
- $data = $api->getResultData();
-
- if ( is_array( $data["query"]["results"] ) && count(
$data["query"]["results"] ) == 0 ) {
- $this->getResult()->addValue( null,
$this->getModuleName(),
- array( 'pageTitle' => $pageTitle,
- 'titleIcons' => array()
- ) );
-
- return true;
- }
-
- if ( array_key_exists( $pageNameWithSpaces,
$data["query"]["results"] ) )
- $titleIconNames =
$data["query"]["results"]["$pageNameWithSpaces"]["printouts"]["$titleIconWithSpaces"];
- else {
- $key = array_shift( array_keys(
$data["query"]["results"] ) );
- $titleIconNames =
$data["query"]["results"][$key]["printouts"]["$titleIconWithSpaces"];
- }
- $titleIconURLs = array();
-
- foreach ( $titleIconNames as $name ) {
-
- $api = new ApiMain(
- new DerivativeRequest(
- $this->getRequest(),
- array(
- 'action' => 'query',
- 'titles' => 'File:' . $name,
- 'prop' => 'imageinfo',
- 'iiprop' => 'url'
- )
- ),
- false
- );
-
- $api->execute();
- $data = $api->getResultData();
- $keys = array_keys( $data['query']['pages'] );
- $key = array_shift( $keys );
- $url =
$data["query"]["pages"][$key]["imageinfo"][0]["url"];
- $titleIconURLs[] = $url;
- }
-
- $this->getResult()->addValue( null, $this->getModuleName(),
- array( 'pageTitle' => $pageTitle,
- 'titleIcons' => $titleIconURLs
- ) );
-
- return true;
-
- }
- public function getDescription() {
- return "Get the URLs of all Title Icons for the page, if any
exist.
-
-Note that because the returned value is a JSON object, you must specify ".
-"format=json in this query; the default xml format will return only an error.";
- }
- public function getAllowedParams() {
- return array(
- 'pageTitle' => array(
- ApiBase::PARAM_TYPE => 'string',
- ApiBase::PARAM_REQUIRED => true
- ),
- 'titleIconProperty' => array(
- ApiBase::PARAM_TYPE => 'string',
- ApiBase::PARAM_REQUIRED => true
- )
- );
- }
- public function getParamDescription() {
- return array(
- 'pageTitle' => 'title of the page whose title icons you
wish to retrieve',
- 'titleIconProperty' => 'name of the property containing
the title icon for a page.'
- );
- }
- public function getExamples() {
- return array(
-
'api.php?action=getTitleIcons&pageTitle=Test_Page_C&titleIconProperty=Logo_Link&format=jsonfm'
);
- }
- public function getHelpUrls() {
- return '';
- }
-}
diff --git a/HierarchyBuilder.class.php b/HierarchyBuilder.class.php
index 215af4d..c885ed5 100644
--- a/HierarchyBuilder.class.php
+++ b/HierarchyBuilder.class.php
@@ -510,14 +510,9 @@
if ( isset( $attributes['titleiconproperty'] ) ) {
$titleiconproperty =
htmlspecialchars(
$attributes['titleiconproperty'] );
- //var_dump($titleiconproperty);
} else {
$titleiconproperty = '';
}
- //$titleicons = 'Logo ';
- //print_r("what the heck: " . $input);
-
- //print_r('titleiconproperty = ' . $titleiconproperty);
// this looks like it gets the property but it eats all the
links.
$input = $parser->recursiveTagParse( $input, $frame );
@@ -545,45 +540,19 @@
}
$titleiconArray = array();
- $pagetitleicon = '';
+ $pagetitleicons = '';
if ( strlen( $titleiconproperty ) > 0 ) {
- $pagetitleicon =
HierarchyBuilder::getPageTitleIcons( $pageName,
+ $pagetitleicons =
HierarchyBuilder::getPageTitleIcons( $pageName,
$titleiconproperty );
-
- /*if ( strlen($pagetitleicon) == 0 ) {
- global
$TitleIcon_TitleIconPropertyName;
- $pagetitleicon =
HierarchyBuilder::getPageTitleIcons( $pageName,
-
$TitleIcon_TitleIconPropertyName );
- }*/
-
- $titleiconArray['src'] = $pagetitleicon;
- $titleiconArray['class'] =
'hierarchy_row_titleicon';
}
$iconElement = '';
- if ( $pagetitleicon !== '' ) {
- $iconElement = Html::element( 'img',
$titleiconArray );
+ if ( $pagetitleicons !== '' ) {
+ $iconElement =
HierarchyBuilder::getIconHTML($pagetitleicons);
}
return $iconElement . Html::element( 'a',
$pageLinkArray, $displayName );
} );
-
- /***
- $hierarchy = HierarchyBuilder::parseHierarchy( $input,
- $titleiconproperty, $dummy,
- function ( $pageName, $titleiconproperty, $data ) {
- $pageLinkArray = array();
- $title = Title::newFromText( $pageName );
- if ( $title ) {
- $pageLinkArray['href'] =
$title->getLinkURL();
- }
- if ( strlen( $titleiconproperty ) > 0 ) {
- $pageName =
HierarchyBuilder::getPageTitleIcons( $pageName,
- $titleiconproperty );
- }
- return Html::element( 'a', $pageLinkArray,
$pageName );
- } );
- ***/
$parser->getOutput()->addModules( 'ext.HierarchyBuilder.render'
);
@@ -751,11 +720,12 @@
*
* @param string $page: Name of the page from which to retrieve a
property.
* @param string $property: Name of the property that should be
returned.
+ * @param boolean $firstonly: Determine if only the first value is
returned.
*
* @return string: The value of the specified property from the given
page
* or the empty string if the property does not exist.
*/
- public static function getPropertyFromPage( $page, $property ) {
+ public static function getPropertyFromPage( $page, $property,
$firstonly = true ) {
$store = smwfGetStore();
$title = Title::newFromText( $page );
$subject = SMWDIWikiPage::newFromTitle( $title );
@@ -767,11 +737,19 @@
foreach ( $values as $value ) {
if ( $value->getDIType() == SMWDataItem::TYPE_STRING ||
$value->getDIType() == SMWDataItem::TYPE_BLOB )
{
- return trim( $value->getString() );
+ if ($firstonly){
+ return trim( $value->getString() );
+ } else {
+ $strings[] = trim( $value->getString()
);
+ }
}
}
- // return $strings;
- return '';
+ if ( $firstonly ) {
+ return '';
+ } else {
+ return $strings;
+ }
+
}
/**
@@ -1035,42 +1013,95 @@
}
/**
- * This function gives the titleicon url for the specified page when
using
+ * This function constructs the img html elements to display each of the
+ * given titleicons.
+ *
+ * @param array $icons: The array of pagename, titleicon pairs to be
shown.
+ *
+ * @return string: The html for rendering all of the titleicons.
+ */
+ public static function getIconHTML( $icons ) {
+ $iconhtml = "";
+ foreach ( $icons as $iconinfo ) {
+
+ $page = $iconinfo["page"];
+ $icon = $iconinfo["icon"];
+
+ $filetitle = Title::newFromText( "File:" . $icon );
+ $imagefile = wfFindFile( $filetitle );
+
+ if ( $imagefile !== false ) {
+
+ $tooltip = $page;
+
+ $frameParams = array();
+ //$frameParams['link-title'] = $page;
+ $frameParams['alt'] = $tooltip;
+ $frameParams['title'] = $tooltip;
+ $handlerParams = array(
+ 'width' => '15',
+ 'height' => '15'
+ );
+
+ $iconhtml .= Linker::makeImageLink(
$GLOBALS['wgParser'],
+ $filetitle, $imagefile, $frameParams,
$handlerParams ) .
+ " ";
+ }
+
+ }
+
+ //extract just the guts of just the img part of the html
+ $imgpattern = '/\<img (.*) \/\>/';
+ $numMatches = preg_match_all( $imgpattern, $iconhtml, $matches
);
+
+ // build the new image html thing
+ $iconhtml = '<img class=\"hierarchy_row_titleicon\" ' .
$matches[1][0] . '/>';
+
+ return $iconhtml;
+ }
+
+ /**
+ * This function gives the titleicons for the specified page when using
* titleicons.
*
* @param string $page: Name of the page.
* @param string $titleIconProperty: Name of the property that stores
* titleicon urls for pages when titleicons are active.
*
- * @return string: The titleicon url of the specified page.
+ * @return array: The pagename, titleiconname pairs for the specified
page.
*/
public static function getPageTitleIcons( $page, $titleIconProperty ) {
- global $wgRequest;
+ // get the title icons for this page
+ $discoveredIcons =
+ HierarchyBuilder::getPropertyFromPage( $page,
$titleIconProperty, false );
- if ( strlen( $titleIconProperty ) == 0 ) {
- return '';
+ $icons = array();
+ if ( $discoveredIcons ) {
+
+ foreach ( $discoveredIcons as $icon ) {
+
+ $found = false;
+ foreach ( $icons as $foundIcon ) {
+
+ if ( $foundIcon["icon"] === $icon ) {
+ $found = true;
+ break;
+ }
+
+ }
+
+ if ( $found == false ) {
+ $entry = array();
+ $entry["page"] = $page;
+ $entry["icon"] = $icon;
+ $icons[] = $entry;
+
+ }
+
+ }
+
}
-
- $api = new ApiMain(
- new DerivativeRequest(
- $wgRequest,
- array(
- 'action' => 'hbGetTitleIcons',
- 'pageTitle' => $page,
- 'titleIconProperty' =>
$titleIconProperty
- )
- ),
- false
- );
- $api->execute();
- $data = $api->getResultData();
-
- $titleiconURLs = $data['hbGetTitleIcons']['titleIcons'];
- if ( count( $titleiconURLs ) > 0 ) {
- return $titleiconURLs[0];
- } else {
- return '';
- }
+ return $icons;
}
}
diff --git a/HierarchyBuilder.php b/HierarchyBuilder.php
index b387c3f..192b05c 100644
--- a/HierarchyBuilder.php
+++ b/HierarchyBuilder.php
@@ -46,7 +46,7 @@
$wgExtensionCredits['parserhook'][] = array (
'path' => __FILE__,
'name' => 'HierarchyBuilder',
- 'version' => '1.9.0',
+ 'version' => '1.9.1',
'author' => array(
'[https://www.mediawiki.org/wiki/User:Cindy.cicalese Cindy
Cicalese]',
'[https://www.mediawiki.org/wiki/User:Kevin.ji Kevin Ji]'
--
To view, visit https://gerrit.wikimedia.org/r/239160
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I9067393f63e43e8eefb3d4b724f2d2d96c8a26b5
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/HierarchyBuilder
Gerrit-Branch: master
Gerrit-Owner: Kji <[email protected]>
Gerrit-Reviewer: Kji <[email protected]>
Gerrit-Reviewer: jenkins-bot <>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits