r91780 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r91779‎ | r91780 | r91781 >
Date:07:32, 9 July 2011
Author:devayon
Status:deferred (Comments)
Tags:
Comment:
Style cleanup
Modified paths:
  • /trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticMediaWiki/specials/AskSpecial/SMW_QueryUIHelper.php
@@ -9,18 +9,18 @@
1010 * @author Sanyam Goyal
1111 * @author Jeroen De Dauw
1212 */
13 -abstract class SMWQueryUI extends SpecialPage{
 13+abstract class SMWQueryUI extends SpecialPage {
1414 protected $m_ui_helper;
15 - protected abstract function execute($p){
 15+ protected abstract function execute( $p ) {
1616 /*
1717 * Extract the parameters from the UI. Use either the constructor or
1818 * $this->m_ui_helper=new SMWUIHelper()
1919 */
2020 }
21 - protected function getUrlTail(){
22 - $urltail='&q=' . urlencode( $this->m_ui_helper->getQuerystring() );
 21+ protected function getUrlTail() {
 22+ $urltail = '&q=' . urlencode( $this->m_ui_helper->getQuerystring() );
2323 $tmp_parray = array();
24 - $params=$this->m_ui_helper->getParams();
 24+ $params = $this->m_ui_helper->getParams();
2525 foreach ( $params as $key => $value ) {
2626 if ( !in_array( $key, array( 'sort', 'order', 'limit', 'offset', 'title' ) ) ) {
2727 $tmp_parray[$key] = $value;
@@ -37,10 +37,10 @@
3838 if ( array_key_exists( 'sort', $params ) ) $urltail .= '&sort=' . $params['sort'];
3939 if ( array_key_exists( 'order', $params ) ) $urltail .= '&order=' . $params['order'];
4040 }
41 - protected function makeHtmlResult(){
 41+ protected function makeHtmlResult() {
4242 global $wgOut;
43 - if(is_a($this->m_ui_helper, 'SMWQueryUIHelper')){
44 -
 43+ if ( is_a( $this->m_ui_helper, 'SMWQueryUIHelper' ) ) {
 44+
4545 }
4646 }
4747 /**
@@ -171,7 +171,7 @@
172172 return $input->getHtml();
173173 }
174174
175 - protected function usesNavigationBar(){
 175+ protected function usesNavigationBar() {
176176 return true;
177177 }
178178
@@ -273,55 +273,55 @@
274274 * @property boolean $enable_validation If set to TRUE causes each of the parametes to be checked for errors.
275275 */
276276 class SMWQueryUIHelper {
277 -
278 -//members
279 - protected $m_querystring = ''; //The query
280 - protected $m_params = array(); //Parameters controlling how the results should be displayed
281 - protected $m_printouts = array(); //Properties to be printed along with results
282 - protected static $m_UIPages = array(); //A list of Query UIs
 277+
 278+// members
 279+ protected $m_querystring = ''; // The query
 280+ protected $m_params = array(); // Parameters controlling how the results should be displayed
 281+ protected $m_printouts = array(); // Properties to be printed along with results
 282+ protected static $m_UIPages = array(); // A list of Query UIs
283283 public $enable_validation;
284 - private $fatal_errors= false;
 284+ private $fatal_errors = false;
285285 private $context;
286 - private $errors=array();
287 - const SPECIAL_PAGE=0;//parameters passed from special page
288 - const WIKI_LINK=1;//parameters passed from 'further links' in the wiki.
 286+ private $errors = array();
 287+ const SPECIAL_PAGE = 0;// parameters passed from special page
 288+ const WIKI_LINK = 1;// parameters passed from 'further links' in the wiki.
289289
290290
291 -//constructor
292 - public function __construct($enable_validation = true, $context=self::SPECIAL_PAGE) {
 291+// constructor
 292+ public function __construct( $enable_validation = true, $context = self::SPECIAL_PAGE ) {
293293 $this -> enable_validation = $enable_validation;
294 - $this->context=$context;
 294+ $this->context = $context;
295295 }
296296
297 - public function hasError(){
 297+ public function hasError() {
298298 return $this->fatal_errors;
299 - }
300 -
 299+ }
 300+
301301 /**
302302 *
303303 * Returns an array of errors, if any have occured.
304304 * @return array of strings
305305 */
306 - public function getErrors(){
 306+ public function getErrors() {
307307 return $this->errors;
308308 }
309309 /**
310310 * Register a Semantic Search Special Page
311311 * @param SpecialPage $page
312312 */
313 - public static function addUI( SpecialPage &$page ){
 313+ public static function addUI( SpecialPage &$page ) {
314314 /*
315315 * This way of registering, instead of using a global variable will cause
316316 * SMWQueryUIHelper to AutoLoad, but the alternate would break encapsulation.
317317 */
318 - self::$m_UIPages[]=$page;
 318+ self::$m_UIPages[] = $page;
319319 }
320320
321321 /**
322322 * Returns an array of Semantic Search Special Pages
323323 * @return array of SpecialPage
324324 */
325 - public static function getUiList(){
 325+ public static function getUiList() {
326326 return self::$m_UIPages;
327327 }
328328
@@ -332,22 +332,22 @@
333333 * @param string $querystring The query
334334 * @return array array of errors, if any.
335335 */
336 - public function setQueryString( $querystring =""){
 336+ public function setQueryString( $querystring = "" ) {
337337 $this -> m_querystring = $querystring;
338 - $errors=array();
339 - if($this->enable_validation){
340 - if($querystring==''){
341 - $errors[]="No query has been specified"; //TODO i18n
 338+ $errors = array();
 339+ if ( $this->enable_validation ) {
 340+ if ( $querystring == '' ) {
 341+ $errors[] = "No query has been specified"; // TODO i18n
342342 }
343343 else
344344 {
345 - $query = SMWQueryProcessor::createQuery($querystring, array());
346 - $errors=$query ->getErrors();
 345+ $query = SMWQueryProcessor::createQuery( $querystring, array() );
 346+ $errors = $query ->getErrors();
347347 }
348 - if (!empty ($errors)){
349 - $this->fatal_errors=true;
 348+ if ( !empty ( $errors ) ) {
 349+ $this->fatal_errors = true;
350350 }
351 - $this->errors = array_merge($errors,$this->errors);
 351+ $this->errors = array_merge( $errors, $this->errors );
352352 return $errors;
353353 }
354354 }
@@ -361,87 +361,85 @@
362362 * @param array $printouts Array of additional properties to be shown in results
363363 * @return array array of errors, if any.
364364 */
365 - public function setPrintOuts( array $printouts=array() ){
 365+ public function setPrintOuts( array $printouts = array() ) {
366366 $errors = array();
367 - if($this -> enable_validation){
368 - foreach($printouts as $key => $prop){
369 - if($prop[0]!='?'){
370 - $printouts[$key]="?".$printouts[$key];
 367+ if ( $this -> enable_validation ) {
 368+ foreach ( $printouts as $key => $prop ) {
 369+ if ( $prop[0] != '?' ) {
 370+ $printouts[$key] = "?" . $printouts[$key];
371371 }
372 - if(!$this->validateProperty($prop)){
373 - $errors[]="$prop may not be a valid property"; //TODO: add i18n
374 - $this->fatal_errors=true;
 372+ if ( !$this->validateProperty( $prop ) ) {
 373+ $errors[] = "$prop may not be a valid property"; // TODO: add i18n
 374+ $this->fatal_errors = true;
375375 }
376376 }
377377 }
378378 $this -> m_printouts = $printouts;
379 - $this->errors = array_merge($errors,$this->errors);
 379+ $this->errors = array_merge( $errors, $this->errors );
380380 return $errors;
381381 }
382382
383 - public function setParams( array $params =array()){
 383+ public function setParams( array $params = array() ) {
384384 /*
385 - *Validate, and add missing params. *
 385+ *Validate, and add missing params. *
386386 */
387387 global $smwgQMaxInlineLimit, $smwgResultFormats;
388388 $errors = array();
389389
390 - //checking for missing parameters and adding them
391 - if( !array_key_exists( 'format', $params ) )
 390+ // checking for missing parameters and adding them
 391+ if ( !array_key_exists( 'format', $params ) )
392392 $params[ 'format' ] = $this->getDefaultResultPrinter ();
393 - if( !array_key_exists('order', $params ) )
 393+ if ( !array_key_exists( 'order', $params ) )
394394 $params[ 'order' ] = '';
395 - if( !array_key_exists( 'limit', $params) )
 395+ if ( !array_key_exists( 'limit', $params ) )
396396 $params[ 'limit' ] = 20;
397397 $params[ 'limit' ] = min( $params[ 'limit' ], $smwgQMaxInlineLimit );
398 - if(!array_key_exists('offset', $params))
399 - $params['offset']=0;
400 -
401 - if ($this->enable_validation){
402 - //validating the format
403 - if(!array_key_exists($params['format'], $smwgResultFormats)){
404 - $errors[]="The chosen format "+$params['format']+" does not exist for this wiki"; //TODO i18n
405 - $this->fatal_errors=true;
 398+ if ( !array_key_exists( 'offset', $params ) )
 399+ $params['offset'] = 0;
 400+
 401+ if ( $this->enable_validation ) {
 402+ // validating the format
 403+ if ( !array_key_exists( $params['format'], $smwgResultFormats ) ) {
 404+ $errors[] = "The chosen format " + $params['format'] + " does not exist for this wiki"; // TODO i18n
 405+ $this->fatal_errors = true;
406406 }
407407 else
408 - { //validating parameters for result printer
 408+ { // validating parameters for result printer
409409 $printer = SMWQueryProcessor::getResultPrinter( $params[ 'format' ] );
410 - $parameters=$printer->getParameters();
411 - if(is_array($parameters)){
412 - //if(is_a($parameters[0], 'Parameter')){
 410+ $parameters = $printer->getParameters();
 411+ if ( is_array( $parameters ) ) {
413412 $validator = new Validator();
414 - $validator -> setParameters($params,$parameters);
 413+ $validator -> setParameters( $params, $parameters );
415414 $validator->validateParameters();
416415 $validator_has_error = $validator->hasFatalError();
417 - if($validator_has_error){
418 - array_merge ($errors, $validator->getErrorMessages ());
419 - $this->fatal_errors=true;
 416+ if ( $validator_has_error ) {
 417+ array_merge ( $errors, $validator->getErrorMessages () );
 418+ $this->fatal_errors = true;
420419 }
421 - //}
422420 }
423421 }
424422 }
425423
426424 $this -> m_params = $params;
427 - $this->errors = array_merge($errors,$this->errors);
 425+ $this->errors = array_merge( $errors, $this->errors );
428426 return $errors;
429427 }
430428
431 - public function makeHTMLResult(){
 429+ public function makeHTMLResult() {
432430 /*
433431 * Once $m_querystring, $m_params, $m_printouts are set, generates the
434432 * results / or link. The pagination links (or navigation bar) are expected
435433 * to be created by the UI designer. (or maybe we can put a method here to
436434 * make the nav-bar which also calls makeHTMLResult().
437435 */
438 - $result='';
439 - $errors=array();
440 - $query=SMWQueryProcessor::createQuery($this->m_querystring, $this->m_params,SMWQueryProcessor::SPECIAL_PAGE , $this->m_params['format'], $this->m_printouts);
441 - $res=smwfGetStore()->getQueryResult($query);
442 - $errors= array_merge($errors, $res->getErrors());
443 - if(!empty($errors)){
444 - $this->fatal_errors=true;
445 - $this->errors = array_merge($errors,$this->errors);
 436+ $result = '';
 437+ $errors = array();
 438+ $query = SMWQueryProcessor::createQuery( $this->m_querystring, $this->m_params, SMWQueryProcessor::SPECIAL_PAGE , $this->m_params['format'], $this->m_printouts );
 439+ $res = smwfGetStore()->getQueryResult( $query );
 440+ $errors = array_merge( $errors, $res->getErrors() );
 441+ if ( !empty( $errors ) ) {
 442+ $this->fatal_errors = true;
 443+ $this->errors = array_merge( $errors, $this->errors );
446444 }
447445
448446 // BEGIN: Try to be smart for rss/ical if no description/title is given and we have a concept query
@@ -471,27 +469,12 @@
472470 }
473471 }
474472 // END: Try to be smart for rss/ical if no description/title is given and we have a concept query
475 -
 473+
476474 $printer = SMWQueryProcessor::getResultPrinter( $this->m_params['format'], SMWQueryProcessor::SPECIAL_PAGE );
477475 $result_mime = $printer->getMimeType( $res );
478476
479 -// global $wgRequest;
480 -//
481 -// $hidequery = $wgRequest->getVal( 'eq' ) == 'no';
482 -//
483 -// // if it's an export format (like CSV, JSON, etc.),
484 -// // don't actually export the data if 'eq' is set to
485 -// // either 'yes' or 'no' in the query string - just
486 -// // show the link instead
487 -// if ( $this->m_editquery || $hidequery ) $result_mime = false;
488 -
489 - //??/if ( $result_mime == false ) {
490477 if ( $res->getCount() > 0 ) {
491 -// if ( $this->m_editquery ) $urltail .= '&eq=yes';
492 -// if ( $hidequery ) $urltail .= '&eq=no';
493478
494 - //$navigation = $this->getNavigationBar( $res, $urltail );//?
495 - //$result .= '<div style="text-align: center;">' . "\n" . $navigation . "\n</div>\n";//?
496479 $query_result = $printer->getResult( $res, $this->m_params, SMW_OUTPUT_HTML );
497480
498481 if ( is_array( $query_result ) ) {
@@ -500,57 +483,34 @@
501484 $result .= $query_result;
502485 }
503486
504 - //$result .= '<div style="text-align: center;">' . "\n" . $navigation . "\n</div>\n";//?
505487 } else {
506488 $result = wfMsg( 'smw_result_noresults' );
507489 }
508490 return $result;
509 -// } else { // make a stand-alone file
510 -// $result = $printer->getResult( $res, $this->m_params, SMW_OUTPUT_FILE );
511 -// $result_name = $printer->getFileName( $res ); // only fetch that after initialising the parameters
512 -// ///////////////////////////////////////
513 -//
514 -// //$result = $this->getInputForm( $printoutstring, 'offset=' . $this->m_params['offset'] . '&limit=' . $this->m_params['limit'] . $urltail ) . $result;
515 -// //$wgOut->addHTML( $result );
516 -// global $wgOut;
517 -// $wgOut->disable();
518 -//
519 -// header( "Content-type: $result_mime; charset=UTF-8" );
520 -//
521 -// if ( $result_name !== false ) {
522 -// header( "content-disposition: attachment; filename=$result_name" );
523 -// }
524 -//
525 -// echo $result;
526 -// }
527 -
528 -
529491 }
530492
531 - //next come form-helper methods which may or may not be used by the UI designer
 493+ // next come form-helper methods which may or may not be used by the UI designer
532494
533 - public function extractParameters( $p ){
534 - if($this->context==self::SPECIAL_PAGE){
535 - //assume setParams(), setPintouts and setQueryString have been called
536 - $rawparams = array_merge($this->m_params, array($this->m_querystring), $this->m_printouts);
 495+ public function extractParameters( $p ) {
 496+ if ( $this->context == self::SPECIAL_PAGE ) {
 497+ // assume setParams(), setPintouts and setQueryString have been called
 498+ $rawparams = array_merge( $this->m_params, array( $this->m_querystring ), $this->m_printouts );
537499 }
538 - else //context is WIKI_LINK
 500+ else // context is WIKI_LINK
539501 {
540502 $rawparams = SMWInfolink::decodeParameters( $p, true );
541 - //calling setParams to fill in missing parameters
542 - $this->setParams($rawparams);
543 - $rawparams= array_merge($this->m_params, $rawparams);
 503+ // calling setParams to fill in missing parameters
 504+ $this->setParams( $rawparams );
 505+ $rawparams = array_merge( $this->m_params, $rawparams );
544506 }
545 -
546 - //var_dump($this->m_params);var_dump($this->m_printouts);var_dump($this->m_querystring);
547 - //var_dump("after<br>");
 507+
 508+
548509 SMWQueryProcessor::processFunctionParams( $rawparams, $this->m_querystring, $this->m_params, $this->m_printouts );
549 - //var_dump($this->m_params);var_dump($this->m_printouts);var_dump($this->m_querystring);
550510 }
551511 /**
552512 * $m_querystring, $m_params, $m_printouts are set, returns the relevant #ask query
553513 */
554 - public function makeAsk(){
 514+ public function makeAsk() {
555515 $result = '{{#ask:' . htmlspecialchars( $this->m_querystring ) . "\n";
556516 foreach ( $this->m_printouts as $printout ) {
557517 $result .= '|' . $printout->getSerialisation() . "\n";
@@ -562,20 +522,20 @@
563523 return $result;
564524 }
565525
566 - public function getQueryString(){
 526+ public function getQueryString() {
567527 return $this->m_querystring;
568528 }
569529
570 - public function getParams(){
 530+ public function getParams() {
571531 return $this->m_params;
572532 }
573533 /**
574534 *
575535 * @return array of SMWPrintRequest
576536 */
577 - public function getPrintOuts(){
578 - if(!empty($this->printouts)){
579 - if(is_a($this->printouts[0],'SMWPrintRequest')){
 537+ public function getPrintOuts() {
 538+ if ( !empty( $this->printouts ) ) {
 539+ if ( is_a( $this->printouts[0], 'SMWPrintRequest' ) ) {
580540 return $this->m_printouts;
581541 }
582542 }
@@ -583,16 +543,16 @@
584544 }
585545 /**
586546 * Constructs a new SMWQueryUIHelper when parameters are passed in the InfoLink style
587 - *
 547+ *
588548 * Errors, if any can be accessed from hasError() and getErrors()
589549 *
590550 * @param string $p parametrs
591551 * @param boolean $enable_validation
592 - * @return SMWQueryUIHelper
 552+ * @return SMWQueryUIHelper
593553 */
594 - public static function makeFromInfoLink($p, $enable_validation = true){
595 - $result = new SMWQueryUIHelper($enable_validation, self::WIKI_LINK);
596 - $result->extractParameters($p);
 554+ public static function makeFromInfoLink( $p, $enable_validation = true ) {
 555+ $result = new SMWQueryUIHelper( $enable_validation, self::WIKI_LINK );
 556+ $result->extractParameters( $p );
597557 return $result;
598558 }
599559 /**
@@ -606,27 +566,27 @@
607567 * @param boolean $enable_validation
608568 * @return SMWQueryUIHelper
609569 */
610 - public static function makeFromUI($query, array $params, array $printouts, $enable_validation = true){
611 - $result = new SMWQueryUIHelper($enable_validation, self::SPECIAL_PAGE);
612 - $result->setParams($params);
613 - $result->setPrintOuts($printouts);
614 - $result->setQueryString($query);
615 - $result->extractParameters("");
 570+ public static function makeFromUI( $query, array $params, array $printouts, $enable_validation = true ) {
 571+ $result = new SMWQueryUIHelper( $enable_validation, self::SPECIAL_PAGE );
 572+ $result->setParams( $params );
 573+ $result->setPrintOuts( $printouts );
 574+ $result->setQueryString( $query );
 575+ $result->extractParameters( "" );
616576 return $result;
617577 }
618578 /**
619579 * Checks if $property exists in the wiki or not
620580 * @return bool
621581 */
622 - protected static function validateProperty($property){
 582+ protected static function validateProperty( $property ) {
623583 /*
624584 * Curently there isn't a simple, back-end agnost way of searching for properties from
625585 * SMWStore. We hence we check if $property has a corresponding page describing it.
626586 */
627 - $prop= substr ($property, 1);//removing the leading '?' while checking.
 587+ $prop = substr ( $property, 1 );// removing the leading '?' while checking.
628588 $propertypage = Title::newFromText( $prop, SMW_NS_PROPERTY );
629 - if(is_a($propertypage, 'Title')){
630 - return($propertypage->exists());
 589+ if ( is_a( $propertypage, 'Title' ) ) {
 590+ return( $propertypage->exists() );
631591 } else {
632592 return false;
633593 }
@@ -638,7 +598,7 @@
639599 *
640600 * @return string
641601 */
642 - protected static function getDefaultResultPrinter(){
 602+ protected static function getDefaultResultPrinter() {
643603 return 'broadtable';
644604 }
645605

Comments

#Comment by Devayon (talk | contribs)   17:49, 9 July 2011

a follow-up to r91780