function execute($query)
 {
     global $wgRequest, $wgOut;
     $wgOut->disable();
     $this->setHeaders();
     $page_name = $query;
     $title = Title::newFromText($page_name);
     if (is_null($title)) {
         return;
     }
     if (!$title->userCan('read')) {
         return;
     }
     $article = new Article($title);
     $page_text = $article->fetchContent();
     // Remove <noinclude> sections and <includeonly> tags from text
     $page_text = StringUtils::delimiterReplace('<noinclude>', '</noinclude>', '', $page_text);
     $page_text = strtr($page_text, array('<includeonly>' => '', '</includeonly>' => ''));
     $orig_lines = explode("\n", $page_text);
     // ignore lines that are either blank or start with a semicolon
     $page_lines = array();
     foreach ($orig_lines as $i => $line) {
         if ($line != '' && $line[0] != ';') {
             $page_lines[] = $line;
         }
     }
     $headers = EDUtils::getValuesFromCSVLine($page_lines[0]);
     $queried_headers = array();
     foreach ($wgRequest->getValues() as $key => $value) {
         foreach ($headers as $header_index => $header_value) {
             $header_value = str_replace(' ', '_', $header_value);
             if ($key == $header_value) {
                 $queried_headers[$header_index] = $value;
             }
         }
     }
     // include header in output
     $text = $page_lines[0];
     foreach ($page_lines as $i => $line) {
         if ($i == 0) {
             continue;
         }
         $row_values = EDUtils::getValuesFromCSVLine($line);
         $found_match = true;
         foreach ($queried_headers as $i => $query_value) {
             $single_value = str_replace(' ', '_', $row_values[$i]);
             if ($single_value != $query_value) {
                 $found_match = false;
             }
         }
         if ($found_match) {
             if ($text != '') {
                 $text .= "\n";
             }
             $text .= $line;
         }
     }
     print $text;
 }
Esempio n. 2
0
 public static function getDataFromURL($url, $format)
 {
     $url_contents = EDUtils::fetchURL($url);
     // exit if there's nothing there
     if (empty($url_contents)) {
         return array();
     }
     if ($format == 'xml') {
         return self::getXMLData($url_contents);
     } elseif ($format == 'csv') {
         return self::getCSVData($url_contents, false);
     } elseif ($format == 'csv with header') {
         return self::getCSVData($url_contents, true);
     } elseif ($format == 'json') {
         return self::getJSONData($url_contents);
     } elseif ($format == 'gff') {
         return self::getGFFData($url_contents);
     }
     return array();
 }
 /**
  * Render the #get_db_data parser function
  */
 static function doGetDBData(&$parser)
 {
     global $wgTitle, $edgCurPageName, $edgValues;
     // if we're handling multiple pages, reset $edgValues
     // when we move from one page to another
     $cur_page_name = $wgTitle->getText();
     if (!isset($edgCurPageName) || $edgCurPageName != $cur_page_name) {
         $edgValues = array();
         $edgCurPageName = $cur_page_name;
     }
     $params = func_get_args();
     array_shift($params);
     // we already know the $parser ...
     $args = EDUtils::parseParams($params);
     // parse params into name-value pairs
     $data = array_key_exists('data', $args) ? $args['data'] : null;
     $dbID = array_key_exists('db', $args) ? $args['db'] : null;
     // For backwards-compatibility - 'db' parameter was added
     // in External Data version 1.3.
     if (is_null($dbID)) {
         $dbID = array_key_exists('server', $args) ? $args['server'] : null;
     }
     $table = array_key_exists('from', $args) ? $args['from'] : null;
     $conds = array_key_exists('where', $args) ? $args['where'] : null;
     $limit = array_key_exists('limit', $args) ? $args['limit'] : null;
     $orderBy = array_key_exists('order by', $args) ? $args['order by'] : null;
     $options = array('LIMIT' => $limit, 'ORDER BY' => $orderBy);
     $mappings = EDUtils::paramToArray($data);
     // parse the data arg into mappings
     $external_values = EDUtils::getDBData($dbID, $table, array_values($mappings), $conds, $options);
     // handle error cases
     if (is_null($external_values)) {
         return;
     }
     // Build $edgValues
     foreach ($mappings as $local_var => $external_var) {
         if (array_key_exists($external_var, $external_values)) {
             foreach ($external_values[$external_var] as $value) {
                 $edgValues[$local_var][] = $value;
             }
         }
     }
     return;
 }
 /**
  * Render the #store_external_table parser function
  */
 static function doStoreExternalTable(&$parser)
 {
     global $smwgDefaultStore;
     if ($smwgDefaultStore != 'SMWSQLStore3' && !class_exists('SIOHandler')) {
         // If SQLStore3 is not installed, we need SIO.
         return EDUtils::formatErrorMessage('Semantic Internal Objects is not installed');
     }
     global $edgValues;
     $params = func_get_args();
     array_shift($params);
     // we already know the $parser...
     // Get the variables used in this expression, get the number
     // of values for each, and loop through.
     $expression = implode('|', $params);
     $matches = array();
     preg_match_all('/{{{([^}]*)}}}/', $expression, $matches);
     $variables = $matches[1];
     $num_loops = 0;
     foreach ($variables as $variable) {
         // ignore the presence of '.urlencode' - it's a command,
         // not part of the actual variable name
         $variable = str_replace('.urlencode', '', $variable);
         if (array_key_exists($variable, $edgValues)) {
             $num_loops = max($num_loops, count($edgValues[$variable]));
         }
     }
     $text = "";
     for ($i = 0; $i < $num_loops; $i++) {
         // re-get $params
         $params = func_get_args();
         array_shift($params);
         foreach ($params as $j => $param) {
             foreach ($variables as $variable) {
                 // If variable name ends with a ".urlencode",
                 // that's a command - URL-encode the value of
                 // the actual variable.
                 if (strrpos($variable, '.urlencode') === strlen($variable) - strlen('.urlencode')) {
                     $real_var = str_replace('.urlencode', '', $variable);
                     $value = urlencode(self::getIndexedValue($real_var, $i));
                 } else {
                     $value = self::getIndexedValue($variable, $i);
                 }
                 $params[$j] = str_replace('{{{' . $variable . '}}}', $value, $params[$j]);
             }
         }
         // If SQLStore3 is being used, we can call #subobject -
         // that's what #set_internal would call anyway, so
         // we're cutting out the middleman.
         if ($smwgDefaultStore == 'SMWSQLStore3') {
             self::callSubobject($parser, $params);
             continue;
         }
         // Add $parser to the beginning of the $params array,
         // and pass the whole thing in as arguments to
         // doSetInternal, to mimic a call to #set_internal.
         array_unshift($params, $parser);
         // As of PHP 5.3.1, call_user_func_array() requires that
         // the function params be references. Workaround via
         // http://stackoverflow.com/questions/2045875/pass-by-reference-problem-with-php-5-3-1
         $refParams = array();
         foreach ($params as $key => $value) {
             $refParams[$key] =& $params[$key];
         }
         call_user_func_array(array('SIOHandler', 'doSetInternal'), $refParams);
     }
     return null;
 }