/[dtapublic]/projs/trunk/libs/php/lib_dta/webpage_pic_org/filenames_canonize.php
ViewVC logotype

Contents of /projs/trunk/libs/php/lib_dta/webpage_pic_org/filenames_canonize.php

Parent Directory Parent Directory | Revision Log Revision Log


Revision 37 - (show annotations) (download)
Wed Oct 12 02:32:21 2016 UTC (8 years, 1 month ago) by dashley
File size: 47314 byte(s)
Initial checkin.
1 <?php
2 //--------------------------------------------------------------------------------
3 //filenames_pics_canonize.php, v0.1a, Copyright David T. Ashley, 2015
4 //--------------------------------------------------------------------------------
5 //This program, a PHP script, modifies all filenames in a directory to a canonical
6 //form (no upper-case letters or unusual characters). This program is part of a
7 //3-program suite designed to collectively create a web page directly from
8 //digital camera files (and of course the web page can be customized by
9 //hand-editing after it is automatically generated). The supported process for
10 //creating a web page involves 3 programs (rather than 1) primarily to circumvent
11 //the involuntary process termination that will occur in some shared environments
12 //if a process consumes too much CPU time.
13 //
14 //The 3 programs in the 3-program suite are:
15 //
16 // filenames_canonize.php (this program):
17 // Converts all names of files in a directory to lower-case, and makes
18 // substitutions for any unusual characters.
19 //
20 // thumbnails_make.php:
21 // Creates thumbnails from recognized image types. The thumbnails are
22 // named relative to the original image file with the suffix "_small".
23 // Only 20 files are converted on each invocation of the program, to
24 // avoid the involuntary process termination that typically occurs in
25 // a shared hosting environment when a process uses too much CPU time.
26 // The program should be run repeatedly until it indicates that it has
27 // no more thumbnails to create.
28 //
29 // If any full-sized photos are modified, any corresponding thumbnails
30 // should be deleted and thumbnails_make.php and indexfile_make.php
31 // should be run again.
32 //
33 // indexfile_make.php
34 // Scans a directory and makes an index file ("index2.php") displaying
35 // all the thumbnail images, each of which link to the corresponding
36 // full-sized image. The index file is tailored to Dave Ashley's
37 // needs, but the created file can be edited and most of the content
38 // pasted into an HTML file. To avoid the accidental loss of
39 // information, any existing "index2.php" file is renamed out of the
40 // way.
41 //
42 //This script is designed to be run manually (rather than automatically invoked
43 //as a result of a web page request). It was written in PHP for convenience
44 //simply because DreamHost (the web hosting company Dave Ashley uses) has as part
45 //of its hosting environment PHP with the ImageMagick library compiled in.
46 //
47 //Usually, this script is invoked using "php <path>/filenames_canonize.php", but
48 //the method of invocation may vary based on computing platform details.
49 //--------------------------------------------------------------------------------
50 //This program is free software: you can redistribute it and/or modify
51 //it under the terms of the GNU General Public License as published by
52 //the Free Software Foundation, either version 3 of the License, or
53 //(at your option) any later version.
54 //
55 //This program is distributed in the hope that it will be useful,
56 //but WITHOUT ANY WARRANTY; without even the implied warranty of
57 //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
58 //GNU General Public License for more details.
59 //
60 //The GNU General Public License is reproduced below, and also is
61 //available at http://www.gnu.org/licenses/.
62 //--------------------------------------------------------------------------------
63 // GNU GENERAL PUBLIC LICENSE
64 // Version 3, 29 June 2007
65 //
66 // Copyright (C) 2007 Free Software Foundation, Inc. <http://fsf.org/>
67 // Everyone is permitted to copy and distribute verbatim copies
68 // of this license document, but changing it is not allowed.
69 //
70 // Preamble
71 //
72 // The GNU General Public License is a free, copyleft license for
73 // software and other kinds of works.
74 //
75 // The licenses for most software and other practical works are designed
76 // to take away your freedom to share and change the works. By contrast,
77 // the GNU General Public License is intended to guarantee your freedom to
78 // share and change all versions of a program--to make sure it remains free
79 // software for all its users. We, the Free Software Foundation, use the
80 // GNU General Public License for most of our software; it applies also to
81 // any other work released this way by its authors. You can apply it to
82 // your programs, too.
83 //
84 // When we speak of free software, we are referring to freedom, not
85 // price. Our General Public Licenses are designed to make sure that you
86 // have the freedom to distribute copies of free software (and charge for
87 // them if you wish), that you receive source code or can get it if you
88 // want it, that you can change the software or use pieces of it in new
89 // free programs, and that you know you can do these things.
90 //
91 // To protect your rights, we need to prevent others from denying you
92 // these rights or asking you to surrender the rights. Therefore, you have
93 // certain responsibilities if you distribute copies of the software, or if
94 // you modify it: responsibilities to respect the freedom of others.
95 //
96 // For example, if you distribute copies of such a program, whether
97 // gratis or for a fee, you must pass on to the recipients the same
98 // freedoms that you received. You must make sure that they, too, receive
99 // or can get the source code. And you must show them these terms so they
100 // know their rights.
101 //
102 // Developers that use the GNU GPL protect your rights with two steps:
103 // (1) assert copyright on the software, and (2) offer you this License
104 // giving you legal permission to copy, distribute and/or modify it.
105 //
106 // For the developers' and authors' protection, the GPL clearly explains
107 // that there is no warranty for this free software. For both users' and
108 // authors' sake, the GPL requires that modified versions be marked as
109 // changed, so that their problems will not be attributed erroneously to
110 // authors of previous versions.
111 //
112 // Some devices are designed to deny users access to install or run
113 // modified versions of the software inside them, although the manufacturer
114 // can do so. This is fundamentally incompatible with the aim of
115 // protecting users' freedom to change the software. The systematic
116 // pattern of such abuse occurs in the area of products for individuals to
117 // use, which is precisely where it is most unacceptable. Therefore, we
118 // have designed this version of the GPL to prohibit the practice for those
119 // products. If such problems arise substantially in other domains, we
120 // stand ready to extend this provision to those domains in future versions
121 // of the GPL, as needed to protect the freedom of users.
122 //
123 // Finally, every program is threatened constantly by software patents.
124 // States should not allow patents to restrict development and use of
125 // software on general-purpose computers, but in those that do, we wish to
126 // avoid the special danger that patents applied to a free program could
127 // make it effectively proprietary. To prevent this, the GPL assures that
128 // patents cannot be used to render the program non-free.
129 //
130 // The precise terms and conditions for copying, distribution and
131 // modification follow.
132 //
133 // TERMS AND CONDITIONS
134 //
135 // 0. Definitions.
136 //
137 // "This License" refers to version 3 of the GNU General Public License.
138 //
139 // "Copyright" also means copyright-like laws that apply to other kinds of
140 // works, such as semiconductor masks.
141 //
142 // "The Program" refers to any copyrightable work licensed under this
143 // License. Each licensee is addressed as "you". "Licensees" and
144 // "recipients" may be individuals or organizations.
145 //
146 // To "modify" a work means to copy from or adapt all or part of the work
147 // in a fashion requiring copyright permission, other than the making of an
148 // exact copy. The resulting work is called a "modified version" of the
149 // earlier work or a work "based on" the earlier work.
150 //
151 // A "covered work" means either the unmodified Program or a work based
152 // on the Program.
153 //
154 // To "propagate" a work means to do anything with it that, without
155 // permission, would make you directly or secondarily liable for
156 // infringement under applicable copyright law, except executing it on a
157 // computer or modifying a private copy. Propagation includes copying,
158 // distribution (with or without modification), making available to the
159 // public, and in some countries other activities as well.
160 //
161 // To "convey" a work means any kind of propagation that enables other
162 // parties to make or receive copies. Mere interaction with a user through
163 // a computer network, with no transfer of a copy, is not conveying.
164 //
165 // An interactive user interface displays "Appropriate Legal Notices"
166 // to the extent that it includes a convenient and prominently visible
167 // feature that (1) displays an appropriate copyright notice, and (2)
168 // tells the user that there is no warranty for the work (except to the
169 // extent that warranties are provided), that licensees may convey the
170 // work under this License, and how to view a copy of this License. If
171 // the interface presents a list of user commands or options, such as a
172 // menu, a prominent item in the list meets this criterion.
173 //
174 // 1. Source Code.
175 //
176 // The "source code" for a work means the preferred form of the work
177 // for making modifications to it. "Object code" means any non-source
178 // form of a work.
179 //
180 // A "Standard Interface" means an interface that either is an official
181 // standard defined by a recognized standards body, or, in the case of
182 // interfaces specified for a particular programming language, one that
183 // is widely used among developers working in that language.
184 //
185 // The "System Libraries" of an executable work include anything, other
186 // than the work as a whole, that (a) is included in the normal form of
187 // packaging a Major Component, but which is not part of that Major
188 // Component, and (b) serves only to enable use of the work with that
189 // Major Component, or to implement a Standard Interface for which an
190 // implementation is available to the public in source code form. A
191 // "Major Component", in this context, means a major essential component
192 // (kernel, window system, and so on) of the specific operating system
193 // (if any) on which the executable work runs, or a compiler used to
194 // produce the work, or an object code interpreter used to run it.
195 //
196 // The "Corresponding Source" for a work in object code form means all
197 // the source code needed to generate, install, and (for an executable
198 // work) run the object code and to modify the work, including scripts to
199 // control those activities. However, it does not include the work's
200 // System Libraries, or general-purpose tools or generally available free
201 // programs which are used unmodified in performing those activities but
202 // which are not part of the work. For example, Corresponding Source
203 // includes interface definition files associated with source files for
204 // the work, and the source code for shared libraries and dynamically
205 // linked subprograms that the work is specifically designed to require,
206 // such as by intimate data communication or control flow between those
207 // subprograms and other parts of the work.
208 //
209 // The Corresponding Source need not include anything that users
210 // can regenerate automatically from other parts of the Corresponding
211 // Source.
212 //
213 // The Corresponding Source for a work in source code form is that
214 // same work.
215 //
216 // 2. Basic Permissions.
217 //
218 // All rights granted under this License are granted for the term of
219 // copyright on the Program, and are irrevocable provided the stated
220 // conditions are met. This License explicitly affirms your unlimited
221 // permission to run the unmodified Program. The output from running a
222 // covered work is covered by this License only if the output, given its
223 // content, constitutes a covered work. This License acknowledges your
224 // rights of fair use or other equivalent, as provided by copyright law.
225 //
226 // You may make, run and propagate covered works that you do not
227 // convey, without conditions so long as your license otherwise remains
228 // in force. You may convey covered works to others for the sole purpose
229 // of having them make modifications exclusively for you, or provide you
230 // with facilities for running those works, provided that you comply with
231 // the terms of this License in conveying all material for which you do
232 // not control copyright. Those thus making or running the covered works
233 // for you must do so exclusively on your behalf, under your direction
234 // and control, on terms that prohibit them from making any copies of
235 // your copyrighted material outside their relationship with you.
236 //
237 // Conveying under any other circumstances is permitted solely under
238 // the conditions stated below. Sublicensing is not allowed; section 10
239 // makes it unnecessary.
240 //
241 // 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
242 //
243 // No covered work shall be deemed part of an effective technological
244 // measure under any applicable law fulfilling obligations under article
245 // 11 of the WIPO copyright treaty adopted on 20 December 1996, or
246 // similar laws prohibiting or restricting circumvention of such
247 // measures.
248 //
249 // When you convey a covered work, you waive any legal power to forbid
250 // circumvention of technological measures to the extent such circumvention
251 // is effected by exercising rights under this License with respect to
252 // the covered work, and you disclaim any intention to limit operation or
253 // modification of the work as a means of enforcing, against the work's
254 // users, your or third parties' legal rights to forbid circumvention of
255 // technological measures.
256 //
257 // 4. Conveying Verbatim Copies.
258 //
259 // You may convey verbatim copies of the Program's source code as you
260 // receive it, in any medium, provided that you conspicuously and
261 // appropriately publish on each copy an appropriate copyright notice;
262 // keep intact all notices stating that this License and any
263 // non-permissive terms added in accord with section 7 apply to the code;
264 // keep intact all notices of the absence of any warranty; and give all
265 // recipients a copy of this License along with the Program.
266 //
267 // You may charge any price or no price for each copy that you convey,
268 // and you may offer support or warranty protection for a fee.
269 //
270 // 5. Conveying Modified Source Versions.
271 //
272 // You may convey a work based on the Program, or the modifications to
273 // produce it from the Program, in the form of source code under the
274 // terms of section 4, provided that you also meet all of these conditions:
275 //
276 // a) The work must carry prominent notices stating that you modified
277 // it, and giving a relevant date.
278 //
279 // b) The work must carry prominent notices stating that it is
280 // released under this License and any conditions added under section
281 // 7. This requirement modifies the requirement in section 4 to
282 // "keep intact all notices".
283 //
284 // c) You must license the entire work, as a whole, under this
285 // License to anyone who comes into possession of a copy. This
286 // License will therefore apply, along with any applicable section 7
287 // additional terms, to the whole of the work, and all its parts,
288 // regardless of how they are packaged. This License gives no
289 // permission to license the work in any other way, but it does not
290 // invalidate such permission if you have separately received it.
291 //
292 // d) If the work has interactive user interfaces, each must display
293 // Appropriate Legal Notices; however, if the Program has interactive
294 // interfaces that do not display Appropriate Legal Notices, your
295 // work need not make them do so.
296 //
297 // A compilation of a covered work with other separate and independent
298 // works, which are not by their nature extensions of the covered work,
299 // and which are not combined with it such as to form a larger program,
300 // in or on a volume of a storage or distribution medium, is called an
301 // "aggregate" if the compilation and its resulting copyright are not
302 // used to limit the access or legal rights of the compilation's users
303 // beyond what the individual works permit. Inclusion of a covered work
304 // in an aggregate does not cause this License to apply to the other
305 // parts of the aggregate.
306 //
307 // 6. Conveying Non-Source Forms.
308 //
309 // You may convey a covered work in object code form under the terms
310 // of sections 4 and 5, provided that you also convey the
311 // machine-readable Corresponding Source under the terms of this License,
312 // in one of these ways:
313 //
314 // a) Convey the object code in, or embodied in, a physical product
315 // (including a physical distribution medium), accompanied by the
316 // Corresponding Source fixed on a durable physical medium
317 // customarily used for software interchange.
318 //
319 // b) Convey the object code in, or embodied in, a physical product
320 // (including a physical distribution medium), accompanied by a
321 // written offer, valid for at least three years and valid for as
322 // long as you offer spare parts or customer support for that product
323 // model, to give anyone who possesses the object code either (1) a
324 // copy of the Corresponding Source for all the software in the
325 // product that is covered by this License, on a durable physical
326 // medium customarily used for software interchange, for a price no
327 // more than your reasonable cost of physically performing this
328 // conveying of source, or (2) access to copy the
329 // Corresponding Source from a network server at no charge.
330 //
331 // c) Convey individual copies of the object code with a copy of the
332 // written offer to provide the Corresponding Source. This
333 // alternative is allowed only occasionally and noncommercially, and
334 // only if you received the object code with such an offer, in accord
335 // with subsection 6b.
336 //
337 // d) Convey the object code by offering access from a designated
338 // place (gratis or for a charge), and offer equivalent access to the
339 // Corresponding Source in the same way through the same place at no
340 // further charge. You need not require recipients to copy the
341 // Corresponding Source along with the object code. If the place to
342 // copy the object code is a network server, the Corresponding Source
343 // may be on a different server (operated by you or a third party)
344 // that supports equivalent copying facilities, provided you maintain
345 // clear directions next to the object code saying where to find the
346 // Corresponding Source. Regardless of what server hosts the
347 // Corresponding Source, you remain obligated to ensure that it is
348 // available for as long as needed to satisfy these requirements.
349 //
350 // e) Convey the object code using peer-to-peer transmission, provided
351 // you inform other peers where the object code and Corresponding
352 // Source of the work are being offered to the general public at no
353 // charge under subsection 6d.
354 //
355 // A separable portion of the object code, whose source code is excluded
356 // from the Corresponding Source as a System Library, need not be
357 // included in conveying the object code work.
358 //
359 // A "User Product" is either (1) a "consumer product", which means any
360 // tangible personal property which is normally used for personal, family,
361 // or household purposes, or (2) anything designed or sold for incorporation
362 // into a dwelling. In determining whether a product is a consumer product,
363 // doubtful cases shall be resolved in favor of coverage. For a particular
364 // product received by a particular user, "normally used" refers to a
365 // typical or common use of that class of product, regardless of the status
366 // of the particular user or of the way in which the particular user
367 // actually uses, or expects or is expected to use, the product. A product
368 // is a consumer product regardless of whether the product has substantial
369 // commercial, industrial or non-consumer uses, unless such uses represent
370 // the only significant mode of use of the product.
371 //
372 // "Installation Information" for a User Product means any methods,
373 // procedures, authorization keys, or other information required to install
374 // and execute modified versions of a covered work in that User Product from
375 // a modified version of its Corresponding Source. The information must
376 // suffice to ensure that the continued functioning of the modified object
377 // code is in no case prevented or interfered with solely because
378 // modification has been made.
379 //
380 // If you convey an object code work under this section in, or with, or
381 // specifically for use in, a User Product, and the conveying occurs as
382 // part of a transaction in which the right of possession and use of the
383 // User Product is transferred to the recipient in perpetuity or for a
384 // fixed term (regardless of how the transaction is characterized), the
385 // Corresponding Source conveyed under this section must be accompanied
386 // by the Installation Information. But this requirement does not apply
387 // if neither you nor any third party retains the ability to install
388 // modified object code on the User Product (for example, the work has
389 // been installed in ROM).
390 //
391 // The requirement to provide Installation Information does not include a
392 // requirement to continue to provide support service, warranty, or updates
393 // for a work that has been modified or installed by the recipient, or for
394 // the User Product in which it has been modified or installed. Access to a
395 // network may be denied when the modification itself materially and
396 // adversely affects the operation of the network or violates the rules and
397 // protocols for communication across the network.
398 //
399 // Corresponding Source conveyed, and Installation Information provided,
400 // in accord with this section must be in a format that is publicly
401 // documented (and with an implementation available to the public in
402 // source code form), and must require no special password or key for
403 // unpacking, reading or copying.
404 //
405 // 7. Additional Terms.
406 //
407 // "Additional permissions" are terms that supplement the terms of this
408 // License by making exceptions from one or more of its conditions.
409 // Additional permissions that are applicable to the entire Program shall
410 // be treated as though they were included in this License, to the extent
411 // that they are valid under applicable law. If additional permissions
412 // apply only to part of the Program, that part may be used separately
413 // under those permissions, but the entire Program remains governed by
414 // this License without regard to the additional permissions.
415 //
416 // When you convey a copy of a covered work, you may at your option
417 // remove any additional permissions from that copy, or from any part of
418 // it. (Additional permissions may be written to require their own
419 // removal in certain cases when you modify the work.) You may place
420 // additional permissions on material, added by you to a covered work,
421 // for which you have or can give appropriate copyright permission.
422 //
423 // Notwithstanding any other provision of this License, for material you
424 // add to a covered work, you may (if authorized by the copyright holders of
425 // that material) supplement the terms of this License with terms:
426 //
427 // a) Disclaiming warranty or limiting liability differently from the
428 // terms of sections 15 and 16 of this License; or
429 //
430 // b) Requiring preservation of specified reasonable legal notices or
431 // author attributions in that material or in the Appropriate Legal
432 // Notices displayed by works containing it; or
433 //
434 // c) Prohibiting misrepresentation of the origin of that material, or
435 // requiring that modified versions of such material be marked in
436 // reasonable ways as different from the original version; or
437 //
438 // d) Limiting the use for publicity purposes of names of licensors or
439 // authors of the material; or
440 //
441 // e) Declining to grant rights under trademark law for use of some
442 // trade names, trademarks, or service marks; or
443 //
444 // f) Requiring indemnification of licensors and authors of that
445 // material by anyone who conveys the material (or modified versions of
446 // it) with contractual assumptions of liability to the recipient, for
447 // any liability that these contractual assumptions directly impose on
448 // those licensors and authors.
449 //
450 // All other non-permissive additional terms are considered "further
451 // restrictions" within the meaning of section 10. If the Program as you
452 // received it, or any part of it, contains a notice stating that it is
453 // governed by this License along with a term that is a further
454 // restriction, you may remove that term. If a license document contains
455 // a further restriction but permits relicensing or conveying under this
456 // License, you may add to a covered work material governed by the terms
457 // of that license document, provided that the further restriction does
458 // not survive such relicensing or conveying.
459 //
460 // If you add terms to a covered work in accord with this section, you
461 // must place, in the relevant source files, a statement of the
462 // additional terms that apply to those files, or a notice indicating
463 // where to find the applicable terms.
464 //
465 // Additional terms, permissive or non-permissive, may be stated in the
466 // form of a separately written license, or stated as exceptions;
467 // the above requirements apply either way.
468 //
469 // 8. Termination.
470 //
471 // You may not propagate or modify a covered work except as expressly
472 // provided under this License. Any attempt otherwise to propagate or
473 // modify it is void, and will automatically terminate your rights under
474 // this License (including any patent licenses granted under the third
475 // paragraph of section 11).
476 //
477 // However, if you cease all violation of this License, then your
478 // license from a particular copyright holder is reinstated (a)
479 // provisionally, unless and until the copyright holder explicitly and
480 // finally terminates your license, and (b) permanently, if the copyright
481 // holder fails to notify you of the violation by some reasonable means
482 // prior to 60 days after the cessation.
483 //
484 // Moreover, your license from a particular copyright holder is
485 // reinstated permanently if the copyright holder notifies you of the
486 // violation by some reasonable means, this is the first time you have
487 // received notice of violation of this License (for any work) from that
488 // copyright holder, and you cure the violation prior to 30 days after
489 // your receipt of the notice.
490 //
491 // Termination of your rights under this section does not terminate the
492 // licenses of parties who have received copies or rights from you under
493 // this License. If your rights have been terminated and not permanently
494 // reinstated, you do not qualify to receive new licenses for the same
495 // material under section 10.
496 //
497 // 9. Acceptance Not Required for Having Copies.
498 //
499 // You are not required to accept this License in order to receive or
500 // run a copy of the Program. Ancillary propagation of a covered work
501 // occurring solely as a consequence of using peer-to-peer transmission
502 // to receive a copy likewise does not require acceptance. However,
503 // nothing other than this License grants you permission to propagate or
504 // modify any covered work. These actions infringe copyright if you do
505 // not accept this License. Therefore, by modifying or propagating a
506 // covered work, you indicate your acceptance of this License to do so.
507 //
508 // 10. Automatic Licensing of Downstream Recipients.
509 //
510 // Each time you convey a covered work, the recipient automatically
511 // receives a license from the original licensors, to run, modify and
512 // propagate that work, subject to this License. You are not responsible
513 // for enforcing compliance by third parties with this License.
514 //
515 // An "entity transaction" is a transaction transferring control of an
516 // organization, or substantially all assets of one, or subdividing an
517 // organization, or merging organizations. If propagation of a covered
518 // work results from an entity transaction, each party to that
519 // transaction who receives a copy of the work also receives whatever
520 // licenses to the work the party's predecessor in interest had or could
521 // give under the previous paragraph, plus a right to possession of the
522 // Corresponding Source of the work from the predecessor in interest, if
523 // the predecessor has it or can get it with reasonable efforts.
524 //
525 // You may not impose any further restrictions on the exercise of the
526 // rights granted or affirmed under this License. For example, you may
527 // not impose a license fee, royalty, or other charge for exercise of
528 // rights granted under this License, and you may not initiate litigation
529 // (including a cross-claim or counterclaim in a lawsuit) alleging that
530 // any patent claim is infringed by making, using, selling, offering for
531 // sale, or importing the Program or any portion of it.
532 //
533 // 11. Patents.
534 //
535 // A "contributor" is a copyright holder who authorizes use under this
536 // License of the Program or a work on which the Program is based. The
537 // work thus licensed is called the contributor's "contributor version".
538 //
539 // A contributor's "essential patent claims" are all patent claims
540 // owned or controlled by the contributor, whether already acquired or
541 // hereafter acquired, that would be infringed by some manner, permitted
542 // by this License, of making, using, or selling its contributor version,
543 // but do not include claims that would be infringed only as a
544 // consequence of further modification of the contributor version. For
545 // purposes of this definition, "control" includes the right to grant
546 // patent sublicenses in a manner consistent with the requirements of
547 // this License.
548 //
549 // Each contributor grants you a non-exclusive, worldwide, royalty-free
550 // patent license under the contributor's essential patent claims, to
551 // make, use, sell, offer for sale, import and otherwise run, modify and
552 // propagate the contents of its contributor version.
553 //
554 // In the following three paragraphs, a "patent license" is any express
555 // agreement or commitment, however denominated, not to enforce a patent
556 // (such as an express permission to practice a patent or covenant not to
557 // sue for patent infringement). To "grant" such a patent license to a
558 // party means to make such an agreement or commitment not to enforce a
559 // patent against the party.
560 //
561 // If you convey a covered work, knowingly relying on a patent license,
562 // and the Corresponding Source of the work is not available for anyone
563 // to copy, free of charge and under the terms of this License, through a
564 // publicly available network server or other readily accessible means,
565 // then you must either (1) cause the Corresponding Source to be so
566 // available, or (2) arrange to deprive yourself of the benefit of the
567 // patent license for this particular work, or (3) arrange, in a manner
568 // consistent with the requirements of this License, to extend the patent
569 // license to downstream recipients. "Knowingly relying" means you have
570 // actual knowledge that, but for the patent license, your conveying the
571 // covered work in a country, or your recipient's use of the covered work
572 // in a country, would infringe one or more identifiable patents in that
573 // country that you have reason to believe are valid.
574 //
575 // If, pursuant to or in connection with a single transaction or
576 // arrangement, you convey, or propagate by procuring conveyance of, a
577 // covered work, and grant a patent license to some of the parties
578 // receiving the covered work authorizing them to use, propagate, modify
579 // or convey a specific copy of the covered work, then the patent license
580 // you grant is automatically extended to all recipients of the covered
581 // work and works based on it.
582 //
583 // A patent license is "discriminatory" if it does not include within
584 // the scope of its coverage, prohibits the exercise of, or is
585 // conditioned on the non-exercise of one or more of the rights that are
586 // specifically granted under this License. You may not convey a covered
587 // work if you are a party to an arrangement with a third party that is
588 // in the business of distributing software, under which you make payment
589 // to the third party based on the extent of your activity of conveying
590 // the work, and under which the third party grants, to any of the
591 // parties who would receive the covered work from you, a discriminatory
592 // patent license (a) in connection with copies of the covered work
593 // conveyed by you (or copies made from those copies), or (b) primarily
594 // for and in connection with specific products or compilations that
595 // contain the covered work, unless you entered into that arrangement,
596 // or that patent license was granted, prior to 28 March 2007.
597 //
598 // Nothing in this License shall be construed as excluding or limiting
599 // any implied license or other defenses to infringement that may
600 // otherwise be available to you under applicable patent law.
601 //
602 // 12. No Surrender of Others' Freedom.
603 //
604 // If conditions are imposed on you (whether by court order, agreement or
605 // otherwise) that contradict the conditions of this License, they do not
606 // excuse you from the conditions of this License. If you cannot convey a
607 // covered work so as to satisfy simultaneously your obligations under this
608 // License and any other pertinent obligations, then as a consequence you may
609 // not convey it at all. For example, if you agree to terms that obligate you
610 // to collect a royalty for further conveying from those to whom you convey
611 // the Program, the only way you could satisfy both those terms and this
612 // License would be to refrain entirely from conveying the Program.
613 //
614 // 13. Use with the GNU Affero General Public License.
615 //
616 // Notwithstanding any other provision of this License, you have
617 // permission to link or combine any covered work with a work licensed
618 // under version 3 of the GNU Affero General Public License into a single
619 // combined work, and to convey the resulting work. The terms of this
620 // License will continue to apply to the part which is the covered work,
621 // but the special requirements of the GNU Affero General Public License,
622 // section 13, concerning interaction through a network will apply to the
623 // combination as such.
624 //
625 // 14. Revised Versions of this License.
626 //
627 // The Free Software Foundation may publish revised and/or new versions of
628 // the GNU General Public License from time to time. Such new versions will
629 // be similar in spirit to the present version, but may differ in detail to
630 // address new problems or concerns.
631 //
632 // Each version is given a distinguishing version number. If the
633 // Program specifies that a certain numbered version of the GNU General
634 // Public License "or any later version" applies to it, you have the
635 // option of following the terms and conditions either of that numbered
636 // version or of any later version published by the Free Software
637 // Foundation. If the Program does not specify a version number of the
638 // GNU General Public License, you may choose any version ever published
639 // by the Free Software Foundation.
640 //
641 // If the Program specifies that a proxy can decide which future
642 // versions of the GNU General Public License can be used, that proxy's
643 // public statement of acceptance of a version permanently authorizes you
644 // to choose that version for the Program.
645 //
646 // Later license versions may give you additional or different
647 // permissions. However, no additional obligations are imposed on any
648 // author or copyright holder as a result of your choosing to follow a
649 // later version.
650 //
651 // 15. Disclaimer of Warranty.
652 //
653 // THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
654 // APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
655 // HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
656 // OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
657 // THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
658 // PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
659 // IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
660 // ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
661 //
662 // 16. Limitation of Liability.
663 //
664 // IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
665 // WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
666 // THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
667 // GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
668 // USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
669 // DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
670 // PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
671 // EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
672 // SUCH DAMAGES.
673 //
674 // 17. Interpretation of Sections 15 and 16.
675 //
676 // If the disclaimer of warranty and limitation of liability provided
677 // above cannot be given local legal effect according to their terms,
678 // reviewing courts shall apply local law that most closely approximates
679 // an absolute waiver of all civil liability in connection with the
680 // Program, unless a warranty or assumption of liability accompanies a
681 // copy of the Program in return for a fee.
682 //--------------------------------------------------------------------------------
683 //C O N F I G U R A T I O N
684 //--------------------------------------------------------------------------------
685 //Configuration switch combinations were not tested. E-mail me any program
686 //corrections (dashley@gmail.com).
687 define ("CFG_PROGNAME", "filenames_canonize.php");
688 //Number of characters per line preferred for console output.
689 define ("CFG_CONSOLE_STD_LINE_LEN", 78);
690 //Number of characters per line preferred for console output.
691 define (
692 "CFG_FILENAME_SUBSTITUTION_MAP",
693 ".." //Dots unchanged.
694 .
695 "aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz" //LC letters unchanged.
696 .
697 "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz" //UC converted to LC.
698 .
699 "00112233445566778899" //Digits unchanged.
700 .
701 "-_" //Hyphens become underscores,
702 //and anything unmapped
703 //becomes an underscore.
704 );
705 //Pairs of characters, the first is the character to match and the second
706 //is the replacement. Any characters not found in the map will be replaced
707 //by the last character of the constant.
708 //--------------------------------------------------------------------------------
709 //Repeats a character to the console output a certain number of times.
710 function rep_char_con($c, $n)
711 {
712 while ($n--)
713 echo $c;
714 }
715 //--------------------------------------------------------------------------------
716 //Writes a standard thick horizontal line to the console.
717 function hor_line_thick()
718 {
719 rep_char_con("=", CFG_CONSOLE_STD_LINE_LEN);
720 echo "\n";
721 }
722 //--------------------------------------------------------------------------------
723 //Writes a standard thin horizontal line to the console.
724 function hor_line_thin()
725 {
726 rep_char_con("-", CFG_CONSOLE_STD_LINE_LEN);
727 echo "\n";
728 }
729 //--------------------------------------------------------------------------------
730 //Returns an array of all files in the working directory.
731 //If no files can be found, returns FALSE.
732 function get_file_names_in_dir()
733 {
734 //Get directory list.
735 $rv = scandir (".");
736
737 //If the list is empty, something went wrong. Return FALSE.
738 if ($rv === FALSE)
739 return FALSE;
740
741 return $rv;
742 }
743 //--------------------------------------------------------------------------------
744 //Canonizes a filename by performing character substitutions.
745 function canonize($in_fname)
746 {
747 //Well, the algorithm below is O(N^2) or worse ... I knew there was a reason
748 //that computers get faster every year.
749 $out_fname = "";
750
751 for ($i = 0; $i < strlen($in_fname); $i++)
752 {
753 $map = CFG_FILENAME_SUBSTITUTION_MAP;
754 $c = substr($in_fname, $i, 1);
755
756 $sub_made = FALSE;
757 while (strlen($map) >= 2)
758 {
759 if (substr($map, 0, 1) == $c)
760 {
761 $c = substr($map, 1, 1);
762 $sub_made = TRUE;
763 break;
764 }
765 else
766 {
767 $map = substr($map, 2);
768 }
769 }
770
771 if (! $sub_made)
772 {
773 $c = substr(CFG_FILENAME_SUBSTITUTION_MAP, strlen(CFG_FILENAME_SUBSTITUTION_MAP) - 1, 1);
774 //echo "Default sub made: " . $c . "\n";
775 }
776
777 $out_fname = $out_fname . $c;
778 }
779
780 return $out_fname;
781 }
782 //--------------------------------------------------------------------------------
783 //Write introductory message.
784 hor_line_thick();
785 echo CFG_PROGNAME . " Copyright (C) 2015 David T. Ashley\n";
786 echo "This program comes with ABSOLUTELY NO WARRANTY; and is licensed under\n";
787 echo "the GNU General Public License, Version 3. A copy of this license is\n";
788 echo "provided in the source code of this program.\n";
789 hor_line_thin();
790 //-----------------------------------------------------------------------------
791 //Get and emit the names of everything in the directory.
792 $file_list = get_file_names_in_dir();
793 if ($file_list === FALSE)
794 {
795 echo "List of files from PHP function scandir() is empty.\n";
796 echo "Serious internal error, or nothing to do. Script cannot continue.\n";
797 hor_line_thick();
798 exit(1);
799 }
800 else
801 {
802 echo "Files in working directory (unsorted, unfiltered, "
803 .
804 count($file_list)
805 .
806 " files):\n";
807 for ($i = 0; $i < count($file_list); $i++)
808 echo " " . sprintf("[%5d]", $i) . " " . $file_list[$i] . "\n";
809 }
810 hor_line_thin();
811 //-----------------------------------------------------------------------------
812 //Remove everything that is not a file we can rename.
813 $temp_list = $file_list;
814 unset($file_list);
815 $n = 0;
816 for ($i = 0; $i < count($temp_list); $i++)
817 {
818 //echo "Checking " . $temp_list[$i] . "\n";
819
820 if (strcmp($temp_list[$i], ".") == 0)
821 {
822 //. entry, not a file.
823 }
824 else if (strcmp($temp_list[$i], "..") == 0)
825 {
826 //.. entry, not a file.
827 }
828 else if (is_file($temp_list[$i]))
829 {
830 //This is a regular file.
831 $file_list[] = $temp_list[$i];
832 $n++;
833 }
834 }
835
836 if ($n == 0)
837 $file_list = FALSE;
838
839 unset($n);
840 unset($temp_list);
841 //-----------------------------------------------------------------------------
842 //If there is nothing to do, end the script.
843 if ($file_list === FALSE)
844 {
845 echo "No files to rename.\n";
846 hor_line_thick();
847 exit(0);
848 }
849 //-----------------------------------------------------------------------------
850 //Sort the list. This is a non-event. The only rationale for sorting is that
851 //it ensures that the same set of files will be processed in the same order,
852 //regardless of the order provided by the underlying OS internals.
853 sort($file_list);
854 //-----------------------------------------------------------------------------
855 //Emit the names we now have.
856 echo "Files in working directory (directory entries removed, sorted, "
857 .
858 count($file_list)
859 .
860 " files):\n";
861 for ($i = 0; $i < count($file_list); $i++)
862 echo " " . sprintf("[%5d]", $i) . " " . $file_list[$i] . "\n";
863 hor_line_thin();
864 //-----------------------------------------------------------------------------
865 //For each file in the directory, form a filename which would be the result
866 //of applying character substitutions and any other corrections.
867 for ($i = 0; $i < count($file_list); $i++)
868 {
869 $rename_target[$i] = canonize($file_list[$i]);
870
871 //echo $file_list[$i] . " " . $rename_target[$i] . "\n";
872 }
873 //-----------------------------------------------------------------------------
874 //In all those cases where the name of the file as we would like it to be is
875 //different from the name of the file as it exists, rename it. Watch for
876 //collisions, because renaming on top of a file represents the loss of
877 //data.
878 $n = 0;
879 for ($i = 0; $i < count($file_list); $i++)
880 {
881 if (strcmp($file_list[$i], $rename_target[$i]) != 0)
882 $n++;
883 }
884
885 if ($n == 0)
886 {
887 echo "No files need to be renamed.\n";
888 hor_line_thin();
889 }
890 else
891 {
892 echo "Renaming " . $n . " file(s).\n";
893 hor_line_thin();
894
895 $count = 0;
896 for ($i = 0; $i < count($file_list); $i++)
897 {
898 if (strcmp($file_list[$i], $rename_target[$i]) != 0)
899 {
900 $count++;
901 echo "[" . sprintf("%5d/%5d", $count, $n) . "]"
902 .
903 " Renaming " . $file_list[$i] . " to " . $rename_target[$i] . " ... ";
904 if (file_exists($rename_target[$i]))
905 {
906 echo "Aborted.\nRename target already exists.\n";
907 echo "Choose non-colliding filenames and run this script again.\n";
908 echo "Script terminating.\n";
909 hor_line_thin();
910 exit(1);
911 }
912
913 $result = rename($file_list[$i], $rename_target[$i]);
914 if ($result)
915 {
916 echo "Done.\n";
917 hor_line_thin();
918 }
919 else
920 {
921 echo "Failed.\nScript must terminate.\n";
922 hor_line_thick();
923 exit(1);
924 }
925 }
926 }
927 }
928 //-----------------------------------------------------------------------------
929 echo CFG_PROGNAME . " execution ends.\n";
930 hor_line_thick();
931 //--------------------------------------------------------------------------------
932 //End of File
933 //--------------------------------------------------------------------------------
934 ?>

Properties

Name Value
svn:executable *

dashley@gmail.com
ViewVC Help
Powered by ViewVC 1.1.25