/**
	 * Parse a query string given in SMW's query language to create
	 * an SMWQuery. Parameters are given as key-value-pairs in the
	 * given array. The parameter $context defines in what context the
	 * query is used, which affects ceretain general settings.
	 * An object of type SMWQuery is returned.
	 *
	 * The format string is used to specify the output format if already
	 * known. Otherwise it will be determined from the parameters when
	 * needed. This parameter is just for optimisation in a common case.
	 *
	 * @param string $querystring
	 * @param array $params These need to be the result of a list fed to getProcessedParams
	 * @param $context
	 * @param string $format
	 * @param array $extraprintouts
	 *
	 * @return SMWQuery
	 */
	static public function createQuery( $querystring, array $params, $context = self::INLINE_QUERY, $format = '', array $extraprintouts = array() ) {
		global $smwgQDefaultNamespaces, $smwgQFeatures, $smwgQConceptFeatures;
		
		// parse query:
		$queryfeatures = ( $context == self::CONCEPT_DESC ) ? $smwgQConceptFeatures : $smwgQFeatures;
		$qp = new SMWQueryParser( $queryfeatures );
		$qp->setDefaultNamespaces( $smwgQDefaultNamespaces );
		$desc = $qp->getQueryDescription( $querystring );

		if ( $format === '' || is_null( $format ) ) {
			$format = $params['format'];
		}
		
		if ( $format == 'count' ) {
			$querymode = SMWQuery::MODE_COUNT;
		} elseif ( $format == 'debug' ) {
			$querymode = SMWQuery::MODE_DEBUG;
		} else {
			$printer = self::getResultPrinter( $format, $context );
			$querymode = $printer->getQueryMode( $context );
		}

		$query = new SMWQuery( $desc, ( $context != self::SPECIAL_PAGE ), ( $context == self::CONCEPT_DESC ) );
		$query->setQueryString( $querystring );
		$query->setExtraPrintouts( $extraprintouts );
		$query->setMainLabel( $params['mainlabel'] );
		$query->addErrors( $qp->getErrors() ); // keep parsing errors for later output

		// set mode, limit, and offset:
		$query->querymode = $querymode;
		if ( ( array_key_exists( 'offset', $params ) ) && ( is_int( $params['offset'] + 0 ) ) ) {
			$query->setOffset( max( 0, trim( $params['offset'] ) + 0 ) );
		}

		if ( $query->querymode == SMWQuery::MODE_COUNT ) { // largest possible limit for "count", even inline
			global $smwgQMaxLimit;
			$query->setOffset( 0 );
			$query->setLimit( $smwgQMaxLimit, false );
		} else {
			if ( ( array_key_exists( 'limit', $params ) ) && ( is_int( trim( $params['limit'] ) + 0 ) ) ) {
				$query->setLimit( max( 0, trim( $params['limit'] ) + 0 ) );
				if ( ( trim( $params['limit'] ) + 0 ) < 0 ) { // limit < 0: always show further results link only
					$query->querymode = SMWQuery::MODE_NONE;
				}
			} else {
				global $smwgQDefaultLimit;
				$query->setLimit( $smwgQDefaultLimit );
			}
		}

		$defaultSort = $format === 'rss' ? 'DESC' : 'ASC';
		$sort = self::getSortKeys( $params['sort'], $params['order'], $defaultSort );
		
		$query->sortkeys = $sort['keys'];
		$query->addErrors( $sort['errors'] );
		$query->sort = count( $query->sortkeys ) > 0; // TODO: Why would we do this here?
		
		return $query;
	}
	static public function addNotify( $rawquery, $name, $rep_all, $show_all, $delegate ) {
		global $wgTitle;
		// Take care at least of some templates -- for better template support use #ask
		$parser = new Parser();
		$parserOptions = new ParserOptions();
		$rawquery = $parser->transformMsg( $rawquery, $parserOptions, $wgTitle );

		wfProfileIn( 'SMWNotifyProcessor::createNotify (SMW)' );
		$sStore = NMStorage::getDatabase();
		global $wgUser;
		$user_id = $wgUser->getId();
		if ( $user_id == 0 ) {
			wfProfileOut( 'SMWNotifyProcessor::createNotify (SMW)' );
			return wfMsg( 'smw_nm_proc_notlogin' );
		}

		// check notify query first, use QueryParser from SMW
		SMWQueryProcessor::processFunctionParams( SMWNotifyProcessor::getQueryRawParams( $rawquery ), $querystring, $params, $printouts );

		$qp = new SMWQueryParser();
		$qp->setDefaultNamespaces( $smwgQDefaultNamespaces );
		$desc = $qp->getQueryDescription( $querystring );

		if ( count( $qp->getErrors() ) > 0 ) {
			wfProfileOut( 'SMWNotifyProcessor::createNotify (SMW)' );
			return wfMsg( 'smw_nm_proc_createfail', implode( "\n", $qp->getErrors() ) );
		}

		$notify_id = $sStore->addNotifyQuery( $user_id, $rawquery, $name, $rep_all, $show_all, $delegate );
		if ( $notify_id == 0 ) {
			wfProfileOut( 'SMWNotifyProcessor::createNotify (SMW)' );
			return wfMsg( 'smw_nm_proc_savefail' );
		}
		wfProfileOut( 'SMWNotifyProcessor::createNotify (SMW)' );

		wfProfileIn( 'SMWNotifyProcessor::enableNotify (SMW)' );
		$msg = '';
		$result  = SMWNotifyProcessor::enableNotify( $notify_id, $rawquery, $msg );
		wfProfileOut( 'SMWNotifyProcessor::enableNotify (SMW)' );
		return "1" . ( $result ? "1":"0" ) . "$notify_id,$msg";
	}
