00001 <?php
00010 class SFUtils {
00011
00015 public static function linkForSpecialPage( $skin, $specialPageName ) {
00016 $specialPage = SpecialPage::getPage( $specialPageName );
00017 return $skin->link( $specialPage->getTitle(), $specialPage->getDescription() );
00018 }
00019
00025 public static function titleURLString( $title ) {
00026 $namespace = wfUrlencode( $title->getNsText() );
00027 if ( $namespace !== '' ) {
00028 $namespace .= ':';
00029 }
00030 if ( MWNamespace::isCapitalized( $title->getNamespace() ) ) {
00031 global $wgContLang;
00032 return $namespace . $wgContLang->ucfirst( $title->getPartialURL() );
00033 } else {
00034 return $namespace . $title->getPartialURL();
00035 }
00036 }
00037
00042 public static function titleString( $title ) {
00043 $namespace = $title->getNsText();
00044 if ( $namespace !== '' ) {
00045 $namespace .= ':';
00046 }
00047 if ( MWNamespace::isCapitalized( $title->getNamespace() ) ) {
00048 global $wgContLang;
00049 return $namespace . $wgContLang->ucfirst( $title->getText() );
00050 } else {
00051 return $namespace . $title->getText();
00052 }
00053 }
00054
00059 public static function getSMWPropertyValues( $store, $subject, $propID, $requestOptions = null ) {
00060
00061 if ( class_exists( 'SMWDIProperty' ) ) {
00062 if ( is_null( $subject ) ) {
00063 $page = null;
00064 } else {
00065 $page = SMWDIWikiPage::newFromTitle( $subject );
00066 }
00067 $property = SMWDIProperty::newFromUserLabel( $propID );
00068 $res = $store->getPropertyValues( $page, $property, $requestOptions );
00069 $values = array();
00070 foreach ( $res as $value ) {
00071 if ( $value instanceof SMWDIUri ) {
00072 $values[] = $value->getURI();
00073 } else {
00074
00075
00076
00077 $values[] = str_replace( '_', ' ', $value->getSortKey() );
00078 }
00079 }
00080 return $values;
00081 } else {
00082 $property = SMWPropertyValue::makeProperty( $propID );
00083 $res = $store->getPropertyValues( $subject, $property, $requestOptions );
00084 $values = array();
00085 foreach ( $res as $value ) {
00086 if ( method_exists( $value, 'getTitle' ) ) {
00087 $valueTitle = $value->getTitle();
00088 if ( !is_null( $valueTitle ) ) {
00089 $values[] = $valueTitle->getText();
00090 }
00091 } else {
00092 $values[] = str_replace( '_' , ' ', $value->getWikiValue() );
00093 }
00094 }
00095 return array_unique( $values );
00096 }
00097 }
00104 public static function getCategoriesForPage( $title = null ) {
00105 $categories = array();
00106 $db = wfGetDB( DB_SLAVE );
00107 $conditions = null;
00108 if ( !is_null( $title ) ) {
00109 $titlekey = $title->getArticleID();
00110 if ( $titlekey == 0 ) {
00111
00112 return $categories;
00113 }
00114 $conditions['cl_from'] = $titlekey;
00115 }
00116 $res = $db->select(
00117 'categorylinks',
00118 'DISTINCT cl_to',
00119 $conditions,
00120 __METHOD__
00121 );
00122 if ( $db->numRows( $res ) > 0 ) {
00123 while ( $row = $db->fetchRow( $res ) ) {
00124 $categories[] = $row[0];
00125 }
00126 }
00127 $db->freeResult( $res );
00128 return $categories;
00129 }
00130
00131 public static function registerProperty( $id, $typeid, $label ) {
00132 if ( class_exists( 'SMWDIProperty' ) ) {
00133 SMWDIProperty::registerProperty( $id, $typeid, $label, true );
00134 } else {
00135 SMWPropertyValue::registerProperty( $id, $typeid, $label, true );
00136 }
00137 }
00138
00139 public static function initProperties() {
00140 global $sfgContLang;
00141
00142
00143
00144
00145
00146
00147 $sf_props = $sfgContLang->getPropertyLabels();
00148 if ( array_key_exists( SF_SP_HAS_DEFAULT_FORM, $sf_props ) ) {
00149 self::registerProperty( '_SF_DF', '__spf', $sf_props[SF_SP_HAS_DEFAULT_FORM] );
00150 } else {
00151 self::registerProperty( '_SF_DF', '__spf', 'Has default form' );
00152 }
00153 if ( array_key_exists( SF_SP_HAS_ALTERNATE_FORM, $sf_props ) ) {
00154 self::registerProperty( '_SF_AF', '__spf', $sf_props[SF_SP_HAS_ALTERNATE_FORM] );
00155 } else {
00156 self::registerProperty( '_SF_AF', '__spf', 'Has alternate form' );
00157 }
00158 if ( array_key_exists( SF_SP_CREATES_PAGES_WITH_FORM, $sf_props ) ) {
00159 self::registerProperty( '_SF_CP', '__spf', $sf_props[SF_SP_CREATES_PAGES_WITH_FORM] );
00160 } else {
00161 self::registerProperty( '_SF_CP', '__spf', 'Creates pages with form' );
00162 }
00163 if ( array_key_exists( SF_SP_PAGE_HAS_DEFAULT_FORM, $sf_props ) ) {
00164 self::registerProperty( '_SF_PDF', '__spf', $sf_props[SF_SP_PAGE_HAS_DEFAULT_FORM] );
00165 } else {
00166 self::registerProperty( '_SF_PDF', '__spf', 'Page has default form' );
00167 }
00168 if ( array_key_exists( SF_SP_HAS_FIELD_LABEL_FORMAT, $sf_props ) ) {
00169 self::registerProperty( '_SF_FLF', '_str', $sf_props[SF_SP_HAS_FIELD_LABEL_FORMAT] );
00170 } else {
00171 self::registerProperty( '_SF_FLF', '_str', 'Has field label format' );
00172 }
00173
00174
00175
00176 self::registerProperty( '_SF_DF_BACKUP', '__spf', 'Has default form' );
00177 self::registerProperty( '_SF_AF_BACKUP', '__spf', 'Has alternate form' );
00178 self::registerProperty( '_SF_CP_BACKUP', '__spf', 'Creates pages with form' );
00179 self::registerProperty( '_SF_PDF_BACKUP', '__spf', 'Page has default form' );
00180 self::registerProperty( '_SF_FLF_BACKUP', '_str', 'Has field label format' );
00181
00182 return true;
00183 }
00184
00188 public static function linkText( $namespace, $name, $text = null ) {
00189 $title = Title::makeTitleSafe( $namespace, $name );
00190 if ( is_null( $title ) ) {
00191 return $name;
00192 }
00193 if ( is_null( $text ) ) {
00194 return '[[:' . $title->getPrefixedText() . '|' . $name . ']]';
00195 } else {
00196 return '[[:' . $title->getPrefixedText() . '|' . $text . ']]';
00197 }
00198 }
00199
00205 public static function printRedirectForm( $title, $page_contents, $edit_summary, $is_save, $is_preview, $is_diff, $is_minor_edit, $watch_this, $start_time, $edit_time ) {
00206 global $wgUser, $sfgScriptPath;
00207
00208 if ( $is_save ) {
00209 $action = "wpSave";
00210 } elseif ( $is_preview ) {
00211 $action = "wpPreview";
00212 } else {
00213 $action = "wpDiff";
00214 }
00215
00216 $text = <<<END
00217 <p style="position: absolute; left: 45%; top: 45%;"><img src="$sfgScriptPath/skins/loading.gif" /></p>
00218
00219 END;
00220 $form_body = Html::hidden( 'wpTextbox1', $page_contents );
00221 $form_body .= Html::hidden( 'wpSummary', $edit_summary );
00222 $form_body .= Html::hidden( 'wpStarttime', $start_time );
00223 $form_body .= Html::hidden( 'wpEdittime', $edit_time );
00224 $form_body .= Html::hidden( 'wpEditToken', $wgUser->isLoggedIn() ? $wgUser->editToken() : EDIT_TOKEN_SUFFIX );
00225 $form_body .= Html::hidden( $action, null );
00226
00227 if ( $is_minor_edit ) {
00228 $form_body .= Html::hidden( 'wpMinoredit' , null );
00229 }
00230 if ( $watch_this ) {
00231 $form_body .= Html::hidden( 'wpWatchthis', null );
00232 }
00233 $text .= Html::rawElement(
00234 'form',
00235 array(
00236 'id' => 'editform',
00237 'name' => 'editform',
00238 'method' => 'post',
00239 'action' => $title instanceof Title ? $title->getLocalURL( 'action=submit' ) : $title
00240 ),
00241 $form_body
00242 );
00243
00244 $text .= <<<END
00245 <script type="text/javascript">
00246 window.onload = function() {
00247 document.editform.submit();
00248 }
00249 </script>
00250
00251 END;
00252 wfRunHooks( 'sfPrintRedirectForm', array( $is_save, $is_preview, $is_diff, &$text ) );
00253 return $text;
00254 }
00255
00259 public static function addJavascriptFiles( $parser ) {
00260 global $wgOut, $wgFCKEditorDir, $wgScriptPath, $wgJsMimeType;
00261
00262 $scripts = array();
00263
00264 wfRunHooks( 'sfAddJavascriptFiles', array( &$scripts ) );
00265
00266
00267
00268
00269 if ( $wgFCKEditorDir && class_exists( 'FCKEditor' ) ) {
00270 $scripts[] = "$wgScriptPath/$wgFCKEditorDir/fckeditor.js";
00271 }
00272
00273 foreach ( $scripts as $js ) {
00274 if ( $parser ) {
00275 $script = "<script type=\"$wgJsMimeType\" src=\"$js\"></script>\n";
00276 $parser->getOutput()->addHeadItem( $script );
00277 } else {
00278 $wgOut->addScriptFile( $js );
00279 }
00280 }
00281 }
00282
00289 public static function addJavascriptAndCSS( $parser = null ) {
00290 global $wgOut;
00291
00292
00293
00294 if ( !$parser ) {
00295 $wgOut->addMeta( 'robots', 'noindex,nofollow' );
00296 $output = $wgOut;
00297 } else {
00298 $output = $parser->getOutput();
00299 self::addJavascriptFiles( $parser );
00300 }
00301
00302 $output->addModules( 'ext.semanticforms.main' );
00303 $output->addModules( 'ext.semanticforms.fancybox' );
00304 $output->addModules( 'ext.semanticforms.imagepreview' );
00305 $output->addModules( 'ext.semanticforms.autogrow' );
00306 $output->addModules( 'ext.semanticforms.submit' );
00307 $output->addModules( 'ext.smw.tooltips' );
00308 $output->addModules( 'ext.smw.sorttable' );
00309 }
00310
00314 public static function getAllForms() {
00315 $dbr = wfGetDB( DB_SLAVE );
00316 $res = $dbr->select( 'page',
00317 'page_title',
00318 array( 'page_namespace' => SF_NS_FORM,
00319 'page_is_redirect' => false ),
00320 __METHOD__,
00321 array( 'ORDER BY' => 'page_title' ) );
00322 $form_names = array();
00323 while ( $row = $dbr->fetchRow( $res ) ) {
00324 $form_names[] = str_replace( '_', ' ', $row[0] );
00325 }
00326 $dbr->freeResult( $res );
00327 return $form_names;
00328 }
00329
00333 public static function formDropdownHTML() {
00334 global $wgContLang;
00335 $namespace_labels = $wgContLang->getNamespaces();
00336 $form_label = $namespace_labels[SF_NS_FORM];
00337 $form_names = SFUtils::getAllForms();
00338 $select_body = "\n";
00339 foreach ( $form_names as $form_name ) {
00340 $select_body .= "\t" . Html::element( 'option', null, $form_name ) . "\n";
00341 }
00342 return "\t" . Html::rawElement( 'label', array( 'for' => 'formSelector' ), $form_label . wfMsg( 'colon-separator' ) ) . "\n" . Html::rawElement( 'select', array( 'id' => 'formSelector', 'name' => 'form' ), $select_body ) . "\n";
00343 }
00344
00351 public static function getAllValuesForProperty( $property_name ) {
00352 global $sfgMaxAutocompleteValues;
00353
00354 $store = smwfGetStore();
00355 $requestoptions = new SMWRequestOptions();
00356 $requestoptions->limit = $sfgMaxAutocompleteValues;
00357 $values = self::getSMWPropertyValues( $store, null, $property_name, $requestoptions );
00358 sort( $values );
00359 return $values;
00360 }
00361
00367 public static function getAllPagesForCategory( $top_category, $num_levels, $substring = null ) {
00368 if ( 0 == $num_levels ) return $top_category;
00369 global $sfgMaxAutocompleteValues;
00370
00371 $db = wfGetDB( DB_SLAVE );
00372 $top_category = str_replace( ' ', '_', $top_category );
00373 $categories = array( $top_category );
00374 $checkcategories = array( $top_category );
00375 $pages = array();
00376 for ( $level = $num_levels; $level > 0; $level-- ) {
00377 $newcategories = array();
00378 foreach ( $checkcategories as $category ) {
00379 $conditions = array();
00380 $conditions[] = 'cl_from = page_id';
00381 $conditions['cl_to'] = $category;
00382 if ( $substring != null ) {
00383 $conditions[] = self::getSQLConditionForAutocompleteInColumn( 'page_title', $substring ) . ' OR page_namespace = ' . NS_CATEGORY;
00384 }
00385 $res = $db->select(
00386 array( 'categorylinks', 'page' ),
00387 array( 'page_title', 'page_namespace' ),
00388 $conditions,
00389 __METHOD__,
00390 'SORT BY cl_sortkey' );
00391 if ( $res ) {
00392 while ( $res && $row = $db->fetchRow( $res ) ) {
00393 if ( array_key_exists( 'page_title', $row ) ) {
00394 $page_namespace = $row['page_namespace'];
00395 if ( $page_namespace == NS_CATEGORY ) {
00396 $new_category = $row[ 'page_title' ];
00397 if ( !in_array( $new_category, $categories ) ) {
00398 $newcategories[] = $new_category;
00399 }
00400 } else {
00401 $cur_title = Title::makeTitleSafe( $row['page_namespace'], $row['page_title'] );
00402 $cur_value = self::titleString( $cur_title );
00403 if ( ! in_array( $cur_value, $pages ) ) {
00404 $pages[] = $cur_value;
00405 }
00406
00407 if ( count( $pages ) > $sfgMaxAutocompleteValues ) {
00408
00409 $pages = array_unique( $pages );
00410 sort( $pages );
00411 return $pages;
00412 }
00413 }
00414 }
00415 }
00416 $db->freeResult( $res );
00417 }
00418 }
00419 if ( count( $newcategories ) == 0 ) {
00420
00421 $pages = array_unique( $pages );
00422 sort( $pages );
00423 return $pages;
00424 } else {
00425 $categories = array_merge( $categories, $newcategories );
00426 }
00427 $checkcategories = array_diff( $newcategories, array() );
00428 }
00429
00430 $pages = array_unique( $pages );
00431 sort( $pages );
00432 return $pages;
00433 }
00434
00435 public static function getAllPagesForConcept( $concept_name, $substring = null ) {
00436 global $sfgMaxAutocompleteValues, $sfgAutocompleteOnAllChars;
00437
00438 $store = smwfGetStore();
00439
00440 $concept = Title::makeTitleSafe( SMW_NS_CONCEPT, $concept_name );
00441
00442 if ( !is_null( $substring ) ) {
00443 $substring = strtolower( $substring );
00444 }
00445
00446
00447 if ( $concept == null ) {
00448 return array();
00449 }
00450
00451 if ( class_exists( 'SMWDIWikiPage' ) ) {
00452
00453 $concept = SMWDIWikiPage::newFromTitle( $concept );
00454 }
00455 $desc = new SMWConceptDescription( $concept );
00456 $printout = new SMWPrintRequest( SMWPrintRequest::PRINT_THIS, "" );
00457 $desc->addPrintRequest( $printout );
00458 $query = new SMWQuery( $desc );
00459 $query->setLimit( $sfgMaxAutocompleteValues );
00460 $query_result = $store->getQueryResult( $query );
00461 $pages = array();
00462 while ( $res = $query_result->getNext() ) {
00463 $pageName = $res[0]->getNextText( SMW_OUTPUT_WIKI );
00464 if ( is_null( $substring ) ) {
00465 $pages[] = $pageName;
00466 } else {
00467
00468
00469
00470
00471 $lowercasePageName = strtolower( $pageName );
00472 if ( $sfgAutocompleteOnAllChars ) {
00473 if ( strpos( $lowercasePageName, $substring ) >= 0 ) {
00474 $pages[] = $pageName;
00475 }
00476 } else {
00477 if ( strpos( $lowercasePageName, $substring ) === 0 ||
00478 strpos( $lowercasePageName, ' ' . $substring ) > 0 ) {
00479 $pages[] = $pageName;
00480 }
00481 }
00482 }
00483 }
00484 sort( $pages );
00485 return $pages;
00486 }
00487
00488 public static function getAllPagesForNamespace( $namespace_name, $substring = null ) {
00489 global $wgContLang, $wgLanguageCode;
00490
00491
00492
00493
00494
00495
00496 if ( $namespace_name == 'Main' || $namespace_name == 'main' ) {
00497 $namespace_name = '';
00498 }
00499 $matchingNamespaceCode = null;
00500 $namespaces = $wgContLang->getNamespaces();
00501 foreach ( $namespaces as $curNSCode => $curNSName ) {
00502 if ( $curNSName == $namespace_name ) {
00503 $matchingNamespaceCode = $curNSCode;
00504 }
00505 }
00506
00507
00508
00509 if ( is_null( $matchingNamespaceCode ) && $wgLanguageCode != 'en' ) {
00510 $englishLang = Language::factory( 'en' );
00511 $namespaces = $englishLang->getNamespaces();
00512 foreach ( $namespaces as $curNSCode => $curNSName ) {
00513 if ( $curNSName == $namespace_name ) {
00514 $matchingNamespaceCode = $curNSCode;
00515 }
00516 }
00517 }
00518
00519 if ( is_null( $matchingNamespaceCode ) ) {
00520 return array();
00521 }
00522
00523 $db = wfGetDB( DB_SLAVE );
00524 $conditions = array();
00525 $conditions['page_namespace'] = $matchingNamespaceCode;
00526 if ( $substring != null ) {
00527 $conditions[] = SFUtils::getSQLConditionForAutocompleteInColumn( 'page_title', $substring );
00528 }
00529 $res = $db->select( 'page',
00530 'page_title',
00531 $conditions, __METHOD__,
00532 array( 'ORDER BY' => 'page_title' ) );
00533
00534 $pages = array();
00535 while ( $row = $db->fetchRow( $res ) ) {
00536 $pages[] = str_replace( '_', ' ', $row[0] );
00537 }
00538 $db->freeResult( $res );
00539
00540 return $pages;
00541 }
00542
00547 public static function getAutocompleteValues( $source_name, $source_type ) {
00548 if ( $source_name == null ) {
00549 return null;
00550 }
00551
00552
00553
00554 if ( $source_type == 'property' ) {
00555 $names_array = self::getAllValuesForProperty( $source_name );
00556 } elseif ( $source_type == 'category' ) {
00557 $names_array = self::getAllPagesForCategory( $source_name, 10 );
00558 } elseif ( $source_type == 'concept' ) {
00559 $names_array = self::getAllPagesForConcept( $source_name );
00560 } else {
00561 $names_array = self::getAllPagesForNamespace( $source_name );
00562 }
00563 return $names_array;
00564 }
00565
00570 public static function getValuesArray( $value, $delimiter ) {
00571 if ( is_array( $value ) ) {
00572 return $value;
00573 } else {
00574
00575 return array_map( 'trim', explode( $delimiter, $value ) );
00576 }
00577 }
00578
00579 public static function getValuesFromExternalURL( $external_url_alias, $substring ) {
00580 global $sfgAutocompletionURLs;
00581 if ( empty( $sfgAutocompletionURLs ) ) return array();
00582 $url = $sfgAutocompletionURLs[$external_url_alias];
00583 if ( empty( $url ) ) return array();
00584 $url = str_replace( '<substr>', $substring, $url );
00585 $page_contents = Http::get( $url );
00586 if ( empty( $page_contents ) ) return array();
00587 $data = json_decode( $page_contents );
00588 if ( empty( $data ) ) return array();
00589 $return_values = array();
00590 foreach ( $data->sfautocomplete as $val ) {
00591 $return_values[] = (array)$val;
00592 }
00593 return $return_values;
00594 }
00595
00599 public static function convertBackToPipes( $s ) {
00600 return str_replace( "\1", '|', $s );
00601 }
00602
00609 public static function getFormTagComponents( $str ) {
00610
00611
00612
00613 $pattern = '/({{.*)\|(.*}})/';
00614 while ( preg_match( $pattern, $str, $matches ) ) {
00615 $str = preg_replace( $pattern, "$1" . "\1" . "$2", $str );
00616 }
00617 return array_map( array( 'SFUtils', 'convertBackToPipes' ), explode( '|', $str ) );
00618 }
00619
00624 public static function getWordForYesOrNo( $isYes ) {
00625 $wordsMsg = ( $isYes ) ? 'smw_true_words' : 'smw_false_words';
00626 $possibleWords = explode( ',', wfMsgForContent( $wordsMsg ) );
00627
00628
00629 $preferredIndex = 2;
00630 if ( count( $possibleWords ) > $preferredIndex ) {
00631 return ucwords( $possibleWords[$preferredIndex] );
00632 } elseif ( count( $possibleWords ) > 0 ) {
00633 return ucwords( $possibleWords[0] );
00634 }
00635
00636 return ( $isYes ) ? '1' : '0';
00637 }
00638
00644 public static function processEditErrors ( $error ) {
00645
00646 switch ( $error ) {
00647 case EditPage::AS_SUCCESS_NEW_ARTICLE:
00648 case EditPage::AS_SUCCESS_UPDATE:
00649 return null;
00650
00651 case EditPage::AS_SPAM_ERROR:
00652 return 'spamprotectiontext';
00653
00654 case EditPage::AS_BLOCKED_PAGE_FOR_USER:
00655 return 'blockedtitle';
00656
00657 case EditPage::AS_IMAGE_REDIRECT_ANON:
00658 return 'uploadnologin';
00659
00660 case EditPage::AS_READ_ONLY_PAGE_ANON:
00661 return 'loginreqtitle';
00662
00663 case EditPage::AS_READ_ONLY_PAGE_LOGGED:
00664 case EditPage::AS_READ_ONLY_PAGE:
00665 return array( 'readonlytext', array ( wfReadOnlyReason() ) );
00666
00667 case EditPage::AS_RATE_LIMITED:
00668 return 'actionthrottledtext';
00669
00670 case EditPage::AS_NO_CREATE_PERMISSION:
00671 return 'nocreatetext';
00672
00673 case EditPage::AS_BLANK_ARTICLE:
00674 return 'autoedit-blankpage';
00675
00676 case EditPage::AS_IMAGE_REDIRECT_LOGGED:
00677 return 'badaccess';
00678
00679 case EditPage::AS_HOOK_ERROR_EXPECTED:
00680 case EditPage::AS_HOOK_ERROR:
00681 return 'sf_formedit_hookerror';
00682
00683 case EditPage::AS_CONFLICT_DETECTED:
00684 return 'editconflict';
00685
00686 case EditPage::AS_CONTENT_TOO_BIG:
00687 case EditPage::AS_ARTICLE_WAS_DELETED:
00688 case EditPage::AS_SUMMARY_NEEDED:
00689 case EditPage::AS_TEXTBOX_EMPTY:
00690 case EditPage::AS_MAX_ARTICLE_SIZE_EXCEEDED:
00691 case EditPage::AS_END:
00692 case EditPage::AS_FILTERING:
00693 default:
00694 return array( 'internalerror_text', array ( $error ) );
00695 }
00696 }
00697
00698 public static function addToAdminLinks( &$admin_links_tree ) {
00699 $data_structure_label = wfMsg( 'smw_adminlinks_datastructure' );
00700 $data_structure_section = $admin_links_tree->getSection( $data_structure_label );
00701 if ( is_null( $data_structure_section ) ) {
00702 return true;
00703 }
00704 $smw_row = $data_structure_section->getRow( 'smw' );
00705 $smw_row->addItem( ALItem::newFromSpecialPage( 'Templates' ), 'Properties' );
00706 $smw_row->addItem( ALItem::newFromSpecialPage( 'Forms' ), 'SemanticStatistics' );
00707 $smw_admin_row = $data_structure_section->getRow( 'smw_admin' );
00708 $smw_admin_row->addItem( ALItem::newFromSpecialPage( 'CreateClass' ), 'SMWAdmin' );
00709 $smw_admin_row->addItem( ALItem::newFromSpecialPage( 'CreateProperty' ), 'SMWAdmin' );
00710 $smw_admin_row->addItem( ALItem::newFromSpecialPage( 'CreateTemplate' ), 'SMWAdmin' );
00711 $smw_admin_row->addItem( ALItem::newFromSpecialPage( 'CreateForm' ), 'SMWAdmin' );
00712 $smw_admin_row->addItem( ALItem::newFromSpecialPage( 'CreateCategory' ), 'SMWAdmin' );
00713 $smw_docu_row = $data_structure_section->getRow( 'smw_docu' );
00714 $sf_name = wfMsg( 'specialpages-group-sf_group' );
00715 $sf_docu_label = wfMsg( 'adminlinks_documentation', $sf_name );
00716 $smw_docu_row->addItem( ALItem::newFromExternalLink( "http://www.mediawiki.org/wiki/Extension:Semantic_Forms", $sf_docu_label ) );
00717
00718 return true;
00719 }
00720
00732 public static function getSpecialPage( $pageName ) {
00733 $hasFactory = class_exists( 'SpecialPageFactory' ) && method_exists( 'SpecialPageFactory', 'getPage' );
00734 return $hasFactory ? SpecialPageFactory::getPage( $pageName ) : SpecialPage::getPage( $pageName );
00735 }
00736
00745 public static function getSQLConditionForAutocompleteInColumn( $column, $substring ) {
00746 global $sfgAutocompleteOnAllChars;
00747
00748 $column_value = "LOWER(CONVERT($column USING utf8))";
00749 $substring = str_replace( ' ', '_', strtolower( $substring ) );
00750 $substring = str_replace( "'", "\'", $substring );
00751 $substring = str_replace( '_', '\_', $substring );
00752 $substring = str_replace( '%', '\%', $substring );
00753
00754 if ( $sfgAutocompleteOnAllChars ) {
00755 return "$column_value LIKE '%$substring%'";
00756 } else {
00757 return "$column_value LIKE '$substring%' OR $column_value LIKE '%\_$substring%'";
00758 }
00759 }
00760
00773 public static function showFormPreview( EditPage $editpage, WebRequest $request ) {
00774 global $wgOut, $sfgFormPrinter;
00775
00776 wfDebug( __METHOD__ . ": enter.\n" );
00777 wfProfileIn( __METHOD__ );
00778
00779
00780 if ( !$editpage->preview ) {
00781 return true;
00782 }
00783
00784 if ( $editpage->getArticle()->getTitle()->getNamespace() != SF_NS_FORM ) {
00785 return true;
00786 }
00787
00788 $editpage->previewTextAfterContent .= Html::element( 'h2', null, wfMsg( 'sf-preview-header' ) ) . "\n" .
00789 '<div class="previewnote" style="font-weight: bold">' . $wgOut->parse( wfMsg( 'sf-preview-note' ) ) . "</div>\n<hr />\n";
00790
00791 $form_definition = StringUtils::delimiterReplace( '<noinclude>', '</noinclude>', '', $editpage->textbox1 );
00792 list ( $form_text, $javascript_text, $data_text, $form_page_title, $generated_page_name ) =
00793 $sfgFormPrinter->formHTML( $form_definition, null, false, null, null, "Semantic Forms form preview dummy title", null );
00794
00795 SFUtils::addJavascriptAndCSS();
00796 $editpage->previewTextAfterContent .=
00797 '<div style="margin-top: 15px">' . $form_text . "</div>";
00798
00799 wfProfileOut( __METHOD__ );
00800
00801 return true;
00802 }
00803
00804 static function createFormLink ( &$parser, $specialPageName, $params ) {
00805 global $wgVersion;
00806
00807
00808 $inFormName = $inLinkStr = $inLinkType = $inTooltip =
00809 $inQueryStr = $inTargetName = '';
00810 if ( $specialPageName == 'RunQuery' ) {
00811 $inLinkStr = wfMsg( 'runquery' );
00812 }
00813 $classStr = "";
00814 $inQueryArr = array();
00815
00816 $positionalParameters = false;
00817
00818
00819
00820
00821 foreach ( $params as $i => $param ) {
00822
00823 $elements = explode( '=', $param, 2 );
00824
00825
00826 if ( count( $elements ) > 1 && !$positionalParameters ) {
00827 $param_name = trim( $elements[0] );
00828
00829
00830 $value = trim( $parser->recursiveTagParse( $elements[1] ) );
00831 } else {
00832 $param_name = null;
00833
00834
00835 $value = trim( $parser->recursiveTagParse( $param ) );
00836 }
00837
00838 if ( $param_name == 'form' ) {
00839 $inFormName = $value;
00840 } elseif ( $param_name == 'link text' ) {
00841 $inLinkStr = $value;
00842 } elseif ( $param_name == 'link type' ) {
00843 $inLinkType = $value;
00844 } elseif ( $param_name == 'query string' ) {
00845
00846
00847
00848 $inQueryStr = str_replace( '&', '%26', $value );
00849
00850 parse_str($inQueryStr, $arr);
00851 $inQueryArr = self::array_merge_recursive_distinct( $inQueryArr, $arr );
00852 } elseif ( $param_name == 'tooltip' ) {
00853 $inTooltip = Sanitizer::decodeCharReferences( $value );
00854 } elseif ( $param_name == 'target' ) {
00855 $inTargetName = $value;
00856 } elseif ( $param_name == null && $value == 'popup' ) {
00857 self::loadScriptsForPopupForm( $parser );
00858 $classStr = 'popupformlink';
00859 } elseif ( $param_name !== null && !$positionalParameters ) {
00860 $value = urlencode($value);
00861 parse_str("$param_name=$value", $arr);
00862 $inQueryArr = self::array_merge_recursive_distinct( $inQueryArr, $arr );
00863 } elseif ( $i == 0 ) {
00864 $inFormName = $value;
00865 $positionalParameters = true;
00866 } elseif ( $i == 1 ) {
00867 $inLinkStr = $value;
00868 } elseif ( $i == 2 ) {
00869 $inLinkType = $value;
00870 } elseif ( $i == 3 ) {
00871
00872
00873
00874 $inQueryStr = str_replace( '&', '%26', $value );
00875
00876 parse_str($inQueryStr, $arr);
00877 $inQueryArr = self::array_merge_recursive_distinct( $inQueryArr, $arr );
00878 }
00879 }
00880
00881 $ad = SFUtils::getSpecialPage( $specialPageName );
00882 $link_url = $ad->getTitle()->getLocalURL() . "/$inFormName";
00883 if ( ! empty( $inTargetName ) ) {
00884 $link_url .= "/$inTargetName";
00885 }
00886 $link_url = str_replace( ' ', '_', $link_url );
00887 $hidden_inputs = "";
00888 if ( ! empty($inQueryArr) ) {
00889
00890
00891 if ( $inLinkType == 'button' || $inLinkType == 'post button' ) {
00892
00893 $query_components = explode( '&', http_build_query( $inQueryArr, '', '&' ) );
00894
00895 foreach ( $query_components as $query_component ) {
00896 $var_and_val = explode( '=', $query_component, 2 );
00897 if ( count( $var_and_val ) == 2 ) {
00898 $hidden_inputs .= Html::hidden( urldecode( $var_and_val[0] ), urldecode( $var_and_val[1] ) );
00899 }
00900 }
00901 } else {
00902 $link_url .= ( strstr( $link_url, '?' ) ) ? '&' : '?';
00903 $link_url .= str_replace( '+', '%20', http_build_query( $inQueryArr, '', '&' ) );
00904 }
00905 }
00906 if ( $inLinkType == 'button' || $inLinkType == 'post button' ) {
00907 $formMethod = ( $inLinkType == 'button' ) ? 'get' : 'post';
00908 $str = Html::rawElement( 'form', array( 'action' => $link_url, 'method' => $formMethod, 'class' => $classStr ),
00909 Html::rawElement( 'button', array( 'type' => 'submit', 'value' => $inLinkStr ), $inLinkStr ) .
00910 $hidden_inputs
00911 );
00912 } else {
00913
00914
00915 if ( ! empty( $inTargetName ) ) {
00916 $targetTitle = Title::newFromText( $inTargetName );
00917 if ( is_null( $targetTitle ) || !$targetTitle->exists() ) {
00918 $classStr .= " new";
00919 }
00920 }
00921 $str = Html::rawElement( 'a', array( 'href' => $link_url, 'class' => $classStr, 'title' => $inTooltip ), $inLinkStr );
00922 }
00923
00924 return $str;
00925 }
00926
00927 static function loadScriptsForPopupForm( &$parser ) {
00928 $parser->getOutput()->addModules( 'ext.semanticforms.popupformedit' );
00929 return true;
00930 }
00931
00951 public static function array_merge_recursive_distinct( array &$array1, array &$array2 ) {
00952
00953 $merged = $array1;
00954
00955 foreach ( $array2 as $key => &$value ) {
00956 if ( is_array( $value ) && isset( $merged[$key] ) && is_array( $merged[$key] ) ) {
00957 $merged[$key] = self::array_merge_recursive_distinct( $merged[$key], $value );
00958 } else {
00959 $merged[$key] = $value;
00960 }
00961 }
00962
00963 return $merged;
00964 }
00965
00976 public static function registerNamespaces( array &$list ) {
00977 global $wgNamespacesWithSubpages;
00978
00979 $list[SF_NS_FORM] = 'Form';
00980 $list[SF_NS_FORM_TALK] = 'Form_talk';
00981
00982
00983 $wgNamespacesWithSubpages = $wgNamespacesWithSubpages + array(
00984 SF_NS_FORM_TALK => true
00985 );
00986
00987 return true;
00988 }
00989
00990 }