Completed
Push — recommendation-api-updates ( 5dc1c7 )
by
unknown
21:29
created

export::getHelp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
  namespace NRC;
3
4
  require_once(elgg_get_plugins_path() . 'missions/api/v0/api.php');
5
6
  class export {
7
8
    public static $version = 'v0.0.3';
9
    public static $help = <<<EOD
10
<!DOCTYPE html>
11
<html>
12
  <body>
13
    <H1>
14
      NRC Recommendation Provider API
15
      <sub style="font-size: 0.5em">::version::</sub>
16
    </H1>
17
    <p>
18
      This API provides the means by which recommendation services can collect
19
      GCConnex data.  A private key is required to access this service.
20
    </p>
21
    <H3>Endpoints</H3>
22
    <ul>
23
      <li>
24
        <h4>/missions/api/v0/export[/guid]</h4>
25
        <p>
26
          Used to export any entity.  If a guid is provided, only that entity
27
          is returned.
28
        </p>
29
        Examples:
30
        <ul>
31
          <li>/missions/api/v0/export</li>
32
          <li>/missions/api/v0/export/205</li>
33
          <li>/missions/api/v0/export/42922688</li>
34
          <li>/missions/api/v0/export?since=1539475200&limit=2</li>
35
        </ul>
36
      </li>
37
      <li>
38
        <h4>/missions/api/v0/user[/guid]</h4>
39
        <p>
40
          Used to export users.  If a guid is provided, only that user is
41
          returned.
42
        </p>
43
        Examples:
44
        <ul>
45
          <li>/missions/api/v0/user</li>
46
          <li>/missions/api/v0/user/205</li>
47
          <li>/missions/api/v0/user?since=1539475200</li>
48
        </ul>
49
      </li>
50
      <li>
51
        <h4>
52
          /missions/api/v0/object[/guid]<br>
53
          /missions/api/v0/object/subtype[/guid]
54
        </h4>
55
        <p>
56
          Used to export objects.  If a guid is provided, only that object is
57
          returned.  A subtype can also be specified.
58
        </p>
59
        Examples:
60
        <ul>
61
          <li>/missions/api/v0/object</li>
62
          <li>/missions/api/v0/object/42922688</li>
63
          <li>/missions/api/v0/object?since=1539475200&limit=1</li>
64
          <li>/missions/api/v0/object/widget?since=1539475200</li>
65
        </ul>
66
      </li>
67
    </ul>
68
    <H3>Query parameters</H3>
69
    <p>Parameters can be used to perform more complex queries.</p>
70
    <table>
71
      <thead>
72
        <tr>
73
          <th>Parameter</th>
74
          <th>Description</th>
75
        </tr>
76
      </thead>
77
      <tbody>
78
        <tr>
79
          <td>before</td>
80
          <td>
81
            Fetch entities that have been modified before the specified time.
82
            Expects a unix timetamp.
83
          </td>
84
        </tr>
85
        <tr>
86
          <td>since</td>
87
          <td>
88
            Fetch entities that have been modified after the specified time.
89
            Expects a unix timetamp.
90
          </td>
91
        </tr>
92
        <tr>
93
          <td>limit</td>
94
          <td>
95
            Limits the number of returned entities.  Expects an positive
96
            integer.
97
          </td>
98
        </tr>
99
        <tr>
100
          <td>resume</td>
101
          <td>
102
            Resume starting at the specified GUID.  Expects a valid GUID.
103
          </td>
104
        </tr>
105
        <tr>
106
          <td>sort</td>
107
          <td>
108
            If specified sorts returned entities by created time - By default
109
            rows are returned in natural order without any sorting guarantees.
110
            This parameter doesn't take any value, its presence enables
111
            sorting.
112
          </td>
113
        </tr>
114
        <tr>
115
          <td>omit</td>
116
          <td>
117
            List of GUIDs to omit from the results.  Useful to omit large
118
            objects we know we are not interested in.  For example GUID "1".
119
            Takes a comma separated list of GUIDs.
120
          </td>
121
        </tr>
122
        <tr>
123
          <td>version<strong>*<strong></td>
124
          <td>
125
            Returns the API version as a JSON object and exits.  No query is
126
            performed regardless of other parameters.
127
          </td>
128
        </tr>
129
        <tr>
130
          <td>help<strong>*<strong></td>
131
          <td>
132
            Returns the API help text as an HTML page and exits.  No query is
133
            performed regardless of other parameters.
134
          </td>
135
        </tr>
136
      </table>
137
      <small><strong>*<strong> These parameters do not require a private key</small>
138
      <H3>Returns</H3>
139
      Successful queries return JSON objects like this:
140
      <pre>
141
      {
142
        "query": {
143
            "object_type": "export",     // type of export
144
            "api_version": "v0.0.3",     // current API version
145
            "subtype": false,            // requested subtype, if specified
146
            "guid": null,                // requested GUID, if specified
147
            "since": "1539475200",       // "since" parameter, if specified
148
            "before": null,              // "before" parameter, if specified
149
            "limit": null,               // "limit" parameter, if specified
150
            "request_time": 1546958611,  // server's current unix timestamp
151
            "count": 14                  // Total number of matched entities
152
        },
153
        "export": [ ... ]                // Array of requested entities
154
      }
155
      </pre>
156
  </body>
157
</html>
158
EOD;
159
160
    private $object_type = false;
161
    private $subtype = false;
162
    private $guid = null;
163
    private $since = null;
164
    private $before = null;
165
    private $limit = null;
166
    private $omit = null;
167
168
    static function getHelp() {
169
      return str_replace('::version::', self::$version, self::$help);
170
    }
171
172
    function __construct($object_type, $subtype = false, $guid = null,
173
      $since = null, $before = null, $limit = null, $resume = null,
174
      $sort = false, $omit = null) {
175
176
      mm_api_secure();
177
178
      $this->object_type = $object_type;
179
      $this->subtype = $subtype;
180
      $this->guid = $guid;
181
      $this->since = $since;
182
      $this->before = $before;
183
      $this->limit = $limit;
184
      $this->resume = $resume;
0 ignored issues
show
Bug introduced by
The property resume does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
185
      $this->sort = $sort;
0 ignored issues
show
Bug introduced by
The property sort does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
186
      $this->omit = $omit;
187
    }
188
189
    /**
190
     * Stream the export results using JSON format
191
     */
192
    function outputJSON() {
193
      while (@ob_end_flush());
194
      $guids = mm_api_get_entity_guids(
195
        $this->object_type,
0 ignored issues
show
Documentation introduced by
$this->object_type is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
196
        $this->subtype,
0 ignored issues
show
Documentation introduced by
$this->subtype is of type boolean, but the function expects a false|string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
197
        $this->guid,
198
        $this->since,
199
        $this->before,
200
        $this->limit,
201
        $this->resume,
202
        $this->sort,
203
        $this->omit
204
      );
205
      echo '{"query":' .json_encode([
206
        'object_type' => $this->object_type,
207
        'api_version' => self::$version,
208
        'subtype' => $this->subtype,
209
        'guid' => $this->guid,
210
        'since' => $this->since,
211
        'before' => $this->before,
212
        'limit' => $this->limit,
213
        'request_time' => time(),
214
        'count' => $guids->current()
215
      ]). ',"export":[';
216
      flush();
217
      $guids->next();
218
      $export = mm_api_entity_export($guids);
0 ignored issues
show
Bug introduced by
Are you sure the assignment to $export is correct as mm_api_entity_export($guids) (which targets mm_api_entity_export()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
219
220
      // ignore the first comma
221
      $export->next();
0 ignored issues
show
Bug introduced by
The method next cannot be called on $export (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
222
223
      while ($export->valid()) {
0 ignored issues
show
Bug introduced by
The method valid cannot be called on $export (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
224
        $output = $export->current();
0 ignored issues
show
Bug introduced by
The method current cannot be called on $export (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
225
        echo $output;
226
        flush();
227
        ob_flush();
228
        $export->next();
0 ignored issues
show
Bug introduced by
The method next cannot be called on $export (of type null).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
229
      }
230
      echo "]}\r\n";
231
      exit;
0 ignored issues
show
Coding Style Compatibility introduced by
The method outputJSON() contains an exit expression.

An exit expression should only be used in rare cases. For example, if you write a short command line script.

In most cases however, using an exit expression makes the code untestable and often causes incompatibilities with other libraries. Thus, unless you are absolutely sure it is required here, we recommend to refactor your code to avoid its usage.

Loading history...
232
    }
233
  }
234
?>
0 ignored issues
show
Best Practice introduced by
It is not recommended to use PHP's closing tag ?> in files other than templates.

Using a closing tag in PHP files that only contain PHP code is not recommended as you might accidentally add whitespace after the closing tag which would then be output by PHP. This can cause severe problems, for example headers cannot be sent anymore.

A simple precaution is to leave off the closing tag as it is not required, and it also has no negative effects whatsoever.

Loading history...
235