r65099 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r65098‎ | r65099 | r65100 >
Date:21:32, 15 April 2010
Author:yaron
Status:deferred
Tags:
Comment:
Re-arranged and simplified, including moving all code into classes; made page includable within other pages; fix for handling more "display parameters"
Modified paths:
  • /trunk/extensions/SemanticDrilldown/specials/SD_BrowseData.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticDrilldown/specials/SD_BrowseData.php
@@ -9,41 +9,176 @@
1010
1111 if (!defined('MEDIAWIKI')) die();
1212
13 -class SDBrowseData extends SpecialPage {
 13+class SDBrowseData extends IncludableSpecialPage {
1414
1515 /**
1616 * Constructor
1717 */
18 - public function SDBrowseData() {
19 - SpecialPage::SpecialPage('BrowseData');
 18+ public function __construct() {
 19+ parent::__construct('BrowseData');
2020 wfLoadExtensionMessages('SemanticDrilldown');
2121 }
2222
2323 function execute( $query ) {
 24+ global $wgRequest, $wgOut, $wgTitle;
 25+ global $sdgScriptPath, $sdgContLang, $sdgNumResultsPerPage;
 26+
 27+ if ($wgTitle->getNamespace() != NS_SPECIAL) {
 28+ global $wgParser;
 29+ $wgParser->disableCache();
 30+ }
2431 $this->setHeaders();
25 - doSpecialBrowseData( $query );
 32+ $mainCssDir = $sdgScriptPath . '/skins/';
 33+ $wgOut->addLink( array(
 34+ 'rel' => 'stylesheet',
 35+ 'type' => 'text/css',
 36+ 'media' => "screen",
 37+ 'href' => $mainCssDir . 'SD_main.css'
 38+ ));
 39+ $wgOut->addLink( array(
 40+ 'rel' => 'stylesheet',
 41+ 'type' => 'text/css',
 42+ 'media' => "screen",
 43+ 'href' => $mainCssDir . 'xtheme-gray.css'
 44+ ));
 45+ $wgOut->addLink( array(
 46+ 'rel' => 'stylesheet',
 47+ 'type' => 'text/css',
 48+ 'media' => "screen",
 49+ 'href' => $mainCssDir . 'combos.css'
 50+ ));
 51+ $javascript_text =<<<END
 52+function toggleFilterDiv(element_id, label_element) {
 53+ element = document.getElementById(element_id);
 54+ if (element.style.display == "none") {
 55+ element.style.display = "block";
 56+ label_element.innerHTML = "<img src=\"$sdgScriptPath/skins/down-arrow.png\">";
 57+ } else {
 58+ element.style.display = "none";
 59+ label_element.innerHTML = "<img src=\"$sdgScriptPath/skins/right-arrow.png\">";
2660 }
2761 }
2862
29 -class BrowseDataPage extends QueryPage {
 63+function highlightRemoveDiv(element) {
 64+ element.innerHTML = "<img src=\"$sdgScriptPath/skins/filter-x-active.png\">";
 65+}
 66+function unhighlightRemoveDiv(element) {
 67+ element.innerHTML = "<img src=\"$sdgScriptPath/skins/filter-x.png\">";
 68+}
 69+
 70+END;
 71+ $wgOut->addScript(' <script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n");
 72+
 73+ // set default
 74+ if ($sdgNumResultsPerPage == null)
 75+ $sdgNumResultsPerPage = 250;
 76+ list( $limit, $offset ) = wfCheckLimits($sdgNumResultsPerPage, 'sdlimit');
 77+ $filters = array();
 78+
 79+ // get information on current category, subcategory and filters
 80+ // that have already been applied from the query string
 81+ $category = str_replace('_', ' ', $wgRequest->getVal('_cat'));
 82+ // if query string did not contain this variables, try the URL
 83+ if (! $category) {
 84+ $queryparts = explode('/', $query, 1);
 85+ $category = isset($queryparts[0]) ? $queryparts[0] : '';
 86+ }
 87+ if (! $category) {
 88+ $category_title = wfMsg('browsedata');
 89+ } else {
 90+ $titles_for_category = SDUtils::getValuesForProperty($category, NS_CATEGORY, '_SD_DT', SD_SP_HAS_DRILLDOWN_TITLE, NS_MAIN);
 91+ if (count($titles_for_category) > 0) {
 92+ $category_title = str_replace('_', ' ', $titles_for_category[0]);
 93+ } else {
 94+ $category_title = wfMsg( 'browsedata' ) . html_entity_decode( wfMsg( 'colon-separator' ) ) . str_replace( '_', ' ', $category );
 95+ }
 96+ }
 97+ // if no category was specified, go with the first
 98+ // category on the site, alphabetically
 99+ if (! $category) {
 100+ $categories = SDUtils::getTopLevelCategories();
 101+ if (count($categories) > 0) {
 102+ $category = $categories[0];
 103+ }
 104+ }
 105+
 106+ $wgOut->setPageTitle($category_title);
 107+ $subcategory = $wgRequest->getVal('_subcat');
 108+
 109+ $filters = SDUtils::loadFiltersForCategory($category);
 110+
 111+ $filters_used = array();
 112+ foreach ($filters as $i => $filter)
 113+ $filter_used[] = false;
 114+ $applied_filters = array();
 115+ $remaining_filters = array();
 116+ foreach ($filters as $i => $filter) {
 117+ $filter_name = str_replace(' ', '_', $filter->name);
 118+ $search_term = $wgRequest->getVal('_search_' . $filter_name);
 119+ $lower_date = $wgRequest->getArray('_lower_' . $filter_name);
 120+ $upper_date = $wgRequest->getArray('_upper_' . $filter_name);
 121+ if ($vals_array = $wgRequest->getArray($filter_name)) {
 122+ foreach ($vals_array as $j => $val) {
 123+ $vals_array[$j] = str_replace('_', ' ', $val);
 124+ }
 125+ $applied_filters[] = SDAppliedFilter::create($filter, $vals_array);
 126+ $filter_used[$i] = true;
 127+ } elseif ($search_term != null) {
 128+ $applied_filters[] = SDAppliedFilter::create($filter, array(), $search_term);
 129+ $filter_used[$i] = true;
 130+ } elseif ($lower_date != null || $upper_date != null) {
 131+ $applied_filters[] = SDAppliedFilter::create($filter, array(), null, $lower_date, $upper_date);
 132+ $filter_used[$i] = true;
 133+ }
 134+ }
 135+ // add every unused filter to the $remaining_filters array,
 136+ // unless it requires some other filter that hasn't been applied
 137+ foreach ($filters as $i => $filter) {
 138+ $required_filters = SDUtils::getValuesForProperty($filter->name, SD_NS_FILTER, '_SD_RF', SD_SP_REQUIRES_FILTER, SD_NS_FILTER);
 139+ $matched_all_required_filters = true;
 140+ foreach ($required_filters as $required_filter) {
 141+ $found_match = false;
 142+ foreach ($applied_filters as $af) {
 143+ if ($af->filter->name == $required_filter) {
 144+ $found_match = true;
 145+ }
 146+ }
 147+ if (! $found_match) {
 148+ $matched_all_required_filters = false;
 149+ continue;
 150+ }
 151+ }
 152+ if ($matched_all_required_filters) {
 153+ if (! $filter_used[$i])
 154+ $remaining_filters[] = $filter;
 155+ }
 156+ }
 157+
 158+ $wgOut->addHTML("\n <div class=\"drilldown-results\">\n");
 159+ $rep = new SDBrowseDataPage($category, $subcategory, $applied_filters, $remaining_filters);
 160+ $num = $rep->doQuery( $offset, $limit );
 161+ $wgOut->addHTML("\n </div> <!-- drilldown-results -->\n");
 162+ return $num;
 163+ }
 164+}
 165+
 166+class SDBrowseDataPage extends QueryPage {
30167 var $category = "";
31168 var $subcategory = "";
32169 var $next_level_subcategories = array();
33170 var $all_subcategories = array();
34171 var $applied_filters = array();
35172 var $remaining_filters = array();
36 - var $browse_data_title;
37173 var $show_single_cat = false;
38174
39175 /**
40176 * Initialize the variables of this page
41177 */
42 - function BrowseDataPage($category, $subcategory, $applied_filters, $remaining_filters) {
 178+ function __construct($category, $subcategory, $applied_filters, $remaining_filters) {
43179 $this->category = $category;
44180 $this->subcategory = $subcategory;
45181 $this->applied_filters = $applied_filters;
46182 $this->remaining_filters = $remaining_filters;
47 - $this->browse_data_title = Title::newFromText('BrowseData', NS_SPECIAL);
48183
49184 $dbr = wfGetDB( DB_SLAVE );
50185 $categorylinks = $dbr->tableName( 'categorylinks' );
@@ -61,7 +196,8 @@
62197 }
63198
64199 function makeBrowseURL($category, $applied_filters = array(), $subcategory = null) {
65 - $url = $this->browse_data_title->getFullURL() . '/' . $category;
 200+ global $wgTitle;
 201+ $url = $wgTitle->getFullURL() . '/' . $category;
66202 if ($this->show_single_cat) {
67203 $url .= (strpos($url, '?')) ? '&' : '?';
68204 $url .= "_single";
@@ -117,9 +253,9 @@
118254 */
119255 function getSQLFromClauseForField($new_filter) {
120256 $sql = "FROM semantic_drilldown_values sdv
121 - LEFT OUTER JOIN semantic_drilldown_filter_values sdfv
122 - ON sdv.id = sdfv.id
123 - WHERE ";
 257+ LEFT OUTER JOIN semantic_drilldown_filter_values sdfv
 258+ ON sdv.id = sdfv.id
 259+ WHERE ";
124260 $sql .= $new_filter->checkSQL("sdfv.value");
125261 return $sql;
126262 }
@@ -250,7 +386,7 @@
251387 */
252388 function getNumResults($subcategory, $subcategories, $new_filter = null) {
253389 $dbr = wfGetDB( DB_SLAVE );
254 - $sql = "SELECT COUNT(*) ";
 390+ $sql = "SELECT COUNT(DISTINCT sdv.id) ";
255391 if ($new_filter)
256392 $sql .= $this->getSQLFromClauseForField($new_filter);
257393 else
@@ -270,30 +406,40 @@
271407 function isSyndicated() { return false; }
272408
273409 function printCategoriesList($categories) {
 410+ global $sdgShowCategoriesAsTabs;
 411+
274412 $choose_category_text = wfMsg('sd_browsedata_choosecategory');
 413+ if ($sdgShowCategoriesAsTabs) {
 414+ $cats_wrapper_class = "drilldown-categories-tabs-wrapper";
 415+ $cats_list_class = "drilldown-categories-tabs";
 416+ } else {
 417+ $cats_wrapper_class = "drilldown-categories-wrapper";
 418+ $cats_list_class = "drilldown-categories";
 419+ }
275420 $text =<<<END
276421
277 - <div class="drilldown-categories-wrapper">
278 - <div class="drilldown-categories">
279 - <div class="drilldown-categories-header">$choose_category_text:</div>
 422+ <div id="$cats_wrapper_class">
 423+ <p id="categories-header">$choose_category_text:</p>
 424+ <ul id="$cats_list_class">
280425
281426 END;
282427 foreach ($categories as $i => $category) {
283428 $category_children = SDUtils::getCategoryChildren($category, false, 5);
284 - $category_str = $category . " (" . count($category_children) . ")";
 429+ $category_str = $category . " (" . count(array_unique($category_children)) . ")";
285430 if (str_replace('_', ' ', $this->category) == $category) {
286 - $text .= ' <div class="drilldown-category selected-category">';
 431+ $text .= ' <li class="category selected">';
287432 $text .= $category_str;
288433 } else {
289 - $text .= ' <div class="drilldown-category">';
 434+ $text .= ' <li class="category">';
290435 $category_url = $this->makeBrowseURL($category);
291436 $text .= "<a href=\"$category_url\" title=\"$choose_category_text\">$category_str</a>";
292437 }
293 - $text .= "</div>\n";
 438+ $text .= "</li>\n";
294439 }
295440 $text .=<<<END
296 - </div>
297 - </div>
 441+ </li>
 442+ </ul>
 443+ </div>
298444
299445 END;
300446 return $text;
@@ -645,7 +791,6 @@
646792 global $sdgFiltersSmallestFontSize, $sdgFiltersLargestFontSize;
647793
648794 $skin = $wgUser->getSkin();
649 - $browse_data_title = Title::newFromText('BrowseData', NS_SPECIAL);
650795 $categories = SDUtils::getTopLevelCategories();
651796 // if there are no categories, escape quickly
652797 if (count($categories) == 0) {
@@ -666,7 +811,7 @@
667812 (count($this->remaining_filters) == 0)) {
668813 return $header;
669814 }
670 - $header .= ' <div class="drilldown-header">' . "\n";
 815+ $header .= ' <div id="drilldown-header">' . "\n";
671816 if (count ($this->applied_filters) > 0 || $this->subcategory) {
672817 $category_url = $this->makeBrowseURL($this->category);
673818 $header .= '<a href="' . $category_url . '" title="' . wfMsg('sd_browsedata_resetfilters') . '">' . str_replace('_', ' ', $this->category) . '</a>';
@@ -857,6 +1002,7 @@
8581003 $display_params = array_map('trim', $all_display_params[0]);
8591004 SMWQueryProcessor::processFunctionParams($display_params, $querystring, $params, $printouts);
8601005 }
 1006+ $query = SMWQueryProcessor::createQuery($querystring, $params);
8611007 if (array_key_exists('format', $params))
8621008 $format = $params['format'];
8631009 else
@@ -866,7 +1012,7 @@
8671013 } else {
8681014 $mainlabel = '';
8691015 }
870 - $r = $this->addSemanticResultWrapper($dbr, $res, $num, $mainlabel, $printouts);
 1016+ $r = $this->addSemanticResultWrapper($dbr, $res, $num, $query, $mainlabel, $printouts);
8711017 $printer = SMWQueryProcessor::getResultPrinter($format, SMWQueryProcessor::SPECIAL_PAGE, $r);
8721018
8731019 $prresult = $printer->getResult($r, $params, SMW_OUTPUT_HTML);
@@ -923,7 +1069,7 @@
9241070 // semantic result printers.
9251071 // Code stolen largely from SMWSQLStore2QueryEngine->getInstanceQueryResult() method.
9261072 // (does this mean it will only work with certain semantic SQL stores?)
927 - function addSemanticResultWrapper($dbr, $res, $num, $mainlabel, $printouts) {
 1073+ function addSemanticResultWrapper($dbr, $res, $num, $query, $mainlabel, $printouts) {
9281074 $qr = array();
9291075 $count = 0;
9301076 $store = smwfGetStore();
@@ -943,7 +1089,6 @@
9441090 $main_printout[$printrequest->getHash()] = $printrequest;
9451091 $printouts = array_merge($main_printout, $printouts);
9461092
947 - $query = new SMWQuery();
9481093 // for SMW 1.5+, handling is quite easy
9491094 if (! class_exists('SMWTemplateResultPrinter')) {
9501095 return new SMWQueryResult($printouts, $query, $qr, $store, ($count > $num) );
@@ -998,138 +1143,3 @@
9991144 return "\n <br style=\"clear: both\" />\n";
10001145 }
10011146 }
1002 -
1003 -function doSpecialBrowseData($query) {
1004 - global $wgRequest, $wgOut, $sdgScriptPath, $sdgContLang, $sdgNumResultsPerPage;
1005 - $mainCssDir = $sdgScriptPath . '/skins/';
1006 - $wgOut->addLink( array(
1007 - 'rel' => 'stylesheet',
1008 - 'type' => 'text/css',
1009 - 'media' => "screen",
1010 - 'href' => $mainCssDir . 'SD_main.css'
1011 - ));
1012 - $wgOut->addLink( array(
1013 - 'rel' => 'stylesheet',
1014 - 'type' => 'text/css',
1015 - 'media' => "screen",
1016 - 'href' => $mainCssDir . 'xtheme-gray.css'
1017 - ));
1018 - $wgOut->addLink( array(
1019 - 'rel' => 'stylesheet',
1020 - 'type' => 'text/css',
1021 - 'media' => "screen",
1022 - 'href' => $mainCssDir . 'combos.css'
1023 - ));
1024 - $javascript_text =<<<END
1025 -function toggleFilterDiv(element_id, label_element) {
1026 - element = document.getElementById(element_id);
1027 - if (element.style.display == "none") {
1028 - element.style.display = "block";
1029 - label_element.innerHTML = "<img src=\"$sdgScriptPath/skins/down-arrow.png\">";
1030 - } else {
1031 - element.style.display = "none";
1032 - label_element.innerHTML = "<img src=\"$sdgScriptPath/skins/right-arrow.png\">";
1033 - }
1034 -}
1035 -
1036 -function highlightRemoveDiv(element) {
1037 - element.innerHTML = "<img src=\"$sdgScriptPath/skins/filter-x-active.png\">";
1038 -}
1039 -function unhighlightRemoveDiv(element) {
1040 - element.innerHTML = "<img src=\"$sdgScriptPath/skins/filter-x.png\">";
1041 -}
1042 -
1043 -END;
1044 - $wgOut->addScript(' <script type="text/javascript">' . "\n" . $javascript_text . '</script>' . "\n");
1045 -
1046 - // set default
1047 - if ($sdgNumResultsPerPage == null)
1048 - $sdgNumResultsPerPage = 250;
1049 - list( $limit, $offset ) = wfCheckLimits($sdgNumResultsPerPage, 'sdlimit');
1050 - $filters = array();
1051 -
1052 - // get information on current category, subcategory and filters that
1053 - // have already been applied from the query string
1054 - $category = str_replace('_', ' ', $wgRequest->getVal('_cat'));
1055 - // if query string did not contain this variables, try the URL
1056 - if (! $category) {
1057 - $queryparts = explode('/', $query, 1);
1058 - $category = isset($queryparts[0]) ? $queryparts[0] : '';
1059 - }
1060 - if (! $category) {
1061 - $category_title = wfMsg('browsedata');
1062 - } else {
1063 - $titles_for_category = SDUtils::getValuesForProperty($category, NS_CATEGORY, '_SD_DT', SD_SP_HAS_DRILLDOWN_TITLE, NS_MAIN);
1064 - if (count($titles_for_category) > 0) {
1065 - $category_title = str_replace('_', ' ', $titles_for_category[0]);
1066 - } else {
1067 - $category_title = wfMsg( 'browsedata' ) . html_entity_decode( wfMsg( 'colon-separator' ) ) . str_replace( '_', ' ', $category );
1068 - }
1069 - }
1070 - // if no category was specified, go with the first
1071 - // category on the site, alphabetically
1072 - if (! $category) {
1073 - $categories = SDUtils::getTopLevelCategories();
1074 - if (count($categories) > 0) {
1075 - $category = $categories[0];
1076 - }
1077 - }
1078 -
1079 - $wgOut->setPageTitle($category_title);
1080 - $subcategory = $wgRequest->getVal('_subcat');
1081 -
1082 - $filters = SDUtils::loadFiltersForCategory($category);
1083 -
1084 - $filters_used = array();
1085 - foreach ($filters as $i => $filter)
1086 - $filter_used[] = false;
1087 - $applied_filters = array();
1088 - $remaining_filters = array();
1089 - foreach ($filters as $i => $filter) {
1090 - $filter_name = str_replace(' ', '_', $filter->name);
1091 - $search_term = $wgRequest->getVal('_search_' . $filter_name);
1092 - $lower_date = $wgRequest->getArray('_lower_' . $filter_name);
1093 - $upper_date = $wgRequest->getArray('_upper_' . $filter_name);
1094 - if ($vals_array = $wgRequest->getArray($filter_name)) {
1095 - foreach ($vals_array as $j => $val) {
1096 - $vals_array[$j] = str_replace('_', ' ', $val);
1097 - }
1098 - $applied_filters[] = SDAppliedFilter::create($filter, $vals_array);
1099 - $filter_used[$i] = true;
1100 - } elseif ($search_term != null) {
1101 - $applied_filters[] = SDAppliedFilter::create($filter, array(), $search_term);
1102 - $filter_used[$i] = true;
1103 - } elseif ($lower_date != null || $upper_date != null) {
1104 - $applied_filters[] = SDAppliedFilter::create($filter, array(), null, $lower_date, $upper_date);
1105 - $filter_used[$i] = true;
1106 - }
1107 - }
1108 - // add every unused filter to the $remaining_filters array, unless
1109 - // it requires some other filter that hasn't been applied
1110 - foreach ($filters as $i => $filter) {
1111 - $required_filters = SDUtils::getValuesForProperty($filter->name, SD_NS_FILTER, '_SD_RF', SD_SP_REQUIRES_FILTER, SD_NS_FILTER);
1112 - $matched_all_required_filters = true;
1113 - foreach ($required_filters as $required_filter) {
1114 - $found_match = false;
1115 - foreach ($applied_filters as $af) {
1116 - if ($af->filter->name == $required_filter) {
1117 - $found_match = true;
1118 - }
1119 - }
1120 - if (! $found_match) {
1121 - $matched_all_required_filters = false;
1122 - continue;
1123 - }
1124 - }
1125 - if ($matched_all_required_filters) {
1126 - if (! $filter_used[$i])
1127 - $remaining_filters[] = $filter;
1128 - }
1129 - }
1130 -
1131 - $wgOut->addHTML("\n <div class=\"drilldown-results\">\n");
1132 - $rep = new BrowseDataPage($category, $subcategory, $applied_filters, $remaining_filters);
1133 - $num = $rep->doQuery( $offset, $limit );
1134 - $wgOut->addHTML("\n </div> <!-- drilldown-results -->\n");
1135 - return $num;
1136 -}

Status & tagging log