Completed
Push — master ( bb3e3e...12ae4c )
by Ed
8s
created

StaticPagesQueue::remove_duplicates()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 5
Bugs 0 Features 1
Metric Value
c 5
b 0
f 1
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
/**
3
 * This class responsibility is twofold:
4
 * 1) Holding the data for a prioritized queue of URLs that needs to be static cached
5
 * 2) Interaction with that queue
6
 *
7
 * @TODO: would be good to refactor this queue to hold not only URLSegment, but also ClassName and ID of the
8
 * associated object (or any other metadata). This would allow FilesystemPublisher::publishPages and others
9
 * to stop having to smuggle the metadata within the URL (see URLArrayData::get_object).
10
 *
11
 */
12
class StaticPagesQueue extends DataObject {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
13
14
	/**
15
	 *
16
	 * @var array
17
	 */
18
	public static $create_table_options = array(
19
		'MySQLDatabase' => 'ENGINE=InnoDB'
20
	);
21
22
	/**
23
	 *
24
	 * @var array
25
	 */
26
	public static $db = array(
27
		'Priority' => 'Int',
28
		'URLSegment' => 'Varchar(255)',
29
		'Freshness' => "Enum('stale, regenerating, error', 'stale')"
30
	);
31
32
	/**
33
	 *
34
	 * @var array
35
	 */
36
	public static $defaults = array(
37
		"Priority" => 3
38
	);
39
40
	/**
41
	 *
42
	 * @var array
43
	 */
44
	public static $default_sort = "\"Priority\"";
45
46
	/**
47
	 * Sets database indexes
48
	 *
49
	 * @var array
50
	 */
51
	public static $indexes = array(
52
		'freshness_priority_created' => '(Freshness, Priority, Created)',
53
	);
54
55
	/**
56
	 *
57
	 * @var boolean
58
	 */
59
	private static $disable_mysql_locks = false;
0 ignored issues
show
Unused Code introduced by
The property $disable_mysql_locks is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
60
61
	/**
62
	 *
63
	 * @var boolean
64
	 */
65
	private static $realtime = false;
0 ignored issues
show
Unused Code introduced by
The property $realtime is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
66
67
	/**
68
	 *
69
	 * @var int
70
	 */
71
	protected static $minutes_until_force_regeneration = 1;
72
73
	/**
74
	 *
75
	 * @var array
76
	 */
77
	protected static $insert_statements = array();
78
79
	/**
80
	 *
81
	 * @var array
82
	 */
83
	protected static $urls = array();
84
	
85
	/**
86
	 *
87
	 * @return bool
0 ignored issues
show
Documentation introduced by
Should the return type not be array|integer|double|string|boolean?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
88
	 */
89
	public static function is_realtime() {
90
		return Config::inst()->get('StaticPagesQueue','realtime');
91
	}
92
93
	/**
94
	 *
95
	 * @param type $priority
96
	 * @param type $URLSegment
97
	 * @return type
0 ignored issues
show
Documentation introduced by
Should the return type not be type|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
98
	 */
99
	public static function add_to_queue($priority, $URLSegment) {
100
		$now = date("Y-m-d H:i:s");
101
		self::$insert_statements[$URLSegment] = '(\''.$now.'\',\''.$now.'\', \''.Convert::raw2sql($priority).'\',\''.Convert::raw2sql($URLSegment).'\')';
102
		self::$urls[md5($URLSegment)] = $URLSegment;
103
	}
104
105
		/**
106
	 * This will push all the currently cached insert statements to be pushed 
107
	 * into the database
108
	 *
109
	 * @return void
110
	 */
111
	public static function push_urls_to_db() {
112
		foreach(self::$insert_statements as $stmt) {
113
			$insertSQL = 'INSERT INTO "StaticPagesQueue" ("Created", "LastEdited", "Priority", "URLSegment") VALUES ' . $stmt;
114
			DB::query($insertSQL);
115
		}
116
		self::remove_old_cache(self::$urls);
117
		// Flush the cache so DataObject::get works correctly
118
		if(!empty(self::$insert_statements) && DB::affectedRows()) {
119
			singleton(__CLASS__)->flushCache();
120
		}
121
		self::$insert_statements = array();
122
	}
123
	
124
	/**
125
	 * Remove an object by the url
126
	 *
127
	 * @param string $URLSegment
128
	 * @return bool - if there was an queue item removed
129
	 *
130
	 */
131
	public static function delete_by_link($URLSegment) {
132
		$object = self::get_by_link($URLSegment);
133
		if(!$object) return false;
134
135
		$object->delete();
136
		unset($object);
137
		return true;
138
	}
139
	
140
	/**
141
	 * Update the queue with the information that this url renders an error somehow
142
	 *
143
	 * @param string $url
144
	 */
145
	public static function has_error( $url ) {
146
		if(!$url) return;
147
		
148
		$existingObject = self::get_by_link($url);
149
		$existingObject->Freshness = 'error';
150
		$existingObject->write();
151
	}
152
153
	/**
154
	 * Returns a single queue object according to a particular priority and freshness measure.
155
	 * This method removes any duplicates and makes the object as "regenerating", so other calls to this method
156
	 * don't grab the same object.
157
	 * If we are using MySQLDatabase with InnoDB, we do row-level locking when updating the dataobject to allow for
158
	 * distributed cache rebuilds
159
	 * @static
160
	 * @param $freshness
161
	 * @param $sortOrder
162
	 */
163
	protected static function get_queue_object($freshness, $interval = null, $sortOrder = array('Priority'=>'DESC', 'ID'=>'ASC')) {
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
164
		$className = __CLASS__;
165
		$queueObject = null;
166
		$filterQuery = array("Freshness" => $freshness);
167
		if ($interval) $filterQuery["LastEdited:LessThan"] = $interval;
168
169
		$query = self::get();
170
		if ($query->Count() > 0) {
171
			$offset = 0;
172
			$filteredQuery = $query->filter($filterQuery)->sort($sortOrder);
173
174
			if ($filteredQuery->Count() > 0) {
175
				if (!self::config()->disable_mysql_locks && DB::getConn() instanceof MySQLDatabase) {   //locking currently only works on MySQL
0 ignored issues
show
Bug introduced by
The class MySQLDatabase does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
176
177
					do {
178
						$queueObject = $filteredQuery->limit(1, $offset)->first();   //get first item
179
180
						if ($queueObject) $lockName = md5($queueObject->URLSegment . $className);
181
						//try to locking the item's URL, keep trying new URLs until we find one that is free to lock
182
						$offset++;
183
					} while($queueObject && !LockMySQL::isFreeToLock($lockName));
0 ignored issues
show
Bug introduced by
The variable $lockName does not seem to be defined for all execution paths leading up to this point.

If you define a variable conditionally, it can happen that it is not defined for all execution paths.

Let’s take a look at an example:

function myFunction($a) {
    switch ($a) {
        case 'foo':
            $x = 1;
            break;

        case 'bar':
            $x = 2;
            break;
    }

    // $x is potentially undefined here.
    echo $x;
}

In the above example, the variable $x is defined if you pass “foo” or “bar” as argument for $a. However, since the switch statement has no default case statement, if you pass any other value, the variable $x would be undefined.

Available Fixes

  1. Check for existence of the variable explicitly:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        if (isset($x)) { // Make sure it's always set.
            echo $x;
        }
    }
    
  2. Define a default value for the variable:

    function myFunction($a) {
        $x = ''; // Set a default which gets overridden for certain paths.
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
        }
    
        echo $x;
    }
    
  3. Add a value for the missing path:

    function myFunction($a) {
        switch ($a) {
            case 'foo':
                $x = 1;
                break;
    
            case 'bar':
                $x = 2;
                break;
    
            // We add support for the missing case.
            default:
                $x = '';
                break;
        }
    
        echo $x;
    }
    
Loading history...
184
185
					if ($queueObject) {
186
						$lockSuccess = LockMySQL::getLock($lockName);  //acquire a lock with the URL of the queue item we have just fetched
187
						if ($lockSuccess) {
188
							self::remove_duplicates($queueObject->ID);  //remove any duplicates
189
							self::mark_as_regenerating($queueObject);   //mark as regenerating so nothing else grabs it
190
							LockMySQL::releaseLock($lockName);			//return the object and release the lock
191
						}
192
					}
193
				} else {
194
					$queueObject = $filteredQuery->first();
195
					self::remove_duplicates($queueObject->ID);
196
					self::mark_as_regenerating($queueObject);
197
				}
198
			}
199
		}
