Index: branches/license-work/phase3/includes/filerepo/FileProperties.php |
— | — | @@ -1,6 +1,13 @@ |
2 | 2 | <?php |
3 | 3 | |
4 | 4 | class FileProperties { |
| 5 | + /** |
| 6 | + * Constructor for the FileProperties class that represents proeprties |
| 7 | + * associated with a file. |
| 8 | + * |
| 9 | + * @param $file File |
| 10 | + * @param $revision int Revision id |
| 11 | + */ |
5 | 12 | public function __construct( $file, $revision = null ) { |
6 | 13 | $this->file = file; |
7 | 14 | $this->revId = $revision; |
— | — | @@ -11,6 +18,9 @@ |
12 | 19 | $this->load(); |
13 | 20 | } |
14 | 21 | |
| 22 | + /** |
| 23 | + * Load the properties for this file from the DB |
| 24 | + */ |
15 | 25 | public function load() { |
16 | 26 | if ( !$this->revId ) { |
17 | 27 | $this->revId = $this->file->getTitle()->getLatestRevID(); |
— | — | @@ -24,7 +34,11 @@ |
25 | 35 | $this->loadFromResult( $res ); |
26 | 36 | } |
27 | 37 | |
28 | | - public function loadFromResult( $res ) { |
| 38 | + /** |
| 39 | + * Construct FileAuthor and FileProperties objects from a database result |
| 40 | + */ |
| 41 | + protected function loadFromResult( $res ) { |
| 42 | + $repo = $this->file->getRepo(); |
29 | 43 | $result = array(); |
30 | 44 | |
31 | 45 | foreach ( $res as $row ) { |
— | — | @@ -33,7 +47,7 @@ |
34 | 48 | $this->authors[] = FileAuthor::newFromRow( $row ); |
35 | 49 | break; |
36 | 50 | case 'license': |
37 | | - $this->licenses[] = FileLicense::newFromRow( $row ); |
| 51 | + $this->licenses[] = FileLicense::newFromRow( $repo, $row ); |
38 | 52 | break; |
39 | 53 | } |
40 | 54 | } |
— | — | @@ -43,13 +57,25 @@ |
44 | 58 | } |
45 | 59 | } |
46 | 60 | |
| 61 | + /** |
| 62 | + * Get all licenses associated with this file |
| 63 | + */ |
47 | 64 | public function getLicenses() { |
48 | 65 | return $this->licenses; |
49 | 66 | } |
| 67 | + /** |
| 68 | + * Get all authors associated with this file |
| 69 | + */ |
50 | 70 | public function getAuthors() { |
51 | 71 | return $this->authors; |
52 | 72 | } |
53 | 73 | |
| 74 | + /** |
| 75 | + * Save the licenses and authors to the database |
| 76 | + * |
| 77 | + * @param $comment string Edit summary |
| 78 | + * @param $minor bool Minor edit |
| 79 | + */ |
54 | 80 | public function save( $comment, $minor = false ) { |
55 | 81 | $dbw = $this->file->getRepo()->getMasterDB(); |
56 | 82 | |
— | — | @@ -68,7 +94,7 @@ |
69 | 95 | 'fp_value_int' => $author->getId() |
70 | 96 | ); |
71 | 97 | |
72 | | - $text = $author->getRawText(); |
| 98 | + $text = $author->getText(); |
73 | 99 | if ( $text ) { |
74 | 100 | $a['fp_value_text'] = $text; |
75 | 101 | } |
— | — | @@ -88,35 +114,80 @@ |
89 | 115 | } |
90 | 116 | |
91 | 117 | class FileAuthor { |
| 118 | + /** |
| 119 | + * Construct a FileAuthor object from a database row |
| 120 | + * |
| 121 | + * @param $row Stdclass |
| 122 | + * @return FileAuthor |
| 123 | + */ |
92 | 124 | public static function newFromRow( $row ) { |
93 | 125 | return new self( $row->fp_value_int, $row->fp_value_text ); |
94 | 126 | } |
95 | | - |
| 127 | + /** |
| 128 | + * Constructor for a FileAuthor object |
| 129 | + * |
| 130 | + * @param $id int User id if any |
| 131 | + * @param $text string User text |
| 132 | + */ |
96 | 133 | public function __construct( $id, $text ) { |
97 | 134 | $this->id = $id; |
98 | 135 | $this->text = $text; |
99 | 136 | } |
| 137 | + /** |
| 138 | + * Get the user id if any, 0 or null otherwise |
| 139 | + * |
| 140 | + * @return int |
| 141 | + */ |
100 | 142 | public function getUserId() { |
101 | 143 | return $this->id; |
102 | 144 | } |
| 145 | + /** |
| 146 | + * Get the user text |
| 147 | + * |
| 148 | + * @return string |
| 149 | + */ |
103 | 150 | public function getText() { |
104 | | - if ( $this->text ) { |
105 | | - return $this->text; |
106 | | - } |
107 | | - return User::newFromId( $this->id )->getName(); |
108 | | - } |
109 | | - public function getRawText() { |
110 | 151 | return $this->text; |
111 | 152 | } |
112 | 153 | } |
113 | 154 | |
114 | 155 | class FileLicense { |
115 | | - public static function newFromRow( $row ) { |
116 | | - return new self( $row->fp_value_int ); |
| 156 | + /** |
| 157 | + * Initialize a license object from a row |
| 158 | + * |
| 159 | + * @param $repo FileRepo |
| 160 | + * @param $row Stdclass |
| 161 | + * @return FileLicense |
| 162 | + */ |
| 163 | + public static function newFromRow( $repo, $row ) { |
| 164 | + $license = new self( $repo ); |
| 165 | + $license->id = $row->fp_value_int; |
| 166 | + return $license; |
117 | 167 | } |
| 168 | + /** |
| 169 | + * Initialize a license object from a name |
| 170 | + * |
| 171 | + * @param $repo FileRepo |
| 172 | + * @param $name string |
| 173 | + * @return FileLicense |
| 174 | + */ |
| 175 | + public static function newFromName( $repo, $name ) { |
| 176 | + $license = new self( $repo ); |
| 177 | + $license->name = $name; |
| 178 | + return $license; |
| 179 | + } |
118 | 180 | |
119 | | - public function __construct( $id ) { |
120 | | - $this->id = $id; |
| 181 | + /** |
| 182 | + * Constructor for the FileLicense |
| 183 | + * |
| 184 | + * @param $repo FileRepo |
| 185 | + */ |
| 186 | + public function __construct( $repo ) { |
| 187 | + $this->repo = $repo; |
| 188 | + $this->id = null; |
| 189 | + $this->name = null; |
| 190 | + $this->url = null; |
| 191 | + $this->count = 0; |
121 | 192 | } |
122 | 193 | public function getId() { |
123 | 194 | return $this->id; |
— | — | @@ -124,29 +195,57 @@ |
125 | 196 | public function getName() { |
126 | 197 | return $this->name; |
127 | 198 | } |
| 199 | + public function setName( $name ) { |
| 200 | + $this->name = $name; |
| 201 | + } |
128 | 202 | public function getUrl() { |
129 | 203 | return $this->url; |
130 | 204 | } |
| 205 | + public function setUrl( $url ) { |
| 206 | + $this->url = $url; |
| 207 | + } |
131 | 208 | public function getCount() { |
132 | 209 | return $this->count; |
133 | 210 | } |
134 | 211 | |
135 | | - public static function loadArray( &$licenses ) { |
| 212 | + /** |
| 213 | + * Initializes an uninitialized array of FileLicense objects from the db. |
| 214 | + * Removes non-existent licenses from the array. Allows loading from id or |
| 215 | + * name. |
| 216 | + * |
| 217 | + * @param &$licenses array Array of FileLicense objects |
| 218 | + * @param $loadFrom string id|name depending on from which field to load |
| 219 | + */ |
| 220 | + public static function loadArray( &$licenses, $loadFrom = 'id' ) { |
136 | 221 | $licenseIds = array(); |
137 | 222 | foreach ( $licenses as $license ) { |
138 | | - $licenseIds[] = $license->getId(); |
| 223 | + switch ( $loadFrom ) { |
| 224 | + case 'id': |
| 225 | + $licenseIds[] = $license->getId(); |
| 226 | + break; |
| 227 | + case 'name': |
| 228 | + $licenseIds[] = $license->getName(); |
| 229 | + break; |
| 230 | + } |
| 231 | + |
139 | 232 | } |
| 233 | + if ( !$licenseIds ) { |
| 234 | + $licenses = array(); |
| 235 | + return; |
| 236 | + } |
140 | 237 | |
141 | | - $dbr = wfGetDB( DB_SLAVE ); |
| 238 | + $dbr = $this->repo->getSlaveDb(); |
142 | 239 | $res = $dbr->select( 'licenses', |
143 | 240 | array( 'lic_id', 'lic_name', 'lic_url', 'lic_count' ), |
144 | | - array( 'lic_id' => $licenseIds ), |
| 241 | + array( "lic_{$loadFrom}" => $licenseIds ), |
145 | 242 | __METHOD__ ); |
146 | | - |
| 243 | + |
| 244 | + // Create an id<>row map |
147 | 245 | $licenseData = array(); |
148 | 246 | foreach ( $res as $row ) { |
149 | 247 | $licenseData[$row->lic_id] = $row; |
150 | 248 | } |
| 249 | + // Initializes the licenses from the rows, removes non-existent licenses |
151 | 250 | foreach ( $licenses as $key => $license ) { |
152 | 251 | if ( isset( $licenseData[$license->getId()] ) ) { |
153 | 252 | $license->loadFromLicenseRow( $licenseData[$license->getId()] ); |
— | — | @@ -156,11 +255,50 @@ |
157 | 256 | } |
158 | 257 | } |
159 | 258 | |
160 | | - |
| 259 | + /** |
| 260 | + * Initializes a license from a row. |
| 261 | + * |
| 262 | + * @param $row Stdclass |
| 263 | + */ |
161 | 264 | public function loadFromLicenseRow( $row ) { |
162 | 265 | $this->name = $row->lic_name; |
163 | 266 | $this->url = $row->lic_url; |
164 | 267 | $this->count = $row->lic_count; |
165 | 268 | } |
166 | 269 | |
| 270 | + /** |
| 271 | + * Inserts a new license into the database. |
| 272 | + */ |
| 273 | + public function insert() { |
| 274 | + $dbw = $this->repo->getMasterDb(); |
| 275 | + $dbw->insert( 'licenses', array( |
| 276 | + 'lic_name' => $this->getName(), |
| 277 | + 'lic_url' => $this->getUrl(), |
| 278 | + 'lic_count' => 0 ), |
| 279 | + __METHOD__ ); |
| 280 | + $this->id = $dbw->insertId(); |
| 281 | + } |
| 282 | + /** |
| 283 | + * Updates information for a license. |
| 284 | + */ |
| 285 | + public function update() { |
| 286 | + $dbw = $this->repo->getMasterDb(); |
| 287 | + $dbw->update( 'licenses', array( |
| 288 | + 'lic_name' => $this->getName(), |
| 289 | + 'lic_url' => $this->getUrl(), |
| 290 | + 'lic_count' => $this->count + 1 ), |
| 291 | + array( 'lic_id' => $this->id ), |
| 292 | + __METHOD__ ); |
| 293 | + } |
| 294 | + /** |
| 295 | + * Inserts a new license into the database if this license does not have |
| 296 | + * an id, updates it otherwise |
| 297 | + */ |
| 298 | + public function save() { |
| 299 | + if ( $this->id ) { |
| 300 | + $this->update(); |
| 301 | + } else { |
| 302 | + $this->insert(); |
| 303 | + } |
| 304 | + } |
167 | 305 | } |