Index: trunk/extensions/SemanticMediaWiki/includes/SMW_QueryProcessor.php |
— | — | @@ -123,74 +123,74 @@ |
124 | 124 | } |
125 | 125 | } |
126 | 126 | |
127 | | - // determine sortkeys and ascendings: |
128 | | - if ( array_key_exists( 'order', $params ) && !is_null( $params['order'] ) ) { |
129 | | - // Compatibility with query printers not using Validator yet |
130 | | - if ( is_string( $params['order'] ) ) { |
131 | | - $params['order'] = explode( ',', $params['order'] ); |
| 127 | + $defaultSort = $format === 'rss' ? 'DESC' : 'ASC'; |
| 128 | + $sort = self::getSortKeys( $params['sort'], $params['order'], $defaultSort ); |
| 129 | + |
| 130 | + $query->sortkeys = $sort['keys']; |
| 131 | + $query->addErrors( $sort['errors'] ); |
| 132 | + $query->sort = count( $query->sortkeys ) > 0; // TODO: Why would we do this here? |
| 133 | + var_dump($query->sortkeys);exit; |
| 134 | + return $query; |
| 135 | + } |
| 136 | + |
| 137 | + /** |
| 138 | + * Takes the sort and order parameters and returns a list of sort keys and a list of errors. |
| 139 | + * |
| 140 | + * @since 1.7 |
| 141 | + * |
| 142 | + * @param array $sortParam |
| 143 | + * @param array $orders |
| 144 | + * @param string $defaultSort |
| 145 | + * |
| 146 | + * @return array ( keys => array(), errors => array() ) |
| 147 | + */ |
| 148 | + protected static function getSortKeys( array $sortParam, array $orderParam, $defaultSort ) { |
| 149 | + $orders = array(); |
| 150 | + $sortKeys = array(); |
| 151 | + $sortErros = array(); |
| 152 | + |
| 153 | + foreach ( $orderParam as $key => $order ) { |
| 154 | + $order = strtolower( trim( $order ) ); |
| 155 | + if ( ( $order == 'descending' ) || ( $order == 'reverse' ) || ( $order == 'desc' ) ) { |
| 156 | + $orders[$key] = 'DESC'; |
| 157 | + } elseif ( ( $order == 'random' ) || ( $order == 'rand' ) ) { |
| 158 | + $orders[$key] = 'RANDOM'; |
| 159 | + } else { |
| 160 | + $orders[$key] = 'ASC'; |
132 | 161 | } |
| 162 | + } |
| 163 | + |
| 164 | + foreach ( $sortParam as $sort ) { |
| 165 | + $sortKey = false; |
133 | 166 | |
134 | | - $orders = $params['order']; |
135 | | - |
136 | | - foreach ( $orders as $key => $order ) { // normalise |
137 | | - $order = strtolower( trim( $order ) ); |
138 | | - if ( ( $order == 'descending' ) || ( $order == 'reverse' ) || ( $order == 'desc' ) ) { |
139 | | - $orders[$key] = 'DESC'; |
140 | | - } elseif ( ( $order == 'random' ) || ( $order == 'rand' ) ) { |
141 | | - $orders[$key] = 'RANDOM'; |
| 167 | + // An empty string indicates we mean the page, such as element 0 on the next line. |
| 168 | + // sort=,Some property |
| 169 | + if ( trim( $sort ) === '' ) { |
| 170 | + $sortKey = ''; |
| 171 | + } |
| 172 | + else { |
| 173 | + $propertyValue = SMWPropertyValue::makeUserProperty( trim( $sort ) ); |
| 174 | + |
| 175 | + if ( $propertyValue->isValid() ) { |
| 176 | + $sortKey = $propertyValue->getDataItem()->getKey(); |
142 | 177 | } else { |
143 | | - $orders[$key] = 'ASC'; |
| 178 | + $sortErros = array_merge( $sortErros, $propertyValue->getErrors() ); |
144 | 179 | } |
145 | 180 | } |
146 | | - } else { |
147 | | - $orders = array(); |
148 | | - } |
149 | | - |
150 | | - reset( $orders ); |
151 | | - |
152 | | - if ( array_key_exists( 'sort', $params ) && !is_null( $params['sort'] ) ) { |
153 | | - // Compatibility with query printers not using Validator yet |
154 | | - if ( is_string( $params['sort'] ) ) { |
155 | | - $params['sort'] = explode( ',', $params['sort'] ); |
156 | | - } |
157 | 181 | |
158 | | - $query->sort = true; |
159 | | - $query->sortkeys = array(); |
160 | | - |
161 | | - foreach ( $params['sort'] as $sort ) { |
162 | | - if ( trim( $sort ) === '' ) { |
163 | | - $query->sortkeys[''] = current( $orders ); |
164 | | - next( $orders ); |
165 | | - } |
166 | | - else { |
167 | | - $propertyValue = SMWPropertyValue::makeUserProperty( trim( $sort ) ); |
168 | | - |
169 | | - if ( $propertyValue->isValid() ) { |
170 | | - $sortkey = $propertyValue->getDataItem()->getKey(); |
171 | | - $order = current( $orders ); |
172 | | - if ( $order === false ) { // default |
173 | | - $order = 'ASC'; |
174 | | - } |
175 | | - $query->sortkeys[$sortkey] = $order; // should we check for duplicate sort keys? |
176 | | - next( $orders ); |
177 | | - } else { |
178 | | - $query->addErrors( $propertyValue->getErrors() ); |
179 | | - } |
180 | | - } |
| 182 | + if ( $sortKey !== false ) { |
| 183 | + $order = empty( $orders ) ? $defaultSort : array_shift( $orders ); |
| 184 | + $sortKeys[$sortKey] = $order; |
181 | 185 | } |
| 186 | + } |
182 | 187 | |
183 | | - if ( current( $orders ) !== false ) { // sort key remaining, apply to page name |
184 | | - $query->sortkeys[''] = current( $orders ); |
185 | | - } |
186 | | - } elseif ( $format == 'rss' ) { // unsorted RSS: use *descending* default order |
187 | | - // TODO: the default sort field should be "modification date" (now it is the title, but |
188 | | - // likely to be overwritten by printouts with label "date"). |
189 | | - $query->sortkeys[''] = ( current( $orders ) != false ) ? current( $orders ) : 'DESC'; |
190 | | - } else { // sort by page title (main column) by default |
191 | | - $query->sortkeys[''] = ( current( $orders ) != false ) ? current( $orders ) : 'ASC'; |
192 | | - } // TODO: check and report if there are further order statements? |
193 | | - |
194 | | - return $query; |
| 188 | + // If more sort arguments are provided then properties, assume the first one is for the page. |
| 189 | + // TODO: we might want to add errors if there is more then one. |
| 190 | + if ( !empty( $orders ) ) { |
| 191 | + $sortKeys[''] = array_shift( $orders ); |
| 192 | + } |
| 193 | + |
| 194 | + return array( 'keys' => $sortKeys, 'errors' => $sortErros ); |
195 | 195 | } |
196 | 196 | |
197 | 197 | /** |
— | — | @@ -513,7 +513,7 @@ |
514 | 514 | |
515 | 515 | $params['sort'] = new ListParameter( 'sort' ); |
516 | 516 | $params['sort']->setMessage( 'smw-paramdesc-sort' ); |
517 | | - $params['sort']->setDefault( array() ); |
| 517 | + $params['sort']->setDefault( array( '' ) ); // The empty string represents the page itself, which should be sorted by default. |
518 | 518 | |
519 | 519 | $params['order'] = new ListParameter( 'order' ); |
520 | 520 | $params['order']->setMessage( 'smw-paramdesc-order' ); |