200
201
		return $queueObject;    //return the object or null
202
	}
203
204
	/**
205
	 * Finds the next most prioritized url that needs recaching
206
	 *
207
	 * @return string
208
	 */
209
	public static function get_next_url() {
210
		$object = self::get_queue_object('stale');
211
		if($object) return $object->URLSegment;
212
213
		$interval = date('Y-m-d H:i:s', strtotime('-'.self::$minutes_until_force_regeneration.' minutes'));
214
215
		// Find URLs that has been stuck in regeneration
216
		$object = self::get_queue_object('regenerating', $interval);
217
		if($object) return $object->URLSegment;
218
219
		// Find URLs that is erronous and might work now (flush issues etc)
220
		$object = self::get_queue_object('error', $interval);
221
		if($object) return $object->URLSegment;
222
223
		return '';
224
	}
225
226
	/**
227
	 * Removes the .html fresh copy of the cache.
228
	 * Keeps the *.stale.html copy in place,
229
	 * in order to notify the user of the stale content.
230
	 *
231
	 * @param array $URLSegments
232
	 */
233
	protected static function remove_old_cache( array $URLSegments ) {
234
		$publisher = singleton('SiteTree')->getExtensionInstance('FilesystemPublisher');
235
		if ($publisher) {
236
			$paths = $publisher->urlsToPaths($URLSegments);
237
			foreach($paths as $absolutePath) {
238
239
				if(!file_exists($publisher->getDestDir().'/'.$absolutePath)) {
240
					continue;
241
				}
242
243
				unlink($publisher->getDestDir().'/'.$absolutePath);
244
			}
245
		}
246
	}
