r103398 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r103397‎ | r103398 | r103399 >
Date:22:27, 16 November 2011
Author:yaron
Status:deferred
Tags:
Comment:
Changed name of class from 'SMWDistributablePrinter' to 'SMWAggregatablePrinter' - this word, though a little more obscure, is a much more accurate description
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php (modified) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Aggregatable.php (added) (history)
  • /trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Distributable.php (deleted) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Distributable.php
@@ -1,225 +0,0 @@
2 -<?php
3 -
4 -/**
5 - * Result printer that supports the distribution parameter,
6 - * and related parameters. It allows the user to choose between
7 - * regular behaviour or getting a distribution of values.
8 - *
9 - * For example, this result set: foo bar baz foo bar bar ohi
10 - * Will be turned into
11 - * * bar (3)
12 - * * foo (2)
13 - * * baz (1)
14 - * * ohi (1)
15 - *
16 - * @since 1.7
17 - *
18 - * @file SMW_QP_Distributable.php
19 - * @ingroup SMWQuery
20 - *
21 - * @licence GNU GPL v3
22 - * @author Jeroen De Dauw < jeroendedauw@gmail.com >
23 - */
24 -abstract class SMWDistributablePrinter extends SMWResultPrinter {
25 -
26 - /**
27 - * Create the formats output given the result data and return it.
28 - *
29 - * @since 1.7
30 - *
31 - * @param array $data label => value
32 - */
33 - protected abstract function getFormatOutput( array $data );
34 -
35 - /**
36 - * Method gets called right before the result is returned
37 - * in case there are values to display. It is meant for
38 - * adding resources such as JS and CSS only needed for this
39 - * format when it has actual values.
40 - *
41 - * @since 1.7
42 - */
43 - protected function addResources() {}
44 -
45 - /**
46 - * (non-PHPdoc)
47 - * @see SMWResultPrinter::getResultText()
48 - */
49 - protected function getResultText( SMWQueryResult $result, $outputmode ) {
50 - $data = $this->getResults( $result, $outputmode );
51 -
52 - if ( count( $data ) == 0 ) {
53 - // This is wikitext, so no escaping needed.
54 - return '<span class="error">' . wfMsgForContent( 'srf-warn-empy-chart' ) . '</span>';
55 -
56 - // This makes the parser go mad :/
57 -// global $wgParser;
58 -// return $wgParser->parse(
59 -// '{{#info:' . wfMsgForContent( 'srf-warn-empy-chart' ) . '|warning}}',
60 -// Title::newMainPage(),
61 -// ( new ParserOptions() )
62 -// )->getText();
63 - }
64 - else {
65 - $this->applyDistributionParams( $data );
66 - $this->addResources();
67 - return $this->getFormatOutput( $data );
68 - }
69 - }
70 -
71 - /**
72 - * Apply the distribution specific parameters.
73 - *
74 - * @since 1.7
75 - *
76 - * @param array $data
77 - */
78 - protected function applyDistributionParams( array &$data ) {
79 - if ( $this->params['distributionsort'] == 'asc' ) {
80 - asort( $data, SORT_NUMERIC );
81 - }
82 - else if ( $this->params['distributionsort'] == 'desc' ) {
83 - arsort( $data, SORT_NUMERIC );
84 - }
85 -
86 - if ( $this->params['distributionlimit'] !== false ) {
87 - $data = array_slice( $data, 0, $this->params['distributionlimit'], true );
88 - }
89 - }
90 -
91 - /**
92 - * Gets and processes the results so they can be fed directly to the
93 - * getFormatOutput method. They are returned as an array with the keys
94 - * being the labels and the values being their corresponding (numeric) values.
95 - *
96 - * @since 1.7
97 - *
98 - * @param SMWQueryResult $result
99 - * @param $outputmode
100 - *
101 - * @return array label => value
102 - */
103 - protected function getResults( SMWQueryResult $result, $outputmode ) {
104 - if ( $this->params['distribution'] ) {
105 - return $this->getDistributionResults( $result, $outputmode );
106 - }
107 - else {
108 - return $this->getNumericResults( $result, $outputmode );
109 - }
110 - }
111 -
112 - /**
113 - * Counts all the occurances of all values in the query result,
114 - * and returns an array with as key the value and as value the count.
115 - *
116 - * @since 1.7
117 - *
118 - * @param SMWQueryResult $res
119 - * @param $outputmode
120 - *
121 - * @return array label => value
122 - */
123 - protected function getDistributionResults( SMWQueryResult $result, $outputmode ) {
124 - $values = array();
125 -
126 - while ( /* array of SMWResultArray */ $row = $result->getNext() ) { // Objects (pages)
127 - for ( $i = 0, $n = count( $row ); $i < $n; $i++ ) { // SMWResultArray for a sinlge property
128 - while ( ( /* SMWDataValue */ $dataValue = $row[$i]->getNextDataValue() ) !== false ) { // Data values
129 -
130 - // Get the HTML for the tag content. Pages are linked, other stuff is just plaintext.
131 - if ( $dataValue->getTypeID() == '_wpg' ) {
132 - $value = $dataValue->getTitle()->getText();
133 - }
134 - else {
135 - $value = $dataValue->getShortText( $outputmode, $this->getLinker( false ) );
136 - }
137 -
138 - if ( !array_key_exists( $value, $values ) ) {
139 - $values[$value] = 0;
140 - }
141 -
142 - $values[$value]++;
143 - }
144 - }
145 - }
146 -
147 - return $values;
148 - }
149 -
150 - /**
151 - * Returns an array with the numerical data in the query result.
152 - *
153 - * @since 1.7
154 - *
155 - * @param SMWQueryResult $res
156 - * @param $outputmode
157 - *
158 - * @return array label => value
159 - */
160 - protected function getNumericResults( SMWQueryResult $res, $outputmode ) {
161 - $values = array();
162 -
163 - // print all result rows
164 - while ( $row = $res->getNext() ) {
165 - $dataValue = $row[0]->getNextDataValue();
166 -
167 - if ( $dataValue !== false ) {
168 - $name = $dataValue->getShortWikiText();
169 -
170 - foreach ( $row as $field ) {
171 - while ( ( /* SMWDataItem */ $dataItem = $field->getNextDataItem() ) !== false ) {
172 - $this->addNumbersForDataItem( $dataItem, $values, $name );
173 - }
174 - }
175 - }
176 - }
177 -
178 - return $values;
179 - }
180 -
181 - /**
182 - * Adds all numbers contained in a dataitem to the list.
183 - *
184 - * @since 1.7
185 - *
186 - * @param SMWDataItem $dataItem
187 - * @param array $values
188 - * @param string $name
189 - */
190 - protected function addNumbersForDataItem( SMWDataItem $dataItem, array &$values, $name ) {
191 - switch ( $dataItem->getDIType() ) {
192 - case SMWDataItem::TYPE_NUMBER:
193 - $values[$name] = $dataItem->getNumber();
194 - break;
195 - case SMWDataItem::TYPE_CONTAINER:
196 - foreach ( $dataItem->getDataItems() as $di ) {
197 - $this->addNumbersForDataItem( $di, $values, $name );
198 - }
199 - break;
200 - default:
201 - }
202 - }
203 -
204 - /**
205 - * @see SMWResultPrinter::getParameters
206 - * @since 1.7
207 - */
208 - public function getParameters() {
209 - $params = parent::getParameters();
210 -
211 - $params['distribution'] = new Parameter( 'distribution', Parameter::TYPE_BOOLEAN, false );
212 - $params['distribution']->setMessage( 'smw-paramdesc-distribution' );
213 -
214 - $params['distributionsort'] = new Parameter( 'distributionsort', Parameter::TYPE_STRING, 'none' );
215 - $params['distributionsort']->setMessage( 'smw-paramdesc-distributionsort' );
216 - $params['distributionsort']->addCriteria( new CriterionInArray( 'asc', 'desc', 'none' ) );
217 -
218 - $params['distributionlimit'] = new Parameter( 'distributionlimit', Parameter::TYPE_INTEGER );
219 - $params['distributionlimit']->setDefault( false, false );
220 - $params['distributionlimit']->setMessage( 'smw-paramdesc-distributionlimit' );
221 - $params['distributionlimit']->addCriteria( new CriterionInRange( 1, false ) );
222 -
223 - return $params;
224 - }
225 -
226 -}
Index: trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Aggregatable.php
@@ -0,0 +1,225 @@
 2+<?php
 3+
 4+/**
 5+ * Result printer that supports the distribution parameter,
 6+ * and related parameters. It allows the user to choose between
 7+ * regular behaviour or getting a distribution of values.
 8+ *
 9+ * For example, this result set: foo bar baz foo bar bar ohi
 10+ * Will be turned into
 11+ * * bar (3)
 12+ * * foo (2)
 13+ * * baz (1)
 14+ * * ohi (1)
 15+ *
 16+ * @since 1.7
 17+ *
 18+ * @file SMW_QP_Aggregatable.php
 19+ * @ingroup SMWQuery
 20+ *
 21+ * @licence GNU GPL v3
 22+ * @author Jeroen De Dauw < jeroendedauw@gmail.com >
 23+ */
 24+abstract class SMWAggregatablePrinter extends SMWResultPrinter {
 25+
 26+ /**
 27+ * Create the formats output given the result data and return it.
 28+ *
 29+ * @since 1.7
 30+ *
 31+ * @param array $data label => value
 32+ */
 33+ protected abstract function getFormatOutput( array $data );
 34+
 35+ /**
 36+ * Method gets called right before the result is returned
 37+ * in case there are values to display. It is meant for
 38+ * adding resources such as JS and CSS only needed for this
 39+ * format when it has actual values.
 40+ *
 41+ * @since 1.7
 42+ */
 43+ protected function addResources() {}
 44+
 45+ /**
 46+ * (non-PHPdoc)
 47+ * @see SMWResultPrinter::getResultText()
 48+ */
 49+ protected function getResultText( SMWQueryResult $result, $outputmode ) {
 50+ $data = $this->getResults( $result, $outputmode );
 51+
 52+ if ( count( $data ) == 0 ) {
 53+ // This is wikitext, so no escaping needed.
 54+ return '<span class="error">' . wfMsgForContent( 'srf-warn-empy-chart' ) . '</span>';
 55+
 56+ // This makes the parser go mad :/
 57+// global $wgParser;
 58+// return $wgParser->parse(
 59+// '{{#info:' . wfMsgForContent( 'srf-warn-empy-chart' ) . '|warning}}',
 60+// Title::newMainPage(),
 61+// ( new ParserOptions() )
 62+// )->getText();
 63+ }
 64+ else {
 65+ $this->applyDistributionParams( $data );
 66+ $this->addResources();
 67+ return $this->getFormatOutput( $data );
 68+ }
 69+ }
 70+
 71+ /**
 72+ * Apply the distribution specific parameters.
 73+ *
 74+ * @since 1.7
 75+ *
 76+ * @param array $data
 77+ */
 78+ protected function applyDistributionParams( array &$data ) {
 79+ if ( $this->params['distributionsort'] == 'asc' ) {
 80+ asort( $data, SORT_NUMERIC );
 81+ }
 82+ else if ( $this->params['distributionsort'] == 'desc' ) {
 83+ arsort( $data, SORT_NUMERIC );
 84+ }
 85+
 86+ if ( $this->params['distributionlimit'] !== false ) {
 87+ $data = array_slice( $data, 0, $this->params['distributionlimit'], true );
 88+ }
 89+ }
 90+
 91+ /**
 92+ * Gets and processes the results so they can be fed directly to the
 93+ * getFormatOutput method. They are returned as an array with the keys
 94+ * being the labels and the values being their corresponding (numeric) values.
 95+ *
 96+ * @since 1.7
 97+ *
 98+ * @param SMWQueryResult $result
 99+ * @param $outputmode
 100+ *
 101+ * @return array label => value
 102+ */
 103+ protected function getResults( SMWQueryResult $result, $outputmode ) {
 104+ if ( $this->params['distribution'] ) {
 105+ return $this->getDistributionResults( $result, $outputmode );
 106+ }
 107+ else {
 108+ return $this->getNumericResults( $result, $outputmode );
 109+ }
 110+ }
 111+
 112+ /**
 113+ * Counts all the occurances of all values in the query result,
 114+ * and returns an array with as key the value and as value the count.
 115+ *
 116+ * @since 1.7
 117+ *
 118+ * @param SMWQueryResult $res
 119+ * @param $outputmode
 120+ *
 121+ * @return array label => value
 122+ */
 123+ protected function getDistributionResults( SMWQueryResult $result, $outputmode ) {
 124+ $values = array();
 125+
 126+ while ( /* array of SMWResultArray */ $row = $result->getNext() ) { // Objects (pages)
 127+ for ( $i = 0, $n = count( $row ); $i < $n; $i++ ) { // SMWResultArray for a sinlge property
 128+ while ( ( /* SMWDataValue */ $dataValue = $row[$i]->getNextDataValue() ) !== false ) { // Data values
 129+
 130+ // Get the HTML for the tag content. Pages are linked, other stuff is just plaintext.
 131+ if ( $dataValue->getTypeID() == '_wpg' ) {
 132+ $value = $dataValue->getTitle()->getText();
 133+ }
 134+ else {
 135+ $value = $dataValue->getShortText( $outputmode, $this->getLinker( false ) );
 136+ }
 137+
 138+ if ( !array_key_exists( $value, $values ) ) {
 139+ $values[$value] = 0;
 140+ }
 141+
 142+ $values[$value]++;
 143+ }
 144+ }
 145+ }
 146+
 147+ return $values;
 148+ }
 149+
 150+ /**
 151+ * Returns an array with the numerical data in the query result.
 152+ *
 153+ * @since 1.7
 154+ *
 155+ * @param SMWQueryResult $res
 156+ * @param $outputmode
 157+ *
 158+ * @return array label => value
 159+ */
 160+ protected function getNumericResults( SMWQueryResult $res, $outputmode ) {
 161+ $values = array();
 162+
 163+ // print all result rows
 164+ while ( $row = $res->getNext() ) {
 165+ $dataValue = $row[0]->getNextDataValue();
 166+
 167+ if ( $dataValue !== false ) {
 168+ $name = $dataValue->getShortWikiText();
 169+
 170+ foreach ( $row as $field ) {
 171+ while ( ( /* SMWDataItem */ $dataItem = $field->getNextDataItem() ) !== false ) {
 172+ $this->addNumbersForDataItem( $dataItem, $values, $name );
 173+ }
 174+ }
 175+ }
 176+ }
 177+
 178+ return $values;
 179+ }
 180+
 181+ /**
 182+ * Adds all numbers contained in a dataitem to the list.
 183+ *
 184+ * @since 1.7
 185+ *
 186+ * @param SMWDataItem $dataItem
 187+ * @param array $values
 188+ * @param string $name
 189+ */
 190+ protected function addNumbersForDataItem( SMWDataItem $dataItem, array &$values, $name ) {
 191+ switch ( $dataItem->getDIType() ) {
 192+ case SMWDataItem::TYPE_NUMBER:
 193+ $values[$name] = $dataItem->getNumber();
 194+ break;
 195+ case SMWDataItem::TYPE_CONTAINER:
 196+ foreach ( $dataItem->getDataItems() as $di ) {
 197+ $this->addNumbersForDataItem( $di, $values, $name );
 198+ }
 199+ break;
 200+ default:
 201+ }
 202+ }
 203+
 204+ /**
 205+ * @see SMWResultPrinter::getParameters
 206+ * @since 1.7
 207+ */
 208+ public function getParameters() {
 209+ $params = parent::getParameters();
 210+
 211+ $params['distribution'] = new Parameter( 'distribution', Parameter::TYPE_BOOLEAN, false );
 212+ $params['distribution']->setMessage( 'smw-paramdesc-distribution' );
 213+
 214+ $params['distributionsort'] = new Parameter( 'distributionsort', Parameter::TYPE_STRING, 'none' );
 215+ $params['distributionsort']->setMessage( 'smw-paramdesc-distributionsort' );
 216+ $params['distributionsort']->addCriteria( new CriterionInArray( 'asc', 'desc', 'none' ) );
 217+
 218+ $params['distributionlimit'] = new Parameter( 'distributionlimit', Parameter::TYPE_INTEGER );
 219+ $params['distributionlimit']->setDefault( false, false );
 220+ $params['distributionlimit']->setMessage( 'smw-paramdesc-distributionlimit' );
 221+ $params['distributionlimit']->addCriteria( new CriterionInRange( 1, false ) );
 222+
 223+ return $params;
 224+ }
 225+
 226+}
Property changes on: trunk/extensions/SemanticMediaWiki/includes/queryprinters/SMW_QP_Aggregatable.php
___________________________________________________________________
Added: svn:eol-style
1227 + native
Index: trunk/extensions/SemanticMediaWiki/includes/SMW_Setup.php
@@ -199,7 +199,7 @@
200200 // Printers
201201 $qpDir = $smwgIP . 'includes/queryprinters/';
202202 $wgAutoloadClasses['SMWResultPrinter'] = $qpDir . 'SMW_QueryPrinter.php';
203 - $wgAutoloadClasses['SMWDistributablePrinter'] = $qpDir . 'SMW_QP_Distributable.php';
 203+ $wgAutoloadClasses['SMWAggregatablePrinter'] = $qpDir . 'SMW_QP_Aggregatable.php';
204204 $wgAutoloadClasses['SMWTableResultPrinter'] = $qpDir . 'SMW_QP_Table.php';
205205 $wgAutoloadClasses['SMWListResultPrinter'] = $qpDir . 'SMW_QP_List.php';
206206 $wgAutoloadClasses['SMWCategoryResultPrinter'] = $qpDir . 'SMW_QP_Category.php';