2 /************************************************************************/
4 /************************************************************************/
5 /* Copyright (c) 2002-2010 */
6 /* Inclusive Design Institute */
8 /* This program is free software. You can redistribute it and/or */
9 /* modify it under the terms of the GNU General Public License */
10 /* as published by the Free Software Foundation. */
11 /************************************************************************/
15 * Class for rewriting pretty urls in tests
20 class FileStorageUrl {
22 var $rule; //an array that maps [lvl->query parts]
25 // @param the filename that was being called, this can be index.php, comments.php, revisions.php
26 function FileStorageUrl($filename) {
28 $filename = 'index.php';
30 $this->rule = array(0=>'action', 1=>'ot', 2=>'oid', 3=>'folder'); //default 3=folder, but it can be id as well for comment
31 $this->filename = $filename;
35 function setRule($id, $ruleName){
36 $this->rule[$id] = $ruleName;
40 * Construct pretty url by the given query string.
41 * Note: This method will be a bit different from ForumsUrl, TestsUrl, ContentUrl because it has browse/comment in the rule which
42 * doesn't exist in the actual query.
43 * @param string the query string of the url
44 * @param string filename of the request, this consists of revisions.php, index.php, comments.php
46 function constructPrettyUrl($query){
51 //If this is already a pretty url,but without mod_apache rule
52 //unwrap it and reconstruct
53 if (is_array($query)){
55 foreach($query as $fk=>$fv){
56 if (preg_match('/\.php/', $fv)==1){
57 continue; //skip the php file
60 //check if this is part of the rule, if so,add it, o/w ignore
61 if (array_search($fv, $this->rule)!==FALSE){
62 $new_query .= $fv . '=' . $query[$fk+1] . SEP;
65 $query = $new_query; //done
68 $temp = explode(SEP, $query);
69 foreach ($temp as $index=>$attributes){
70 if(empty($attributes)){
71 //skip the ones that are empty.
74 list($key, $value) = preg_split('/\=/', $attributes, 2);
75 $query_parts[$key] = $value;
79 //determine if this uses 'browse' or 'comment'
80 $prefix = $this->configRule($this->filename);
82 $url .= $prefix.'/' ; //add either index, revision or comment to the url
85 //construct pretty url on mapping
86 foreach ($this->rule as $key=>$value){
88 //if this is action, skip it.
89 if ($value == 'action'){
91 } elseif ($query_parts[$value] ==''){
92 //if this value is empty, the url construction should quit.
95 $url .= $query_parts[$value].'/';
97 //if the query parts are not in the defined rules, set it back to query string again
98 if ($query_parts[$this->rule[$key]]!=''){
99 $query_parts[$this->rule[$key]] = '';
103 //Go through the query_parts again, and for those values that are not empty
104 // add it to the querystring
105 foreach($query_parts as $key=>$value){
106 //paginator are handle differently
107 if ($value!='' && $key!='page'){
108 $query_string .= $key.'='.$value.SEP;
111 //take out the last sep.
112 $query_string = substr($query_string, 0, -1);
115 if ($query_parts['page']!=''){
116 $url .= '/'.$query_parts['page'].'.html';
119 //append query string at the back
120 if ($query_string!=''){
121 $url .= '?'.$query_string;
129 * A helper method for constructPrettyUrl
130 * @param string filename
132 function configRule($filename){
133 //run through the query once, extract if it uses id or folder.
134 //if 'id', it is comments.php
135 //if 'folder', it is index.php
136 if ($filename=='comments.php'){
137 $this->setRule(3, 'id');
139 } elseif ($filename=='revisions.php'){
140 $this->setRule(3, 'id');
143 $this->setRule(3, 'folder');