Zend Framework dom problem - html

I want to get website shortcut icon(favicon) and stylesheet path with zend_dom query
$dom = new Zend_Dom_Query($html);
$stylesheet = $dom->query('link[rel="stylesheet"]');
$shortcut = $dom->query('link[rel="shortcut icon"]');
Stylesheet query is work but shortcut icon query not work. How i do?
Thanks.

This appears to be an issue with Zend's css style query implementation. In Zend/Dom/Query.php, the query function calls a conversion function to convert the query into proper xpath format:
public function query($query)
{
$xpathQuery = Zend_Dom_Query_Css2Xpath::transform($query);
return $this->queryXpath($xpathQuery, $query);
}
However within the transform() method, they seem to be using some pretty basic regex to split up the string by spaces:
$segments = preg_split('/\s+/', $path);
Which basically means your link[rel="shortcut icon"] query now becomes two queries: link[rel="shortcut and icon"]
To get around this, you can use the method Zend_Dom_Query::queryXpath() and provide it with a proper xPath query. Like this:
$dom->queryXpath('//link[#rel="shortcut icon"]');

Related

RDF4J SPARQL query to JSON

I am trying to move data from a SPARQL endpoint to a JSONObject. Using RDF4J.
RDF4J documentation does not address this directly (some info about using endpoints, less about converting to JSON, and nothing where these two cases meet up).
Sofar I have:
SPARQLRepository repo = new SPARQLRepository(<My Endpoint>);
Map<String, String> headers = new HashMap<String, String>();
headers.put("Accept", "SPARQL/JSON");
repo.setAdditionalHttpHeaders(headers);
try (RepositoryConnection conn = repo.getConnection())
{
String queryString = "SELECT * WHERE {GRAPH <urn:x-evn-master:mwadata> {?s ?p ?o}}";
GraphQuery query = conn.prepareGraphQuery(queryString);
debug("Mark 2");
try (GraphQueryResult result = query.evaluate())
this fails because "Server responded with an unsupported file format: application/sparql-results+json"
I figured a SPARQLGraphQuery should take the place of GraphQuery, but RepositoryConnection does not have a relevant prepare statement.
If I exchange
try (RepositoryConnection conn = repo.getConnection())
with
try (SPARQLConnection conn = (SPARQLConnection)repo.getConnection())
I run into the problem that SPARQLConnection does not generate a SPARQLGraphQuery. The closest I can get is:
SPARQLGraphQuery query = (SPARQLGraphQuery)conn.prepareQuery(QueryLanguage.SPARQL, queryString);
which gives a runtime error as these types cannot be cast to eachother.
I do not know how to proceed from here. Any help or advise much appreciated. Thank you
this fails because "Server responded with an unsupported file format: application/sparql-results+json"
In RDF4J, SPARQL SELECT queries are tuple queries, so named because each result is a set of bindings, which are tuples of the form (name, value). In contrast, CONSTRUCT (and DESCRIBE) queries are graph queries, so called because their result is a graph, that is, a collection of RDF statements.
Furthermore, setting additional headers for the response format as you have done here is not necessary (except in rare circumstances), the RDF4J client handles this for you automatically, based on the registered set of parsers.
So, in short, simplify your code as follows:
SPARQLRepository repo = new SPARQLRepository(<My Endpoint>);
try (RepositoryConnection conn = repo.getConnection()) {
String queryString = "SELECT * WHERE {GRAPH <urn:x-evn-master:mwadata> {?s ?p ?o}}";
TupleQuery query = conn.prepareTupleQuery(queryString);
debug("Mark 2");
try (TupleQueryResult result = query.evaluate()) {
...
}
}
If you want to write the result of the query in JSON format, you could use a TupleQueryResultHandler, for example the SPARQLResultsJSONWriter, as follows:
SPARQLRepository repo = new SPARQLRepository(<My Endpoint>);
try (RepositoryConnection conn = repo.getConnection()) {
String queryString = "SELECT * WHERE {GRAPH <urn:x-evn-master:mwadata> {?s ?p ?o}}";
TupleQuery query = conn.prepareTupleQuery(queryString);
query.evaluate(new SPARQLResultsJSONWriter(System.out));
}
This will write the result of the query (in this example to standard output) using the SPARQL Query Results JSON format. If you have a non-standard format in mind, you could of course also create your own TupleQueryResultHandler implementation.
For more details on the various ways in which you can process the result (including iterating, streaming, adding to a List, or just directly sending to a result handler), see the documentation on querying a repository. As an aside, the javadoc on the RDF4J APIs is pretty extensive too, so if your Java editing environment has support for displaying that, I'd advise you to make use of it.

Laravel Query Builder Issue

I'm trying to make it so that the items pulled from the database are only from the current month, however when I do this using various methods it throws an error (which I can't debug due to a the issue mentioned here). In the code below if I just simply paginate the full results it works fine, but when I start to use query builder methods it gives an error, does anybody have any idea why this error is occurring?
public function index()
{
// $trades=Trade::paginate(10);
$currentMonth = date('m');
$trades = DB::table('trades')->whereMonth('date', $currentMonth)->paginate(10);
// dd($trades);
$pastwinners=Winner::paginate(10);
return view('raffle', compact('pastwinners'), compact('trades'));
}
As you can see above the $pastwinners variable and the first $trades variable provide the proper values, but when I'm trying to select only the ones from this month (using the 'date' field) it seems to break.
Try this one. And if you want to paginate your results, do not forget to add pagination links to your view. Also do not forget the import Carbon in your controller class.
https://laravel.com/docs/5.6/pagination
public function index()
{
$trades = Trade::whereMonth('date' , Carbon::today()->month)->paginate();
$pastwinners = Winner::all();
return view('raffle', compact('pastwinners', 'trades');
}

insert query symfony 2 without form

I'm working with Symfony 2 and I need to insert some data in a MySQL table. I know how to do it using a form:
$m=new table();
$form=$this->container->get('form.factory')->create(new tableType(),$m);
$request=$this->getRequest();
if($request->getMethod()=='POST')
{
$form->bind($request);
if ($form->isValid())
{
$rm=$this->container->get('doctrine')->getEntityManager();
$rm->persist($m);
$rm->flush();
}
that works but I dont want to use a pre-defined form because I need complex control on my input. I need to generate the value with jQuery.
So how can I proceed to insert the values of my input into my table?
Generally you can pass the whole request to the action as following
use Symfony\Component\HttpFoundation\Request;
// <...>
public function fooAction(Request $request)
{
$foo = $request->query->get('foo', 'default_value_for_foo'); // get the foo param from request query string (GET params)
$bar = $request->query->get('bar', 'default_value_for_bar'); // get the bar param from request POST params
}
Also you might be interested in collection form types which can allow you to generate multiple rows or entities for form (not fixed)
That's actually easier than using forms :D
<?php
// ...
// fetch your params
$m = new table();
$m->setWhatever($request->get('whatever'));
// persist
$em = $this->get('doctrine.orm.entity_manager');
$em->persist($m);
Note:
use camel-case PHP-class names: e.g. Table, NiceTable
$form->bind is deprecated I think, use $form->handleRequest
anyway if you need to validate your input, I recommend using validators and forms anyway. Setting up a model and validate it is quite smart. You don't need to create the view createView() of it of course, but the validation component in Symfony is very mighty :).

parsing HTML as string to get values using keyword

I have an html file which is read as a string.. i want to parse that and get values using <TD colSpan=2>Value :
So there are around 10 values i should get from the html file.. how can i do that.. i am trying to use something like
startindex endindex getsubstring
sMainBeginKeyword = "<td>Value : ";
sBeginKeyword = "<td>Value : ";
sEndKeyword = "</td>";
main_begin_index = result.indexOf(sMainBeginKeyword);
while (main_begin_index != -1) {
begin_index = main_begin_index;
end_index = result.indexOf(sEndKeyword, begin_index);
String deloc= result.substring(begin_index + sBeginKeyword.length(), end_index);
But this looks complicated and i can not retrieve more values .. As i have a lot of values with different keywords..
This sort of thing really does need to be done using an XML or DOM parser: Trying to do it with string searches is setting yourself up for failure.
If you loaded the HTML into an XML or DOM parser, the task you're trying to do would be trivial to achieve using XPath notation to find the relevant elements.
You haven't specified which language or platform you're working on (and the code sample you've given is insufficient to be sure either), so it's hard to be any more specific.
Hope that helps.

LINQ to SQL Projection: Func vs Inline

I am finding some unexpected behavior when using a projection in a LINQ to SQL query using a Func. Example code will explain better than words.
A basic L2S lambda query using projection:
db.Entities.Select(e => new DTO(e.Value));
It translates to the desired SQL:
SELECT [t1].[Value]
FROM [Entity] AS [t1]
However, when the projection is put into a Func like this:
Func<Entity, DTO> ToDTO = (e) => new DTO(e.Value);
And called like this:
db.Entities.Select(e => ToDTO(e));
The SQL is now pulling back all of the columns in the table, not just the one in the projection:
SELECT [t1].[Id], [t1].[Value], [t1].[timestamp], [t1].[etc...]
FROM [Entity] AS [t1]
So my question is, how do I encapsulate this projection without the LINQ to SQL instantiating the whole Entity?
Things to keep in mind, the DTO I am using has a protected default constructor, so I can't use an object initializer. And since the DTO class cannot be modified, I'd have to make a subclass to implement that behavior. Which is fine, if that's the only solution.
Thanks.
Edit:
Thanks to Brian for the solution. I had previously tried an Expression but couldn't figure out the syntax. Here's the working code:
Expression<Entity, DTO> ToDTO = (e) => new DTO(e.Value);
Then call it like this:
db.Entities.Select(ToDTO);
At first I was trying to call it like this, which wouldn't compile. This is the proper syntax for calling a Func, but not an Expression.
db.Entities.Select(e => ToDTO(e));
You probably need to create an Expression, not a Func
Expression<Func<Entity, DTO>> ToDTO = (e) => new DTO(e.Value);
IQueryable extension methods work with Expressions, not Funcs
By passing in a Func, you are probably invoking the IEnumerable extension method, which is why Linq2Sql is acting the way it is.