00001 <?php
00008 class SDUtils {
00021 public static function getSMWPropertyValues( SMWStore $store, $pageName, $pageNamespace, $propID, $requestOptions = null ) {
00022
00023 if ( class_exists( 'SMWDIProperty' ) ) {
00024 $pageName = str_replace( ' ', '_', $pageName );
00025 $page = new SMWDIWikiPage( $pageName, $pageNamespace, null );
00026 $property = new SMWDIProperty( $propID );
00027 return $store->getPropertyValues( $page, $property, $requestOptions );
00028 } else {
00029 $title = Title::makeTitleSafe( $pageNamespace, $pageName );
00030 $property = SMWPropertyValue::makeProperty( $propID );
00031 return $store->getPropertyValues( $title, $property, $requestOptions );
00032 }
00033 }
00034
00041 static function getTopLevelCategories() {
00042 $categories = array();
00043 $dbr = wfGetDB( DB_SLAVE );
00044 extract( $dbr->tableNames( 'page', 'categorylinks', 'page_props' ) );
00045 $cat_ns = NS_CATEGORY;
00046 $sql = "SELECT page_title FROM $page p LEFT OUTER JOIN $categorylinks cl ON p.page_id = cl.cl_from WHERE p.page_namespace = $cat_ns AND cl.cl_to IS NULL";
00047 $res = $dbr->query( $sql );
00048 if ( $dbr->numRows( $res ) > 0 ) {
00049 while ( $row = $dbr->fetchRow( $res ) ) {
00050 $categories[] = str_replace( '_', ' ', $row[0] );
00051 }
00052 }
00053 $dbr->freeResult( $res );
00054
00055
00056 $hidden_cats = $shown_cats = array();
00057 $sql2 = "SELECT p.page_title, pp.pp_propname FROM $page p JOIN $page_props pp ON p.page_id = pp.pp_page WHERE p.page_namespace = $cat_ns AND (pp.pp_propname = 'hidefromdrilldown' OR pp.pp_propname = 'showindrilldown') AND pp.pp_value = 'y'";
00058 $res2 = $dbr->query( $sql2 );
00059 if ( $dbr->numRows( $res2 ) > 0 ) {
00060 while ( $row = $dbr->fetchRow( $res2 ) ) {
00061 if ( $row[1] == 'hidefromdrilldown' )
00062 $hidden_cats[] = str_replace( '_', ' ', $row[0] );
00063 else
00064 $shown_cats[] = str_replace( '_', ' ', $row[0] );
00065 }
00066 }
00067 $dbr->freeResult( $res2 );
00068 $categories = array_merge( $categories, $shown_cats );
00069 foreach ( $hidden_cats as $hidden_cat ) {
00070 foreach ( $categories as $i => $cat ) {
00071 if ( $cat == $hidden_cat ) {
00072 unset( $categories[$i] );
00073 }
00074 }
00075 }
00076 sort( $categories );
00077 return $categories;
00078 }
00079
00084 static function getOnlyExplicitlyShownCategories() {
00085 $shown_cats = array();
00086
00087 $dbr = wfGetDB( DB_SLAVE );
00088 $res = $dbr->select(
00089 array( 'p' => 'page', 'pp' => 'page_props' ),
00090 'p.page_title',
00091 array(
00092 'p.page_namespace' => NS_CATEGORY,
00093 'pp.pp_propname' => 'showindrilldown',
00094 'pp.pp_value' => 'y'
00095 ),
00096 'SDUtils::getOnlyExplicitlyShownCategories',
00097 array( 'ORDER BY' => 'p.page_title' ),
00098 array( 'pp' => array( 'JOIN', 'p.page_id = pp.pp_page' ) )
00099 );
00100
00101 while ( $row = $dbr->fetchRow( $res ) ) {
00102 $shown_cats[] = str_replace( '_', ' ', $row[0] );
00103 }
00104 $dbr->freeResult( $res );
00105
00106 return $shown_cats;
00107 }
00108
00113 public static function getCategoriesForBrowsing() {
00114 global $sdgHideCategoriesByDefault;
00115
00116 if ( $sdgHideCategoriesByDefault ) {
00117 return self::getOnlyExplicitlyShownCategories();
00118 } else {
00119 return self::getTopLevelCategories();
00120 }
00121 }
00122
00126 static function getSemanticProperties() {
00127 global $smwgContLang;
00128 $smw_namespace_labels = $smwgContLang->getNamespaces();
00129 $all_properties = array();
00130
00131 $options = new SMWRequestOptions();
00132 $options->limit = 10000;
00133 $used_properties = smwfGetStore()->getPropertiesSpecial( $options );
00134 foreach ( $used_properties as $property ) {
00135 if ( $property[0] instanceof SMWDIProperty ) {
00136
00137 $propName = $property[0]->getKey();
00138 if ( $propName{0} != '_' ) {
00139 $all_properties[] = str_replace( '_', ' ', $propName );
00140 }
00141 } else {
00142 $all_properties[] = $property[0]->getWikiValue();
00143 }
00144 }
00145 $unused_properties = smwfGetStore()->getUnusedPropertiesSpecial( $options );
00146 foreach ( $unused_properties as $property ) {
00147 if ( $property instanceof SMWDIProperty ) {
00148
00149 $all_properties[] = str_replace( '_', ' ', $property->getKey() );
00150 } else {
00151 $all_properties[] = $property->getWikiValue();
00152 }
00153 }
00154
00155 global $sdgContLang;
00156 $sd_props = $sdgContLang->getPropertyLabels();
00157 $sd_prop_aliases = $sdgContLang->getPropertyAliases();
00158 foreach ( $all_properties as $i => $prop_name ) {
00159 foreach ( $sd_props as $prop => $label ) {
00160 if ( $prop_name == $label ) {
00161 unset( $all_properties[$i] );
00162 }
00163 }
00164 foreach ( $sd_prop_aliases as $alias => $cur_prop ) {
00165 if ( $prop_name == $alias ) {
00166 unset( $all_properties[$i] );
00167 }
00168 }
00169 }
00170 sort( $all_properties );
00171 return $all_properties;
00172 }
00173
00178 static function getFilters() {
00179 $dbr = wfGetDB( DB_SLAVE );
00180 $res = $dbr->select( 'page', 'page_title', array( 'page_namespace' => SD_NS_FILTER ) );
00181 $filters = array();
00182 while ( $row = $dbr->fetchRow( $res ) ) {
00183 $filters[] = $row[0];
00184 }
00185 $dbr->freeResult( $res );
00186 return $filters;
00187 }
00188
00193 static function getValuesForProperty( $subject, $subject_namespace, $special_prop ) {
00194 $store = smwfGetStore();
00195 $res = self::getSMWPropertyValues( $store, $subject, $subject_namespace, $special_prop );
00196 $values = array();
00197 foreach ( $res as $prop_val ) {
00198
00199 if ( $prop_val instanceof SMWDIWikiPage ) {
00200 $actual_val = $prop_val->getDBkey();
00201 } elseif ( $prop_val instanceof SMWDIString ) {
00202 $actual_val = $prop_val->getString();
00203 } elseif ( method_exists( $prop_val, 'getValueKey' ) ) {
00204 $actual_val = $prop_val->getValueKey();
00205 } else {
00206 $actual_val = $prop_val->getXSDValue();
00207 }
00208 $values[] = html_entity_decode( str_replace( '_', ' ', $actual_val ) );
00209 }
00210 return $values;
00211 }
00212
00216 static function loadFiltersForCategory( $category ) {
00217 $filters = array();
00218 $filters_ps = array();
00219 $filter_names = SDUtils::getValuesForProperty( str_replace( ' ', '_', $category ), NS_CATEGORY, '_SD_F' );
00220 foreach ( $filter_names as $filter_name ) {
00221 $filters[] = SDFilter::load( $filter_name );
00222 }
00223
00224
00225 if ( class_exists( 'PSSchema' ) ) {
00226 $pageSchemaObj = new PSSchema( $category );
00227 if ( $pageSchemaObj->isPSDefined() ) {
00228 $filters_ps = SDFilter::loadAllFromPageSchema( $pageSchemaObj );
00229 $result_filters = array_merge( $filters, $filters_ps );
00230 return $result_filters;
00231 }
00232 }
00233 return $filters;
00234 }
00235
00239 static function getDisplayParamsForCategory( $category ) {
00240 $all_display_params = SDUtils::getValuesForProperty( str_replace( ' ', '_', $category ), NS_CATEGORY, '_SD_DP' );
00241
00242 $return_display_params = array();
00243 foreach ( $all_display_params as $display_params ) {
00244 $return_display_params[] = explode( ';', $display_params );
00245 }
00246 return $return_display_params;
00247 }
00248
00249 static function getCategoryChildren( $category_name, $get_categories, $levels ) {
00250 if ( $levels == 0 ) {
00251 return array();
00252 }
00253 $pages = array();
00254 $subcategories = array();
00255 $dbr = wfGetDB( DB_SLAVE );
00256 extract( $dbr->tableNames( 'page', 'categorylinks' ) );
00257 $cat_ns = NS_CATEGORY;
00258 $query_category = str_replace( ' ', '_', $category_name );
00259 $query_category = str_replace( "'", "\'", $query_category );
00260 $sql = "SELECT p.page_title, p.page_namespace FROM $categorylinks cl
00261 JOIN $page p on cl.cl_from = p.page_id
00262 WHERE cl.cl_to = '$query_category'\n";
00263 if ( $get_categories )
00264 $sql .= "AND p.page_namespace = $cat_ns\n";
00265 $sql .= "ORDER BY cl.cl_sortkey";
00266 $res = $dbr->query( $sql );
00267 while ( $row = $dbr->fetchRow( $res ) ) {
00268 if ( $get_categories ) {
00269 $subcategories[] = $row[0];
00270 $pages[] = $row[0];
00271 } else {
00272 if ( $row[1] == $cat_ns )
00273 $subcategories[] = $row[0];
00274 else
00275 $pages[] = $row[0];
00276 }
00277 }
00278 $dbr->freeResult( $res );
00279 foreach ( $subcategories as $subcategory ) {
00280 $pages = array_merge( $pages, SDUtils::getCategoryChildren( $subcategory, $get_categories, $levels - 1 ) );
00281 }
00282 return $pages;
00283 }
00284
00285 static function monthToString( $month ) {
00286 if ( $month == 1 ) {
00287 return wfMsg( 'january' );
00288 } elseif ( $month == 2 ) {
00289 return wfMsg( 'february' );
00290 } elseif ( $month == 3 ) {
00291 return wfMsg( 'march' );
00292 } elseif ( $month == 4 ) {
00293 return wfMsg( 'april' );
00294 } elseif ( $month == 5 ) {
00295
00296 return wfMsg( 'may_long' );
00297 } elseif ( $month == 6 ) {
00298 return wfMsg( 'june' );
00299 } elseif ( $month == 7 ) {
00300 return wfMsg( 'july' );
00301 } elseif ( $month == 8 ) {
00302 return wfMsg( 'august' );
00303 } elseif ( $month == 9 ) {
00304 return wfMsg( 'september' );
00305 } elseif ( $month == 10 ) {
00306 return wfMsg( 'october' );
00307 } elseif ( $month == 11 ) {
00308 return wfMsg( 'november' );
00309 } else {
00310 return wfMsg( 'december' );
00311 }
00312 }
00313
00314 static function stringToMonth( $str ) {
00315 if ( $str == wfMsg( 'january' ) ) {
00316 return 1;
00317 } elseif ( $str == wfMsg( 'february' ) ) {
00318 return 2;
00319 } elseif ( $str == wfMsg( 'march' ) ) {
00320 return 3;
00321 } elseif ( $str == wfMsg( 'april' ) ) {
00322 return 4;
00323 } elseif ( $str == wfMsg( 'may_long' ) ) {
00324 return 5;
00325 } elseif ( $str == wfMsg( 'june' ) ) {
00326 return 6;
00327 } elseif ( $str == wfMsg( 'july' ) ) {
00328 return 7;
00329 } elseif ( $str == wfMsg( 'august' ) ) {
00330 return 8;
00331 } elseif ( $str == wfMsg( 'september' ) ) {
00332 return 9;
00333 } elseif ( $str == wfMsg( 'october' ) ) {
00334 return 10;
00335 } elseif ( $str == wfMsg( 'november' ) ) {
00336 return 11;
00337 } else {
00338 return 12;
00339 }
00340 }
00341
00342 static function booleanToString( $bool_value ) {
00343 $words_field_name = ( $bool_value == true ) ? 'smw_true_words' : 'smw_false_words';
00344 $words_array = explode( ',', wfMsgForContent( $words_field_name ) );
00345
00346
00347 $index_of_word = 2;
00348
00349 if ( count( $words_array ) > $index_of_word ) {
00350 $string_value = ucwords( $words_array[$index_of_word] );
00351 } elseif ( count( $words_array ) == 0 ) {
00352 $string_value = $bool_value;
00353 } else {
00354 $string_value = ucwords( $words_array[0] );
00355 }
00356 return $string_value;
00357 }
00358
00364 static function printRedirectForm( $title, $page_contents, $edit_summary, $is_save, $is_preview, $is_diff, $is_minor_edit, $watch_this ) {
00365 $article = new Article( $title );
00366 $new_url = $title->getLocalURL( 'action=submit' );
00367 $starttime = wfTimestampNow();
00368 $edittime = $article->getTimestamp();
00369 global $wgUser;
00370 if ( $wgUser->isLoggedIn() )
00371 $token = htmlspecialchars( $wgUser->editToken() );
00372 else
00373 $token = EDIT_TOKEN_SUFFIX;
00374
00375 if ( $is_save )
00376 $action = "wpSave";
00377 elseif ( $is_preview )
00378 $action = "wpPreview";
00379 else
00380 $action = "wpDiff";
00381
00382 $text = <<<END
00383 <form id="editform" name="editform" method="post" action="$new_url">
00384 <input type="hidden" name="wpTextbox1" id="wpTextbox1" value="$page_contents" />
00385 <input type="hidden" name="wpSummary" value="$edit_summary" />
00386 <input type="hidden" name="wpStarttime" value="$starttime" />
00387 <input type="hidden" name="wpEdittime" value="$edittime" />
00388 <input type="hidden" name="wpEditToken" value="$token" />
00389 <input type="hidden" name="$action" />
00390
00391 END;
00392 if ( $is_minor_edit )
00393 $text .= ' <input type="hidden" name="wpMinoredit">' . "\n";
00394 if ( $watch_this )
00395 $text .= ' <input type="hidden" name="wpWatchthis">' . "\n";
00396 $text .= <<<END
00397 </form>
00398 <script type="text/javascript">
00399 document.editform.submit();
00400 </script>
00401
00402 END;
00403 return $text;
00404 }
00405
00409 static function addMagicWordVariableIDs( &$magicWordVariableIDs ) {
00410 $magicWordVariableIDs[] = 'MAG_HIDEFROMDRILLDOWN';
00411 $magicWordVariableIDs[] = 'MAG_SHOWINDRILLDOWN';
00412 return true;
00413 }
00414
00419 static function handleShowAndHide( &$parser, &$text ) {
00420 global $wgOut, $wgAction;
00421 $mw_hide = MagicWord::get( 'MAG_HIDEFROMDRILLDOWN' );
00422 if ( $mw_hide->matchAndRemove( $text ) ) {
00423 $parser->mOutput->setProperty( 'hidefromdrilldown', 'y' );
00424 }
00425 $mw_show = MagicWord::get( 'MAG_SHOWINDRILLDOWN' );
00426 if ( $mw_show->matchAndRemove( $text ) ) {
00427 $parser->mOutput->setProperty( 'showindrilldown', 'y' );
00428 }
00429 return true;
00430 }
00431
00432 public static function addToAdminLinks( &$admin_links_tree ) {
00433 $browse_search_section = $admin_links_tree->getSection( wfMsg( 'adminlinks_browsesearch' ) );
00434 $sd_row = new ALRow( 'sd' );
00435 $sd_row->addItem( ALItem::newFromSpecialPage( 'BrowseData' ) );
00436 $sd_row->addItem( ALItem::newFromSpecialPage( 'Filters' ) );
00437 $sd_row->addItem( ALItem::newFromSpecialPage( 'CreateFilter' ) );
00438 $sd_name = wfMsg( 'specialpages-group-sd_group' );
00439 $sd_docu_label = wfMsg( 'adminlinks_documentation', $sd_name );
00440 $sd_row->addItem( AlItem::newFromExternalLink( "http://www.mediawiki.org/wiki/Extension:Semantic_Drilldown", $sd_docu_label ) );
00441
00442 $browse_search_section->addRow( $sd_row );
00443
00444 return true;
00445 }
00446 }