Utils/WikiTagUtils.php
author cavaliet
Fri, 24 Feb 2012 10:55:01 +0100
changeset 77 021131fbe2c5
parent 76 bb7808e180c3
parent 74 901463f9b11c
child 112 14653baf4f6b
permissions -rwxr-xr-x
merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
74
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     2
/*
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     3
 * This file is part of the WikiTagBundle package.
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     4
 *
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     5
 * (c) IRI <http://www.iri.centrepompidou.fr/>
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     6
 *
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     7
 * For the full copyright and license information, please view the LICENSE
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     8
 * file that was distributed with this source code.
901463f9b11c add headers for public repository release
ymh <ymh.work@gmail.com>
parents: 68
diff changeset
     9
 */
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
namespace IRI\Bundle\WikiTagBundle\Utils;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
use IRI\Bundle\WikiTagBundle\Entity\Tag;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
class WikiTagUtils
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
{
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
    // Constants
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
    private static $WIKIPEDIA_API_URL = "http://fr.wikipedia.org/w/api.php";
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
    private static $WIKIPEDIA_VERSION_PERMALINK_TEMPLATE = "http://fr.wikipedia.org/w/index.php?oldid=%s";
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
    private static $DBPEDIA_URI_TEMPLATE = "http://dbpedia.org/resource/%s";
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    21
    
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
    
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
     * Cleans the tag label
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
     */
8
7d2fb5d7c9ff debug addtag for document
cavaliet
parents: 2
diff changeset
    26
    public static function normalizeTag($tag_label)
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
        if(strlen($tag_label)==0){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    29
            return $tag_label;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
        $tag_label = trim($tag_label);//tag.strip()
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
        $tag_label = str_replace("_", " ", $tag_label);//tag.replace("_", " ")
43
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    33
        $tag_label = preg_replace('/\s+/u', ' ', $tag_label);//" ".join(tag.split())
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
        $tag_label = ucfirst($tag_label);//tag[0].upper() + tag[1:]
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
        return $tag_label;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    37
    
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    38
    /**
43
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    39
     * Query wikipedia with a normalized label or a pageid
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    40
     * return an array with the form
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    41
     * array(
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    42
     *      'new_label'=>$new_label,
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    43
     *   	'alternative_label'=>$alternative_label,
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    44
     *   	'status'=>$status,
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    45
     *   	'wikipedia_url'=>$url,
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    46
     *      'wikipedia_alternative_url'=>$alternative_url,
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    47
     *   	'pageid'=>$pageid,
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    48
     *   	'alternative_pageid'=>$alternative_pageid,
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    49
     *   	'dbpedia_uri'=>$dbpedia_uri,
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    50
     *   	'revision_id'=> ,
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    51
     *   	'response'=> the original wikipedia json response
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    52
     *   	)
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
     *
43
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    54
     * @param string $tag_label_normalized
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    55
     * @param bigint $page_id
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
    56
     * @return array
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
     */
68
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    58
    public static function getWikipediaInfo($tag_label_normalized, $page_id=null, $ignore_wikipedia_error=false, $logger = null)
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
    {
68
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    60
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
        $params = array('action'=>'query', 'prop'=>'info|categories|langlinks', 'inprop'=>'url', 'lllimit'=>'500', 'cllimit'=>'500', 'rvprop'=>'ids', 'format'=>'json');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    62
        if($tag_label_normalized!=null){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
            $params['titles'] = urlencode($tag_label_normalized);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
        else if($page_id!=null){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
            $params['pageids'] = $page_id;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
        else{
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
            return WikiTagUtils::returnNullResult(null);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
        
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    72
        try {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    73
            $ar = WikiTagUtils::requestWikipedia($params);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    74
        }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    75
        catch(\Exception $e) {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    76
            if($ignore_wikipedia_error) {
68
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    77
                if(!is_null($logger)) {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    78
                    $logger->err("Error when querying wikipedia : ".$e->getMessage()." with trace : ".$e->getTraceAsString());
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
    79
                }
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    80
                return WikiTagUtils::returnNullResult(null);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    81
            }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    82
            else {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    83
                throw $e;
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    84
            }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
    85
        }
60
984ba20c150b Remove unnecessary try/catch
ymh <ymh.work@gmail.com>
parents: 50
diff changeset
    86
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
        $res = $ar[0];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
        $original_response = $res;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
        $pages = $ar[1];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
        // If there 0 or more than 1 result, the query has failed
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
        if(count($pages)>1 || count($pages)==0){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
            return WikiTagUtils::returnNullResult($res);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
        // get first result
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
        $page = reset($pages);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
        // Unknow entry ?
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
        if(array_key_exists('missing', $page) || array_key_exists('invalid', $page)){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
            return WikiTagUtils::returnNullResult($res);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
        // The entry exists, we get the datas.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
        $url = array_key_exists('fullurl', $page) ? $page['fullurl'] : null;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
        $pageid = array_key_exists('pageid', $page) ? $page['pageid'] : null;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
        $new_label = array_key_exists('title', $page) ? $page['title'] : null;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
        // We test the status (redirect first because a redirect has no categories key)
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
        if(array_key_exists('redirect', $page)){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
            //return " REDIRECT";
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
            $status = Tag::$TAG_URL_STATUS_DICT["redirection"];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   109
        else if(WikiTagUtils::isHomonymy($page)){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
            //return " HOMONYMY";
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
            $status = Tag::$TAG_URL_STATUS_DICT["homonyme"];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
        else{
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
            //return " MATCH";
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
            $status = Tag::$TAG_URL_STATUS_DICT["match"];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
        // In redirection, we have to get more datas by adding redirects=true to the params
42
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   118
        $alternative_label = null;
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   119
        $alternative_url = null;
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   120
        $alternative_pageid = null;
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   121
        if($status==Tag::$TAG_URL_STATUS_DICT["redirection"])
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   122
        {
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
            $params['redirects'] = "true";
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   124
            try {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   125
                $ar = WikiTagUtils::requestWikipedia($params);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   126
            }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   127
            catch(\Exception $e) {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   128
                if($ignore_wikipedia_error) {
68
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   129
                    if(!is_null($logger)) {
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   130
                        $logger->error("Error when querying wikipedia for redirection : ".$e->getMessage()." with trace : ".$e->getTraceAsString());
e7384fb35f7a improve search test and documentation
ymh <ymh.work@gmail.com>
parents: 67
diff changeset
   131
                    }
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   132
                    return WikiTagUtils::returnNullResult(null);
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   133
                }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   134
                else {
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   135
                    throw $e;
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   136
                }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   137
            }
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   138
            
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
            $res = $ar[0];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   140
            $pages = $ar[1];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   141
            #we know that we have at least one answer
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   142
            if(count($pages)>1 || count($pages)==0){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   143
                return WikiTagUtils::returnNullResult($res);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   144
            }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   145
            // get first result
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
            $page = reset($pages);
42
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   147
            $alternative_label = array_key_exists('title', $page) ? $page['title'] : null;
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   148
            $alternative_url = array_key_exists('fullurl', $page) ? $page['fullurl'] : null;
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   149
            $alternative_pageid = array_key_exists('pageid', $page) ? $page['pageid'] : null;
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
        
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
        $revision_id = $page['lastrevid'];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
        
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
        // process language to extract the english label
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   155
        $english_label = null;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   156
        if($status==Tag::$TAG_URL_STATUS_DICT["match"] || $status==Tag::$TAG_URL_STATUS_DICT["redirection"]){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
            if(array_key_exists("langlinks", $page)){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
                foreach ($page["langlinks"] as $ar) {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
                    if($ar["lang"]=="en"){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
                        $english_label = $ar["*"];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
                        break;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
                    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   163
                }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
            }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   165
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
        // We create the dbpedia uri.
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
        $dbpedia_uri = null;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
        if($english_label!=null && strpos($english_label, '#')===false){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
            $dbpedia_uri = WikiTagUtils::getDbpediaUri($english_label);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
        
42
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   172
        $wp_response = array(
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   173
            'new_label'=>$new_label,
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   174
        	'alternative_label'=>$alternative_label,
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   175
        	'status'=>$status,
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   176
        	'wikipedia_url'=>$url,
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   177
            'wikipedia_alternative_url'=>$alternative_url,
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   178
        	'pageid'=>$pageid,
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   179
        	'alternative_pageid'=>$alternative_pageid,
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   180
        	'dbpedia_uri'=>$dbpedia_uri,
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   181
        	'revision_id'=>$revision_id,
0e57c730bb18 Documentation and add alternative wp url and label + migrations
ymh <ymh.work@gmail.com>
parents: 32
diff changeset
   182
        	'response'=>$original_response);
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   183
        
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
        return $wp_response;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
    
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
    /**
43
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   189
     * build and do the request to Wikipedia.
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   190
     *
43
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   191
     * @param array $params
54f204bceb28 Correct migration
ymh <ymh.work@gmail.com>
parents: 42
diff changeset
   192
     * @return array
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   193
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   194
    private static function requestWikipedia($params)
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   196
        $params_str = '';
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
        foreach ($params as $key => $value) {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
            if ($params_str==''){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
                $params_str = $key.'='.$value;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
            }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
            else{
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
                $params_str .= '&'.$key.'='.$value;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   203
            }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   205
        
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
        $url = WikiTagUtils::$WIKIPEDIA_API_URL.'?'.$params_str;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
        
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
        $ch = curl_init();
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
        curl_setopt($ch, CURLOPT_URL, $url);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
76
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   211
        // default values
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   212
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:10.0.1) Gecko/20100101 Firefox/10.0.1');
50
e967654e90cb First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents: 43
diff changeset
   213
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, 5000);
76
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   214
        // Set options if they are set in the config.yml file, typically for proxy configuration.
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   215
        // Thanks to the configuration file, it will execute commands like "curl_setopt($ch, CURLOPT_PROXYTYPE, CURLPROXY_HTTP);" or "curl_setopt($ch, CURLOPT_PROXY, "xxx.yyy.zzz:PORT");"
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   216
        $curl_options = $GLOBALS["kernel"]->getContainer()->getParameter("wiki_tag.curl_options");
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   217
        foreach ($curl_options as $key => $value) {
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   218
            if(strtoupper($value)=='TRUE'){
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   219
                $value = TRUE;
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   220
            }
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   221
            else if (strtoupper($value)=='FALSE'){
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   222
                $value = FALSE;
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   223
            }
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   224
            else if (defined($value)){
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   225
                $value = constant($value);
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   226
            }
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   227
            curl_setopt($ch, constant($key), $value);
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   228
        }
bb7808e180c3 Add configuration to enable curl request with proxy or any other CURLOPT
cavaliet
parents: 68
diff changeset
   229
        // end of treatment
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
        $res = curl_exec($ch);
50
e967654e90cb First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents: 43
diff changeset
   231
        $curl_errno = curl_errno($ch);
e967654e90cb First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents: 43
diff changeset
   232
        $curl_error = curl_error($ch);
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
        curl_close($ch);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
        
50
e967654e90cb First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents: 43
diff changeset
   235
        if ($curl_errno > 0) {
63
774ba82dca59 add tests and fixtures
ymh <ymh.work@gmail.com>
parents: 60
diff changeset
   236
            throw new \Exception("Wikipedia request failed. cURLError #$curl_errno: $curl_error\n", $curl_errno, null);
50
e967654e90cb First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents: 43
diff changeset
   237
        }
e967654e90cb First step of error management when Wikipedia request fails. Set up in whole list and document list.
cavaliet
parents: 43
diff changeset
   238
        
2
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   239
        $val = json_decode($res, true);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
        $pages = $val["query"]["pages"];
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   241
        return array($res, $pages);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
    
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   245
     * Returns tag with a null result, usually used after a failed request on Wikipedia
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   247
    private static function returnNullResult($response)
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
        return array('new_label'=>null, 'status'=>Tag::$TAG_URL_STATUS_DICT['null_result'], 'wikipedia_url'=>null, 'pageid'=>null, 'dbpedia_uri'=>null, 'revision_id'=>null, 'response'=>$response);
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
    
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
     * Returns tag with a null result, usually used after a failed request on Wikipedia
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   255
    private static function isHomonymy($page)
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   256
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
        //$s = "";
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   258
        foreach ($page["categories"] as $ar) {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   259
            //$s .= ", b : ".$ar." - title = ".$ar["title"].", strpos = ".strpos($ar["title"], 'Catégorie:Homonymie');
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   260
            // Strict test because false can be seen as "O".
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
            if(strpos($ar["title"], 'Catégorie:Homonymie')!==false || strpos($ar["title"], 'Category:Disambiguation')!==false){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   262
                //$s .= "TRUE";
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
                return true;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
            }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
        }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   266
        return false;
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   267
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   268
    
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   269
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   270
     * Builds DbPedia URI
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   271
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
    private static function getDbpediaUri($english_label)
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
    {
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
        return sprintf(WikiTagUtils::$DBPEDIA_URI_TEMPLATE, WikiTagUtils::urlize_for_wikipedia($english_label));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   275
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
    
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   277
    /**
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
     * URLencode label for wikipedia
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   279
     */
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   280
    private static function urlize_for_wikipedia($label){
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   281
        return urlencode(str_replace(" ", "_", $label));
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   282
    }
13f43f53d0ba first implementation
ymh <ymh.work@gmail.com>
parents:
diff changeset
   283
}