diff options
Diffstat (limited to 'www/wiki/extensions/ExternalData/includes/ED_GetData.php')
-rw-r--r-- | www/wiki/extensions/ExternalData/includes/ED_GetData.php | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/www/wiki/extensions/ExternalData/includes/ED_GetData.php b/www/wiki/extensions/ExternalData/includes/ED_GetData.php new file mode 100644 index 00000000..ab084426 --- /dev/null +++ b/www/wiki/extensions/ExternalData/includes/ED_GetData.php @@ -0,0 +1,75 @@ +<?php +/** + * A special page for retrieving selected rows of any wiki page that contains + * data in CSV format + */ + +class EDGetData extends SpecialPage { + + /** + * Constructor + */ + function __construct() { + parent::__construct( 'GetData' ); + } + + 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; + } + $wikiPage = WikiPage::factory( $title ); + $page_text = ContentHandler::getContentText( $wikiPage->getContent() ); + // 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; + } + + protected function getGroupName() { + return 'pagetools'; + } +} |