made a copy
[atutor.git] / include / classes / UrlRewrite / FileStorageUrl.class.php
1 <?php
2 /************************************************************************/
3 /* ATutor                                                                                                                               */
4 /************************************************************************/
5 /* Copyright (c) 2002-2008 by Greg Gay, Joel Kronenberg & Heidi Hazelton*/
6 /* Adaptive Technology Resource Centre / University of Toronto                  */
7 /* http://atutor.ca                                                                                                             */
8 /*                                                                                                                                              */
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 /************************************************************************/
13 // $Id$
14 /**
15 * FileStorageUrl
16 * Class for rewriting pretty urls in tests
17 * @access       public
18 * @author       Harris Wong
19 * @package      UrlParser
20 */
21 class FileStorageUrl {
22         // local variables
23         var $rule;              //an array that maps [lvl->query parts]
24
25         // constructor
26         // @param the filename that was being called, this can be index.php, comments.php, revisions.php
27         function FileStorageUrl($filename) {
28                 if ($filename == ''){
29                         $filename = 'index.php';
30                 }
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;
33         }
34
35         //
36         function setRule($id, $ruleName){
37                 $this->rule[$id] = $ruleName;
38         }
39
40         /**
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
46          */
47         function constructPrettyUrl($query){
48                 if (empty($query)){
49                         return '';
50                 }
51
52                 //If this is already a pretty url,but without mod_apache rule
53                 //unwrap it and reconstruct
54                 if (is_array($query)){
55                         $new_query = '';
56                         foreach($query as $fk=>$fv){
57                                 if      (preg_match('/\.php/', $fv)==1){
58                                         continue;       //skip the php file
59                                 }
60
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;
64                                 }
65                         }
66                         $query = $new_query;    //done
67                 }
68
69                 $temp = explode(SEP, $query);
70                 foreach ($temp as $index=>$attributes){
71                         if(empty($attributes)){
72                                 //skip the ones that are empty.
73                                 continue;
74                         }
75                         list($key, $value) = preg_split('/\=/', $attributes, 2);
76                         $query_parts[$key] = $value;
77                 }
78
79                 $query_string = '';
80                 //determine if this uses 'browse' or 'comment'
81                 $prefix = $this->configRule($this->filename);
82                 if ($prefix != '') {
83                         $url .= $prefix.'/' ;   //add either index, revision or comment to the url
84                 }
85
86                 //construct pretty url on mapping
87                 foreach ($this->rule as $key=>$value){
88
89                         //if this is action, skip it.
90                         if ($value == 'action'){
91                                 continue;
92                         } elseif ($query_parts[$value] ==''){
93                                 //if this value is empty, the url construction should quit.
94                                 break;
95                         }
96                         $url .= $query_parts[$value].'/';
97
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]] = '';
101                         }
102                 }
103
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;
110                         }
111                 }
112                 //take out the last sep.
113                 $query_string = substr($query_string, 0, -1);
114
115                 //handle paginators
116                 if ($query_parts['page']!=''){
117                         $url .= '/'.$query_parts['page'].'.html';
118                 }
119
120                 //append query string at the back
121                 if ($query_string!=''){
122                         $url .= '?'.$query_string;
123                 }
124
125                 return $url;
126         }
127
128
129         /**
130          * A helper method for constructPrettyUrl
131          * @param       string  filename
132          */
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');
139                         return 'comments';
140                 } elseif ($filename=='revisions.php'){
141                         $this->setRule(3, 'id');
142                         return 'revisions';
143                 } else {
144                         $this->setRule(3, 'folder');
145 //                      return 'index';
146                         return '';
147                 }
148
149         }
150
151
152 }
153 ?>