Obtain data as simple array

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

Obtain data as simple array

s9gf4ult

Hello, I am hacking SMW, because I need to retrive some data from the mediawiki database and use it to render skin. I know this is the dirty hack, but I do not see another way.

 

I need execute query like this

 

{{

#ask: [[Title picture::+]] [[Category: some category]]

| ?Title picture

| format = ## here some special format ? ##

| limit=10

}}

 

and receive the answer like a simple array

 

array(0 => array("self" => Title object,

"Title_picture" => Title object),

1 => array("self" => Title objec ....

.................

 

While the actual data is receiving by format function, specified in the query, I dont have any idea how to receive data like simple array. Do I need to write my own format function ?


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

Yury Katkov
Hi-hi! 

I would use SMW ask API for that. The returning json object is little bit complex, but this is not usually a big problem
http://semantic-mediawiki.org/wiki/Ask_API
-----
Yury Katkov, WikiVote



On Thu, Jan 31, 2013 at 4:21 PM, <[hidden email]> wrote:

Hello, I am hacking SMW, because I need to retrive some data from the mediawiki database and use it to render skin. I know this is the dirty hack, but I do not see another way.

 

I need execute query like this

 

{{

#ask: [[Title picture::+]] [[Category: some category]]

| ?Title picture

| format = ## here some special format ? ##

| limit=10

}}

 

and receive the answer like a simple array

 

array(0 => array("self" => Title object,

"Title_picture" => Title object),

1 => array("self" => Title objec ....

.................

 

While the actual data is receiving by format function, specified in the query, I dont have any idea how to receive data like simple array. Do I need to write my own format function ?


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel



------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
Reply | Threaded
Open this post in threaded view
|

Queries & pagenames

John McClure
A practice I've seen on semantic wikis is to always store a page's {{PAGENAME}} in a semantic property, so that it can be queried with the like operator, among other reasons I suppose.
 
Is there any way around this, eg allowing magic words in the query? It seems silly to me to store pagenames, problematic whenever the page is moved. Or is the accepted approach that, technically & deliberately, pagetitles are meant to be opaque?
 
Here's a question about namespaces and queries. If there is a property named "has", should I be able to query for property values of a particular namespace such as [[has::<q>[[somenamespace:+]]</q>]]... I think I tried it once and it didn't work out well. Is this construct supposed to work?
 
thanks - john

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

s9gf4ult
In reply to this post by Yury Katkov

> Hi-hi! 

>

> I would use SMW ask API for that. The returning json object is little bit complex, but this is not usually a big problem

> http://semantic-mediawiki.org/wiki/Ask_API

> -----
Yury Katkov, WikiVote


I was affraid of this answer, that means I must parse json by the same program it was generated, this is odd.


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

s9gf4ult
In reply to this post by Yury Katkov
> I would use SMW ask API for that.

Is this means I must use HTTP GET query to retrive the data from myself ? Can
I receive JSON encoded data without query ?

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

Yury Katkov
Yes, you use GET in this case. If you use API from your javascript, it's good enough and very common. 

The other way is to construct the query on a server-side. I have some amount of examples here, but some of them are outdated since the class structure have changed:

http://wikimedia.7.n6.nabble.com/How-to-get-property-values-from-SMW-Was-The-right-type-for-storing-IP-addresses-td4982384.html - here are two ways (mine is dirty and slow but universal and Markus' is clean but specifically for the task) to make queries from PHP

Cheers! 
-----
Yury Katkov, WikiVote



On Fri, Feb 1, 2013 at 9:38 AM, <[hidden email]> wrote:
> I would use SMW ask API for that.

Is this means I must use HTTP GET query to retrive the data from myself ? Can
I receive JSON encoded data without query ?

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

s9gf4ult
In reply to this post by s9gf4ult
> -----------------------------------------------------------
> $result = array();
>
> $query = "[[Title picutre::+]] [[Category: some category]] ?Title picture";
>
> $result = SF_Utils::getAllPagesForQuery( $query );
>

Very interesting, but doesnt work for me

 % php maintenance/eval.php
> $query = "[[Title picutre::+]]";
> $result = SFUtils::getAllPagesForQuery( $query );
> print_r($result);
Array
(
)

While regular query with ask returns the right result

Versions:
SemanticForms - 2.5.1 or master(9b1fead3e94b) (tryed both)
SemanticMediaWiki - 1.8.0.1
Mediawiki - 1.20.2 (8e68cbd)


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

s9gf4ult
In reply to this post by Yury Katkov

>Yes, you use GET in this case. If you use API from your javascript

No, I dont need to get data from javascript, I need to receive data with php on server side and generate the page, I hoped that I can do it with some Semantic API, but it is not so simple as I thought.


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

Yury Katkov
I'm not sure that it should work in a maintenance script. Also why do you think that the result shouldn't be empty in your case? 

Anyway, here how I figured this way - I've executed the query from Special:Ask and ran the debugger :)
-----
Yury Katkov, WikiVote



On Fri, Feb 1, 2013 at 4:51 PM, <[hidden email]> wrote:

>Yes, you use GET in this case. If you use API from your javascript

No, I dont need to get data from javascript, I need to receive data with php on server side and generate the page, I hoped that I can do it with some Semantic API, but it is not so simple as I thought.


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel



------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

Yaron Koren-2
In reply to this post by s9gf4ult
Hi,

A few thoughts:

- Could it be that that call to SFUtils::getAllPagesForQuery() failed because "picture" was misspelled? Stranger things have happened...

- If I understand it correctly, you're modifying SMW itself to add in that query. If so, I'd say that's a bad idea - you're patching existing code, which will make upgrades a pain. If the goal is to display the query in the skin, I would recommend copying over Vector or some such to create a new skin, and then adding the code to that new skin.

- If you end up calling #ask in one way or another, and parsing JSON is difficult, you could also use "format=csv" instead.

-Yaron

On Fri, Feb 1, 2013 at 7:51 AM, <[hidden email]> wrote:

>Yes, you use GET in this case. If you use API from your javascript

No, I dont need to get data from javascript, I need to receive data with php on server side and generate the page, I hoped that I can do it with some Semantic API, but it is not so simple as I thought.


------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel




--
WikiWorks · MediaWiki Consulting · http://wikiworks.com

------------------------------------------------------------------------------
Everyone hates slow websites. So do we.
Make your web apps faster with AppDynamics
Download AppDynamics Lite for free today:
http://p.sf.net/sfu/appdyn_d2d_jan
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

planetenxin
In reply to this post by Yury Katkov
I just came across this old question because we need to execute an ask
query from php and work with the results.

Is an internal API call really the best/most efficient way to do this?

/Alexander

Am 31.01.2013 um 17:29 schrieb Yury Katkov:

> Hi-hi!
>
> I would use SMW ask API for that. The returning json object is little
> bit complex, but this is not usually a big problem
> http://semantic-mediawiki.org/wiki/Ask_API
> -----
> Yury Katkov, WikiVote
>
>
>
> On Thu, Jan 31, 2013 at 4:21 PM, <[hidden email]
> <mailto:[hidden email]>> wrote:
>
>     __
>
>     Hello, I am hacking SMW, because I need to retrive some data from
>     the mediawiki database and use it to render skin. I know this is the
>     dirty hack, but I do not see another way.
>
>     I need execute query like this
>
>     {{
>
>     #ask: [[Title picture::+]] [[Category: some category]]
>
>     | ?Title picture
>
>     | format = ## here some special format ? ##
>
>     | limit=10
>
>     }}
>
>     and receive the answer like a simple array
>
>     array(0 => array("self" => Title object,
>
>     "Title_picture" => Title object),
>
>     1 => array("self" => Title objec ....
>
>     .................
>
>     While the actual data is receiving by format function, specified in
>     the query, I dont have any idea how to receive data like simple
>     array. Do I need to write my own format function ?
>
>
>     ------------------------------------------------------------------------------
>     Everyone hates slow websites. So do we.
>     Make your web apps faster with AppDynamics
>     Download AppDynamics Lite for free today:
>     http://p.sf.net/sfu/appdyn_d2d_jan
>     _______________________________________________
>     Semediawiki-devel mailing list
>     [hidden email]
>     <mailto:[hidden email]>
>     https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
>
>
>
>
> ------------------------------------------------------------------------------
> Everyone hates slow websites. So do we.
> Make your web apps faster with AppDynamics
> Download AppDynamics Lite for free today:
> http://p.sf.net/sfu/appdyn_d2d_jan
>
>
>
> _______________________________________________
> Semediawiki-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
>


--
________________________________________________
semantic::apps by gesinn.it
Business Applications with Semantic Mediawiki.
http://semantic-apps.com

------------------------------------------------------------------------------
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

Fannon
In reply to this post by s9gf4ult
Hi!

I'm having the same problem: I need to programmatically call an ASK query and get the result in a plain datastructure (arrays) from within PHP and find it very difficult to find a solution that isn't deprecated or completely overkill.

The deprecated solution is using SMWQueryProcessor::getResultFromFunctionParams (should have been removed as of SMW 1.8). It's downside is that it's already applying postprocessing, so I would have to strip the wikitext/html markup out.

On the other hand, I can always make an internal API call, but this is a very expensive and inefficient solution for just getting a list of a few page titles (in my case).

I've found Yurys programming examples, how to create (low level) ASK queries by hand, but these examples don't work for me anymore (API changes?)
https://semantic-mediawiki.org/wiki/User:Yury_Katkov/programming_examples#Ask-queries

What I'd like to have is some convenient method, like:

SOMETHING->askQueryReturnAsArray($querytext);

Which returns a 2dimensional array (or 1dim, if it's only a list)

Best,
Simon
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

Frank Baxmann
Hi,

I had a similar problem in the past.
I used "$parser->recursiveTagParse" to solve it.
Meaning I created a string including the ASK query in the common wiki
notation.

Then I just parsed the string and get the ASK result back:

$askResult =
$parser->mStripState->unstripBoth($parser->recursiveTagParse(
$askQueryStr, $frame ),$parser->mStripState);

Greetings
Frank

On 23.10.2015 16:18, Fannon wrote:

> Hi!
>
> I'm having the same problem: I need to programmatically call an ASK query
> and get the result in a plain datastructure (arrays) from within PHP and
> find it very difficult to find a solution that isn't deprecated or
> completely overkill.
>
> The deprecated solution is using
> SMWQueryProcessor::getResultFromFunctionParams (should have been removed as
> of SMW 1.8). It's downside is that it's already applying postprocessing, so
> I would have to strip the wikitext/html markup out.
>
> On the other hand, I can always make an internal API call, but this is a
> very expensive and inefficient solution for just getting a list of a few
> page titles (in my case).
>
> I've found Yurys programming examples, how to create (low level) ASK queries
> by hand, but these examples don't work for me anymore (API changes?)
> https://semantic-mediawiki.org/wiki/User:Yury_Katkov/programming_examples#Ask-queries
>
> What I'd like to have is some convenient method, like:
>
> SOMETHING->askQueryReturnAsArray($querytext);
>
> Which returns a 2dimensional array (or 1dim, if it's only a list)
>
> Best,
> Simon
>
>
>
> --
> View this message in context: http://wikimedia.7.x6.nabble.com/Obtain-data-as-simple-array-tp4994985p5054189.html
> Sent from the Semantic Mediawiki - Development mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Semediawiki-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
>


------------------------------------------------------------------------------
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

Fannon
Hello Frank,

I couldn't get this working - could you please provide some more context?

This solution is nicer, because it's not so much code to write - but it also contains all those parsing, un-parsing boilerplate steps.
Reply | Threaded
Open this post in threaded view
|

Re: Obtain data as simple array

Frank Baxmann
Hello Simon,

here is an example:

$searchStr = "{{#ask: <your search>=|mainlabel=-|format=list}}";
// maybe you have to add param "sep" here if comma is not good for you
// http://semantic-mediawiki.org/wiki/Help:List_format

$searchResult =
$parser->mStripState->unstripBoth($parser->recursiveTagParse(
$searchStr, $frame ),$parser->mStripState);
// if you don't use template parameter in the ask, you don't need "$frame".

$searchResult = str_replace ( ', ' , ',' , $searchResult );
$valArray = explode( ',' , $searchResult );

This is a working example from one of my extensions.
If you don't get your's running, pls send me the source part...

Greetings
Frank

On 26.10.2015 08:20, Fannon wrote:

> Hello Frank,
>
> I couldn't get this working - could you please provide some more context?
>
> This solution is nicer, because it's not so much code to write - but it also
> contains all those parsing, un-parsing boilerplate steps.
>
>
>
> --
> View this message in context: http://wikimedia.7.x6.nabble.com/Obtain-data-as-simple-array-tp4994985p5054239.html
> Sent from the Semantic Mediawiki - Development mailing list archive at Nabble.com.
>
> ------------------------------------------------------------------------------
> _______________________________________________
> Semediawiki-devel mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/semediawiki-devel
>


------------------------------------------------------------------------------
_______________________________________________
Semediawiki-devel mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/semediawiki-devel