Example #1
0
 function getNewParserOutput($xml_file)
 {
     $newXmlQueryParser = new XmlQueryParser();
     $xml_obj = $newXmlQueryParser->getXmlFileContent($xml_file);
     $parser = new QueryParser($xml_obj->query);
     return $parser->toString();
 }
 public function __construct($expression, $field_weights = array())
 {
     // Add the colon in the first pass.
     $kf = array();
     foreach ($field_weights as $key => $value) {
         $kf[$key . ':'] = $value;
     }
     $lexer = new QueryLexer();
     $parser = new QueryParser($lexer);
     $this->query = $this->buildQuery($parser->parse($expression), $kf);
 }
Example #3
0
 /**
  * Parse a query string into a Query object
  *
  * $urlEncoding is used to control how the query string is parsed and how
  * it is ultimately serialized. The value can be set to one of the
  * following:
  *
  * - true: (default) Parse query strings using RFC 3986 while still
  *   converting "+" to " ".
  * - false: Disables URL decoding of the input string and URL encoding when
  *   the query string is serialized.
  * - 'RFC3986': Use RFC 3986 URL encoding/decoding
  * - 'RFC1738': Use RFC 1738 URL encoding/decoding
  *
  * @param string      $query       Query string to parse
  * @param bool|string $urlEncoding Controls how the input string is decoded
  *                                 and encoded.
  * @return self
  */
 public static function fromString($query, $urlEncoding = true)
 {
     static $qp;
     if (!$qp) {
         $qp = new QueryParser();
     }
     $q = new static();
     if ($urlEncoding !== true) {
         $q->setEncodingType($urlEncoding);
     }
     $qp->parseInto($q, $query, $urlEncoding);
     return $q;
 }
 /**
  * 1. Read xml file<br />
  * 2. Check the action<br />
  * 3. Parsing and write a cache file<br />
  * @return QueryParser object
  */
 function &parse_xml_query($query_id, $xml_file, $cache_file)
 {
     // Read xml file
     $xml_obj = $this->getXmlFileContent($xml_file);
     // insert, update, delete, select action
     $action = strtolower($xml_obj->query->attrs->action);
     if (!$action) {
         return;
     }
     // Write query cache file
     $parser = new QueryParser($xml_obj->query);
     FileHandler::writeFile($cache_file, $parser->toString());
     return $parser;
 }
Example #5
0
 /**
  * Get posts archived by year, month
  *
  * @return  array
  */
 public function get_archives()
 {
     $archive = [];
     if ($this->is_archive()) {
         $posts = $this->posts;
     } else {
         $queryStr = 'post_status = published : post_type = post : orderBy = post_created, DESC';
         $parser = new QueryParser($queryStr);
         $posts = $parser->parseQuery($queryStr);
     }
     if (empty($posts)) {
         return [];
     }
     foreach ($posts as $post) {
         $year = date('Y', $post->created);
         $month = date('F', $post->created);
         $archive[$year][$month][] = $post;
     }
     return $archive;
 }
