File: RewriteCheckConflictTask.class

package info (click to toggle)
gallery2 2.1.2-2.0.etch.1
  • links: PTS
  • area: main
  • in suites: etch
  • size: 46,156 kB
  • ctags: 5,615
  • sloc: php: 75,481; xml: 1,683; sh: 731; perl: 479; sql: 202; makefile: 54
file content (138 lines) | stat: -rw-r--r-- 4,362 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/*
 * $RCSfile: RewriteCheckConflictTask.class,v $
 *
 * Gallery - a web based photo album viewer and editor
 * Copyright (C) 2000-2006 Bharat Mediratta
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or (at
 * your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA  02110-1301, USA.
 */

/**
 * @version $Revision: 1.11 $ $Date: 2006/02/12 20:09:25 $
 * @package Rewrite
 * @subpackage Classes
 * @author Douglas Cau <douglas@cau.se>
 */

GalleryCoreApi::requireOnce('modules/core/AdminMaintenance.inc');

/**
 * This is a MaintenanceTask that will check the short style URL for a
 * file system conflicts through all Gallery items.
 *
 * @package Rewrite
 * @subpackage Classes
 *
 */
class RewriteCheckConflictTask extends MaintenanceTask {

    /**
     * @see MaintenanceTask::getInfo()
     */
    function getInfo() {
	global $gallery;

	return array(
	    'l10Domain' => 'modules_rewrite',
	    'title' => $gallery->i18n('Check short style URLs for filesystem conflicts'),
	    'description' =>
	    $gallery->i18n('This will go through all your Gallery items and check if the short ' .
			   'style URL links to an existing file or directory on your webserver.'));
    }

    /**
     * @see MaintenanceTask::run()
     */
    function run() {
	global $gallery;
	$templateAdapter =& $gallery->getTemplateAdapter();
	$urlGenerator =& $gallery->getUrlGenerator();
	$platform =& $gallery->getPlatform();
	$pluginBaseDirs = GalleryCoreApi::getPluginBaseDirs();
	$baseDir = rtrim($pluginBaseDirs['base'], '/');

	list ($ret, $module) = GalleryCoreApi::loadPlugin('module', 'rewrite');
	if ($ret) {
	    return array($ret->wrap(__FILE__, __LINE__), null, null);
	}
	$message = $module->translate('Processing...');

	$ret = $templateAdapter->updateProgressBar('', $message, '');
	if ($ret) {
	    return array($ret->wrap(__FILE__, __LINE__), null, null);
	}

	list ($ret, $results) =
	    $gallery->search('SELECT [Item::id] FROM [Item] ' .
			     'ORDER BY [Item::id]');
	if ($ret) {
	    return array($ret->wrap(__FILE__, __LINE__), null, null);
	}

	$i = 0;
	$badPath = 0;
	$status = array('');
	$total = $results->resultCount();
	while ($result = $results->nextResult()) {
	    $params = array('view' => 'core.ShowItem',
			    'itemId' => (int)$result[0]);
	    $path = substr($urlGenerator->generateUrl($params, array('forceFullUrl' => true)),
		    strlen($urlGenerator->getCurrentUrlDir()) - 1);
	    if (($query = strpos($path, '?')) !== false) {
		$path = substr($path, 0, $query);
	    }

	    if ($platform->file_exists($baseDir . $path) && $path != ('/' . GALLERY_MAIN_PHP)) {
		list ($ret, $item) = GalleryCoreApi::loadEntitiesById((int)$result[0]);
		if ($ret) {
		    return array($ret->wrap(__FILE__, __LINE__), null, null);
		}
		$params = array('view' => 'core.ItemAdmin',
				'subView' => 'core.ItemEdit',
				'editPlugin' => 'ItemEditItem',
				'itemId' => (int)$result[0]);

		$status[++$badPath] = $module->translate('Bad path:') .
			sprintf(' <a href="%s">%s</a>: %s',
				$urlGenerator->generateUrl($params),
				$item->getTitle(),
				$urlGenerator->getCurrentUrlDir() . substr($path, 1));
	    }

	    if (++$i % 5 == 0 || $i == $total) {
		$message = $module->translate(
		    array('text' => 'Checking item %d of %d', 'arg1' => $i, 'arg2' => $total));
		$ret = $templateAdapter->updateProgressBar($message, '', $i / $total);
		if ($ret) {
		    return array($ret->wrap(__FILE__, __LINE__), null, null);
		}
	    }
	}

	$status[0] = $module->translate(array('text' => 'Checked %d items and found %d conflicts',
					   'arg1' => $total, 'arg2' => $badPath));
	return array(null, true, $status);
    }


    /**
     * @see MaintenanceTask::requiresProgressBar()
     */
    function requiresProgressBar() {
	return true;
    }
}
?>