Repository navigation
Expand file tree
/
Copy pathclass.ParserController.inc.php
More file actions
2442 lines (2031 loc) · 58.8 KB
/
Copy pathclass.ParserController.inc.php
File metadata and controls
2442 lines (2031 loc) · 58.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* Parser Controller
*
* PHP version 8
*
* @license http://www.gnu.org/licenses/gpl.html GPL 3
* @version 1.1
* @link https://www.statedecoded.com/
* @since 0.7
*/
require_once(INCLUDE_PATH . 'task/class.MigrateAction.inc.php');
#[\AllowDynamicProperties]
class ParserController
{
public $db;
public $logger;
public $events;
public $permalink_obj;
public $import_data_dir;
/*
* Temporary variables
*/
public $edition_id;
public $previous_edition_id;
/*
* True when the edition ID was written to .htaccess, or a string explaining
* why it could not be. Set by handle_editions(); read by the callers, which
* report it rather than claiming an import succeeded outright.
*/
public $edition_id_export_error = true;
public $downloads_url;
public $downloads_dir;
public function __construct($args)
{
/*
* Set our defaults
*/
foreach($args as $key=>$value)
{
$this->$key = $value;
}
/*
* Setup a logger.
*/
$this->init_logger();
/*
* Prior to PHP v5.3.6, the PDO does not pass along to MySQL the DSN charset configuration
* option, and it must be done manually.
*/
if (version_compare(PHP_VERSION, '5.3.6', '<'))
{
$this->db->exec("SET NAMES utf8");
}
/*
* Set our default execution limits.
*/
$this->set_execution_limits();
/*
* Set up our event manager.
*/
if(!isset($this->events))
{
$this->events = new EventManager();
$this->events->registerService('logger', $this->logger);
}
/*
* Set the default import location;
*/
if(!isset($this->import_data_dir))
{
$this->import_data_dir = IMPORT_DATA_DIR;
}
/*
* Set our objects
*/
$this->permalink_obj = new Permalink(
['db' => $this->db, 'logger' => $this->logger]);
/*
* Setup downloads directory.
*/
if(!isset($this->downloads_dir))
{
/*
* Define the location of the downloads directory.
*/
$this->downloads_dir = WEB_ROOT . '/downloads/';
}
if(!isset($this->downloads_url))
{
$this->downloads_url = '/downloads/';
}
$this->downloads_dir = join_paths([realpath($this->downloads_dir)]);
}
// {{{ init_logger()
/*
*
* @access public
* @static
* @since Method available since Release 0.7
*/
public function init_logger()
{
if (!isset($this->logger))
{
$this->logger = new Logger();
}
}
public function set_execution_limits()
{
/*
* Let this script run for as long as is necessary to finish.
*/
set_time_limit(0);
/*
* Give PHP lots of RAM.
*/
ini_set('memory_limit', IMPORT_MEMORY_LIMIT);
}
/**
* Check if we need to populate the db.
*/
public function check_db_populated()
{
/*
* To see if the database tables exist, just issue a query to the laws table.
*/
try
{
$sql = 'SELECT 1
FROM laws
LIMIT 1';
$statement = $this->db->prepare($sql);
$result = $statement->execute();
} catch (Exception $except)
{
return false;
}
return true;
}
/**
* Populate the database
*/
public function populate_db()
{
if(!$this->check_db_populated())
{
/*
* We expect an exception here. If there's not one,
* the table exists and we can go on our merry way.
*/
$this->logger->message('Creating the database tables', 5);
/*
* The database tables do not exist, so see if the MySQL import file can be found.
*/
if (file_exists(WEB_ROOT . '/admin/statedecoded.sql') === false)
{
$this->logger->message('Could not read find ' . WEB_ROOT . '/admin/statedecoded.sql to '
. 'populate the database. Database tables could not be created.', 10);
return false;
}
/*
* Load the MySQL import file into MySQL. We don't prepare this query because PDO_MySQL
* didn't support multiple queries until PHP 5.3.
*/
$sql = file_get_contents(WEB_ROOT . '/admin/statedecoded.sql');
/*
* DELIMITER is an instruction to the mysql command-line client, not SQL that the
* server understands, so PDO chokes on it. It exists only to stop that client from
* splitting a stored routine on the semicolons inside its body. Remove the
* directives, and separate the routines from the rest of the file so that each can
* be sent to the server on its own.
*/
list($sql, $routines) = $this->extract_routines($sql);
$result = $this->db->exec($sql);
if ($result === false)
{
return false;
}
foreach ($routines as $routine)
{
if ($this->db->exec($routine) === false)
{
$this->logger->message('Could not create a stored routine. If binary logging '
. 'is enabled, the database user may need log_bin_trust_function_creators '
. 'to be set.', 10);
return false;
}
}
}
return true;
}
/**
* Separate stored routines from the rest of a SQL file.
*
* The mysql command-line client uses DELIMITER to change what it treats as the end of a
* statement, so that a stored routine's internal semicolons do not split it. PDO has no
* such concept: it passes the text straight to the server, which rejects DELIMITER as a
* syntax error. So we strip the directives and hand back the routines separately, to be
* executed one at a time.
*
* @param string $sql The contents of a SQL file.
* @return array [the SQL with routines removed, an array of routine definitions]
*/
public function extract_routines($sql)
{
$routines = [];
/*
* Match each DELIMITER <token> ... <token> DELIMITER ; block, capturing the statement
* between the custom delimiters.
*/
$pattern = '/^[ \t]*DELIMITER[ \t]+(\S+)[ \t]*$(.*?)^[ \t]*\1[ \t]*$'
. '(?:\s*^[ \t]*DELIMITER[ \t]+;[ \t]*$)?/ms';
$sql = preg_replace_callback(
$pattern,
function ($matches) use (&$routines)
{
$routine = trim($matches[2]);
/*
* A routine body ends with "END;" -- the semicolon belonging to the routine's
* final statement, not a statement terminator we need to keep.
*/
if ($routine !== '')
{
$routines[] = $routine;
}
return '';
},
$sql
);
return [$sql, $routines];
}
/**
* Check for any outstanding migrations that need running.
*/
public function check_migrations()
{
$migrate_action = new MigrateAction([
'db' => $this->db
]);
$migrations = $migrate_action->getUndoneMigrations();
return $migrations;
}
public function run_migrations()
{
$migrate_action = new MigrateAction([
'db' => $this->db
]);
ob_start();
$result = $migrate_action->doMigrations();
$result = nl2br(ob_get_contents()) . $result;
ob_end_clean();
return $result;
}
/**
* If the "editions" table lacks any entries, create a stock one within the database and within
* the configuration file.
*/
public function populate_editions()
{
$this->logger->message('Checking for an existing edition in the database', 5);
/*
* If we have an entries in the editions table, then return true, indicating that the
* editions table has been populated.
*/
$sql = 'SELECT *
FROM editions
LIMIT 1';
$statement = $this->db->prepare($sql);
$result = $statement->execute();
if ($result !== false && $statement->rowCount() > 0)
{
return true;
}
$this->logger->message('Adding an inaugural “editions” record to the database', 5);
/*
* Add a record to editions, setting the label to today's date.
*/
$sql = 'INSERT INTO
editions
SET year = :date, date_created=now()';
$sql_args = [
':date' => date('Y-M-d')
];
$statement = $this->db->prepare($sql);
$result = $statement->execute($sql_args);
/*
* If we could not add the record to the database.
*/
if ($result === false)
{
$this->logger->message('Could not add an “editions” record to the database', 5);
return false;
}
return true;
}
/*
* Get a list of all editions
*/
public function get_editions()
{
$sql = 'SELECT *
FROM editions
ORDER BY order_by';
$statement = $this->db->prepare($sql);
$result = $statement->execute();
if ($result === false || $statement->rowCount() == 0)
{
return false;
}
else
{
$editions = [];
$editions = $statement->fetchAll(PDO::FETCH_OBJ);
}
return $editions;
}
public function get_current_edition()
{
$sql = 'SELECT *
FROM editions
WHERE current = :current
ORDER BY order_by';
$sql_args[':current'] = 1;
$statement = $this->db->prepare($sql);
$result = $statement->execute($sql_args);
if ($result === false || $statement->rowCount() == 0)
{
return false;
}
else
{
$edition = $statement->fetch(PDO::FETCH_OBJ);
}
return $edition;
}
public function handle_editions($post_data)
{
$previous_edition = $this->get_current_edition();
if($previous_edition && $previous_edition->id !== $this->edition_id)
{
$this->previous_edition_id = $previous_edition->id;
}
$errors = [];
$create_data = [];
if (!empty($post_data['make_current']))
{
if ($current = filter_var($post_data['make_current'], FILTER_VALIDATE_INT))
{
$create_data['current'] = (int) $current;
}
else
{
$errors[] = 'Unexpected value for “make this edition current.”';
}
}
else
{
$create_data['current'] = 0;
}
if ($post_data['edition_option'] == 'new')
{
if ($name = filter_var($post_data['new_edition_name'], FILTER_DEFAULT))
{
$create_data['name'] = $name;
}
else
{
$errors[] = 'Please enter a valid edition name.';
}
if ($slug = filter_var($post_data['new_edition_slug'], FILTER_DEFAULT))
{
$create_data['slug'] = $slug;
}
else
{
$errors[] = 'Please enter a valid edition URL.';
}
if (count($errors) === 0)
{
$edition_id = $this->create_edition($create_data);
if ($edition_id)
{
$this->edition_id = $edition_id;
}
else
{
$errors[] = 'Unable to create edition.';
}
}
}
elseif ($post_data['edition_option'] == 'existing')
{
if ($edition_id = filter_var($post_data['edition'], FILTER_VALIDATE_INT))
{
$this->edition_id = $edition_id;
if($create_data['current'] > 0)
{
$create_data['id'] = $this->edition_id;
$this->update_edition($create_data);
}
}
else
{
$errors[] = 'Please select an edition to update.';
}
}
else
{
$errors[] = 'Please select if you would like to create a new edition or ' .
'update an existing one.';
}
if(isset($this->edition_id))
{
/*
* Get the edition from the database and store a copy locally.
*/
$edition = new Edition();
$edition_result = $edition->find_by_id($this->edition_id);
if ($edition_result !== false)
{
$this->set_edition($edition_result);
// Write the EDITION_ID
if($this->edition->current)
{
$edition->unset_current($this->edition_id);
/*
* A failed write is recorded rather than returned as an
* error, because the callers treat an error here as fatal.
* The edition is already current in the database, which is
* what the rest of the import depends on; only the .htaccess
* copy of the ID is missing. The caller reports it so that
* the import does not claim success.
*
* Only a string counts as a failure, so that a caller which
* stubs out export_edition_id() -- as the tests do -- is not
* mistaken for a failed write.
*/
$export_result = $this->export_edition_id($this->edition_id);
if (is_string($export_result))
{
$this->edition_id_export_error = $export_result;
}
}
}
else
{
$errors[] = 'The edition could not be found.';
}
}
return $errors;
}
/**
* Set the edition.
*/
public function set_edition($edition)
{
$this->edition = $edition;
$this->downloads_dir = join_paths([$this->downloads_dir, $edition->slug]);
$this->downloads_url = join_paths([$this->downloads_url, $edition->slug]);
}
/**
* Create a new edition.
*/
public function create_edition($edition = [])
{
$edition_obj = new Edition(['db' => $this->db]);
/*
* Make sure we have a unique edition.
*/
if($edition_obj->find_by_name($edition['name']))
{
$this->logger->message('The name for that edition is already in use. Please choose a different name.', 10);
return false;
}
if($edition_obj->find_by_slug($edition['slug']))
{
$this->logger->message('The slug for that edition is already in use. Please choose a different slug.', 10);
return false;
}
return $edition_obj->create($edition);
}
/**
* Update an edition.
*/
public function update_edition($data)
{
if($data['id'])
{
$sql_args[':id'] = $data['id'];
unset($data['id']);
if(count($data))
{
$sql = 'UPDATE editions
SET ';
$update = [];
foreach($data as $key => $value)
{
$sql_args[':' . $key] = $value;
$update[] = $key .' = :' . $key;
}
$sql .= implode(',', $update);
$sql .= ' WHERE id = :id';
$statement = $this->db->prepare($sql);
$result = $statement->execute($sql_args);
}
else
{
trigger_error('Nothing to update on editions. Cowardly refusing.', E_USER_WARNING);
}
}
else
{
trigger_error('Updating editions without an id! Cowardly refusing.', E_USER_WARNING);
}
}
/**
* Store the edition id in the .htaccess file.
*
* Returns true on success, or a string explaining the failure. The caller
* reports that as an error rather than merely logging it: EDITION_ID is
* where several queries get their default edition, so an import that leaves
* it pointing at an older edition looks like a success while quietly
* serving the wrong edition of the code.
*/
public function export_edition_id($edition_id)
{
$error = false;
$htaccess_path = WEB_ROOT . '/.htaccess';
/*
* If possible, modify the .htaccess file, to store permanently the edition ID.
*/
if (is_writable($htaccess_path) == true)
{
$htaccess = file_get_contents($htaccess_path);
if ($htaccess === false)
{
$error = 'Could not read ' . $htaccess_path . ' to store the edition ID. '
. 'EDITION_ID still names the previous edition.';
}
else
{
/*
* If there isn't already an edition ID in .htaccess, then write a new record.
* Otherwise, update the existing record.
*/
if (strpos($htaccess, ' EDITION_ID ') === false)
{
$htaccess .= PHP_EOL . PHP_EOL . 'SetEnv EDITION_ID ' . $edition_id . PHP_EOL;
}
else
{
$htaccess = preg_replace('/SetEnv EDITION_ID (\d+)/', 'SetEnv EDITION_ID ' . $edition_id, $htaccess);
}
/*
* Compare against false explicitly: file_put_contents() returns
* the number of bytes written, and 0 is falsy but not an error.
*/
if (file_put_contents($htaccess_path, $htaccess) === false)
{
$error = 'Could not write the edition ID to ' . $htaccess_path . '. '
. 'EDITION_ID still names the previous edition.';
}
else
{
$this->logger->message('Wrote edition ID to .htaccess', 5);
}
}
}
else
{
$error = 'Cannot write to ' . $htaccess_path . ', so the edition ID could not be '
. 'stored. EDITION_ID still names the previous edition. Make the file writable '
. 'by the user running the import.';
}
if ($error !== false)
{
$this->logger->message($error, 10);
}
/*
* Store the edition ID as a constant, so that we can use it elsewhere in the import
* process.
*/
if(!defined('EDITION_ID'))
{
define('EDITION_ID', $edition_id);
}
return $error === false ? true : $error;
}
/**
* Clear out our database.
*/
public function clear_db()
{
$tables = ['dictionary', 'laws', 'laws_references', 'text', 'laws_views',
'tags', 'text_sections', 'structure', 'permalinks', 'laws_meta'];
foreach ($tables as $table)
{
/*
* Note that we *cannot* prepare the table name as an argument here.
* PDO doesn't work that way.
* We are deleting instead of truncating, to handle foreign keys.
*/
$sql = 'DELETE FROM ' . $table;
$statement = $this->db->prepare($sql);
$result = $statement->execute();
if ($result === false)
{
$this->logger->message('Error in SQL: ' . $sql, 10);
die();
}
$this->logger->message('Deleted all records from ' . $table, 5);
}
/*
* Delete law histories.
*/
$sql = 'DELETE FROM laws_meta
WHERE meta_key = :meta_key_history
OR meta_key = :meta_key_repealed';
$sql_args = [
':meta_key_history' => 'history',
':meta_key_repealed' => 'repealed'
];
$statement = $this->db->prepare($sql);
$result = $statement->execute($sql_args);
return true;
}
public function clear_edition($edition_id)
{
$tables = [
'dictionary',
'laws',
'laws_references',
'text',
'text_sections',
//'laws_views',
'tags',
'structure',
'permalinks',
'laws_meta'
];
foreach ($tables as $table)
{
/*
* Note that we *cannot* prepare the table name as an argument here.
* PDO doesn't work that way.
* We are deleting instead of truncating, to handle foreign keys.
*/
$sql = 'DELETE FROM ' . $table . ' WHERE edition_id = :edition_id';
$sql_args = [':edition_id' => $edition_id];
$statement = $this->db->prepare($sql);
$result = $statement->execute($sql_args);
if ($result === false)
{
$this->logger->message('Error in SQL: ' . $sql, 10);
die();
}
$this->logger->message('Deleted ' . $table, 5);
}
return true;
}
/**
* Remove law-view records greater than one year old.
*/
public function prune_views()
{
$sql = 'DELETE FROM
laws_views
WHERE DATEDIFF(now(), date) > :date_diff';
$sql_args = [
':date_diff' => 365
];
$statement = $this->db->prepare($sql);
$result = $statement->execute($sql_args);
$this->logger->message('Pruned view records greater than one year old', 5);
return true;
}
/**
* Parse the provided legal code
*/
public function parse()
{
$this->logger->message('Starting file parsing.', 5);
/*
* Create a new instance of Parser.
* Check for obvious errors (files not present, etc);
*/
try
{
$parser = new Parser(
[
/*
* Tell the parser what the working directory
* should be for the data files to import.
*/
'directory' => $this->import_data_dir,
/*
* Set the database
*/
'db' => $this->db,
/*
* Set the edition
*/
'edition_id' => $this->edition_id,
/*
* Set the logger
*/
'logger' => $this->logger,
/*
* Set the downloads directories
*/
'downloads_dir' => $this->downloads_dir,
'downloads_url' => $this->downloads_url
]
);
if(method_exists($parser, 'pre_parse'))
{
$parser->pre_parse();
}
/*
* Iterate through the files.
*/
$this->logger->message('Importing the law files in the import-data directory', 5);
$this->logger->progress('parsefiles');
while ($section = $parser->iterate())
{
$parser->section = $section;
if ($parser->parse() === true)
{
$this->db->beginTransaction();
try
{
$parser->store();
$this->db->commit();
}
catch (Exception $e)
{
$this->db->rollBack();
throw $e;
}
}
$this->logger->updateProgressFiles('parsefiles', $parser->file, count($parser->files));
}
$this->logger->finishProgress('parsefiles');
if(method_exists($parser, 'post_parse'))
{
$parser->post_parse();
}
}
catch(Exception $e)
{
$this->logger->message('Import error: ' . $e->getMessage(), 10);
return false;
}
/*
* Handle references
*/
$this->update_laws_references();
/*
* Break up law histories into their components and save those.
*/
$this->logger->message('Analyzing law histories', 5);
$sql = 'SELECT id, history
FROM laws';
$statement = $this->db->prepare($sql);
$result = $statement->execute();
if ($result !== false && $statement->rowCount() > 0)
{
/*
* Step through the history of every law that we have a record of.
*/
$sql = 'INSERT INTO laws_meta
SET law_id = :law_id,
meta_key = :meta_key,
meta_value = :meta_value,
date_created = now(),
edition_id = :edition_id';
$statement = $this->db->prepare($sql);
while ($law = $statement->fetch(PDO::FETCH_OBJ))
{
/*
* Turn the string of text that comprises the history into an object of atomic
* history history data.
*/
$parser->history = $law->history;
$history = $parser->extract_history();
if ($history !== false)
{
/*
* Save this object to the metadata table pair.
*/
$sql_args = [
':law_id' => $law->id,
':meta_key' => 'history',
':meta_value' => serialize($history),
':edition_id' => $this->edition_id
];
$result = $statement->execute($sql_args);
}
}
}
/*
* If we already have a view, replace it with this new one.
*/
$sql = 'DROP VIEW IF EXISTS structure_unified';
$statement = $this->db->prepare($sql);
$result = $statement->execute();
/*
* Get the depth of our structures.
*/
$sql = 'SELECT MAX(depth) AS depth FROM structure';
$statement = $this->db->prepare($sql);
$result = $statement->execute();
if($result)
{
$structure_depth = $statement->fetchColumn();
}
else
{
throw new Exception('Cannot get depth of structures.');
}
$select = [];
$from = [];
$order = [];
for ($i=1; $i<=$structure_depth; $i++)
{
$select[] = 's'.$i.'.id AS s'.$i.'_id, s'.$i.'.name AS s'.$i.'_name,
s'.$i.'.identifier AS s'.$i.'_identifier, s'.$i.'.label AS s'.$i.'_label';
$from[] = 's'.$i;
$order[] = 's'.$i.'.identifier';
}
/*
* We want to to order from highest to lowest, so flip this array around.
*/
$order = array_reverse($order);