We discussed this on the [[ https://www.mediawiki.org/wiki/Extension_talk:Cargo/Archive_January_to_February_2017 | talk page]] under the heading "Show List of Strings as links to Special:Drilldown".
Here is a patch which allows the user to add an extra "add drilldown links" parameter to #cargo_query so that, for selected fields, each value is automatically displayed as a link to Special:Drilldown with the relevant filter applied.
Currently it works for (a) Lists of String, and (b) String field types.
- To convert individual items in a List to links it is necessary to amend Cargo code (outside a List we could just use CONCAT in a query).
- You may think it ought to apply to other types which get a filter on Special:Drilldown (Page, Integer, Float, Date and Datetime). I think Page should be left alone (i.e. a red link if non-existent), and you mentioned practical problems with doing it for Date/Datetime, and I guess Float too (?) - perhaps it should just be extended to Integer.
The automatic drilldown links have the Miga magnifying glass beside them to indicate that they are not normal links.
I have used global variables to create the links but I imagine there is a better way using MediaWiki's classes.
The new parameter takes a list of fields, whose values are to be displayed as Special:Drilldown links. It can be used in the following format.
```
{{#cargo_query:
tables=Contact
|fields= _pageName, Address, Telephones_numbers, Fax_number
|format=dynamic table
|add drilldown links=Address, Telephone_numbers
}}
```
I've not finished it or tested it fully, but would appreciate your thoughts!
```
diff --git a/CargoQueryDisplayer.php b/CargoQueryDisplayer.php
index f37bc7c..90d2b52 100644
--- a/CargoQueryDisplayer.php
+++ b/CargoQueryDisplayer.php
@@ -86,6 +86,13 @@ class CargoQueryDisplayer {
public function getFormattedQueryResults( $queryResults ) {
// The assignment will do a copy.
$formattedQueryResults = $queryResults;
+
+ if ( array_key_exists( 'add drilldown links', $this->mDisplayParams ) ) {
+ $drillDownFields = explode (',', $this->mDisplayParams['add drilldown links']);
+ #Allow users to separate fields with comma, with or without spaces:
+ $drillDownFields = array_map('trim', $drillDownFields);
+ }
+
foreach ( $queryResults as $rowNum => $row ) {
foreach ( $row as $fieldName => $value ) {
if ( trim( $value ) == '' ) {
@@ -122,8 +129,19 @@ class CargoQueryDisplayer {
// list parsing worked.
$text .= " · ";
}
- $text .= self::formatFieldValue( $fieldValue, $fieldType, $fieldDescription, $this->mParser );
- }
+
+ if ( (in_array( $fieldName, $drillDownFields ) ) && ($fieldType == 'String') ) {
+ # Add drilldown links if requested by user (for Lists of Strings)
+ global $wgServer, $wgScriptPath;
+ $text .= '<a href="' . $wgServer . $wgScriptPath . '/index.php?title=Special:Drilldown/' . $tableName . '&' . $fieldName . '=' . $fieldValue . '" class="drilldownlink">';
+ $text .= self::formatFieldValue( $fieldValue, $fieldType, $fieldDescription, $this->mParser );
+ $text .= '</a>';
+ } else {
+ # Default: do not add drilldown links
+ $text .= self::formatFieldValue( $fieldValue, $fieldType, $fieldDescription, $this->mParser );
+ }
+
+ }
} elseif ( $fieldType == 'Date' || $fieldType == 'Datetime' ) {
$datePrecisionField = str_replace( '_', ' ', $fieldName ) . '__precision';
if ( array_key_exists( $datePrecisionField, $row ) ) {
@@ -150,6 +168,12 @@ class CargoQueryDisplayer {
} elseif ( $fieldType == 'Searchtext' && $this->mSQLQuery && array_key_exists( $fieldName, $this->mSQLQuery->mSearchTerms ) ) {
$searchTerms = $this->mSQLQuery->mSearchTerms[$fieldName];
$text = Html::rawElement( 'span', array( 'class' => 'searchresult' ), self::getTextSnippet( $value, $searchTerms ) );
+ } elseif ( (in_array( $fieldName, $drillDownFields ) ) && ($fieldType == 'String') ) {
+ # Add drilldown links if requested by user (for Strings which aren't in Lists)
+ global $wgServer, $wgScriptPath;
+ $text .= '<a href="' . $wgServer . $wgScriptPath . '/index.php?title=Special:Drilldown/' . $tableName . '&' . $fieldName . '=' . $value . '" class="drilldownlink">';
+ $text .= self::formatFieldValue( $value, $fieldType, $fieldDescription, $this->mParser );
+ $text .= '</a>';
} else {
$text = self::formatFieldValue( $value, $fieldType, $fieldDescription, $this->mParser );
}
```