/**
	 * Contact the MWDaemon search server and return a wrapper
	 * object with the set of results. Results may be cached.
	 *
	 * @param $method String: the protocol verb to use
	 * @param $query String
	 * @param $limit Integer
	 * @param $offset Integer
	 * @param $searchAll Boolean
	 * @return array
	 */
	public static function newFromQuery( $method, $query, $namespaces = array(), $limit = 20, $offset = 0, $searchAll = false ) {
		wfProfileIn( __METHOD__ );

		$wgSolrTalker = new SolrTalker();

		$query = $wgSolrTalker->queryChecker( $query );
		$xml = $wgSolrTalker->solrQuery( $query, $offset, $limit, true, true ); // Abfrage ok, ergebniss in XML
		$totalHits = $xml->result['numFound'];

		$resultLines = array(); ;

		$highl = $xml->xpath( '//lst[@name="highlighting"]/lst' );

		$hli = 0;

		foreach ( $xml->result->doc as $doc ) {
			if ( isset( $highl[$hli]->arr ) ) {
				foreach ( $highl[$hli]->arr as $feld ) {
					if ( isset( $feld['name'] ) ) {
						switch ( $feld['name'] ) {
							case 'title':
								$doc[]->highlight->title = $feld->str;
								break;
							case 'Inhalt de_t':
								$doc[]->highlight->Inhalt = $feld->str;
								break;
						}
					}
				}
			}
			$hli++;
			$resultLines[] = $doc;
		}

		$suggestion = null;
		$info = null;
		$interwiki = null;

		$resultSet = new SolrSearchSet(
			$method, $query, $resultLines, count( $resultLines ), $totalHits,
			$suggestion, $info, $interwiki
		);

		wfProfileOut( __METHOD__ );
		return $resultSet;
	}
	/**
	 * Execute the provided query and return the result as an
	 * SMWQueryResult if the query was a usual instance retrieval query. In
	 * the case that the query asked for a plain string (querymode
	 * MODE_COUNT or MODE_DEBUG) a plain wiki and HTML-compatible string is
	 * returned.
	 *
	 * @param SMWQuery $query
	 *
	 * @return SMWQueryResult
	 */
	public function getQueryResult( SMWQuery $query ) {
		// IF YOU SEE THIS HERE PLEASE IGNORE IT!
		// Our first approach was to create new SMWStore for querying data
		// but we had big problems recreating and parsing the SMW query syntax,
		// so we just stopped at this point here.
		// Maybe we will finish it someday
		$wgSolrTalker = new SolrTalker();
		if ( property_exists( $query, 'params' ) &&
				array_key_exists( 'source', $query->params ) &&
				$query->params['source'] == 'solr' ) {

			$results = array();
			$dbkey = '';
			$namespace = 0;
			$interwiki = '';

			echo( "SOLR query: {$query->getQueryString()}\n" );

			echo 'Search is powered by Solr!';
			echo $queryStr = urldecode( $wgSolrTalker->parseSolrQuery( $query->getQueryString() ) );

			// Get sort parameters and add them to the query string
			if ( $query->sort ) {
				// TODO: Der Inhalt von Sort muss genau der Name eines der Felder von Solr sein
				//	  um danach Sortieren zu können. Deshalb Wird eine Liste alle Solr Felder
				//	  Benötigt um Festzustellen welches Feld gemeint ist bzw. welche _XYZ Endung
				//	  an dem Ende des Feldes angehängt wurde.
				//
				$sort = $wgSolrTalker->findField( $query->params['sort'], $query->params['order'] );
				$queryStr .= '&sort%3D' . $sort . '+' . trim( $query->params['order'] );
				//  $queryStr = $queryStr . '&sort=' . trim($sort . '+' . trim($query->params['order']));
				// TODO: Mehrer Sort parameter auslesen wenn sie vorhanden sind.
			} //else {
//				$queryStr = $queryStr . '&sort=pagetitle';
//			}

			// TODO: Prüfen wieso nur 1 Ergebniss ausgegeben wird
			echo 'Query Limit:' . $query->getLimit();

			echo ( 'SEARCHRESULT: ' . $xml = $wgSolrTalker->solrQuery( $queryStr, $query->getOffset(), $query->getLimit() ) );
			echo '<br />';
			// TODO: Move this code to parseSolrResult
			$numFound = $xml->result['numFound'];
			foreach ( $xml->result->doc as $doc ) {
				foreach ( $doc->str as $field ) {
					switch ( $field['name'] ) {
						case 'dbkey':
							$dbkey = $field;
							break;
						case 'interwiki':
							$interwiki = $field;
							break;
						case 'namespace':
							$namespace = $field;
							break;
					}
				}
				// Multivalue fields
				foreach ( $doc->arr as $field ) {
					switch ( $field['name'] ) {
						case 'dbkey':
							$dbkey = $field;
							break;
						case 'interwiki':
							$interwiki = $field;
							break;
						case 'namespace':
							$namespace = $field;
							break;
					}
					foreach ( $field->str as $value ) {
						$value;
					}
				}
				$results[] = new SMWDIWikiPage( $dbkey, $namespace, $interwiki );
			}

			// Do we have more results?
			$more = false;
			// TODO: Does this work?
			echo 'Number of records: ' . $numFound;
			if ( $numFound > 10 ) {
				$more = true;
			}

			// return new SMWQueryResult($printRequests, $query, $results, $store);
			return new SMWQueryResult( $query->getDescription()->getPrintrequests(), $query, $results, $this, $more );
		} else {
			return self::getBaseStore()->getQueryResult( $query );
		}
	}