247
248
	/**
249
	 * Mark this current StaticPagesQueue as a work in progress
250
	 *
251
	 * @param StaticPagesQueue $object 
252
	 */
253
	protected static function mark_as_regenerating(StaticPagesQueue $object) {
254
		$now = date('Y-m-d H:i:s');
255
		DB::query('UPDATE "StaticPagesQueue" SET "LastEdited" = \''.$now.'\', "Freshness"=\'regenerating\' WHERE "ID" = '.$object->ID);
256
		singleton(__CLASS__)->flushCache();
257
	}
258
259
	/**
260
	 * Removes all duplicates that has the same URLSegment as $ID
261
	 *
262
	 * @param int $ID - ID of the object whose duplicates we want to remove
263
	 * @return void
0 ignored issues
show
Documentation introduced by
Should the return type not be integer|null?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
264
	 */
265
	static function remove_duplicates( $ID ) {
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
266
		$obj = DataObject::get_by_id('StaticPagesQueue', $ID);
267
		if(!$obj) return 0;
268
		DB::query(
269
			sprintf('DELETE FROM "StaticPagesQueue" WHERE "URLSegment" = \'%s\' AND "ID" != %d', $obj->URLSegment, (int)$ID)
270
		);
271
	}
272
273
	/**
274
	 *
275
	 * @param string $url
276
	 * @param bool $onlyStale - Get only stale entries
0 ignored issues
show
Bug introduced by
There is no parameter named $onlyStale. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
277
	 * @return DataObject || false - The first item matching the query
0 ignored issues
show
Documentation introduced by
Should the return type not be false|StaticPagesQueue?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
278
	 */
279
	protected static function get_by_link($url) {
280
		$filter = '"URLSegment" = \''.Convert::raw2sql($url).'\'';
281
		$res = DB::query('SELECT * FROM "StaticPagesQueue" WHERE '.$filter.' LIMIT 1;');
282
		if(!$res->numRecords()){
283
			return false;
284
		}
285
		return new StaticPagesQueue($res->first());
286
	}
287
}
288