Completed
Push — master ( a4261d...fc5e52 )
by John
14s
created

CrawlerBase   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 172
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 116
dl 0
loc 172
rs 10
c 0
b 0
f 0
wmc 26

10 Methods

Rating   Name   Duplication   Size   Complexity  
A updateProblem() 0 4 1
A _resetPro() 0 25 1
A line() 0 6 2
A procInfo() 0 11 5
A __construct() 0 2 1
B _cacheImage() 0 50 11
A insertProblem() 0 4 1
A cmp() 0 3 2
A importCommandLine() 0 2 1
A getUrl() 0 11 1
1
<?php
2
3
namespace App\Babel\Crawl;
4
5
use App\Models\ProblemModel;
6
use KubAT\PhpSimple\HtmlDomParser;
7
use Auth;
8
9
class CrawlerBase
10
{
11
    public $pro=[
12
        'file'=> 0,
13
        'pcode'=>'',
14
        'solved_count'=>'',
15
        'time_limit'=>'',
16
        'memory_limit'=>'',
17
        'title'=>'',
18
        'OJ'=>'',
19
        'description'=>'',
20
        'input'=>'',
21
        'output'=>'',
22
        'note'=>'',
23
        'input_type'=>'',
24
        'output_type'=>'',
25
        'contest_id'=>'',
26
        'index_id'=>'',
27
        'origin'=>'',
28
        'source'=>'',
29
        'sample'=>[],
30
        'markdown'=>0,
31
        'tot_score'=>1,
32
        'partial'=>0,
33
        'special_compiler'=>null,
34
    ];
35
36
    public $data=null;
37
    public $command=null;
38
39
    /**
40
     * Initial
41
     *
42
     * @return Response
0 ignored issues
show
Bug introduced by
The type App\Babel\Crawl\Response was not found. Did you mean Response? If so, make sure to prefix the type with \.
Loading history...
43
     */
44
    public function __construct()
45
    {
46
    }
47
48
    protected function _resetPro()
49
    {
50
        $this->pro=[
51
            'file'=> 0,
52
            'pcode'=>'',
53
            'solved_count'=>'',
54
            'time_limit'=>'',
55
            'memory_limit'=>'',
56
            'title'=>'',
57
            'OJ'=>'',
58
            'description'=>'',
59
            'input'=>'',
60
            'output'=>'',
61
            'note'=>'',
62
            'input_type'=>'',
63
            'output_type'=>'',
64
            'contest_id'=>'',
65
            'index_id'=>'',
66
            'origin'=>'',
67
            'source'=>'',
68
            'sample'=>[],
69
            'markdown'=>0,
70
            'tot_score'=>1,
71
            'partial'=>0,
72
            'special_compiler'=>null,
73
        ];
74
    }
75
76
    public function importCommandLine($commandTemp){
77
        $this->command=$commandTemp;
78
    }
79
80
    public static function cmp($a, $b)
81
    {
82
        return ($a[1]>$b[1]) ?-1 : 1;
83
    }
84
85
    private function _cacheImage($data)
86
    {
87
        if(!isset($data["ori"]) || !isset($data["path"]) || !isset($data["baseurl"]) || !isset($data["space_deli"]) || !isset($data["cookie"])){
88
            throw new Exception("data is not completely exist in cacheImage");
0 ignored issues
show
Bug introduced by
The type App\Babel\Crawl\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
89
        }
90
        $ori = $data["ori"];
91
        $path = $data["path"];
92
        $baseurl = $data["baseurl"];
93
        $space_deli = $data["space_deli"];
94
        $cookie = $data["cookie"];
95
96
        $para["path"]=$path;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$para was never initialized. Although not strictly required by PHP, it is generally a good practice to add $para = array(); before regardless.
Loading history...
97
        $para["base"]=$baseurl;
98
        $para["trans"]=!$space_deli;
99
        $para["cookie"]=$cookie;
100
101
        if ($space_deli) {
102
            $reg="/< *im[a]?g[^>]*src *= *[\"\\']?([^\"\\' >]*)[^>]*>/si";
103
        } else {
104
            $reg="/< *im[a]?g[^>]*src *= *[\"\\']?([^\"\\'>]*)[^>]*>/si";
105
        }
106
107
        return preg_replace_callback($reg, function($matches) use ($para) {
108
            global $config;
109
            $url=trim($matches[1]);
110
            if (stripos($url, "http://")===false && stripos($url, "https://")===false) {
111
                if ($para["trans"]) {
112
                    $url=str_replace(" ", "%20", $url);
113
                }
114
                $url=$para["base"].$url;
115
            }
116
            $name=basename($url);
117
            $name="images/".strtr($name, ":", "_");
118
            $result=str_replace(trim($matches[1]), "online_Judges/spoj/images/".strtr(basename($url), ":", "_"), $matches[0]);
119
            $ch=curl_init();
120
            curl_setopt($ch, CURLOPT_URL, $url);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

120
            curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url);
Loading history...
121
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
122
            curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
123
            if ($para["cookie"]!="") {
124
                curl_setopt($ch, CURLOPT_COOKIEFILE, $para["cookie"]);
125
            }
126
            curl_setopt($ch, CURLOPT_HEADER, 0);
127
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
128
            $content=curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

128
            $content=curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
129
            curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

129
            curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
130
            $fp=fopen($name, "wb");
131
            fwrite($fp, $content);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fwrite() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

131
            fwrite(/** @scrutinizer ignore-type */ $fp, $content);
Loading history...
132
            fclose($fp);
0 ignored issues
show
Bug introduced by
It seems like $fp can also be of type false; however, parameter $handle of fclose() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

132
            fclose(/** @scrutinizer ignore-type */ $fp);
Loading history...
133
            return $result;
134
        }, $ori);
135
    }