Exemplo n.º 3
0
    static function addAutocomplete()
    {
        global $smwgNMDelegateQuery, $smwgNMDelegateUserGroup;
        if (!$smwgNMDelegateQuery && !$smwgNMDelegateUserGroup) {
            return;
        }
        global $wgOut, $smwgNMScriptPath;
        $nmScriptPath = $smwgNMScriptPath . '/specials/SMWNotifyMe';
        $nmYUIBase = "http://yui.yahooapis.com/2.7.0/build/";
        $wgOut->addLink(array('rel' => 'stylesheet', 'type' => 'text/css', 'media' => "screen, projection", 'href' => $nmScriptPath . '/skins/NM_yui_autocompletion.css'));
        $wgOut->addScript('<script type="text/javascript" src="' . $nmYUIBase . 'yahoo/yahoo-min.js"></script>' . "\n");
        $wgOut->addScript('<script type="text/javascript" src="' . $nmYUIBase . 'dom/dom-min.js"></script>' . "\n");
        $wgOut->addScript('<script type="text/javascript" src="' . $nmYUIBase . 'event/event-min.js"></script>' . "\n");
        $wgOut->addScript('<script type="text/javascript" src="' . $nmYUIBase . 'get/get-min.js"></script>' . "\n");
        $wgOut->addScript('<script type="text/javascript" src="' . $nmYUIBase . 'connection/connection-min.js"></script>' . "\n");
        $wgOut->addScript('<script type="text/javascript" src="' . $nmYUIBase . 'json/json-min.js"></script>' . "\n");
        $wgOut->addScript('<script type="text/javascript" src="' . $nmYUIBase . 'datasource/datasource-min.js"></script>' . "\n");
        $wgOut->addScript('<script type="text/javascript" src="' . $nmYUIBase . 'autocomplete/autocomplete-min.js"></script>' . "\n");
        $wgOut->addScript('<script type="text/javascript" src="' . $nmScriptPath . '/libs/NM_yui_autocompletion.js"></script>' . "\n");
        $pages = array();
        if ($smwgNMDelegateUserGroup) {
            if ($smwgNMDelegateUserGroup != "*" && !is_array($smwgNMDelegateUserGroup)) {
                $groups = split(",", $smwgNMDelegateUserGroup);
                for ($i = count($groups) - 1; $i >= 0; --$i) {
                    $groups[$i] = strtolower(trim(str_replace("\\'", "\\\\'", $groups[$i])));
                }
            }
            $pages = NMStorage::getDatabase()->getGroupedUsers($groups);
            for ($i = count($pages) - 1; $i >= 0; --$i) {
                $pages[$i] = "['" . str_replace("\\'", "\\\\'", $pages[$i]) . "']";
            }
        }
        if ($smwgNMDelegateQuery) {
            global $smwgQDefaultNamespaces, $smwgQFeatures;
            $qp = new SMWQueryParser($smwgQFeatures);
            $qp->setDefaultNamespaces($smwgQDefaultNamespaces);
            $desc = $qp->getQueryDescription($smwgNMDelegateQuery);
            $desc->prependPrintRequest(new SMWPrintRequest(SMWPrintRequest::PRINT_THIS, ""));
            $query = new SMWQuery($desc, true);
            $query_result = smwfGetStore()->getQueryResult($query);
            while ($res = $query_result->getNext()) {
                $pages[] = "['" . str_replace("\\'", "\\\\'", $res[0]->getNextObject()->getWikiValue()) . "']";
            }
        }
        global $smwgNMMaxAutocompleteValues;
        if ($smwgNMMaxAutocompleteValues <= 0) {
            $smwgNMMaxAutocompleteValues = 10;
        }
        $wgOut->addScript('<script type="text/javascript">
		nmautocompletestrings = [' . join(',', $pages) . '];
		nmMaxResultsDisplayed = ' . $smwgNMMaxAutocompleteValues . ';
		</script>');
    }