summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/ExternalData/includes/ED_GetData.php
blob: ab08442621d1e5be915c705f59cb753ef48d68b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
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';
	}
}