Subversion Repositories PHPX

Rev

Rev 16 | Rev 19 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 PointedEar 1
<?php
2
 
3
require_once 'global.inc';
4
 
13 PointedEar 5
define('FEATURES_ENCODING',
6
  mb_detect_encoding(file_get_contents($_SERVER['SCRIPT_FILENAME'])));
7
 
2 PointedEar 8
/**
9
 * A list of language features with URNs definitions
10
 * for reference links.
11
 */
12
class FeatureList
13
{
14
  public $versions = array();
15
 
16
  /**
17
   * Versions of implementations that are considered safe.
18
   * A feature is considered safe if it does not require
19
   * an implementation version above these versions.
20
   *
21
   * @var Array[string=>string]
22
   */
23
  public $safeVersions = array();
24
 
25
  /**
26
   * URNs that can be used for reference links.
27
   *
28
   * @var Array[string=>string]
29
   */
30
  protected $urns = array();
31
 
32
  /**
33
   * The list of language features
34
   *
35
   * @var array[Features]
36
   */
37
  protected $items = array();
38
 
39
  /**
40
   * Determines the number of printed items the table headings should be repeated
41
   *
42
   * @var int
43
   */
44
  protected $headerRepeat = 25;
45
 
46
  /**
47
   * Initializes the FeatureList object
48
   *
49
   * @param array|Object $a
50
   * @return FeatureList
51
   */
52
  public function __construct($a)
53
  {
9 PointedEar 54
    $aVars = get_class_vars(get_class($this));
55
 
2 PointedEar 56
    while ((list($key, $value) = each($aVars)))
57
    {
58
      if (isset($a[$key]))
59
      {
60
        $this->$key = $a[$key];
61
      }
62
    }
63
 
64
    /* Inform items of ourself so that URNs can be used for links */
65
    if (is_array($this->items))
66
    {
67
      foreach ($this->items as &$item)
68
      {
4 PointedEar 69
        $item->setList($this);
2 PointedEar 70
      }
71
    }
72
 
73
    /* resolve URN references that are URNs */
74
    if (is_array($this->urns))
75
    {
76
      foreach ($this->urns as &$urn)
77
      {
78
        if (($url = $this->resolveURN($urn)))
79
        {
80
          $urn = $url;
81
        }
82
      }
83
    }
84
  }
85
 
86
  public function printHeaders()
87
  {
88
    foreach ($this->versions as $ver)
89
    {
90
?>
91
          <th><?php echo $ver; ?></th>
92
<?php
93
    }
94
  }
95
 
96
  /**
97
   * Prints the list of features.
98
   *
99
   * @see Feature::printMe()
100
   */
101
  public function printItems()
102
  {
103
    $counter = 0;
104
    $headerRepeat = $this->headerRepeat;
105
    $repeatHeaders = ($headerRepeat > 1);
106
 
107
    foreach ($this->items as $feature)
108
    {
109
      if ($feature instanceof Feature)
110
      {
16 PointedEar 111
        /*
112
         * TODO: Disabled header repetition until footnote ref. name/ID
113
         * problem has been solved
114
         */
115
//        if ($repeatHeaders
116
//            && $counter > 1
117
//            && $counter % $headerRepeat === 0)
118
//        {
119
//          echo <<<HTML
120
//        <tr class="header">
121
//          <th>Feature</th>
122
//          {$this->printHeaders()}
123
//        </tr>
124
//HTML;
125
//        }
2 PointedEar 126
 
127
        $feature->printMe();
128
 
129
        $counter++;
130
      }
131
    }
132
  }
133
 
134
  /**
135
   * Resolves a URN according to the value of the
136
   * object's <code>$urn</code> property.
137
   *
138
   * @param string $urn
139
   *   URN to be resolved
140
   * @return string|boolean
141
   *   The resolved URN if successful,
142
   *   <code>false</code> otherwise.
143
   */
144
  public function resolveURN($urn)
145
  {
146
    if (is_array($this->urns))
147
    {
148
      $reURN = '|^(.+?):(?!//)|';
149
 
150
      if (preg_match($reURN, $urn, $m) && isset($this->urns[$m[1]]))
151
      {
152
        return preg_replace($reURN, $this->urns[$m[1]], $urn);
153
      }
154
    }
155
 
156
    return $urn;
157
  }
158
}
159
 
160
/**
161
 * A language feature.
162
 */
