Yaron Koren has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/189749

Change subject: Added handling for Page Schemas extension
......................................................................

Added handling for Page Schemas extension

Change-Id: I7c901e59cfc0a4b4fdce98ffade3fb59bd6d93b6
---
M Cargo.php
A CargoPageSchemas.php
M i18n/en.json
3 files changed, 259 insertions(+), 2 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Cargo 
refs/changes/49/189749/1

diff --git a/Cargo.php b/Cargo.php
index 687be1d..e9fd0ac 100644
--- a/Cargo.php
+++ b/Cargo.php
@@ -8,7 +8,7 @@
 
 if ( !defined( 'MEDIAWIKI' ) ) die();
 
-define( 'CARGO_VERSION', '0.5.2' );
+define( 'CARGO_VERSION', '0.6-alpha' );
 
 $wgExtensionCredits['parserhook'][] = array(
        'path' => __FILE__,
@@ -43,6 +43,7 @@
 $wgHooks['UnknownAction'][] = 'CargoPageValuesAction::show';
 $wgHooks['SkinTemplateNavigation'][] = 'CargoHooks::addPurgeCacheTab';
 $wgHooks['AdminLinks'][] = 'CargoHooks::addToAdminLinks';
+$wgHooks['PageSchemasRegisterHandlers'][] = 'CargoPageSchemas::registerClass';
 
 $wgMessagesDirs['Cargo'] = $dir . '/i18n';
 $wgExtensionMessagesFiles['Cargo'] = $dir . '/Cargo.i18n.php';
@@ -103,6 +104,7 @@
 $wgAutoloadClasses['CargoTimelineFormat'] = $dir . 
'/formats/CargoTimelineFormat.php';
 $wgAutoloadClasses['CargoCategoryFormat'] = $dir . 
'/formats/CargoCategoryFormat.php';
 $wgAutoloadClasses['CargoBarChartFormat'] = $dir . 
'/formats/CargoBarChartFormat.php';
+$wgAutoloadClasses['CargoPageSchemas'] = $dir . '/CargoPageSchemas.php';
 
 // Drilldown
 $wgAutoloadClasses['CargoAppliedFilter'] = $dir . 
'/drilldown/CargoAppliedFilter.php';
diff --git a/CargoPageSchemas.php b/CargoPageSchemas.php
new file mode 100644
index 0000000..959a6fb
--- /dev/null
+++ b/CargoPageSchemas.php
@@ -0,0 +1,254 @@
+<?php
+/**
+ * Static functions for use by the Page Schemas extension.
+ *
+ * @author Yaron Koren
+ */
+
+class CargoPageSchemas extends PSExtensionHandler {
+       public static function registerClass() {
+               global $wgPageSchemasHandlerClasses;
+               $wgPageSchemasHandlerClasses[] = 'CargoPageSchemas';
+               return true;
+       }
+
+       /**
+        * Returns an object containing information on a Cargo field,
+        * based on XML from the Page Schemas extension.
+        */
+       public static function createPageSchemasObject( $tagName, $xml ) {
+               $cargoArray = array();
+               if ( $tagName == "cargo_TemplateDetails" ) {
+                       foreach ( $xml->children() as $tag => $child ) {
+                               if ( $tag == $tagName ) {
+                                       foreach ( $child->children() as $tag => 
$elem ) {
+                                               $cargoArray[$tag] = 
(string)$elem;
+                                       }
+                                       return $cargoArray;
+                               }
+                       }
+               }
+               if ( $tagName == "cargo_Field" ) {
+                       foreach ( $xml->children() as $tag => $child ) {
+                               if ( $tag != $tagName ) {
+                                       continue;
+                               }
+                               $allowedValues = array();
+                               foreach ( $child->children() as $prop => 
$value) {
+                                       if ( $prop == "AllowedValue" ){
+                                               $allowedValues[] = 
(string)$value;
+                                       } else {
+                                               $cargoArray[$prop] = 
(string)$value;
+                                       }
+                               }
+                               $cargoArray['AllowedValues'] = $allowedValues;
+                               return $cargoArray;
+                       }
+               }
+
+               return null;
+       }
+
+       public static function getDisplayColor() {
+               return '#e9cdff';
+       }
+
+       public static function getTemplateDisplayString() {
+               return wfMessage( 'specialpages-group-cargo' )->escaped();
+       }
+
+       public static function getTemplateValues( $psTemplate ) {
+               // TODO - fix this.
+               $values = array();
+               if ( $psTemplate instanceof PSTemplate ) {
+                       $psTemplate = $psTemplate->getXML();
+               }
+               foreach ( $psTemplate->children() as $tag => $child ) {
+                       if ( $tag == "cargo_TemplateDetails" ) {
+                               foreach ( $child->children() as $prop ) {
+                                       $values[$prop->getName()] = 
(string)$prop;
+                               }
+                       }
+               }
+               return $values;
+       }
+
+       /**
+        * Displays Cargo details for one template in the Page Schemas XML.
+        */
+       public static function getTemplateDisplayValues( $templateXML ) {
+               $templateValues = self::getTemplateValues( $templateXML );
+               if ( count( $templateValues ) == 0 ) {
+                       return null;
+               }
+
+               $displayValues = array();
+               foreach ( $templateValues as $key => $value ) {
+                       if ( $key == 'Table' ) {
+                               $propName = 'Table';
+                       }
+                       $displayValues[$propName] = $value;
+               }
+               return array( null, $displayValues );
+       }
+
+       public static function getFieldDisplayString() {
+               return wfMessage( 'cargo-pageschemas-cargofield' )->text();
+       }
+
+       public static function isTemplateDataMultipleInstanceOnly() {
+               return false;
+       }
+
+       public static function getTemplateEditingHTML( $psTemplate ) {
+               $hasExistingValues = false;
+               $tableName = null;
+               if ( !is_null( $psTemplate ) ) {
+                       $cargoArray = $psTemplate->getObject( 
'cargo_TemplateDetails' );
+                       if ( !is_null( $cargoArray ) ) {
+                               $hasExistingValues = true;
+                               $tableName = PageSchemas::getValueFromObject( 
$cargoArray, 'Table' );
+                       }
+               }
+
+               $text = "\t<p>" . 'Table name:' . ' ' . Html::input( 
'cargo_template_table_name_num', $tableName, 'text', array( 'size' => 30 ) ) . 
"</p>\n";
+
+               return array( $text, $hasExistingValues );
+       }
+
+       /**
+        * Returns the HTML for setting the options for the Cargo section
+        * in Page Schemas' "edit schema" page.
+        */
+       public static function getFieldEditingHTML( $psField ){
+               global $wgCargoFieldTypes;
+
+               $cargoArray = array();
+               $hasExistingValues = false;
+               if ( !is_null( $psField ) ) {
+                       $cargoArray = $psField->getObject( 'cargo_Field' );
+                       if ( !is_null( $cargoArray ) ) {
+                               $hasExistingValues = true;
+                       }
+               }
+
+               $fieldType = PageSchemas::getValueFromObject( $cargoArray, 
'Type' );
+
+               $allowedValues = PageSchemas::getValueFromObject( $cargoArray, 
'AllowedValues' );
+               if ( is_null( $allowedValues ) ) {
+                       $allowedValuesString = '';
+               } else {
+                       $allowedValuesString = implode( ', ', $allowedValues );
+               }
+
+               $typeLabel = wfMessage( 'sf_createproperty_proptype' 
)->escaped();
+               if ( $typeLabel == '' ) {
+                       $typeLabel = 'Type:';
+               }
+               $allowedValuesLabel = wfMessage( 
'sf_createproperty_allowedvalsinput' )->escaped();
+               if ( $allowedValuesLabel == '' ) {
+                       $allowedValuesLabel = 'Type:';
+               }
+
+               $html_text = "<p>$typeLabel ";
+
+               $selectBody = '';
+               foreach ( $wgCargoFieldTypes as $type ) {
+                       $optionAttrs = array( 'value' => $type );
+                       if ( $type == $fieldType ) {
+                               $optionAttrs['selected'] = true;
+                       }
+                       $selectBody .= Html::element( 'option', $optionAttrs, 
$type ) . "\n";
+               }
+               $html_text .= Html::rawElement( 'select', array( 'name' => 
'cargo_field_type_num' ), $selectBody ) . "\n";
+               $html_text .= "<p>$allowedValuesLabel<br />\n";
+               $html_text .= Html::input( 'cargo_field_allowed_values_num', 
$allowedValuesString, 'text', array( 'size' => 100 ) );
+               $html_text .= "\t</p>\n";
+
+               return array( $html_text, $hasExistingValues );
+       }
+
+       /**
+        * Creates Page Schemas XML from Cargo information on templates.
+        */
+       public static function createTemplateXMLFromForm() {
+               global $wgRequest;
+
+               $xmlPerTemplate = array();
+               $templateNum = -1;
+               foreach ( $wgRequest->getValues() as $var => $val ) {
+                       $val = str_replace( array( '<', '>' ), array( '&lt;', 
'&gt;' ), $val );
+                       if ( substr( $var, 0, 26 ) == 
'cargo_template_table_name_' ) {
+                               $templateNum = substr( $var, 26 );
+                               $xml = '<cargo_TemplateDetails>';
+                               if ( !empty( $val ) ) {
+                                       $xml .= "<Table>$val</Table>";
+                               }
+                               $xml .= '</cargo_TemplateDetails>';
+                                                               
$xmlPerTemplate[$templateNum] = $xml;
+                       }
+               }
+               return $xmlPerTemplate;
+       }
+
+       public static function createFieldXMLFromForm() {
+               global $wgRequest;
+
+               $fieldNum = -1;
+               $xmlPerField = array();
+               foreach ( $wgRequest->getValues() as $var => $val ) {
+                       if ( substr( $var, 0, 17 ) == 'cargo_field_type_' ) {
+                               $xml = '<cargo_Field>';
+                               $fieldNum = substr( $var, 17 );
+                               if ( !empty( $val ) ) {
+                                       $xml .= "<Type>$val</Type>";
+                               }
+                       } elseif ( substr( $var, 0, 27 ) == 
'cargo_field_allowed_values_') {
+                               if ( !empty( $val ) ) {
+                                       // Replace the comma substitution 
character that has no chance of
+                                       // being included in the values list - 
namely, the ASCII beep.
+                                       $listSeparator = ',';
+                                       $allowedValuesStr = str_replace( 
"\\$listSeparator", "\a", $val );
+                                       $allowedValuesArray = explode( 
$listSeparator, $allowedValuesStr );
+                                       foreach ( $allowedValuesArray as $value 
) {
+                                               // Replace beep back with 
comma, trim.
+                                               $value = str_replace( "\a", 
$listSeparator, trim( $value ) );
+                                               $xml .= '<AllowedValue>' . 
$value . '</AllowedValue>';
+                                       }
+                               }
+                               $xml .= '</cargo_Field>';
+                               $xmlPerField[$fieldNum] = $xml;
+                       }
+               }
+
+               return $xmlPerField;
+       }
+
+       /**
+        * Displays the information about the Cargo field (if any)
+        * for one field in the Page Schemas XML.
+        */
+       public static function getFieldDisplayValues( $field_xml ) {
+               foreach ( $field_xml->children() as $tag => $child ) {
+                       if ( $tag == "cargo_Field" ) {
+                               $values = array();
+                               $allowedValues = array();
+                               foreach ( $child->children() as $prop => 
$value) {
+                                       if ( $prop == "AllowedValue" ) {
+                                               $allowedValues[] = $value;
+                                       } else {
+                                               $values[$prop] = $value;
+                                       }
+                               }
+                               $allowedValuesStr = implode( ', ', 
$allowedValues );
+                               $allowedValuesLabel = wfMessage( 
'sf_createclass_allowedvalues' )->escaped();
+                               if ( $allowedValuesLabel == '' ) {
+                                       $allowedValuesLabel = 'Allowed values:';
+                               }
+                               $values[$allowedValuesLabel] = 
$allowedValuesStr;
+                               return array( null, $values );
+                       }
+               }
+               return null;
+       }
+}
diff --git a/i18n/en.json b/i18n/en.json
index 59142c1..308664e 100644
--- a/i18n/en.json
+++ b/i18n/en.json
@@ -40,5 +40,6 @@
        "cargo-viewcsv": "View CSV",
        "cargo-viewjson": "View JSON",
        "cargo-purgecache": "Purge cache",
-       "specialpages-group-cargo": "Cargo"
+       "specialpages-group-cargo": "Cargo",
+       "cargo-pageschemas-cargofield": "Cargo field"
 }

-- 
To view, visit https://gerrit.wikimedia.org/r/189749
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I7c901e59cfc0a4b4fdce98ffade3fb59bd6d93b6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Cargo
Gerrit-Branch: master
Gerrit-Owner: Yaron Koren <[email protected]>

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

Reply via email to