Intro
This weblog combines a simple implementation of grep written for ABAP programs with a PHP front end. The PHP UI will use Web Services to communicate with the RFC.
The program searches the programs listed in TRDIR for the text supplied.
The development environment used was the fantastic Scripting in a Box v0.1 from craig.cmehil/blog
Caveat
The ABAP code is a little rough around the edges. It does a simple search of the ABAP reports in TRDIR. For it to be more robust you may want to consider adding more options as parameters and using a cursor to limit the search results to a reasonable size.
grep
For the unlucky few that have not used unix, you may be unfamiliar with the grep tool. This tool will search files for a given pattern. It is wonderful for finding entries in source code, log files etc.
ABAP
The abap code is implemented as a Remote Enabled Function Module with 2 importing parameters and one exporting parameter. The importing parameters represent the string to search for and the files to search. The exporting parameter in the example is simply a table of strings that identify the program, its line and the line of text.
Importing
iv_string type string (pass by value)
iv_rep type string (pass by value)
Exporting
et_results type ztt_strings (pass by value)
ABAP Function Module Source
TYPE string.
" Simple check so that not everything is returned.
IF iv_string IS INITIAL AND iv_rep IS INITIAL.
RETURN.
ENDIF.
IF NOT iv_rep CS '%'.
CONCATENATE iv_rep '%' INTO lv_repname.
ELSE.
lv_repname = iv_rep.
ENDIF.
TRANSLATE lv_repname TO UPPER CASE.
lv_text = iv_string.
SELECT * FROM trdir INTO TABLE lt_trdir WHERE name LIKE lv_repname.
IF sy-subrc EQ 0.
TRANSLATE lv_repname TO UPPER CASE.
lv_text = iv_string.
SELECT * FROM trdir INTO TABLE lt_trdir WHERE name LIKE lv_repname .
IF sy-subrc EQ 0.
LOOP AT lt_trdir INTO ls_trdir.
READ REPORT ls_trdir-name INTO lt_text.
LOOP AT lt_text ASSIGNING
PHP
The PHP client is very simple. It contains a form for submitting the search and a report area for the results. I would not normally combine both in this way, but it makes it easier for example purposes.
On receiving the post, the client is created using the supplied WSDL file.
$client = new SoapClient("grep.wsdl",
array( "login" => "username",
'trace' => 1,
"password" => "password") );
The Function Module has been remote enabled and the WSDL file can be retrieved using the BSP application "webservicebrowser". For this example I placed the downloaded WSDL file in the same directory as the php source file.
WSDL Source
My wsdl is included as a reference only. unless you know how to edit one by hand and also know your service location you are better of using the one you can get from "webservicebrowser" application.
PHP Source
To reuse this code you will simply need to change the client creation to use your wsdl file, password and username.
if (isset($_POST["sstring"])) {
echo "Call the Web Service to get the document list
";
// Same call using the PHP Web Service
try {
$client = new SoapClient("grep.wsdl",
array( "login" => "username",
'trace' => 1,
"password" => "password") );
// Create the complex array to match the structure defined in the WSDL.
$res = array( "IV_STRING" => $sstring,
"IV_REP" => $repname,
"ET_RESULTS"=>"");
$res = $client->zja_grep($res );
} catch (SoapFault $exception) {
echo "
Exception
";
echo $exception;
echo "
Request :
", htmlspecialchars($client->__getLastRequest()), " |
";
echo "
Response :
", htmlspecialchars($client->__getLastResponse()), " |
";
}
echo "
";
$results = $res->ET_RESULTS;
if (isset($results->item)) {
echo "";
echo "";
while (list($k,$v)= each($results->item) ) {
echo "";
}
echo "
| Program and entry |
".($k+1)." | ".$v." |
";
}
}
?>
A repeated word of warning, the service is not limiting the number of results returned, so if you specify a vague search, be prepared for a long list of values in the results.