163
class Feature
164
{
165
  /**
166
   * Fragment identifiers to be defined for quickly accessing
167
   * the feature description.
168
   *
169
   * @var Array[String]
170
   */
171
  protected $anchors = array();
172
 
173
  /**
174
   * Value of the explanatory <code>title</code> attribute for the feature.
175
   *
176
   * @var string
177
   */
178
  protected $title = '';
179
 
180
  /**
181
   * Name or example code of the feature
182
   *
183
   * @var string
184
   */
185
  protected $content = '';
186
 
187
  /**
188
   * Description of the feature.  Displayed directly if code is missing,
189
   * otherwise used as `title' attribute value.
190
   *
191
   * @var string
192
   */
193
  protected $descr = '';
194
 
195
  /**
196
   * Versions that support this feature
197
   *
198
   * @var Array
199
   */
200
  protected $versions = array();
201
 
202
  /**
203
   * Reference to the FeatureList that this feature belongs to
204
   *
205
   * @var FeatureList
206
   */
207
  protected $list = null;
208
 
4 PointedEar 209
  public function setList(&$oList)
2 PointedEar 210
  {
211
    $this->list =& $oList;
212
  }
213
 
214
  /**
215
   * Creates a new Feature object, using values from the passed parameters
216
   * array.
217
   *
218
   * @param array|Object $params
219
   * @return Feature
220
   */
221
  public function __construct($params = array())
222
  {
14 PointedEar 223
    $aVars = get_class_vars(__CLASS__);
9 PointedEar 224
 
225
    while ((list($key, $value) = each($aVars)))
2 PointedEar 226
    {
9 PointedEar 227
      if (isset($params[$key]))
228
      {
229
        $this->$key = $params[$key];
230
      }
2 PointedEar 231
    }
232
  }
233
 
234
  /**
17 PointedEar 235
   * Determines whether one version is greater than another.
236
   *
237
   * @param string $v1  Version string #1
238
   * @param string $v2  Version string #2
239
   * @return bool
240
   *   <code>true</code> if the version <var>$v1</var> is greater than
241
   *   the version <var>$v2</var>, <code>false</code> otherwise
242
   */
243
  protected static function _versionIsGreater($v1, $v2)
244
  {
245
    $v1 = explode('.', $v1);
246
    $v2 = explode('.', $v2);
247
 
248
    foreach ($v1 as $key => $value)
249
    {
250
      if ((int)$value <= (int)$v2[$key])
251
      {
252
        return false;
253
      }
254
    }
255
 
256
    return true;
257
  }
258
 
259
  /**
2 PointedEar 260
   * Returns <code>' class="safe"'</code> if the feature
261
   * can be considered safe.  The required information
262
   * is stored in the <code>safeVersions</code> property
263
   * of the associated <code>FeatureList</code> object.
264
   *
265
   * @return string
266
   * @see FeatureList::defaultSafeVersions
267
   */
268
  protected function getSafeStr()
269
  {
270
    if (!is_null($this->list))
271
    {
272
      foreach ($this->list->safeVersions as $impl => &$safeVer)
273
      {
274
        $thisImplVer =& $this->versions[$impl];
275
        if (is_array($thisImplVer))
276
        {
277
          if (isset($thisImplVer['tested']) && !is_bool($thisImplVer['tested']))
278
          {
279
            $thisImplVer =& $thisImplVer['tested'];
280
          }
281
          else
282
        {
283
            $thisImplVer =& $thisImplVer[0];
284
          }
285
        }
286
 
287
        /* DEBUG */
288
        // echo " $impl=$thisImplVer ";
289
 
17 PointedEar 290
        if (preg_match('/^-?$/', $thisImplVer) || self::_versionIsGreater($thisImplVer, $safeVer))
2 PointedEar 291
        {
292
          return '';
293
        }
294
      }
295
 
296
      return ' class="safe"';
297
    }
298
    else
299
    {
300
      return '';
301
    }
302
  }
303
 
304
  protected function getTitleStr()
305
  {
306
    if (!empty($this->title))
307
    {
308
      return " title=\"{$this->title}\"";
309
    }
310
    else
311
    {
312
      return '';
313
    }
314
  }
315
 
316
  protected function getAnchors()
317
  {
318
    $result = array();
319
 
320
    foreach ($this->anchors as $anchor)
321
    {
322
      $result[] = "<a name=\"{$anchor}\"";
323
 
324
      if (preg_match('/^[a-z][a-z0-9_:.-]*/i', $anchor))
325
      {
326
        $result[] = " id=\"{$anchor}\"";
327
      }
328
 
329
      $result[] = '></a>';
330
    }
331
 
332
    return join('', $result);
333
  }
334
 
335
  protected function getAssumed($v)
336
  {
337
    if (is_array($v) && isset($v['assumed']) && $v['assumed'])
338
    {
339
      return ' class="assumed"';
340
    }
341
 
342
    return '';
343
  }
344
 
345
  protected function getTested($v)
346
  {
347
    if (is_array($v) && isset($v['tested']) && $v['tested'])
348
    {
349
      return ' class="tested"';
350
    }
351
 
352
    return '';
353
  }
354
 
355
  /**
356
   * Returns the version of a feature.
357
   *
358
   * @param string|VersionInfo $vInfo
359
   * @return mixed
360
   */
361
  protected function getVer($vInfo)
362
  {
363
    if (is_array($vInfo))
364
    {
365
      /* TODO: Return all versions: documented, assumed, and tested */
366
      $vNumber = (isset($vInfo['tested'])
367
                  && gettype($vInfo['tested']) !== 'boolean')
368
                     ? $vInfo['tested']
369
                     : $vInfo[0];
370
      $section = isset($vInfo['section'])
371
                   ? ' <span class="section" title="Specification section">['
372
                      . $vInfo['section'] . ']</span>'
373
                   : '';
374
 
375
      if (isset($vInfo['urn']))
376
      {
377
        if ($this->list instanceof FeatureList)
378
        {
379
          $url = $this->list->resolveURN($vInfo['urn']);
380
          $vNumber = '<a href="' . $url . '">' . $vNumber
6 PointedEar 381
            . ($section ? $section : '') . '</a>';
2 PointedEar 382
        }
383
      }
384
      else if ($section)
385
      {
386
        $vNumber .= $section;
387
      }
388
 
6 PointedEar 389
      $vInfo = $vNumber;
2 PointedEar 390
    }
6 PointedEar 391
 
392
    return ($vInfo === '-')
393
      ? '<span title="Not supported">&#8722;</span>'
394
      : $vInfo;
2 PointedEar 395
  }
17 PointedEar 396
 
397
  /**
398
   * Returns a syntax-highlighted version of a string
399
   *
400
   * @param string $s
401
   * @return string
402
   */
2 PointedEar 403
  protected static function shl($s)
404
  {
405
    /* stub */
17 PointedEar 406
    return $s;
2 PointedEar 407
  }
408
 
409
  public function printMe()
410
  {
411
    ?>
412
<tr<?php echo $this->getSafeStr(); ?>>
413
          <th<?php echo $this->getTitleStr(); ?>><?php
414
            echo $this->getAnchors();
415
            echo /*preg_replace_callback(
416
              '#(<code>)(.+?)(</code>)#',
417
              array('self', 'shl'),*/
418
              preg_replace('/&hellip;/', '&#8230;', $this->content)/*)*/;
419
            ?></th>
420
<?php
421
    $versions = $this->versions;
422
    if (!is_null($this->list))
423
    {
424
      $versions =& $this->list->versions;
425
    }
426
 
427
    static $row = 0;
428
    $row++;
429
 
430
    $column = 0;
17 PointedEar 431
    $thisVersions =& $this->versions;
2 PointedEar 432
 
433
    foreach ($versions as $key => $value)
434
    {
435
      $column++;
436
      $id = "td$row-$column";
17 PointedEar 437
      $ver = isset($thisVersions[$key]) ? $thisVersions[$key] : '';
2 PointedEar 438
?>
17 PointedEar 439
          <td<?php
440
                if (!$key)
441
                {
442
                                                        echo " id='$id'";
443
                }
444
 
2 PointedEar 445
            echo $this->getAssumed($ver) . $this->getTested($ver);
17 PointedEar 446
 
2 PointedEar 447
            if (!$key)
448
            {
449
              if (!empty($ver))
450
              {
13 PointedEar 451
                echo ' title="Test code: '
452
                  . htmlspecialchars(
17 PointedEar 453
                      preg_replace('/\\\(["\'])/', '\1',
454
                        reduceWhitespace($ver)
455
                      ),
13 PointedEar 456
                      ENT_COMPAT,
17 PointedEar 457
                      FEATURES_ENCODING
458
                    )
13 PointedEar 459
                  . '"';
2 PointedEar 460
              }
461
              else
462
              {
463
                echo ' title="Not applicable: No automated test case'
17 PointedEar 464
                  . ' is available for this feature.  If possible, please'
465
                  . ' click the feature code in the first column to run'
2 PointedEar 466
                  . ' a manual test."';
467
              }
468
            }
469
            ?>><?php
470
            if ($key)
471
            {
472
              echo $this->getVer($ver);
17 PointedEar 473
 
474
              /* General footnotes support: include footnotes.class.php to enable */
475
              if (is_array($ver) && isset($ver['footnote']) && $ver['footnote'])
476
              {
477
                echo $ver['footnote'];
478
              }
2 PointedEar 479
            }
480
            else
481
            {
482
              if (!empty($ver))
483
              {
484
                ?><script type="text/javascript">
485
  // <![CDATA[
17 PointedEar 486
  var s = test(<?php echo $ver; ?>, '<span title="Supported">+<\/span>',
487
    '<span title="Not supported">&#8722;<\/span>');
2 PointedEar 488
  tryThis("document.write(s);",
489
          "document.getElementById('<?php echo $id; ?>').appendChild("
490
          + "document.createTextNode(s));");
491
  // ]]>
492
</script><?php
493
              }
494
              else
495
              {
496
                echo '<abbr>N/A</abbr>';
497
              }
498
            }
499
            ?></td>
500
<?php
501
    }
502
?>
503
        </tr>
504
<?php
505
  }
506
}
507
 
508
?>