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