Example #6
0
 function setTableColumnTypes($tables)
 {
     $query_id = $this->getQueryId();
     if (!isset($this->column_type[$query_id])) {
         $table_tags = $tables->getTables();
         $column_type = array();
         foreach ($table_tags as $table_tag) {
             if (is_a($table_tag, 'TableTag')) {
                 $table_name = $table_tag->getTableName();
                 $table_alias = $table_tag->getTableAlias();
                 $tag_column_type = QueryParser::getTableInfo($query_id, $table_name);
                 $column_type[$table_alias] = $tag_column_type;
             }
         }
         $this->column_type[$query_id] = $column_type;
     }
 }
 /**
  * Creates the database structure of the wcf.
  */
 protected function createDB()
 {
     $this->initDB();
     // get content of the sql structure file
     $sql = file_get_contents(TMP_DIR . TMP_FILE_PREFIX . 'mysql.sql');
     // installation number value 'n' (WCF_N) must be reflected in the executed sql queries
     $sql = StringUtil::replace('wcf1_', 'wcf' . WCF_N . '_', $sql);
     // replace charset configuration
     if (Database::$dbCharsets[self::$charset] != 'utf8') {
         $sql = StringUtil::replace('DEFAULT CHARSET=utf8', 'DEFAULT CHARSET=' . Database::$dbCharsets[self::$charset], $sql);
     }
     // execute sql queries
     $tables = QueryParser::sendQueries($sql);
     // log sql queries
     foreach ($tables as $tableName) {
         $sql = "INSERT INTO\twcf" . WCF_N . "_package_installation_sql_log\n\t\t\t\t\t\t(sqlTable)\n\t\t\t\tVALUES\t\t('" . escapeString($tableName) . "')";
         self::getDB()->sendQuery($sql);
     }
     $this->gotoNextStep('logFiles');
 }
 /**
  * Deletes from alter statements those definitions (columns or indices) which 
  * could lead to an exception. 
  * 
  * @param	string		$alterStatement
  * @param	string		$alterDefinitions
  * @param 	string		$tableName
  */
 protected function handleAlterUpdate($alterStatement, $alterDefinitions, $tableName)
 {
     $definitions = QueryParser::splitAlterStatement($alterStatement);
     // get existing columns & indices
     $columns = WCF::getDB()->getColumns($tableName);
     $indices = WCF::getDB()->getIndices($tableName, true);
     // "checksum"
     $definitionsCount = count($definitions);
     // check for predictable problems
     foreach ($definitions as $key => $definition) {
         // check CHANGE COLUMN
         if (preg_match("%CHANGE(?:\\s+COLUMN)?\\s+`?(\\w+)`?\\s+`?(\\w+)`?([^,]+),?%i", $definition, $changeColumn)) {
             // column is already renamed.
             if (!in_array($changeColumn[1], $columns) && in_array($changeColumn[2], $columns)) {
                 unset($definitions[$key]);
             }
         } elseif (preg_match("%ADD%i", $definition)) {
             // check ADD COLUMN
             if (preg_match("%(?:\\s+COLUMN)\\s+\\(?`?(\\w+)`?,?%i", $definition, $addColumn)) {
                 // column exists already
                 if (in_array($addColumn[1], $columns)) {
                     unset($definitions[$key]);
                 }
             } elseif (preg_match("%(?:INDEX|KEY)\\s+`?(\\w+)`?%i", $definition, $addIndex)) {
                 // index exist already
                 if (in_array($addIndex[1], $indices)) {
                     unset($definitions[$key]);
                 }
             } elseif (preg_match("%(?:FULLTEXT|SPATIAL)(?:\\s+INDEX)?\\s*`?(\\w+)`?%i", $definition, $addFulltext)) {
                 // index exist already
                 if (in_array($addFulltext[1], $indices)) {
                     unset($definitions[$key]);
                 }
             } elseif (preg_match("%(?:\\s+CONSTRAINT(?:\\s+`?\\w+`?)?)?\\s*PRIMARY\\s+KEY%i", $definition, $addPrimaryKey)) {
                 // index exist already
                 if (in_array('PRIMARY', $indices)) {
                     unset($definitions[$key]);
                 }
             } elseif (preg_match("%(?:\\s+CONSTRAINT(?:\\s+`?\\w+`?)?)?\\s*UNIQUE(?:\\s+INDEX)?\\s`?(\\w+)`?%i", $definition, $addUniqueKey)) {
                 // index exist already
                 if (in_array($addUniqueKey[1], $indices)) {
                     unset($definitions[$key]);
                 }
             }
         } elseif (preg_match("%RENAME(?:\\s+TO)?\\s+`?(\\w+)`?%si", $definition, $rename)) {
             // update package failure
             if (!in_array($tableName, $this->existingTables) && !in_array($rename[1], $this->existingTables)) {
                 throw new SystemException("Illegal statement '" . $rename[0] . "'. ", 13021);
             }
             // table is already renamed
             if (in_array($rename[1], $this->existingTables)) {
                 unset($definitions[$key]);
             }
         }
     }
     // alter statement is empty. delete it
     if (count($definitions) == 0) {
         $this->deleteStatement($alterStatement);
     } elseif ($definitionsCount > count($definitions)) {
         preg_match("%ALTER(\\s+IGNORE)?\\s+TABLE\\s+`?\\w+`?\\s+%i", $alterStatement, $matches);
         $replaceStatement = $matches[0];
         foreach ($definitions as $key => $definition) {
             $definition = preg_replace("%\\s*,\\s*\$%", "", $definition);
             $replaceStatement .= $definition;
             if (isset($definitions[$key + 1])) {
                 $replaceStatement .= ',';
             } else {
                 $replaceStatement .= ';';
             }
         }
         $this->replaceStatement($alterStatement, $replaceStatement);
     }
 }