136
137
    public function procInfo($data)
138
    {
139
        if(isset($data["path"]))       $path = $data["path"];             else throw new Exception("path is not exist in data");
140
        if(isset($data["baseurl"]))    $baseurl = $data["baseurl"];       else throw new Exception("baseurl is not exist in data");
141
        if(isset($data["space_deli"])) $space_deli = $data["space_deli"]; else $space_deli = true;
142
        if(isset($data["cookie"]))     $cookie = $data["cookie"];         else $cookie = "";
143
144
        $this->pro["description"]=$this->_cacheImage($this->pro["description"], $path, $baseurl, $space_deli, $cookie);
0 ignored issues
show
Unused Code introduced by
The call to App\Babel\Crawl\CrawlerBase::_cacheImage() has too many arguments starting with $path. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

144
        /** @scrutinizer ignore-call */ 
145
        $this->pro["description"]=$this->_cacheImage($this->pro["description"], $path, $baseurl, $space_deli, $cookie);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
145
        $this->pro["input"]=$this->_cacheImage($this->pro["input"], $path, $baseurl, $space_deli, $cookie);
146
        $this->pro["output"]=$this->_cacheImage($this->pro["output"], $path, $baseurl, $space_deli, $cookie);
147
        $this->pro["note"]=$this->_cacheImage($this->pro["note"], $path, $baseurl, $space_deli, $cookie);
148
    }
149
150
    public function getUrl($url)
151
    {
152
        $ch=curl_init();
153
        curl_setopt($ch, CURLOPT_URL, $url);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_setopt() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

153
        curl_setopt(/** @scrutinizer ignore-type */ $ch, CURLOPT_URL, $url);
Loading history...
154
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
155
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
156
        curl_setopt($ch, CURLOPT_HEADER, 0);
157
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
158
        $content=curl_exec($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_exec() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

158
        $content=curl_exec(/** @scrutinizer ignore-type */ $ch);
Loading history...
159
        curl_close($ch);
0 ignored issues
show
Bug introduced by
It seems like $ch can also be of type false; however, parameter $ch of curl_close() does only seem to accept resource, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

159
        curl_close(/** @scrutinizer ignore-type */ $ch);
Loading history...
160
        return $content;
161
    }
162
163
    public function insertProblem($oid=2)
0 ignored issues
show
Unused Code introduced by
The parameter $oid is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

163
    public function insertProblem(/** @scrutinizer ignore-unused */ $oid=2)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
164
    {
165
        $problemModel=new ProblemModel();
166
        return $problemModel->insertProblem($this->pro);
167
    }
168
169
    public function updateProblem($oid=2)
0 ignored issues
show
Unused Code introduced by
The parameter $oid is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

169
    public function updateProblem(/** @scrutinizer ignore-unused */ $oid=2)

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
170
    {
171
        $problemModel=new ProblemModel();
172
        return $problemModel->updateProblem($this->pro);
173
    }
174
175
    protected function line($line)
176
    {
177
        if(is_null($this->command)){
178
            echo $line;
179
        }else{
180
            $this->command->line($line);
181
        }
182
    }
183
}
184