2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto */
9 /* This program is free software. You can redistribute it and/or */
10 /* modify it under the terms of the GNU General Public License */
11 /* as published by the Free Software Foundation. */
12 /************************************************************************/
16 * Class for rewriting pretty urls in tests
21 class FileStorageUrl {
23 var $rule; //an array that maps [lvl->query parts]
26 // @param the filename that was being called, this can be index.php, comments.php, revisions.php
27 function FileStorageUrl($filename) {
29 $filename = 'index.php';
31 $this->rule = array(0=>'action', 1=>'ot', 2=>'oid', 3=>'folder'); //default 3=folder, but it can be id as well for comment
32 $this->filename = $filename;
36 function setRule($id, $ruleName){
37 $this->rule[$id] = $ruleName;
41 * Construct pretty url by the given query string.
42 * Note: This method will be a bit different from ForumsUrl, TestsUrl, ContentUrl because it has browse/comment in the rule which
43 * doesn't exist in the actual query.
44 * @param string the query string of the url
45 * @param string filename of the request, this consists of revisions.php, index.php, comments.php
47 function constructPrettyUrl($query){
52 //If this is already a pretty url,but without mod_apache rule
53 //unwrap it and reconstruct
54 if (is_array($query)){
56 foreach($query as $fk=>$fv){
57 if (preg_match('/\.php/', $fv)==1){
58 continue; //skip the php file
61 //check if this is part of the rule, if so,add it, o/w ignore
62 if (array_search($fv, $this->rule)!==FALSE){
63 $new_query .= $fv . '=' . $query[$fk+1] . SEP;
66 $query = $new_query; //done
69 $temp = explode(SEP, $query);
70 foreach ($temp as $index=>$attributes){
71 if(empty($attributes)){
72 //skip the ones that are empty.
75 list($key, $value) = preg_split('/\=/', $attributes, 2);
76 $query_parts[$key] = $value;
80 //determine if this uses 'browse' or 'comment'
81 $prefix = $this->configRule($this->filename);
83 $url .= $prefix.'/' ; //add either index, revision or comment to the url
86 //construct pretty url on mapping
87 foreach ($this->rule as $key=>$value){
89 //if this is action, skip it.
90 if ($value == 'action'){
92 } elseif ($query_parts[$value] ==''){
93 //if this value is empty, the url construction should quit.
96 $url .= $query_parts[$value].'/';
98 //if the query parts are not in the defined rules, set it back to query string again
99 if ($query_parts[$this->rule[$key]]!=''){
100 $query_parts[$this->rule[$key]] = '';
104 //Go through the query_parts again, and for those values that are not empty
105 // add it to the querystring
106 foreach($query_parts as $key=>$value){
107 //paginator are handle differently
108 if ($value!='' && $key!='page'){
109 $query_string .= $key.'='.$value.SEP;
112 //take out the last sep.
113 $query_string = substr($query_string, 0, -1);
116 if ($query_parts['page']!=''){
117 $url .= '/'.$query_parts['page'].'.html';
120 //append query string at the back
121 if ($query_string!=''){
122 $url .= '?'.$query_string;
130 * A helper method for constructPrettyUrl
131 * @param string filename
133 function configRule($filename){
134 //run through the query once, extract if it uses id or folder.
135 //if 'id', it is comments.php
136 //if 'folder', it is index.php
137 if ($filename=='comments.php'){
138 $this->setRule(3, 'id');
140 } elseif ($filename=='revisions.php'){
141 $this->setRule(3, 'id');
144 $this->setRule(3, 'folder');