Example #9
0
 /**
  * Checks whether if an item exists on the queried path or not
  * @param string $query
  * @return bool
  */
 public function exists($query)
 {
     if (empty($query)) {
         throw new \InvalidArgumentException('Argument 1 should not be empty.');
     }
     $keyExists = 'exists' . $this->type;
     $key = '';
     $result = null;
     while (strlen($key .= $this->shiftKey($query)) > 0 and $result === null) {
         $realKey = $this->parseKey($key);
         if ($this->{$keyExists}($realKey)) {
             switch ($this->type) {
                 case static::TYPE_ARRAY:
                     $result =& $this->object[$realKey];
                     break;
                 case static::TYPE_ACCESS:
                     if ($key[0] !== '@' and $key[0] !== '!' and $this->object->offsetExists($realKey)) {
                         $result =& $this->object->offsetGet($realKey);
                         break;
                     }
                 case static::TYPE_OBJECT:
                     if ($key[0] === '@') {
                         $call = $this->object[$realKey];
                         if (!$call instanceof Provider) {
                             throw new \RuntimeException('"' . $key . '" is not a Provider. Using "@" (service-getter) prefix only allowed on Provider item.');
                         }
                         $result = $call($this->object);
                     } else {
                         if ($key[0] == '!') {
                             $call = $this->object[$realKey];
                             if (!$call instanceof Provider) {
                                 throw new \RuntimeException('"' . $key . '" is not a Provider. Using "!" (mutation-getter) prefix only allowed on Provider item.');
                             }
                             if (!$call->isMutated()) {
                                 throw new \RuntimeException('Provider "' . $key . '" is not mutated.');
                             }
                             $result =& $call->getMutatedItem();
                         } else {
                             $result =& $this->object->{$realKey};
                         }
                     }
                     break;
             }
             if ($query !== '' and (is_array($result) or is_object($result))) {
                 $q = new static($result);
                 if ($q->exists($query)) {
                     $result =& QueryParser::Create($result)->find($query);
                 } else {
                     return false;
                 }
             }
         } else {
             if ($query === '') {
                 return false;
             }
         }
     }
     return true;
 }
Example #10
0
 /**
  * Recovers this backup to the database.
  */
 public function recover()
 {
     // we wont use ZipFile::getFileSize(), because our algorithm seems to be faster (and not so buggy ?!).
     // reads the isize block of the gzip file; see RFC 1952 (http://tools.ietf.org/html/rfc1952)
     //$filesize = $this->getFile()->getFileSize();
     //$filesize = $this->getZipFile(true)->getFileSize();
     $filesize = 1 << 27;
     // 128 MiB
     var_dump($filesize);
     $sqlStr = $this->getZipFile(true)->read($filesize);
     echo '->' . $sqlStr . '<-';
     require_once WCF_DIR . 'lib/system/database/QueryParser.class.php';
     //var_dump($sqlStr);
     QueryParser::sendQueries($sqlStr);
 }