Download P4PHP from the Perforce web site downloads page. You must build the interface from source, as described in the release notes packaged with P4PHP.
The following example illustrates the basic structure of a P4PHP script. The example establishes a connection, issues a command, and tests for errors resulting from the command.
$p4 = new P4();$p4->port = "1666"; $p4->user = "fred"; $p4->client = "fred-ws";
try { $p4->connect(); $info = $p4->run("info"); foreach ($info[0] as $key => $val) { print "$key = $val\n"; } $p4->run("edit", "file.txt"); $p4->disconnect(); } catch (P4_Exception $e) { print $e->getMessage() . "\n"; foreach ($p4->errors as $error) { print "Error: $error\n"; } }
|
// Convert client spec into an array
$client = $p4->fetch_client("-t", $template); $client['Root'] = $client_root; $p4->save_client($client); $p4->run_sync();
} catch (P4_Exception $e) { // If any errors occur, we'll jump in here. Just log them // and raise the exception up to the higher level }
|
$p4 = new P4();$p4->user = "Sven"; $p4->password = "my_password"; $p4->connect(); $p4->run_login();
|
$p4 = new P4();$p4->user = "Sven"; $p4->password = "MyOldPassword"; $p4->connect();
|
The following table lists properties of the class P4 in P4PHP. The properties are readable and writable unless indicated otherwise. The properties can be strings, arrays, or integers.
|
|
|
|
|
|
|
P4CLIENT, the name of the client workspace to use.
|
|
|
|
|
|
|
|
P4HOST, the name of the host used
|
|
|
|
|
|
|
|
|
|
|
|
P4PASSWD, the password used.
|
|
P4PORT, the port used for the connection
|
|
|
|
|
|
|
|
P4TICKETS, the ticket file location used
|
|
P4USER, the user under which the connection is run
|
|
|
|
|
Exception class. Instances of this class are raised when errors and/or (depending on the exception_level setting) warnings are returned by the server. The exception contains the errors in the form of a string.
P4_Exception extends the standard PHP Exception class.
Container class returned by P4::run_filelog(). Contains the name of the depot file and an array of
P4_Revision objects.
Each P4 object represents a connection to the Perforce Server, and multiple commands may be executed (serially) over a single connection (which of itself can result in substantially improved performance if executing long sequences of Perforce commands).
Contains the API compatibility level desired. This is useful when writing scripts using Perforce commands that do not yet support tagged output. In these cases, upgrading to a later server that supports tagged output for the commands in question can break your script. Using this method allows you to lock your script to the output format of an older Perforce release and facilitate seamless upgrades. Must be called before calling
P4::connect().
Contains the character set to use when connect to a Unicode enabled server. Do not use when working with non-Unicode-enabled servers. By default, the character set is the value of the
P4CHARSET environment variable. If the character set is invalid, this method raises a
P4_Exception.
Contains the name of your client workspace. By default, this is the value of the P4CLIENT taken from any
P4CONFIG file present, or from the environment according to the normal Perforce conventions.
Contains the current working directly. Can be called prior to executing any Perforce command. Sometimes necessary if your script executes a
chdir() as part of its processing.
$p4 = new P4();$p4->connect(); $p4->exception_level = 1; $p4->connect(); # P4_Exception on failure $p4->run_sync(); # File(s) up-to-date is a warning - no exception raised
|
$p4 = new P4();$p4->exception_level = 1; $p4->connect(); # P4_Exception on failure $p4->run_sync(); # File(s) up-to-date is a warning - no exception raised $p4->disconnect();
|
Contains the name of the current host. It defaults to the value of P4HOST taken from any
P4CONFIG file present, or from the environment as per the usual Perforce convention. Must be called before connecting to the Perforce server.
Set this property prior to running a command that requires input from the user. When the command requests input, the specified data is supplied to the command. Typically, commands of the form
p4 cmd -i are invoked using the
P4::save_spectype methods, which retrieve the value from
P4::input internally; there is no need to set
P4::input when using the
P4::save_spectype shortcuts.
You may pass a string, an array, or (for commands that take multiple inputs from the user) an array of strings or arrays. If you pass an array, note that the first element of the array will be popped each time Perforce asks the user for input.
Limit the amount of time (in milliseconds) spent during data scans to prevent the server from locking tables for too long. Commands that take longer than the limit will be aborted. The limit remains in force until you disable it by setting it to zero. See
p4 help maxlocktime for information on the commands that support this limit.
Limit the number of results Perforce permits for subsequent commands. Commands that produce more than this number of results will be aborted. The limit remains in force until you disable it by setting it to zero. See
p4 help maxresults for information on the commands that support this limit.
Limit the number of database records Perforce scans for subsequent commands. Commands that attempt to scan more than this number of records will be aborted. The limit remains in force until you disable it by setting it to zero. See
p4 help maxscanrows for information on the commands that support this limit.
Contains the name of the current P4CONFIG file, if any. This property cannot be set.
Contains your Perforce password or login ticket. If not used, takes the value of P4PASSWD from any
P4CONFIG file in effect, or from the environment according to the normal Perforce conventions.
This password is also used if you later call P4::run_login() to log in using the 2003.2 and later ticket system. After running
P4::run_login(), the property contains the ticket the allocated by the server.
$p4 = new P4();$p4->password = "mypass"; $p4->connect(); $p4->run_login(); ...
|
Contains the host and port of the Perforce server to which you want to connect. It defaults to the value of
P4PORT in any
P4CONFIG file in effect, and then to the value of
P4PORT taken from the environment.
$p4 = new P4();$p4->prog = "sync-script"; print $p4->prog; $p4->connect(); ...
|
Returns the current Perforce server level. Each iteration of the Perforce Server is given a level number. As part of the initial communication this value is passed between the client application and the Perforce Server. This value is used to determine the communication that the Perforce Server will understand. All subsequent requests can therefore be tailored to meet the requirements of this Server level.
If true,
P4::tagged enables tagged output. By default, tagged output is on.
Contains the Perforce username. It defaults to the value of P4USER taken from any
P4CONFIG file present, or from the environment as per the usual Perforce convention.
$p4 = new P4();$p4->connect(); # P4_Exception on failure $p4->exception_level = 2;
|
Construct a new P4 object. For example:
<?phpprint P4::identify(); ?>
|
If the connection is successfully established, returns None. If the connection fails and
exception_level is 0, returns False, otherwise raises a
P4_Exception. If already connected, prints a message.
The delete_spectype methods are shortcut methods that allow you to delete the definitions of clients, labels, branches, etc. These methods are equivalent to:
The following code uses delete_client to delete client workspaces that have not been accessed in more than 365 days:
<?php$p4 = new P4(); try { $p4->connect(); foreach ($p4->run_clients() as $client) { $atime = int($client['Access']); // If the client has not been accessed for a year, delete it if ((time() - $atime) > 31536000) { // seconds in 365 days $p4->delete_client("-f", $client["Client"]); } } } catch (P4_Exception $e) { // handle exception } ?>
|
The fetch_spectype methods are shortcuts for running
$p4->run("spectype", "-o") and returning the first element of the array.
For example:
$label = $p4->fetch_label(labelname); $change = $p4->fetch_change(changeno); $clientspec = $p4->fetch_client(clientname);
|
$label = $p4->run("label", "-o", labelname); $change = $p4->run("change", "-o", changeno); $clientspec = $p4->run("client", "-o", clientname);
|
Converts the fields in the array containing the elements of a Perforce form (spec) into the string representation familiar to users. The first argument is the type of spec to format: for example, client, branch, label, and so on. The second argument is the hash to parse.
There are shortcuts available for this method. You can use $p4->format_spectype( array ) instead of
$p4->format_spec( spectype, array ), where
spectype is the name of a Perforce spec, such as client, label, etc.
The format_spectype methods are shortcut methods that allow you to quickly fetch the definitions of clients, labels, branches, etc. They're equivalent to:
Parses a Perforce form (spec) in text form into an array using the spec definition obtained from the server. The first argument is the type of spec to parse:
client,
branch,
label, and so on. The second argument is the string buffer to parse.
where spectype is one of
client,
branch,
label, and so on.
This is equivalent to $p4->parse_spec( spectype, string ).
For example, $p4->parse_job(myJob) converts the String representation of a job spec into an array.
To parse a spec, P4 needs to have the spec available. When not connected to the Perforce Server, P4 assumes the default format for the spec, which is hardcoded. This assumption can fail for jobs if the Server's jobspec has been modified. In this case, your script can load a job from the Server first with the command
fetch_job('somename'), andP4 will cache and use the spec format in subsequent
parse_job() calls.
Base interface to all the run methods in this API. Runs the specified Perforce command with the arguments supplied. Arguments may be in any form as long as they can be converted to strings.
The P4::run() method returns an array of results whether the command succeeds or fails; the array may, however, be empty. Whether the elements of the array are strings or arrays depends on:
In the event of errors or warnings, and depending on the exception level in force at the time,
run() raises a
P4_Exception. If the current exception level is below the threshold for the error/warning,
run() returns the output as normal and the caller must explicitly review
P4::errors and
P4::warnings to check for errors or warnings.
Shortcuts are available for P4::run. For example,
$p4->run_command( args ) is equivalent to
$p4->run( "command", args )
<?php$p4 = new P4(); $p4->connect();
|
<?php$p4 = new P4(); $p4->connect();
|
As the commands associated with fetch_spectype typically return only one item, these methods do not return an array, but instead return the first result element.
<?php$p4 = new P4(); $p4->connect();
|
Shorthand for P4::run("cmd", arguments... )
Runs a p4 filelog on the
fileSpec provided and returns an array of
P4_DepotFile results (when executed in tagged mode), or an array of strings when executed in nontagged mode. By default, the raw output of
p4 filelog is tagged; this method restructures the output into a more user-friendly (and object-oriented) form.
<?php$p4 = new P4(); try { $p4->connect(); $filelog = $p4->run_filelog("index.html"); foreach ($filelog->revisions as $revision) { // do something } } catch (P4_Exception $e) { // handle exception } ?>
|
Runs p4_login using a password (or other arguments) set by the user.
<?php$p4->input = array( $oldpass, $newpass, $newpass ); $p4->run( "password" ); ?>
|
<?php$p4 = new P4(); $p4->password = "myoldpass";
try { $p4->connect(); $p4->run_password("myoldpass", "mynewpass"); $p4->disconnect();
|
Run a p4 resolve command. Interactive resolves require the
resolver parameter to be an object of a class derived from
P4_Resolver. In these cases, the
resolve method of this class is called to handle the resolve. For example:
<?php$p4->run_resolve(new MyResolver()); ?>
|
<?phpclass MyResolver extends P4_Resolver { public function resolve($merge_data) { if ($merge_data->merge_hint != 'e') { return $merge_data->merge_hint; } else { return "s"; // skip, there's a conflict } } } ?>
|
In non-interactive resolves, no P4_Resolver object is required. For example:
The save_spectype methods are shortcut methods that allow you to quickly update the definitions of clients, labels, branches, etc. They are equivalent to:
try { $p4->connect(); $client = $p4->fetch_client(); $client["Owner"] = $p4->user; $p4->save_client($client); $p4->disconnect();
|
Instances of this class are raised when P4 encounters an error or a warning from the server. The exception contains the errors in the form of a string.
P4_Exception is an extension of the standard Exception class.
Utility class providing easy access to the attributes of a file in a Perforce depot. Each P4_DepotFile object contains summary information about the file and an array of revisions (
P4_Revision objects) of that file. Currently, only the
P4::run_filelog method returns an array of
P4_DepotFile objects.
Returns an array of P4_Revision objects, one for each revision of the depot file.
Returns the description of the change which created this revision. Note that only the first 31 characters are returned unless you use
p4 filelog -L for the first 250 characters, or
p4 filelog -l for the full text.
Returns the array of P4_Integration objects for this revision.
The P4_Map class allows users to create and work with Perforce mappings, without requiring a connection to a Perforce server.
Join two P4_Map objects and create a third
P4_Map. The new map is composed of the left-hand side of the first mapping, as joined to the right-hand side of the second mapping. For example:
May be called with one or two arguments. If called with one argument, the string is assumed to be a string containing either a half-map, or a string containing both halves of the mapping. In this form, mappings with embedded spaces must be quoted. If called with two arguments, each argument is assumed to be half of the mapping, and quotes are optional.
Translate a string through a map, and return the result. If the optional second argument is 1, translate forward, and if it is 0, translate in the reverse direction. By default, translation is in the forward direction.
Return a new P4_Map object with the left and right sides of the mapping swapped. The original object is unchanged.
Returns the path to the merge result. This is typically a path to a temporary file on your local machine in which the contents of the automatic merge performed by the server have been loaded.
P4_Resolver is a class for handling resolves in Perforce. It must be subclassed, to be used; subclasses can override the
resolve() method. When
P4::run_resolve() is called with a
P4_Resolver object, it calls the
resolve() method of the object once for each scheduled resolve.
By default, all automatic merges are accepted, and all merges with conflicts are skipped. The
resolve method is called with a single parameter, which is a reference to a
P4_MergeData object.
Copyright 2008-2009 Perforce Software.