r89200 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89199‎ | r89200 | r89201 >
Date:04:21, 31 May 2011
Author:yaron
Status:deferred
Tags:
Comment:
* Modified getSMWPropertyValues() to try to return an array of actual values, instead of objects
* Tried to improve handling for SMW 1.6
* Added addToAdminLinks() method (formerly a global function)
* Declared all functions explicitly public
* Other small changes
Modified paths:
  • /trunk/extensions/SemanticForms/includes/SF_Utils.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/includes/SF_Utils.php
@@ -58,7 +58,7 @@
5959 * A very similar function to titleURLString(), to get the
6060 * non-URL-encoded title string
6161 */
62 - static function titleString( $title ) {
 62+ public static function titleString( $title ) {
6363 $namespace = $title->getNsText();
6464 if ( $namespace != '' ) {
6565 $namespace .= ':';
@@ -83,12 +83,31 @@
8484 } else {
8585 $page = new SMWDIWikiPage( $pageName, $pageNamespace, null );
8686 }
87 - $property = new SMWDIProperty( $propID );
88 - return $store->getPropertyValues( $page, $property, $requestOptions );
 87+ $property = SMWDIProperty::newFromUserLabel( $propID );
 88+ $res = $store->getPropertyValues( $page, $property, $requestOptions );
 89+ $values = array();
 90+ foreach ( $res as $value ) {
 91+ // getSortKey() seems to return the correct
 92+ // value for every data type.
 93+ $values[] = $value->getSortKey();
 94+ }
 95+ return $values;
8996 } else {
9097 $title = Title::makeTitleSafe( $pageNamespace, $pageName );
9198 $property = SMWPropertyValue::makeProperty( $propID );
92 - return $store->getPropertyValues( $title, $property, $requestOptions );
 99+ $res = $store->getPropertyValues( $title, $property, $requestOptions );
 100+ $values = array();
 101+ foreach ( $res as $value ) {
 102+ if ( method_exists( $value, 'getTitle' ) ) {
 103+ $title = $value->getTitle();
 104+ if ( !is_null( $title ) ) {
 105+ $values[] = $title->getText();
 106+ }
 107+ } else {
 108+ $values[] = str_replace( '_' , ' ', $title->getWikiValue() );
 109+ }
 110+ }
 111+ return array_unique( $values );
93112 }
94113 }
95114
@@ -98,7 +117,7 @@
99118 * - this function doubles as a function to get all categories on
100119 * the site, if no article is specified
101120 */
102 - static function getCategoriesForPage( $title = null ) {
 121+ public static function getCategoriesForPage( $title = null ) {
103122 $categories = array();
104123 $db = wfGetDB( DB_SLAVE );
105124 $conditions = null;
@@ -121,7 +140,7 @@
122141 return $categories;
123142 }
124143
125 - static function registerProperty( $id, $typeid, $label ) {
 144+ public static function registerProperty( $id, $typeid, $label ) {
126145 if ( class_exists( 'SMWDIProperty' ) ) {
127146 SMWDIProperty::registerProperty( $id, $typeid, $label, true );
128147 } else {
@@ -129,7 +148,7 @@
130149 }
131150 }
132151
133 - static function initProperties() {
 152+ public static function initProperties() {
134153 global $sfgContLang;
135154 $sf_props = $sfgContLang->getPropertyLabels();
136155 if ( array_key_exists( SF_SP_HAS_DEFAULT_FORM, $sf_props ) )
@@ -155,7 +174,7 @@
156175 /**
157176 * Creates HTML linking to a wiki page
158177 */
159 - static function linkText( $namespace, $name, $text = null ) {
 178+ public static function linkText( $namespace, $name, $text = null ) {
160179 $title = Title::makeTitleSafe( $namespace, $name );
161180 if ( is_null( $title ) ) {
162181 return $name; // TODO maybe report an error here?
@@ -172,7 +191,7 @@
173192 * allows pages to spoof a normal edit page, that can preview, save,
174193 * etc.
175194 */
176 - static function printRedirectForm( $title, $page_contents, $edit_summary, $is_save, $is_preview, $is_diff, $is_minor_edit, $watch_this, $start_time, $edit_time ) {
 195+ public static function printRedirectForm( $title, $page_contents, $edit_summary, $is_save, $is_preview, $is_diff, $is_minor_edit, $watch_this, $start_time, $edit_time ) {
177196 global $wgUser, $sfgScriptPath;
178197
179198 if ( $is_save ) {
@@ -189,17 +208,17 @@
190209 <p style="position: absolute; left: 45%; top: 45%;"><img src="$sfgScriptPath/skins/loading.gif" /></p>
191210
192211 END;
193 - $form_body = "\t" . Html::Hidden( 'wpTextbox1', $page_contents ) . "\n";
194 - $form_body .= "\t" . Html::Hidden( 'wpSummary', $edit_summary ) . "\n";
195 - $form_body .= "\t" . Html::Hidden( 'wpStarttime', $start_time ) . "\n";
196 - $form_body .= "\t" . Html::Hidden( 'wpEdittime', $edit_time ) . "\n";
197 - $form_body .= "\t" . Html::Hidden( 'wpEditToken', $wgUser->isLoggedIn() ? $wgUser->editToken() : EDIT_TOKEN_SUFFIX ) . "\n";
198 - $form_body .= "\t" . Html::Hidden( $action, null ) . "\n";
 212+ $form_body = "\t" . Html::hidden( 'wpTextbox1', $page_contents ) . "\n";
 213+ $form_body .= "\t" . Html::hidden( 'wpSummary', $edit_summary ) . "\n";
 214+ $form_body .= "\t" . Html::hidden( 'wpStarttime', $start_time ) . "\n";
 215+ $form_body .= "\t" . Html::hidden( 'wpEdittime', $edit_time ) . "\n";
 216+ $form_body .= "\t" . Html::hidden( 'wpEditToken', $wgUser->isLoggedIn() ? $wgUser->editToken() : EDIT_TOKEN_SUFFIX ) . "\n";
 217+ $form_body .= "\t" . Html::hidden( $action, null ) . "\n";
199218
200219 if ( $is_minor_edit )
201 - $form_body .= "\t" . Html::Hidden( 'wpMinoredit' , null ) . "\n";
 220+ $form_body .= "\t" . Html::hidden( 'wpMinoredit' , null ) . "\n";
202221 if ( $watch_this )
203 - $form_body .= "\t" . Html::Hidden( 'wpWatchthis', null ) . "\n";
 222+ $form_body .= "\t" . Html::hidden( 'wpWatchthis', null ) . "\n";
204223 $text .= Xml::tags(
205224 'form',
206225 array(
@@ -227,7 +246,7 @@
228247 * Uses the ResourceLoader (available with MediaWiki 1.17 and higher)
229248 * to load all the necessary JS and CSS files for Semantic Forms.
230249 */
231 - static function loadJavascriptAndCSS( $parser = null ) {
 250+ public static function loadJavascriptAndCSS( $parser = null ) {
232251 // Handling depends on whether or not this form is embedded
233252 // in another page.
234253 if ( !is_null( $parser ) ) {
@@ -248,7 +267,7 @@
249268 * Javascript files to be added regardless of the MediaWiki version
250269 * (i.e., even if the ResourceLoader is installed).
251270 */
252 - static function addJavascriptFiles( $parser ) {
 271+ public static function addJavascriptFiles( $parser ) {
253272 global $wgOut, $wgFCKEditorDir, $wgScriptPath, $wgJsMimeType;
254273
255274 $scripts = array();
@@ -278,7 +297,7 @@
279298 *
280299 * Accepts an optional Parser instance, or uses $wgOut if omitted.
281300 */
282 - static function addJavascriptAndCSS( $parser = null ) {
 301+ public static function addJavascriptAndCSS( $parser = null ) {
283302 global $wgOut;
284303
285304 if ( !$parser ) {
@@ -367,7 +386,7 @@
368387 /**
369388 * Return an array of all form names on this wiki
370389 */
371 - static function getAllForms() {
 390+ public static function getAllForms() {
372391 $dbr = wfGetDB( DB_SLAVE );
373392 $res = $dbr->select( 'page',
374393 'page_title',
@@ -383,7 +402,7 @@
384403 return $form_names;
385404 }
386405
387 - static function formDropdownHTML() {
 406+ public static function formDropdownHTML() {
388407 // create a dropdown of possible form names
389408 global $sfgContLang;
390409 $namespace_labels = $sfgContLang->getNamespaces();
@@ -391,10 +410,9 @@
392411 $form_names = SFUtils::getAllForms();
393412 $select_body = "";
394413 foreach ( $form_names as $form_name ) {
395 - $select_body .= ' ' . Xml::element( 'option', null, $form_name ) . "\n";
 414+ $select_body .= "\t" . Xml::element( 'option', null, $form_name ) . "\n";
396415 }
397 - $str = " $form_label:" . Xml::tags( 'select', array( 'name' => 'form' ), $select_body ) . "\n";
398 - return $str;
 416+ return "\t$form_label:" . Xml::tags( 'select', array( 'name' => 'form' ), $select_body ) . "\n";
399417 }
400418
401419 /*
@@ -403,21 +421,13 @@
404422 * case-insensitive queries; for queries with a substring, the
405423 * function SFAutocompleteAPI::getAllValuesForProperty() exists.
406424 */
407 - static function getAllValuesForProperty( $property_name ) {
 425+ public static function getAllValuesForProperty( $property_name ) {
408426 global $sfgMaxAutocompleteValues;
409427
410428 $store = smwfGetStore();
411429 $requestoptions = new SMWRequestOptions();
412430 $requestoptions->limit = $sfgMaxAutocompleteValues;
413 - $data_values = self::getSMWPropertyValues( $store, null, null, $property_name, $requestoptions );
414 - $values = array();
415 - foreach ( $data_values as $dv ) {
416 - // getPropertyValues() gets many repeat values - we want
417 - // only one of each value
418 - $string_value = str_replace( '_', ' ', $dv->getWikiValue() );
419 - if ( array_search( $string_value, $values ) === false )
420 - $values[] = $string_value;
421 - }
 431+ $values = self::getSMWPropertyValues( $store, null, null, $property_name, $requestoptions );
422432 sort( $values );
423433 return $values;
424434 }
@@ -427,7 +437,7 @@
428438 * subcategories, down a certain number of levels - heavily based on
429439 * SMW's SMWInlineQuery::includeSubcategories()
430440 */
431 - static function getAllPagesForCategory( $top_category, $num_levels, $substring = null ) {
 441+ public static function getAllPagesForCategory( $top_category, $num_levels, $substring = null ) {
432442 if ( 0 == $num_levels ) return $top_category;
433443 global $sfgMaxAutocompleteValues;
434444
@@ -500,7 +510,7 @@
501511 return $pages;
502512 }
503513
504 - static function getAllPagesForConcept( $concept_name, $substring = null ) {
 514+ public static function getAllPagesForConcept( $concept_name, $substring = null ) {
505515 global $sfgMaxAutocompleteValues;
506516
507517 $store = smwfGetStore();
@@ -542,7 +552,7 @@
543553 return $pages;
544554 }
545555
546 - static function getAllPagesForNamespace( $namespace_name, $substring = null ) {
 556+ public static function getAllPagesForNamespace( $namespace_name, $substring = null ) {
547557 // cycle through all the namespace names for this language, and
548558 // if one matches the namespace specified in the form, add the
549559 // names of all the pages in that namespace to $names_array
@@ -581,7 +591,7 @@
582592 * Creates an array of values that match the specified source name and type,
583593 * for use by both Javascript autocompletion and comboboxes.
584594 */
585 - static function getAutocompleteValues( $source_name, $source_type ) {
 595+ public static function getAutocompleteValues( $source_name, $source_type ) {
586596 $names_array = array();
587597 // the query depends on whether this is a property, category, concept
588598 // or namespace
@@ -604,7 +614,7 @@
605615 * Helper function to get an array of values out of what may be either
606616 * an array or a delimited string
607617 */
608 - static function getValuesArray( $value, $delimiter ) {
 618+ public static function getValuesArray( $value, $delimiter ) {
609619 if ( is_array( $value ) ) {
610620 return $value;
611621 } else {
@@ -613,7 +623,7 @@
614624 }
615625 }
616626
617 - static function getValuesFromExternalURL( $external_url_alias, $substring ) {
 627+ public static function getValuesFromExternalURL( $external_url_alias, $substring ) {
618628 global $sfgAutocompletionURLs;
619629 if ( empty( $sfgAutocompletionURLs ) ) return array();
620630 $url = $sfgAutocompletionURLs[$external_url_alias];
@@ -633,7 +643,7 @@
634644 /**
635645 * A helper function, used by getFormTagComponents().
636646 */
637 - static function convertBackToPipes( $s ) {
 647+ public static function convertBackToPipes( $s ) {
638648 return str_replace( "\1", '|', $s );
639649 }
640650
@@ -643,7 +653,7 @@
644654 * that are within parser function calls - i.e., pipes within
645655 * double curly brackets.
646656 */
647 - static function getFormTagComponents( $str ) {
 657+ public static function getFormTagComponents( $str ) {
648658 // Turn each pipe within double curly brackets into another,
649659 // unused character (here, "\1"), then do the explode, then
650660 // convert them back.
@@ -658,7 +668,7 @@
659669 * Parse the form definition and store the resulting HTML in the
660670 * page_props table, if caching has been specified in LocalSettings.php
661671 */
662 - static function cacheFormDefinition( $parser, $text ) {
 672+ public static function cacheFormDefinition( $parser, $text ) {
663673 global $sfgCacheFormDefinitions;
664674 if ( ! $sfgCacheFormDefinitions )
665675 return true;
@@ -694,11 +704,11 @@
695705 }
696706
697707 /**
698 - * Tranlates an EditPage storing error into the corresponding message id
 708+ * Translates an EditPage error code into a corresponding message ID
699709 * @param $error The error code
700710 * @return String
701711 */
702 - static function processEditErrors ( $error ) {
 712+ public static function processEditErrors ( $error ) {
703713
704714 switch ( $error ) {
705715 case EditPage::AS_SUCCESS_NEW_ARTICLE:
@@ -752,4 +762,26 @@
753763 }
754764 }
755765
 766+ public static function addToAdminLinks( &$admin_links_tree ) {
 767+ $data_structure_label = wfMsg( 'smw_adminlinks_datastructure' );
 768+ $data_structure_section = $admin_links_tree->getSection( $data_structure_label );
 769+ if ( is_null( $data_structure_section ) ) {
 770+ return true;
 771+ }
 772+ $smw_row = $data_structure_section->getRow( 'smw' );
 773+ $smw_row->addItem( ALItem::newFromSpecialPage( 'Templates' ), 'Properties' );
 774+ $smw_row->addItem( ALItem::newFromSpecialPage( 'Forms' ), 'SemanticStatistics' );
 775+ $smw_admin_row = $data_structure_section->getRow( 'smw_admin' );
 776+ $smw_admin_row->addItem( ALItem::newFromSpecialPage( 'CreateClass' ), 'SMWAdmin' );
 777+ $smw_admin_row->addItem( ALItem::newFromSpecialPage( 'CreateProperty' ), 'SMWAdmin' );
 778+ $smw_admin_row->addItem( ALItem::newFromSpecialPage( 'CreateTemplate' ), 'SMWAdmin' );
 779+ $smw_admin_row->addItem( ALItem::newFromSpecialPage( 'CreateForm' ), 'SMWAdmin' );
 780+ $smw_admin_row->addItem( ALItem::newFromSpecialPage( 'CreateCategory' ), 'SMWAdmin' );
 781+ $smw_docu_row = $data_structure_section->getRow( 'smw_docu' );
 782+ $sf_name = wfMsg( 'specialpages-group-sf_group' );
 783+ $sf_docu_label = wfMsg( 'adminlinks_documentation', $sf_name );
 784+ $smw_docu_row->addItem( ALItem::newFromExternalLink( "http://www.mediawiki.org/wiki/Extension:Semantic_Forms", $sf_docu_label ) );
 785+
 786+ return true;
 787+ }
756788 }

Follow-up revisions

RevisionCommit summaryAuthorDate
r89203Fixes for bugs in r89200yaron04:36, 31 May 2011
r89256More fixes for r89200yaron13:47, 1 June 2011